From bdc864f443652ba0ba9bb5541711f0fcd0bf2cd9 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sat, 13 Aug 2022 13:05:46 +0300 Subject: [PATCH] storage and collection fixes --- resolvers/collection.py | 18 +++++++++--------- schema.graphql | 7 ++++--- services/stat/reacted.py | 31 ++++++++----------------------- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/resolvers/collection.py b/resolvers/collection.py index b8bb06f5..e7091fcd 100644 --- a/resolvers/collection.py +++ b/resolvers/collection.py @@ -57,22 +57,22 @@ async def delete_collection(_, info, slug): return {} -@query.field("getCollection") -async def get_collection(_, info, userslug, colslug): +@query.field("getUserCollections") +async def get_user_collections(_, info, userslug): + collections = [] with local_session() as session: user = session.query(User).filter(User.slug == userslug).first() if user: - collection = session.\ + # TODO: check rights here + collections = session.\ query(Collection).\ - where(and_(Collection.createdBy == user.id, Collection.slug == colslug)).\ - first() - if not collection: - return {"error": "collection not found"} - return collection + where(and_(Collection.createdBy == userslug, Collection.publishedAt != None)).\ + all() + return collections @query.field("getMyColelctions") @login_required -async def get_collections(_, info): +async def get_my_collections(_, info): auth = info.context["request"].auth user_id = auth.user_id with local_session() as session: diff --git a/schema.graphql b/schema.graphql index 97338152..d75c0891 100644 --- a/schema.graphql +++ b/schema.graphql @@ -252,9 +252,9 @@ type Query { topicsByCommunity(community: String!): [Topic]! topicsByAuthor(author: String!): [Topic]! - # collection - getCollection(author: String!, slug: String!): Collection! - shoutsByCollection(collection: String, page: Int, size: Int): [Shout]! + # collections + getUserCollections(author: String!): [Collection]! + shoutsByCollection(collection: String!, page: Int, size: Int): [Shout]! # communities getCommunity(slug: String): Community! @@ -429,6 +429,7 @@ type Collection { title: String! desc: String pic: String! + publishedAt: DateTime createdAt: DateTime! createdBy: User! } diff --git a/services/stat/reacted.py b/services/stat/reacted.py index e7576066..8fc3905a 100644 --- a/services/stat/reacted.py +++ b/services/stat/reacted.py @@ -35,7 +35,7 @@ class ReactedStorage: lock = asyncio.Lock() @staticmethod - def prepare(session): + def init(session): self = ReactedStorage all_reactions = session.query(ReactedByDay).all() day_start = datetime.now().replace(hour=0, minute=0, second=0) @@ -101,25 +101,10 @@ class ReactedStorage: self = ReactedStorage reaction: ReactedByDay = None async with self.lock: - reaction = ReactedByDay.create(shout=shout_slug, kind=kind, reply=reply_id) - self.reacted['shouts'][shout_slug] = self.reacted['shouts'].get(shout_slug, []) - self.reacted['shouts'][shout_slug].append(reaction) - if reply_id: - self.reacted['reaction'][reply_id] = self.reacted['reactions'].get(shout_slug, []) - self.reacted['reaction'][reply_id].append(reaction) - - @staticmethod - async def worker(): - while True: - try: - with local_session() as session: - ReactedStorage.prepare(session) - print("[stat.reacted] updated") - except Exception as err: - print("[stat.reacted] error: %s" % (err)) - raise err - await asyncio.sleep(ReactedStorage.period) - - @staticmethod - def init(session): - ReactedStorage.prepare(session) \ No newline at end of file + with local_session() as session: + reaction = ReactedByDay.create(shout=shout_slug, kind=kind, reply=reply_id) + self.reacted['shouts'][shout_slug] = self.reacted['shouts'].get(shout_slug, []) + self.reacted['shouts'][shout_slug].append(reaction) + if reply_id: + self.reacted['reaction'][reply_id] = self.reacted['reactions'].get(shout_slug, []) + self.reacted['reaction'][reply_id].append(reaction) \ No newline at end of file