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

@ -15,7 +15,6 @@ class ShoutTopic(Base):
id = None # type: ignore id = None # type: ignore
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True) shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
topic = Column(ForeignKey("topic.id"), primary_key=True, index=True) topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
main = Column(Boolean, default=False)
class ShoutReactionsFollower(Base): class ShoutReactionsFollower(Base):

View File

@ -22,22 +22,26 @@ from services.zine.gittask import GitTask
async def create_shout(_, info, inp): async def create_shout(_, info, inp):
auth: AuthCredentials = info.context["request"].auth auth: AuthCredentials = info.context["request"].auth
body = inp.get("body")
with local_session() as session: with local_session() as session:
if body: topics = session.query(Topic).filter(Topic.slug.in_(inp.get('topics', []))).all()
# now we should create a draft shout (can be viewed only by authors)
authors = inp.get("authors", [])
new_shout = Shout.create(**{ new_shout = Shout.create(**{
"title": inp.get("title", body[:12] + '...'), "title": inp.get("title"),
"body": body, "subtitle": inp.get('subtitle'),
"authors": authors, "body": inp.get("body"),
"authors": inp.get("authors", []),
"slug": inp.get("slug"), "slug": inp.get("slug"),
"topics": inp.get("topics", []), "mainTopic": inp.get("mainTopic"),
"mainTopic": inp.get("topics", []).pop(), "visibility": "community",
"visibility": "authors" # "createdBy": auth.user_id
}) })
if auth.user_id in authors:
authors.remove(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 # Chat room code, uncomment it
# if authors: # if authors:
# chat = create_chat(None, info, new_shout.title, members=authors) # chat = create_chat(None, info, new_shout.title, members=authors)
@ -55,25 +59,26 @@ async def create_shout(_, info, inp):
sa = ShoutAuthor.create(shout=new_shout.id, user=auth.user_id) sa = ShoutAuthor.create(shout=new_shout.id, user=auth.user_id)
session.add(sa) session.add(sa)
if "mainTopic" in inp: # if "mainTopic" in inp:
new_shout.topics.append(inp["mainTopic"]) # new_shout.topics.append(inp["mainTopic"])
session.add(new_shout) session.add(new_shout)
reactions_follow(auth.user_id, new_shout.id, True) reactions_follow(auth.user_id, new_shout.id, True)
for slug in new_shout.topics: # for slug in new_shout.topics:
topic = session.query(Topic).where(Topic.slug == slug).one() # topic = session.query(Topic).where(Topic.slug == slug).one()
#
st = ShoutTopic.create(shout=new_shout.id, topic=topic.id) # st = ShoutTopic.create(shout=new_shout.id, topic=topic.id)
session.add(st) # session.add(st)
tf = session.query(TopicFollower).where( #
and_(TopicFollower.follower == auth.user_id, TopicFollower.topic == topic.id) # 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) # if not tf:
session.add(tf) # tf = TopicFollower.create(follower=auth.user_id, topic=topic.id, auto=True)
# session.add(tf)
session.commit() session.commit()

View File

@ -142,10 +142,9 @@ def check_to_hide(session, user_id, reaction):
return False 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 = session.query(Shout).where(Shout.id == shout_id).first()
s.publishedAt = datetime.now(tz=timezone.utc) s.publishedAt = datetime.now(tz=timezone.utc)
s.publishedBy = publisher
s.visibility = text('public') s.visibility = text('public')
session.add(s) session.add(s)
session.commit() session.commit()
@ -153,9 +152,7 @@ def set_published(session, shout_id, publisher):
def set_hidden(session, shout_id): def set_hidden(session, shout_id):
s = session.query(Shout).where(Shout.id == shout_id).first() s = session.query(Shout).where(Shout.id == shout_id).first()
s.visibility = text('authors') s.visibility = text('community')
s.publishedAt = None # TODO: discuss
s.publishedBy = None # TODO: store changes history in git
session.add(s) session.add(s)
session.commit() session.commit()
@ -227,7 +224,7 @@ async def create_reaction(_, info, reaction):
if check_to_hide(session, auth.user_id, r): if check_to_hide(session, auth.user_id, r):
set_hidden(session, r.shout) set_hidden(session, r.shout)
elif check_to_publish(session, auth.user_id, r): elif check_to_publish(session, auth.user_id, r):
set_published(session, r.shout, r.createdBy) set_published(session, r.shout)
try: try:
reactions_follow(auth.user_id, reaction["shout"], True) reactions_follow(auth.user_id, reaction["shout"], True)

View File

@ -455,7 +455,6 @@ type Shout {
updatedBy: User updatedBy: User
deletedAt: DateTime deletedAt: DateTime
deletedBy: User deletedBy: User
publishedBy: User
publishedAt: DateTime publishedAt: DateTime
media: String # json [ { title pic url body }, .. ] media: String # json [ { title pic url body }, .. ]
stat: Stat stat: Stat
@ -501,7 +500,8 @@ type TopicStat {
} }
type Topic { type Topic {
slug: String! # ID id: Int!
slug: String!
title: String title: String
body: String body: String
pic: String pic: String