add shoutsByUserSubscriptions; use user slug in shout_author table

This commit is contained in:
knst-kotov 2022-01-31 14:34:43 +03:00
parent 1f3983fc07
commit 020a3a5abf
3 changed files with 29 additions and 6 deletions

View File

@ -16,7 +16,7 @@ class ShoutAuthor(Base):
id = None id = None
shout = Column(ForeignKey('shout.slug'), primary_key = True) shout = Column(ForeignKey('shout.slug'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True) user = Column(ForeignKey('user.slug'), primary_key = True)
class ShoutViewer(Base): class ShoutViewer(Base):
__tablename__ = "shout_viewer" __tablename__ = "shout_viewer"
@ -198,7 +198,7 @@ class TopicStat:
subs = session.query(TopicSubscription) subs = session.query(TopicSubscription)
for sub in subs: for sub in subs:
topic = sub.topic topic = sub.topic
user = sub.user user = sub.subscriber
if topic in self.subs_by_topic: if topic in self.subs_by_topic:
self.subs_by_topic[topic].append(user) self.subs_by_topic[topic].append(user)
else: else:

View File

@ -1,7 +1,8 @@
from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource,\ from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource,\
ShoutRatingStorage, ShoutViewStorage, Comment, CommentRating, Topic ShoutRatingStorage, ShoutViewStorage, Comment, CommentRating, Topic
from orm.base import local_session from orm.base import local_session
from orm.user import UserStorage from orm.user import UserStorage, AuthorSubscription
from orm.topic import TopicSubscription
from resolvers.base import mutation, query from resolvers.base import mutation, query
@ -222,7 +223,7 @@ async def create_shout(_, info, input):
new_shout = Shout.create(**input) new_shout = Shout.create(**input)
ShoutAuthor.create( ShoutAuthor.create(
shout = new_shout.slug, shout = new_shout.slug,
user = user.id) user = user.slug)
if "mainTopic" in input: if "mainTopic" in input:
topic_slugs.append(input["mainTopic"]) topic_slugs.append(input["mainTopic"])
@ -375,7 +376,7 @@ async def shouts_by_author(_, info, author, page, size):
shouts = session.query(Shout).\ shouts = session.query(Shout).\
join(ShoutAuthor).\ join(ShoutAuthor).\
where(and_(ShoutAuthor.user == user.id, Shout.publishedAt != None)).\ where(and_(ShoutAuthor.user == author, Shout.publishedAt != None)).\
order_by(desc(Shout.publishedAt)).\ order_by(desc(Shout.publishedAt)).\
limit(size).\ limit(size).\
offset(page * size) offset(page * size)
@ -397,6 +398,28 @@ async def shouts_by_community(_, info, community, page, size):
offset(page * size) offset(page * size)
return shouts return shouts
@query.field("shoutsByUserSubscriptions")
async def shouts_by_user_subscriptions(_, info, userSlug, page, size):
user = await UserStorage.get_user_by_slug(userSlug)
if not user:
return
with local_session() as session:
shouts_by_topic = session.query(Shout).\
join(ShoutTopic).\
join(TopicSubscription, ShoutTopic.topic == TopicSubscription.topic).\
where(and_(Shout.publishedAt != None, TopicSubscription.subscriber == userSlug))
shouts_by_author = session.query(Shout).\
join(ShoutAuthor).\
join(AuthorSubscription, ShoutAuthor.user == AuthorSubscription.author).\
where(and_(Shout.publishedAt != None, AuthorSubscription.subscriber == userSlug))
shouts = shouts_by_topic.union(shouts_by_author).\
order_by(desc(Shout.publishedAt)).\
limit(size).\
offset( (page - 1) * size)
return shouts
@query.field("shoutsByUserRatingOrComment") @query.field("shoutsByUserRatingOrComment")
async def shouts_by_user_rating_or_comment(_, info, userSlug, page, size): async def shouts_by_user_rating_or_comment(_, info, userSlug, page, size):
user = await UserStorage.get_user_by_slug(userSlug) user = await UserStorage.get_user_by_slug(userSlug)

View File

@ -197,7 +197,7 @@ type Query {
getCommunity(slug: String): Community! getCommunity(slug: String): Community!
getCommunities: [Community]! getCommunities: [Community]!
# shoutsByUserSubscriptions(): [Shout]! shoutsByUserSubscriptions(userSlug: String!, page: Int!, size: Int!): [Shout]!
shoutsByUserRatingOrComment(userSlug: String!, page: Int!, size: Int!): [Shout]! shoutsByUserRatingOrComment(userSlug: String!, page: Int!, size: Int!): [Shout]!
newShoutsWithoutRating(userSlug: String!, size: Int = 10): [Shout]! newShoutsWithoutRating(userSlug: String!, size: Int = 10): [Shout]!
} }