core/resolvers/feed.py

54 lines
1.6 KiB
Python
Raw Normal View History

from typing import List
from sqlalchemy import and_, desc
from auth.authenticate import login_required
2022-08-11 05:53:14 +00:00
from base.orm import local_session
2022-08-11 09:09:57 +00:00
from base.resolvers import query
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import TopicFollower
from orm.user import AuthorFollower
2022-09-14 09:45:31 +00:00
from services.zine.shoutscache import prepare_shouts
2022-09-03 10:50:14 +00:00
@query.field("shoutsForFeed")
@login_required
2022-09-19 13:50:43 +00:00
async def get_user_feed(_, info, offset, limit) -> List[Shout]:
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
shouts = []
with local_session() as session:
shouts = (
session.query(Shout)
.join(ShoutAuthor)
.join(AuthorFollower)
.where(AuthorFollower.follower == user.slug)
.order_by(desc(Shout.createdAt))
)
topic_rows = (
2022-09-03 10:50:14 +00:00
session.query(Shout)
.join(ShoutTopic)
.join(TopicFollower)
.where(TopicFollower.follower == user.slug)
.order_by(desc(Shout.createdAt))
)
shouts = shouts.union(topic_rows).limit(limit).offset(offset).all()
2022-09-03 10:50:14 +00:00
return shouts
2022-09-22 10:31:44 +00:00
@query.field("recentCandidates")
@login_required
2022-09-14 09:45:31 +00:00
async def user_unpublished_shouts(_, info, offset, limit) -> List[Shout]:
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
with local_session() as session:
2022-09-14 09:45:31 +00:00
shouts = prepare_shouts(
2022-09-03 10:50:14 +00:00
session.query(Shout)
.join(ShoutAuthor)
2022-09-22 10:31:44 +00:00
.where(and_(Shout.publishedAt.is_(None), ShoutAuthor.user == user.slug))
2022-09-03 10:50:14 +00:00
.order_by(desc(Shout.createdAt))
2022-11-10 05:40:32 +00:00
.group_by(Shout.id)
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset(offset)
2022-09-03 10:50:14 +00:00
.all()
)
return shouts