From 2e04d2cac529dfac3e43c2426c7b72955dc4314a Mon Sep 17 00:00:00 2001 From: bniwredyc Date: Mon, 27 Mar 2023 16:46:14 +0200 Subject: [PATCH] shout create --- orm/shout.py | 1 - resolvers/create/editor.py | 87 ++++++++++++++++++++----------------- resolvers/zine/reactions.py | 9 ++-- schema.graphql | 4 +- 4 files changed, 51 insertions(+), 50 deletions(-) diff --git a/orm/shout.py b/orm/shout.py index acfdbe81..c9044a83 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -15,7 +15,6 @@ class ShoutTopic(Base): id = None # type: ignore shout = Column(ForeignKey("shout.id"), primary_key=True, index=True) topic = Column(ForeignKey("topic.id"), primary_key=True, index=True) - main = Column(Boolean, default=False) class ShoutReactionsFollower(Base): diff --git a/resolvers/create/editor.py b/resolvers/create/editor.py index 98283b30..b03a3933 100644 --- a/resolvers/create/editor.py +++ b/resolvers/create/editor.py @@ -22,58 +22,63 @@ from services.zine.gittask import GitTask async def create_shout(_, info, inp): auth: AuthCredentials = info.context["request"].auth - body = inp.get("body") with local_session() as session: - if body: - # now we should create a draft shout (can be viewed only by authors) - authors = inp.get("authors", []) - new_shout = Shout.create(**{ - "title": inp.get("title", body[:12] + '...'), - "body": body, - "authors": authors, - "slug": inp.get("slug"), - "topics": inp.get("topics", []), - "mainTopic": inp.get("topics", []).pop(), - "visibility": "authors" - }) - if auth.user_id in authors: - authors.remove(auth.user_id) - # Chat room code, uncomment it - # if authors: - # chat = create_chat(None, info, new_shout.title, members=authors) - # # create a cooperative chatroom - # await MessagesStorage.register_chat(chat) - # # now we should create a collab - # new_collab = Collab.create({ - # "shout": new_shout.id, - # "authors": [auth.user_id, ], - # "invites": authors - # }) - # session.add(new_collab) + topics = session.query(Topic).filter(Topic.slug.in_(inp.get('topics', []))).all() + + new_shout = Shout.create(**{ + "title": inp.get("title"), + "subtitle": inp.get('subtitle'), + "body": inp.get("body"), + "authors": inp.get("authors", []), + "slug": inp.get("slug"), + "mainTopic": inp.get("mainTopic"), + "visibility": "community", + # "createdBy": auth.user_id + }) + + for topic in topics: + t = ShoutTopic.create(topic=topic.id, shout=new_shout.id) + session.add(t) + + # if auth.user_id in authors: + # authors.remove(auth.user_id) + # Chat room code, uncomment it + # if authors: + # chat = create_chat(None, info, new_shout.title, members=authors) + # # create a cooperative chatroom + # await MessagesStorage.register_chat(chat) + # # now we should create a collab + # new_collab = Collab.create({ + # "shout": new_shout.id, + # "authors": [auth.user_id, ], + # "invites": authors + # }) + # session.add(new_collab) # NOTE: shout made by one first author sa = ShoutAuthor.create(shout=new_shout.id, user=auth.user_id) session.add(sa) - if "mainTopic" in inp: - new_shout.topics.append(inp["mainTopic"]) + # if "mainTopic" in inp: + # new_shout.topics.append(inp["mainTopic"]) session.add(new_shout) reactions_follow(auth.user_id, new_shout.id, True) - for slug in new_shout.topics: - topic = session.query(Topic).where(Topic.slug == slug).one() - - st = ShoutTopic.create(shout=new_shout.id, topic=topic.id) - session.add(st) - tf = session.query(TopicFollower).where( - and_(TopicFollower.follower == auth.user_id, TopicFollower.topic == topic.id) - ) - - if not tf: - tf = TopicFollower.create(follower=auth.user_id, topic=topic.id, auto=True) - session.add(tf) + # for slug in new_shout.topics: + # topic = session.query(Topic).where(Topic.slug == slug).one() + # + # st = ShoutTopic.create(shout=new_shout.id, topic=topic.id) + # session.add(st) + # + # tf = session.query(TopicFollower).where( + # and_(TopicFollower.follower == auth.user_id, TopicFollower.topic == topic.id) + # ) + # + # if not tf: + # tf = TopicFollower.create(follower=auth.user_id, topic=topic.id, auto=True) + # session.add(tf) session.commit() diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index 03ec1a57..9ee2f098 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -142,10 +142,9 @@ def check_to_hide(session, user_id, reaction): return False -def set_published(session, shout_id, publisher): +def set_published(session, shout_id): s = session.query(Shout).where(Shout.id == shout_id).first() s.publishedAt = datetime.now(tz=timezone.utc) - s.publishedBy = publisher s.visibility = text('public') session.add(s) session.commit() @@ -153,9 +152,7 @@ def set_published(session, shout_id, publisher): def set_hidden(session, shout_id): s = session.query(Shout).where(Shout.id == shout_id).first() - s.visibility = text('authors') - s.publishedAt = None # TODO: discuss - s.publishedBy = None # TODO: store changes history in git + s.visibility = text('community') session.add(s) session.commit() @@ -227,7 +224,7 @@ async def create_reaction(_, info, reaction): if check_to_hide(session, auth.user_id, r): set_hidden(session, r.shout) elif check_to_publish(session, auth.user_id, r): - set_published(session, r.shout, r.createdBy) + set_published(session, r.shout) try: reactions_follow(auth.user_id, reaction["shout"], True) diff --git a/schema.graphql b/schema.graphql index 3598ba57..74036c6f 100644 --- a/schema.graphql +++ b/schema.graphql @@ -455,7 +455,6 @@ type Shout { updatedBy: User deletedAt: DateTime deletedBy: User - publishedBy: User publishedAt: DateTime media: String # json [ { title pic url body }, .. ] stat: Stat @@ -501,7 +500,8 @@ type TopicStat { } type Topic { - slug: String! # ID + id: Int! + slug: String! title: String body: String pic: String