diff --git a/orm/author.py b/orm/author.py index f451e1b8..4bf79657 100644 --- a/orm/author.py +++ b/orm/author.py @@ -1,10 +1,12 @@ import time from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String -# from sqlalchemy_utils import TSVectorType from services.db import Base +# from sqlalchemy_utils import TSVectorType + + class AuthorRating(Base): __tablename__ = "author_rating" diff --git a/resolvers/__init__.py b/resolvers/__init__.py index dbc3f35e..bff24eef 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -11,12 +11,7 @@ from resolvers.author import ( # search_authors, ) from resolvers.community import get_communities_all, get_community from resolvers.editor import create_shout, delete_shout, update_shout -from resolvers.follower import ( - follow, - get_shout_followers, - get_topic_followers, - unfollow, -) +from resolvers.follower import follow, get_shout_followers, get_topic_followers, unfollow from resolvers.notifier import ( load_notifications, notification_mark_seen, @@ -40,12 +35,7 @@ from resolvers.reader import ( load_shouts_search, load_shouts_unrated, ) -from resolvers.topic import ( - get_topic, - get_topics_all, - get_topics_by_author, - get_topics_by_community, -) +from resolvers.topic import get_topic, get_topics_all, get_topics_by_author, get_topics_by_community from services.triggers import events_register events_register() diff --git a/resolvers/author.py b/resolvers/author.py index 08a30e4e..6da5dc23 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -315,10 +315,13 @@ async def get_author_followers(_, _info, slug: str): if isinstance(cached, str): followers_cached = json.loads(cached) if isinstance(followers_cached, list): - logger.debug(f"@{slug} got {len(followers_cached)} followers cached") + logger.debug( + f"@{slug} got {len(followers_cached)} followers cached" + ) for fc in followers_cached: - if fc["id"] not in followers_ids: + if fc["id"] not in followers_ids and fc["id"] != author_id: followers.append(fc) + followers_ids.append(fc["id"]) return followers author_follower_alias = aliased(AuthorFollower, name="af") @@ -327,12 +330,16 @@ async def get_author_followers(_, _info, slug: str): and_( author_follower_alias.author == author_id, author_follower_alias.follower == Author.id, + Author.id != author_id, # exclude the author from the followers ), ) results = get_with_stat(q) if isinstance(results, list): + followers_ids = [r.id for r in results] for follower in results: - await cache_follow_author_change(follower.dict(), author.dict()) + if follower.id not in followers_ids: + await cache_follow_author_change(follower.dict(), author.dict()) + followers_ids.append(follower.id) logger.debug(f"@{slug} cache updated with {len(results)} followers") return results except Exception as exc: diff --git a/resolvers/editor.py b/resolvers/editor.py index 1ec6c8c2..6e32d111 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -4,15 +4,15 @@ from sqlalchemy import and_, desc, select from sqlalchemy.orm import joinedload from sqlalchemy.sql.functions import coalesce +from orm.author import Author from orm.rating import is_negative, is_positive from orm.reaction import Reaction, ReactionKind from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.topic import Topic -from orm.author import Author from resolvers.follower import reactions_follow, reactions_unfollow from resolvers.stat import get_with_stat from services.auth import login_required -from services.cache import cache_topic, cache_author +from services.cache import cache_author, cache_topic from services.db import local_session from services.diff import apply_diff, get_diff from services.logger import root_logger as logger @@ -99,7 +99,7 @@ async def get_shouts_drafts(_, info): .group_by(Shout.id) ) shouts = [shout for [shout] in session.execute(q).unique()] - return { "shouts": shouts } + return {"shouts": shouts} @mutation.field("create_shout") diff --git a/resolvers/notifier.py b/resolvers/notifier.py index 8ce824e3..8a3b9a91 100644 --- a/resolvers/notifier.py +++ b/resolvers/notifier.py @@ -8,12 +8,7 @@ from sqlalchemy.orm import aliased from sqlalchemy.sql import not_ from orm.author import Author -from orm.notification import ( - Notification, - NotificationAction, - NotificationEntity, - NotificationSeen, -) +from orm.notification import Notification, NotificationAction, NotificationEntity, NotificationSeen from orm.shout import Shout from services.auth import login_required from services.db import local_session diff --git a/server.py b/server.py index ae8fabef..531a3b3c 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,8 @@ +import subprocess + from granian.constants import Interfaces from granian.server import Granian -import subprocess + from services.logger import root_logger as logger from settings import PORT diff --git a/services/cache.py b/services/cache.py index 273b666b..6638abc0 100644 --- a/services/cache.py +++ b/services/cache.py @@ -1,9 +1,9 @@ import json from orm.topic import TopicFollower +from services.db import local_session from services.encoders import CustomJSONEncoder from services.rediscache import redis -from services.db import local_session DEFAULT_FOLLOWS = { "topics": [], @@ -64,10 +64,9 @@ async def cache_author(author: dict): # author not found in the list, so add the new author with the updated stat field followed_author_followers.append(author) await redis.execute( - "SET", f"author:{author_id}:followers", json.dumps( - followed_author_followers, - cls=CustomJSONEncoder - ) + "SET", + f"author:{author_id}:followers", + json.dumps(followed_author_followers, cls=CustomJSONEncoder), ) @@ -138,7 +137,6 @@ async def cache_follow_author_change(follower: dict, author: dict, is_insert=Tru return followers - async def cache_topic(topic_dict: dict): # update stat all field for followers' caches in list followers = ( diff --git a/services/triggers.py b/services/triggers.py index 0eace99a..76a5267e 100644 --- a/services/triggers.py +++ b/services/triggers.py @@ -30,8 +30,12 @@ async def handle_author_follower_change( [follower] = get_with_stat(follower_query) if follower and author: await cache_author(author.dict()) - await cache_follows(follower.dict(), "author", author.dict(), is_insert) # cache_author(follower_dict) inside - await cache_follow_author_change(follower.dict(), author.dict(), is_insert) # cache_author(follower_dict) inside + await cache_follows( + follower.dict(), "author", author.dict(), is_insert + ) # cache_author(follower_dict) inside + await cache_follow_author_change( + follower.dict(), author.dict(), is_insert + ) # cache_author(follower_dict) inside async def handle_topic_follower_change( diff --git a/services/viewed.py b/services/viewed.py index f9540fe2..f04de2db 100644 --- a/services/viewed.py +++ b/services/viewed.py @@ -7,12 +7,7 @@ from typing import Dict # ga from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) +from google.analytics.data_v1beta.types import DateRange, Dimension, Metric, RunReportRequest from orm.author import Author from orm.shout import Shout, ShoutAuthor, ShoutTopic