wip refactoring: reactions, storages isolated
This commit is contained in:
61
orm/topic.py
61
orm/topic.py
@@ -1,15 +1,18 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, JSON as JSONType
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy import Column, String, ForeignKey, DateTime, JSON as JSONType
|
||||
from orm.base import Base
|
||||
|
||||
import asyncio
|
||||
|
||||
class TopicSubscription(Base):
|
||||
__tablename__ = "topic_subscription"
|
||||
class ShoutTopic(Base):
|
||||
__tablename__ = 'shout_topic'
|
||||
|
||||
id = None
|
||||
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
||||
class TopicFollower(Base):
|
||||
__tablename__ = "topic_followers"
|
||||
|
||||
id = None
|
||||
follower = Column(ForeignKey('user.slug'), primary_key = True)
|
||||
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||
|
||||
@@ -26,47 +29,3 @@ class Topic(Base):
|
||||
community = Column(ForeignKey("community.slug"), nullable=False, comment="Community")
|
||||
oid: str = Column(String, nullable=True, comment="Old ID")
|
||||
|
||||
class TopicStorage:
|
||||
topics = {}
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
self = TopicStorage
|
||||
topics = session.query(Topic)
|
||||
self.topics = dict([(topic.slug, topic) for topic in topics])
|
||||
for topic in self.topics.values():
|
||||
self.load_parents(topic)
|
||||
|
||||
@staticmethod
|
||||
def load_parents(topic):
|
||||
self = TopicStorage
|
||||
parents = []
|
||||
for parent in self.topics.values():
|
||||
if topic.slug in parent.children:
|
||||
parents.append(parent.slug)
|
||||
topic.parents = parents
|
||||
return topic
|
||||
|
||||
@staticmethod
|
||||
async def get_topics(slugs):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
if not slugs:
|
||||
return self.topics.values()
|
||||
topics = filter(lambda topic: topic.slug in slugs, self.topics.values())
|
||||
return list(topics)
|
||||
|
||||
@staticmethod
|
||||
async def get_topics_by_community(community):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
topics = filter(lambda topic: topic.community == community, self.topics.values())
|
||||
return list(topics)
|
||||
|
||||
@staticmethod
|
||||
async def add_topic(topic):
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
self.topics[topic.slug] = topic
|
||||
self.load_parents(topic)
|
||||
|
Reference in New Issue
Block a user