diff --git a/orm/shout.py b/orm/shout.py index be28bb9d..0631c7bc 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -16,7 +16,7 @@ class ShoutAuthor(Base): id = None 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): __tablename__ = "shout_viewer" @@ -198,7 +198,7 @@ class TopicStat: subs = session.query(TopicSubscription) for sub in subs: topic = sub.topic - user = sub.user + user = sub.subscriber if topic in self.subs_by_topic: self.subs_by_topic[topic].append(user) else: diff --git a/resolvers/zine.py b/resolvers/zine.py index 231e8532..ed6413c8 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -1,7 +1,8 @@ from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource,\ ShoutRatingStorage, ShoutViewStorage, Comment, CommentRating, Topic 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 @@ -222,7 +223,7 @@ async def create_shout(_, info, input): new_shout = Shout.create(**input) ShoutAuthor.create( shout = new_shout.slug, - user = user.id) + user = user.slug) if "mainTopic" in input: topic_slugs.append(input["mainTopic"]) @@ -375,7 +376,7 @@ async def shouts_by_author(_, info, author, page, size): shouts = session.query(Shout).\ join(ShoutAuthor).\ - where(and_(ShoutAuthor.user == user.id, Shout.publishedAt != None)).\ + where(and_(ShoutAuthor.user == author, Shout.publishedAt != None)).\ order_by(desc(Shout.publishedAt)).\ limit(size).\ offset(page * size) @@ -397,6 +398,28 @@ async def shouts_by_community(_, info, community, page, size): offset(page * size) 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") async def shouts_by_user_rating_or_comment(_, info, userSlug, page, size): user = await UserStorage.get_user_by_slug(userSlug) diff --git a/schema.graphql b/schema.graphql index 5427fbf4..099702e3 100644 --- a/schema.graphql +++ b/schema.graphql @@ -197,7 +197,7 @@ type Query { getCommunity(slug: String): Community! getCommunities: [Community]! - # shoutsByUserSubscriptions(): [Shout]! + shoutsByUserSubscriptions(userSlug: String!, page: Int!, size: Int!): [Shout]! shoutsByUserRatingOrComment(userSlug: String!, page: Int!, size: Int!): [Shout]! newShoutsWithoutRating(userSlug: String!, size: Int = 10): [Shout]! }