2024-10-21 08:29:57 +00:00
|
|
|
import enum
|
|
|
|
import time
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-10-21 13:59:25 +00:00
|
|
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text, distinct, func
|
2024-10-21 08:48:51 +00:00
|
|
|
from sqlalchemy.ext.hybrid import hybrid_property
|
|
|
|
|
2025-05-16 06:23:48 +00:00
|
|
|
from auth.orm import Author
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.db import Base
|
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
|
2024-10-21 08:29:57 +00:00
|
|
|
|
2024-10-21 08:48:51 +00:00
|
|
|
@classmethod
|
|
|
|
def as_string_array(cls, roles):
|
|
|
|
return [role.value for role in roles]
|
|
|
|
|
2024-10-21 08:29:57 +00:00
|
|
|
|
2024-06-05 14:45:55 +00:00
|
|
|
class CommunityFollower(Base):
|
2024-04-17 15:32:23 +00:00
|
|
|
__tablename__ = "community_author"
|
2022-06-12 07:51:22 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
author = Column(ForeignKey("author.id"), primary_key=True)
|
|
|
|
community = Column(ForeignKey("community.id"), primary_key=True)
|
2023-11-03 10:10:22 +00:00
|
|
|
joined_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2024-10-21 13:59:25 +00:00
|
|
|
roles = Column(Text, nullable=True, comment="Roles (comma-separated)")
|
2024-10-21 08:48:51 +00:00
|
|
|
|
|
|
|
def set_roles(self, roles):
|
|
|
|
self.roles = CommunityRole.as_string_array(roles)
|
|
|
|
|
|
|
|
def get_roles(self):
|
|
|
|
return [CommunityRole(role) for role in self.roles]
|
2022-06-12 07:51:22 +00:00
|
|
|
|
2021-08-26 21:14:20 +00:00
|
|
|
|
|
|
|
class Community(Base):
|
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)
|
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):
|
|
|
|
self.roles = ",".join(value) if value else None
|
|
|
|
|
2024-10-21 07:52:23 +00:00
|
|
|
|
2024-10-21 08:08:16 +00:00
|
|
|
class CommunityStats:
|
|
|
|
def __init__(self, community):
|
|
|
|
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-16 06:23:48 +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.author))
|
|
|
|
.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(Base):
|
|
|
|
__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):
|
|
|
|
self.roles = ",".join(value) if value else None
|