my feed fixes

This commit is contained in:
tonyrewin 2023-02-16 13:08:55 +03:00
parent dc0042bc4b
commit 64e1bea4cd
3 changed files with 57 additions and 46 deletions

View File

@ -1,12 +1,12 @@
import asyncio import asyncio
from base.orm import local_session from base.orm import local_session
from base.resolvers import mutation, subscription, query from base.resolvers import mutation, subscription
from auth.authenticate import login_required from auth.authenticate import login_required
from auth.credentials import AuthCredentials from auth.credentials import AuthCredentials
# from resolvers.community import community_follow, community_unfollow # from resolvers.community import community_follow, community_unfollow
from orm.user import AuthorFollower from orm.user import AuthorFollower
from orm.topic import TopicFollower from orm.topic import TopicFollower
from orm.shout import Shout, ShoutReactionsFollower from orm.shout import ShoutReactionsFollower
from resolvers.zine.profile import author_follow, author_unfollow from resolvers.zine.profile import author_follow, author_unfollow
from resolvers.zine.reactions import reactions_follow, reactions_unfollow from resolvers.zine.reactions import reactions_follow, reactions_unfollow
from resolvers.zine.topics import topic_follow, topic_unfollow from resolvers.zine.topics import topic_follow, topic_unfollow
@ -14,22 +14,6 @@ from services.following import Following, FollowingManager, FollowingResult
from graphql.type import GraphQLResolveInfo from graphql.type import GraphQLResolveInfo
@query.field("myFeed")
@login_required
async def get_my_feed(_, info):
auth: AuthCredentials = info.context["request"].auth
user_id = auth.user_id
try:
with local_session() as session:
following_authors = session.query(AuthorFollower).where(AuthorFollower.follower == user_id).unique().all()
following_topics = session.query(TopicFollower).where(TopicFollower.follower == user_id).unique().all()
# TODO: my feed query
shouts = []
return shouts
except Exception:
pass
@mutation.field("follow") @mutation.field("follow")
@login_required @login_required
async def follow(_, info, what, slug): async def follow(_, info, what, slug):
@ -99,11 +83,9 @@ async def shout_generator(_, info: GraphQLResolveInfo):
tasks = [] tasks = []
with local_session() as session: with local_session() as session:
following_authors = session.query(AuthorFollower).where(
AuthorFollower.follower == user_id).all()
following_topics = session.query(TopicFollower).where(TopicFollower.follower == user_id).all()
# notify new shout # notify new shout by followed authors
following_topics = session.query(TopicFollower).where(TopicFollower.follower == user_id).all()
for topic_id in following_topics: for topic_id in following_topics:
following_topic = Following('topic', topic_id) following_topic = Following('topic', topic_id)
@ -111,12 +93,27 @@ async def shout_generator(_, info: GraphQLResolveInfo):
following_topic_task = following_topic.queue.get() following_topic_task = following_topic.queue.get()
tasks.append(following_topic_task) tasks.append(following_topic_task)
# by followed topics
following_authors = session.query(AuthorFollower).where(
AuthorFollower.follower == user_id).all()
for author_id in following_authors: for author_id in following_authors:
following_author = Following('author', author_id) following_author = Following('author', author_id)
await FollowingManager.register('author', following_author) await FollowingManager.register('author', following_author)
following_author_task = following_author.queue.get() following_author_task = following_author.queue.get()
tasks.append(following_author_task) tasks.append(following_author_task)
# TODO: use communities
# by followed communities
# following_communities = session.query(CommunityFollower).where(
# CommunityFollower.follower == user_id).all()
# for community_id in following_communities:
# following_community = Following('community', author_id)
# await FollowingManager.register('community', following_community)
# following_community_task = following_community.queue.get()
# tasks.append(following_community_task)
while True: while True:
shout = await asyncio.gather(*tasks) shout = await asyncio.gather(*tasks)
yield shout yield shout

View File

@ -199,10 +199,10 @@ async def load_shouts_by(_, info, options):
@query.field("myFeed") @query.field("myFeed")
@login_required @login_required
async def get_my_feed(_, info): async def get_my_feed(_, info, options):
auth: AuthCredentials = info.context["request"].auth auth: AuthCredentials = info.context["request"].auth
user_id = auth.user_id user_id = auth.user_id
with local_session() as session:
q = select(Shout).options( q = select(Shout).options(
joinedload(Shout.authors), joinedload(Shout.authors),
joinedload(Shout.topics), joinedload(Shout.topics),
@ -223,8 +223,21 @@ async def get_my_feed(_, info):
).where(TopicFollower.follower == user_id) ).where(TopicFollower.follower == user_id)
q = add_stat_columns(q) q = add_stat_columns(q)
q = apply_filters(q, options.get("filters", {}), user_id)
order_by = options.get("order_by", Shout.createdAt)
if order_by == 'reacted':
aliased_reaction = aliased(Reaction)
q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted'))
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
offset = options.get("offset", 0)
limit = options.get("limit", 10)
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
shouts = [] shouts = []
with local_session() as session:
for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique(): for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique():
shouts.append(shout) shouts.append(shout)
shout.stat = { shout.stat = {

View File

@ -302,6 +302,7 @@ type Query {
userFollowedTopics(slug: String!): [Topic]! userFollowedTopics(slug: String!): [Topic]!
authorsAll: [Author]! authorsAll: [Author]!
getAuthor(slug: String!): User getAuthor(slug: String!): User
myFeed(options: LoadShoutsOptions): [Shout]
# draft/collab # draft/collab
loadDrafts: [DraftCollab]! loadDrafts: [DraftCollab]!