core/orm/community.py

154 lines
5.2 KiB
Python
Raw Normal View History

2024-10-21 08:29:57 +00:00
import enum
import time
2023-10-23 14:47:11 +00:00
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String, Text, distinct, func
2024-10-21 08:48:51 +00:00
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
2024-10-21 08:48:51 +00:00
2025-05-16 06:23:48 +00:00
from auth.orm import Author
from services.db import BaseModel
2023-10-23 14:47:11 +00:00
2021-08-26 21:14:20 +00:00
2024-10-21 08:29:57 +00:00
class CommunityRole(enum.Enum):
2024-10-21 13:59:25 +00:00
READER = "reader" # can read and comment
AUTHOR = "author" # + can vote and invite collaborators
ARTIST = "artist" # + can be credited as featured artist
EXPERT = "expert" # + can add proof or disproof to shouts, can manage topics
EDITOR = "editor" # + can manage topics, comments and community settings
ADMIN = "admin"
2024-10-21 08:29:57 +00:00
2024-10-21 08:48:51 +00:00
@classmethod
2025-06-30 18:25:26 +00:00
def as_string_array(cls, roles) -> list[str]:
2024-10-21 08:48:51 +00:00
return [role.value for role in roles]
@classmethod
def from_string(cls, value: str) -> "CommunityRole":
return cls(value)
2024-10-21 08:29:57 +00:00
class CommunityFollower(BaseModel):
__tablename__ = "community_follower"
2024-04-17 15:32:23 +00:00
community = Column(ForeignKey("community.id"), primary_key=True)
follower = Column(ForeignKey("author.id"), primary_key=True)
roles = Column(String, nullable=True)
2024-10-21 08:48:51 +00:00
def __init__(self, community: int, follower: int, roles: list[str] | None = None) -> None:
self.community = community # type: ignore[assignment]
self.follower = follower # type: ignore[assignment]
if roles:
self.roles = ",".join(roles) # type: ignore[assignment]
2024-10-21 08:48:51 +00:00
def get_roles(self) -> list[CommunityRole]:
roles_str = getattr(self, "roles", "")
return [CommunityRole(role) for role in roles_str.split(",")] if roles_str else []
2021-08-26 21:14:20 +00:00
class Community(BaseModel):
2024-04-17 15:32:23 +00:00
__tablename__ = "community"
2022-09-03 10:50:14 +00:00
2023-10-23 14:47:11 +00:00
name = Column(String, nullable=False)
2024-02-19 14:22:38 +00:00
slug = Column(String, nullable=False, unique=True)
2024-04-17 15:32:23 +00:00
desc = Column(String, nullable=False, default="")
pic = Column(String, nullable=False, default="")
2023-11-03 10:10:22 +00:00
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
2024-10-21 13:42:30 +00:00
created_by = Column(ForeignKey("author.id"), nullable=False)
settings = Column(JSON, nullable=True)
updated_at = Column(Integer, nullable=True)
deleted_at = Column(Integer, nullable=True)
private = Column(Boolean, default=False)
followers = relationship("Author", secondary="community_follower")
2025-06-30 18:25:26 +00:00
created_by_author = relationship("Author", foreign_keys=[created_by])
2024-10-21 07:52:23 +00:00
2024-10-21 08:08:16 +00:00
@hybrid_property
def stat(self):
return CommunityStats(self)
2024-10-21 07:52:23 +00:00
2024-10-21 13:59:25 +00:00
@property
def role_list(self):
return self.roles.split(",") if self.roles else []
@role_list.setter
def role_list(self, value) -> None:
self.roles = ",".join(value) if value else None # type: ignore[assignment]
def is_followed_by(self, author_id: int) -> bool:
# Check if the author follows this community
from services.db import local_session
with local_session() as session:
follower = (
session.query(CommunityFollower)
.filter(CommunityFollower.community == self.id, CommunityFollower.follower == author_id)
.first()
)
return follower is not None
def get_role(self, author_id: int) -> CommunityRole | None:
# Get the role of the author in this community
from services.db import local_session
with local_session() as session:
follower = (
session.query(CommunityFollower)
.filter(CommunityFollower.community == self.id, CommunityFollower.follower == author_id)
.first()
)
if follower and follower.roles:
roles = follower.roles.split(",")
return CommunityRole.from_string(roles[0]) if roles else None
return None
2024-10-21 13:59:25 +00:00
2024-10-21 07:52:23 +00:00
2024-10-21 08:08:16 +00:00
class CommunityStats:
def __init__(self, community) -> None:
2024-10-21 08:08:16 +00:00
self.community = community
2024-10-21 07:52:23 +00:00
2024-10-21 08:08:16 +00:00
@property
def shouts(self):
2024-10-21 13:59:25 +00:00
from orm.shout import Shout
2025-05-29 09:37:39 +00:00
return self.community.session.query(func.count(Shout.id)).filter(Shout.community == self.community.id).scalar()
2024-10-21 08:08:16 +00:00
@property
def followers(self):
2024-10-21 08:29:57 +00:00
return (
self.community.session.query(func.count(CommunityFollower.follower))
2024-10-21 08:29:57 +00:00
.filter(CommunityFollower.community == self.community.id)
2024-10-21 08:08:16 +00:00
.scalar()
2024-10-21 08:29:57 +00:00
)
2024-10-21 13:42:30 +00:00
@property
def authors(self):
2024-10-21 13:59:25 +00:00
from orm.shout import Shout
2024-10-21 13:57:03 +00:00
# author has a shout with community id and its featured_at is not null
2024-10-21 13:42:30 +00:00
return (
self.community.session.query(func.count(distinct(Author.id)))
.join(Shout)
2025-05-16 06:23:48 +00:00
.filter(
Shout.community == self.community.id,
Shout.featured_at.is_not(None),
Author.id.in_(Shout.authors),
)
2024-10-21 13:42:30 +00:00
.scalar()
)
2024-10-21 13:59:25 +00:00
class CommunityAuthor(BaseModel):
2024-10-21 13:59:25 +00:00
__tablename__ = "community_author"
id = Column(Integer, primary_key=True)
community_id = Column(Integer, ForeignKey("community.id"))
author_id = Column(Integer, ForeignKey("author.id"))
roles = Column(Text, nullable=True, comment="Roles (comma-separated)")
@property
def role_list(self):
return self.roles.split(",") if self.roles else []
@role_list.setter
def role_list(self, value) -> None:
self.roles = ",".join(value) if value else None # type: ignore[assignment]