recent-commented

This commit is contained in:
tonyrewin 2022-09-19 19:14:20 +03:00
parent c479b12e65
commit a8313d1324
4 changed files with 43 additions and 2 deletions

View File

@ -40,6 +40,7 @@ from resolvers.topics import (
topics_by_community,
topics_all,
)
from resolvers.zine import (
get_shout_by_slug,
follow,
@ -49,6 +50,8 @@ from resolvers.zine import (
top_overall,
recent_published,
recent_all,
recent_commented,
recent_reacted,
top_viewed,
shouts_by_authors,
shouts_by_topics,
@ -72,6 +75,8 @@ __all__ = [
"get_top_authors",
# zine
"recent_published",
"recent_commented",
"recent_reacted",
"recent_all",
"shouts_by_topics",
"shouts_by_authors",

View File

@ -64,6 +64,12 @@ async def recent_reacted(_, _info, offset, limit):
return ShoutsCache.recent_reacted[offset : offset + limit]
@query.field("recentCommented")
async def recent_commented(_, _info, offset, limit):
async with ShoutsCache.lock:
return ShoutsCache.recent_commented[offset : offset + limit]
@query.field("getShoutBySlug")
async def get_shout_by_slug(_, info, slug):
all_fields = [

View File

@ -236,7 +236,8 @@ type Query {
topOverall(offset: Int!, limit: Int!): [Shout]!
topCommented(offset: Int!, limit: Int!): [Shout]!
recentPublished(offset: Int!, limit: Int!): [Shout]! # homepage
recentReacted(offset: Int!, limit: Int!): [Shout]! # test
recentReacted(offset: Int!, limit: Int!): [Shout]!
recentCommented(offset: Int!, limit: Int!): [Shout]!
recentAll(offset: Int!, limit: Int!): [Shout]!
# reactons

View File

@ -36,6 +36,7 @@ class ShoutsCache:
recent_published = []
recent_all = []
recent_reacted = []
recent_commented = []
top_month = []
top_overall = []
top_viewed = []
@ -102,7 +103,7 @@ class ShoutsCache:
selectinload(Shout.authors),
selectinload(Shout.topics),
)
.where(and_(bool(Shout.publishedAt), Shout.slug.in_(list(reacted_slugs))))
.where(Shout.slug.in_(list(reacted_slugs)))
.filter(not bool(Shout.deletedAt))
.group_by(Shout.slug)
.order_by(Shout.publishedAt)
@ -113,6 +114,33 @@ class ShoutsCache:
ShoutsCache.recent_reacted = shouts
print("[zine.cache] %d recently reacted shouts " % len(shouts))
@staticmethod
async def prepare_recent_commented():
with local_session() as session:
reactions = session.query(Reaction).order_by(Reaction.createdAt).limit(ShoutsCache.limit)
commented_slugs = set([])
for r in reactions:
if bool(r.body):
commented_slugs.add(r.shout)
shouts = await prepare_shouts(
session,
(
select(Shout)
.options(
selectinload(Shout.authors),
selectinload(Shout.topics),
)
.where(Shout.slug.in_(list(commented_slugs)))
.filter(not bool(Shout.deletedAt))
.group_by(Shout.slug)
.order_by(Shout.publishedAt)
.limit(ShoutsCache.limit)
)
)
async with ShoutsCache.lock:
ShoutsCache.recent_commented = shouts
print("[zine.cache] %d recently commented shouts " % len(shouts))
@staticmethod
async def prepare_top_overall():
with local_session() as session:
@ -240,6 +268,7 @@ class ShoutsCache:
await ShoutsCache.prepare_recent_published()
await ShoutsCache.prepare_recent_all()
await ShoutsCache.prepare_recent_reacted()
await ShoutsCache.prepare_recent_commented()
await ShoutsCache.prepare_by_author()
await ShoutsCache.prepare_by_topic()