diff --git a/resolvers/create/editor.py b/resolvers/create/editor.py index 79aa8dbf..d11a4111 100644 --- a/resolvers/create/editor.py +++ b/resolvers/create/editor.py @@ -93,63 +93,34 @@ async def create_shout(_, info, inp): @mutation.field("updateShout") @login_required -async def update_shout(_, info, shout_id, shout_input): +async def update_shout(_, info, shout_id, shout_input=None, publish=False): auth: AuthCredentials = info.context["request"].auth with local_session() as session: shout = session.query(Shout).filter(Shout.id == shout_id).first() - if not shout: - return {"error": "shout not found"} - - authors = [author.id for author in shout.authors] - if auth.user_id not in authors: - scopes = auth.scopes - print(scopes) - if Resource.shout not in scopes: - return {"error": "access denied"} - else: - shout.update(shout_input) - shout.updatedAt = datetime.now(tz=timezone.utc) - - if shout_input.get("topics"): - # remove old links - links = session.query(ShoutTopic).where(ShoutTopic.shout == shout.id).all() - for topiclink in links: - session.delete(topiclink) - # add new topic links - # for topic_slug in inp.get("topics", []): - # topic = session.query(Topic).filter(Topic.slug == topic_slug).first() - # shout_topic = ShoutTopic.create(shout=shout.id, topic=topic.id) - # session.add(shout_topic) - session.commit() - # GitTask(inp, user.username, user.email, "update shout %s" % slug) - - return {"shout": shout} - - -@mutation.field("publishShout") -@login_required -async def publish_shout(_, info, shout_id, shout_input=None): - if shout_input is None: - shout_input = {} - auth: AuthCredentials = info.context["request"].auth - - with local_session() as session: - shout = session.query(Shout).filter(Shout.id == shout_id).first() if not shout: return {"error": "shout not found"} if shout.createdBy != auth.user_id: return {"error": "access denied"} + updated = False + if shout_input is not None: shout.update(shout_input) + updated = True + + if publish and shout.visibility == 'owner': + shout.visibility = "community" + shout.publishedAt = datetime.now(tz=timezone.utc) + updated = True + + if updated: + shout.updatedAt = datetime.now(tz=timezone.utc) - shout.visibility = "community" - shout.updatedAt = datetime.now(tz=timezone.utc) - shout.publishedAt = datetime.now(tz=timezone.utc) session.commit() + # GitTask(inp, user.username, user.email, "update shout %s" % slug) return {"shout": shout} @@ -161,12 +132,16 @@ async def delete_shout(_, info, shout_id): with local_session() as session: shout = session.query(Shout).filter(Shout.id == shout_id).first() + if not shout: return {"error": "invalid shout id"} + if auth.user_id != shout.createdBy: return {"error": "access denied"} + for author_id in shout.authors: reactions_unfollow(author_id, shout_id) + shout.deletedAt = datetime.now(tz=timezone.utc) session.commit() diff --git a/resolvers/zine/load.py b/resolvers/zine/load.py index 19a250b8..888c1221 100644 --- a/resolvers/zine/load.py +++ b/resolvers/zine/load.py @@ -70,17 +70,27 @@ def apply_filters(q, filters, user_id=None): return q + @query.field("loadShout") -async def load_shout(_, info, slug): +async def load_shout(_, info, slug=None, shout_id=None): with local_session() as session: q = select(Shout).options( joinedload(Shout.authors), joinedload(Shout.topics), ) q = add_stat_columns(q) + + if slug is not None: + q = q.filter( + Shout.slug == slug + ) + + if shout_id is not None: + q = q.filter( + Shout.id == shout_id + ) + q = q.filter( - Shout.slug == slug - ).filter( Shout.deletedAt.is_(None) ).group_by(Shout.id) @@ -195,6 +205,7 @@ async def load_shouts_by(_, info, options): return shouts + @query.field("loadDrafts") async def get_drafts(_, info): auth: AuthCredentials = info.context["request"].auth @@ -217,7 +228,6 @@ async def get_drafts(_, info): return shouts - @query.field("myFeed") @login_required async def get_my_feed(_, info, options): diff --git a/schema.graphql b/schema.graphql index 04fccba0..e34db3ab 100644 --- a/schema.graphql +++ b/schema.graphql @@ -168,9 +168,8 @@ type Mutation { # shout createShout(inp: ShoutInput!): Result! - updateShout(shout_id: Int!, shout_input: ShoutInput!): Result! + updateShout(shout_id: Int!, shout_input: ShoutInput, publish: Boolean): Result! deleteShout(shout_id: Int!): Result! - publishShout(shout_id: Int!, shout_input: ShoutInput): Result! # user profile rateUser(slug: String!, value: Int!): Result! @@ -274,7 +273,7 @@ type Query { # zine loadAuthorsBy(by: AuthorsBy, limit: Int, offset: Int): [Author]! - loadShout(slug: String!): Shout + loadShout(slug: String, shout_id: Int): Shout loadShouts(options: LoadShoutsOptions): [Shout]! loadDrafts: [Shout]! loadReactionsBy(by: ReactionBy!, limit: Int, offset: Int): [Reaction]!