my feed query

This commit is contained in:
bniwredyc 2023-02-06 15:27:23 +01:00
parent 14d52f1c55
commit 3a678a7343

View File

@ -3,13 +3,15 @@ from datetime import datetime, timedelta, timezone
from sqlalchemy.orm import joinedload, aliased
from sqlalchemy.sql.expression import desc, asc, select, func, case
from auth.authenticate import login_required
from auth.credentials import AuthCredentials
from base.exceptions import ObjectNotExist
from base.orm import local_session
from base.resolvers import query
from orm import ViewedEntry
from orm import ViewedEntry, TopicFollower
from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.user import AuthorFollower
def add_stat_columns(q):
@ -193,3 +195,43 @@ async def load_shouts_by(_, info, options):
shouts_map[shout_id].stat['viewed'] = viewed_stat
return shouts
@query.field("myFeed")
@login_required
async def get_my_feed(_, info):
auth: AuthCredentials = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
q = select(Shout).options(
joinedload(Shout.authors),
joinedload(Shout.topics),
).where(
Shout.deletedAt.is_(None)
)
q = q.join(
ShoutAuthor
).join(
AuthorFollower
).where(
AuthorFollower.follower == user_id
).join(
ShoutTopic
).join(
TopicFollower
).where(TopicFollower.follower == user_id)
q = add_stat_columns(q)
shouts = []
for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique():
shouts.append(shout)
shout.stat = {
"viewed": 0,
"reacted": reacted_stat,
"commented": commented_stat,
"rating": rating_stat
}
return shouts