shout create

This commit is contained in:
bniwredyc
2023-03-27 16:46:14 +02:00
parent 31b4f8106c
commit 2e04d2cac5
4 changed files with 51 additions and 50 deletions

View File

@@ -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()

View File

@@ -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)