From e2faec5893b54b7f88c60ef26fa0d1da4d7acdd1 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 28 Mar 2024 14:05:46 +0300 Subject: [PATCH] scalar-fix --- resolvers/notifier.py | 121 ++++++++++++++++++++++-------------------- resolvers/reader.py | 2 +- services/search.py | 7 +-- 3 files changed, 67 insertions(+), 63 deletions(-) diff --git a/resolvers/notifier.py b/resolvers/notifier.py index 67b59196..b015bf23 100644 --- a/resolvers/notifier.py +++ b/resolvers/notifier.py @@ -122,9 +122,9 @@ def get_notifications_grouped( if (groups_amount + offset) >= limit: break - payload = json.loads(notification.payload) + payload = json.loads(notification.payload.scalar()) - if notification.entity == NotificationEntity.SHOUT.value: + if notification.entity.scalar() == NotificationEntity.SHOUT.value: shout = payload shout_id = shout.get('id') author_id = shout.get('created_by') @@ -139,68 +139,71 @@ def get_notifications_grouped( thread_id, shout=shout, authors=[author], - action=notification.action, - entity=notification.entity, + action=notification.action.scalar(), + entity=notification.entity.scalar(), ) groups_by_thread[thread_id] = group groups_amount += 1 - elif notification.entity == NotificationEntity.REACTION.value: + elif notification.entity.scalar() == NotificationEntity.REACTION.value: reaction = payload + if not isinstance(shout, dict): + raise ValueError('reaction data is not consistent') shout_id = shout.get('shout') - author_id = shout.get('created_by') - with local_session() as session: - author = session.query(Author).filter(Author.id == author_id).first() - shout = session.query(Shout).filter(Shout.id == shout_id).first() - if shout and author: - author = author.dict() - shout = shout.dict() - reply_id = reaction.get('reply_to') - thread_id = f'shout-{shout_id}' - if reply_id and reaction.get('kind', '').lower() == 'comment': - thread_id += f'{reply_id}' - existing_group = groups_by_thread.get(thread_id) - if existing_group: - existing_group['seen'] = False - existing_group['authors'].append(author_id) - existing_group['reactions'] = existing_group['reactions'] or [] - existing_group['reactions'].append(reaction) - groups_by_thread[thread_id] = existing_group - else: - group = group_notification( - thread_id, - authors=[author], - shout=shout, - reactions=[reaction], - entity=notification.entity, - action=notification.action, - ) - if group: - groups_by_thread[thread_id] = group - groups_amount += 1 + author_id = shout.get('created_by', 0) + if shout_id and author_id: + with local_session() as session: + author = session.query(Author).filter(Author.id == author_id).first() + shout = session.query(Shout).filter(Shout.id == shout_id).first() + if shout and author: + author = author.dict() + shout = shout.dict() + reply_id = reaction.get('reply_to') + thread_id = f'shout-{shout_id}' + if reply_id and reaction.get('kind', '').lower() == 'comment': + thread_id += f'{reply_id}' + existing_group = groups_by_thread.get(thread_id) + if existing_group: + existing_group['seen'] = False + existing_group['authors'].append(author_id) + existing_group['reactions'] = existing_group['reactions'] or [] + existing_group['reactions'].append(reaction) + groups_by_thread[thread_id] = existing_group + else: + group = group_notification( + thread_id, + authors=[author], + shout=shout, + reactions=[reaction], + entity=notification.entity.scalar(), + action=notification.action.scalar(), + ) + if group: + groups_by_thread[thread_id] = group + groups_amount += 1 - elif notification.entity == 'follower': - thread_id = 'followers' - follower = json.loads(payload) - group = groups_by_thread.get(thread_id) - if group: - if notification.action == 'follow': - group['authors'].append(follower) - elif notification.action == 'unfollow': - follower_id = follower.get('id') - for author in group['authors']: - if author.get('id') == follower_id: - group['authors'].remove(author) - break - else: - group = group_notification( - thread_id, - authors=[follower], - entity=notification.entity, - action=notification.action, - ) - groups_amount += 1 - groups_by_thread[thread_id] = group + elif notification.entity.scalar() == 'follower': + thread_id = 'followers' + follower = json.loads(payload) + group = groups_by_thread.get(thread_id) + if group: + if notification.action.scalar() == 'follow': + group['authors'].append(follower) + elif notification.action.scalar() == 'unfollow': + follower_id = follower.get('id') + for author in group['authors']: + if author.get('id') == follower_id: + group['authors'].remove(author) + break + else: + group = group_notification( + thread_id, + authors=[follower], + entity=notification.entity.scalar(), + action=notification.action.scalar(), + ) + groups_amount += 1 + groups_by_thread[thread_id] = group return groups_by_thread, unread, total @@ -302,11 +305,11 @@ async def notifications_seen_thread(_, info, thread: str, after: int): ) exclude = set() for nr in removed_reaction_notifications: - reaction = json.loads(nr.payload) + reaction = json.loads(nr.payload.scalar()) reaction_id = reaction.get('id') exclude.add(reaction_id) for n in new_reaction_notifications: - reaction = json.loads(n.payload) + reaction = json.loads(n.payload.scalar()) reaction_id = reaction.get('id') if ( reaction_id not in exclude diff --git a/resolvers/reader.py b/resolvers/reader.py index 2d39e8dd..895ec48b 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -338,7 +338,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0): q = add_reaction_stat_columns(q, aliased_reaction) q = q.group_by(Shout.id).order_by(func.random()).limit(limit).offset(offset) - user_id = info.context.get('user_id') if isinstance(info.context, {}) else None + user_id = info.context.get('user_id') if isinstance(info.context, dict) else None if user_id: with local_session() as session: author = session.query(Author).filter(Author.user == user_id).first() diff --git a/services/search.py b/services/search.py index 9ac9fddf..9a168a21 100644 --- a/services/search.py +++ b/services/search.py @@ -120,9 +120,10 @@ class SearchService: await self.recreate_index() async def recreate_index(self): - async with self.lock: - self.client.indices.delete(index=self.index_name, ignore_unavailable=True) - await self.check_index() + if self.client: + async with self.lock: + self.client.indices.delete(index=self.index_name, ignore_unavailable=True) + await self.check_index() def index(self, shout): if self.client: