diff --git a/resolvers/__init__.py b/resolvers/__init__.py index e0cc0b8b..d20e9666 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -1,6 +1,6 @@ from resolvers.auth import login, sign_out, is_email_free, register, confirm from resolvers.inbox import create_message, delete_message, update_message, get_messages -from resolvers.zine import create_shout, get_shout_by_slug +from resolvers.zine import create_shout, get_shout_by_slug, recent_shouts, top_authors, top_shouts_by_rating, top_shouts_by_view from resolvers.profile import get_user_by_slug, get_current_user from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, topics_by_community, topics_by_slugs @@ -19,6 +19,10 @@ __all__ = [ "get_current_user", "get_user_by_slug", "get_shout_by_slug", + "recent_shouts", + "favorite_shouts", + "top_shouts_by_views", + "top_shouts_by_rating", "topics_by_slugs", "topics_by_community", "topics_by_author", diff --git a/resolvers/zine.py b/resolvers/zine.py index 3e7e3a4b..1d6e4274 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -105,7 +105,7 @@ class TopShouts: ).\ join(ShoutViewByDay).\ join(ShoutRating).\ - where(Shouts.createdAt > month_ago).\ + where(Shout.createdAt > month_ago).\ group_by(Shout.id).\ order_by(desc("createdAt")).\ limit(TopShouts.limit) @@ -116,11 +116,11 @@ class TopShouts: shout.rating = row.rating shouts.append(shout) async with TopShouts.lock: - TopShouts.shouts_by_view = shouts + TopShouts.recent_shouts = shouts @staticmethod - async def prepare_favorites_shouts(): + async def prepare_favorite_shouts(): with local_session() as session: stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\ join(ShoutRating).\ @@ -133,7 +133,7 @@ class TopShouts: shout.rating = row.rating shouts.append(shout) async with TopShouts.lock: - TopShouts.favorites_shouts = shouts + TopShouts.favorite_shouts = shouts @staticmethod async def prepare_shouts_by_view(): @@ -182,10 +182,10 @@ class TopShouts: while True: try: print("top shouts: update cache") - await TopShouts.prepare_favorites_shouts() + await TopShouts.prepare_favorite_shouts() + await TopShouts.prepare_recent_shouts() await TopShouts.prepare_shouts_by_rating() await TopShouts.prepare_shouts_by_view() - await TopShouts.prepare_recent_shouts() await TopShouts.prepare_top_authors() print("top shouts: update finished") except Exception as err: @@ -205,10 +205,15 @@ async def top_shouts_by_rating(_, info, limit): return TopShouts.shouts_by_rating[:limit] -@query.field("favoritesShouts") -async def favorites_shouts(_, info, limit): +@query.field("favoriteShouts") +async def favorite_shouts(_, info, limit): async with TopShouts.lock: - return TopShouts.favorites_shouts[:limit] + return TopShouts.favorite_shouts[:limit] + +@query.field("recentShouts") +async def recent_shouts(_, info, limit): + async with TopShouts.lock: + return TopShouts.recent_shouts[:limit] @query.field("topAuthors") diff --git a/schema.graphql b/schema.graphql index 74f4d49e..58f1c2d5 100644 --- a/schema.graphql +++ b/schema.graphql @@ -100,7 +100,6 @@ type Query { # shouts getShoutBySlug(slug: String!): Shout! # NOTE: with .comments: Comments[] - recentShouts(limit: Int): [Shout]! shoutsByTopic(topic: String!, limit: Int!): [Shout]! shoutsByAuthor(author: String!, limit: Int!): [Shout]! shoutsByCommunity(community: String!, limit: Int!): [Shout]! @@ -108,7 +107,8 @@ type Query { # mainpage topShoutsByView(limit: Int): [Shout]! topShoutsByRating(limit: Int): [Shout]! - favoritesShouts(limit: Int): [Shout]! + favoriteShouts(limit: Int): [Shout]! + recentShouts(limit: Int): [Shout]! topAuthors(limit: Int): [User]! # topics