iffix
All checks were successful
deploy / deploy (push) Successful in 2m20s

This commit is contained in:
Untone 2023-11-27 19:15:34 +03:00
parent caa2dbfdf3
commit 53a0f2e328

View File

@ -18,19 +18,20 @@ async def get_drafts(_, info):
user_id = info.context["user_id"] user_id = info.context["user_id"]
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
q = ( if author:
select(Shout) q = (
.options( select(Shout)
joinedload(Shout.authors), .options(
joinedload(Shout.topics), joinedload(Shout.authors),
joinedload(Shout.topics),
)
.where(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id))
) )
.where(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id)) q = q.group_by(Shout.id)
) shouts = []
q = q.group_by(Shout.id) for [shout] in session.execute(q).unique():
shouts = [] shouts.append(shout)
for [shout] in session.execute(q).unique(): return shouts
shouts.append(shout)
return shouts
@mutation.field("createShout") @mutation.field("createShout")
@ -39,42 +40,43 @@ async def create_shout(_, info, inp):
user_id = info.context["user_id"] user_id = info.context["user_id"]
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
topics = session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all() if author:
authors = inp.get("authors", []) topics = session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all()
if author.id not in authors: authors = inp.get("authors", [])
authors.insert(0, author.id) if author.id not in authors:
current_time = int(time.time()) authors.insert(0, author.id)
new_shout = Shout( current_time = int(time.time())
**{ new_shout = Shout(
"title": inp.get("title"), **{
"subtitle": inp.get("subtitle"), "title": inp.get("title"),
"lead": inp.get("lead"), "subtitle": inp.get("subtitle"),
"description": inp.get("description"), "lead": inp.get("lead"),
"body": inp.get("body", ""), "description": inp.get("description"),
"layout": inp.get("layout"), "body": inp.get("body", ""),
"authors": authors, "layout": inp.get("layout"),
"slug": inp.get("slug"), "authors": authors,
"topics": inp.get("topics"), "slug": inp.get("slug"),
"visibility": ShoutVisibility.AUTHORS, "topics": inp.get("topics"),
"created_at": current_time, # Set created_at as Unix timestamp "visibility": ShoutVisibility.AUTHORS,
} "created_at": current_time, # Set created_at as Unix timestamp
) }
for topic in topics: )
t = ShoutTopic(topic=topic.id, shout=new_shout.id) for topic in topics:
session.add(t) t = ShoutTopic(topic=topic.id, shout=new_shout.id)
# NOTE: shout made by one first author session.add(t)
sa = ShoutAuthor(shout=new_shout.id, author=author.id) # NOTE: shout made by one first author
session.add(sa) sa = ShoutAuthor(shout=new_shout.id, author=author.id)
session.add(new_shout) session.add(sa)
reactions_follow(author.id, new_shout.id, True) session.add(new_shout)
session.commit() reactions_follow(author.id, new_shout.id, True)
if new_shout.slug is None:
new_shout.slug = f"draft-{new_shout.id}"
session.commit() session.commit()
else:
await notify_shout(new_shout.dict(), "create") if new_shout.slug is None:
return {"shout": new_shout} new_shout.slug = f"draft-{new_shout.id}"
session.commit()
else:
await notify_shout(new_shout.dict(), "create")
return {"shout": new_shout}
@mutation.field("updateShout") @mutation.field("updateShout")
@ -83,79 +85,80 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
user_id = info.context["user_id"] user_id = info.context["user_id"]
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
shout = ( if author:
session.query(Shout) shout = (
.options( session.query(Shout)
joinedload(Shout.authors), .options(
joinedload(Shout.topics), joinedload(Shout.authors),
) joinedload(Shout.topics),
.filter(Shout.id == shout_id)
.first()
)
if not shout:
return {"error": "shout not found"}
if shout.created_by != author.id:
return {"error": "access denied"}
updated = False
if shout_input is not None:
topics_input = shout_input["topics"]
del shout_input["topics"]
new_topics_to_link = []
new_topics = [topic_input for topic_input in topics_input if topic_input["id"] < 0]
for new_topic in new_topics:
del new_topic["id"]
created_new_topic = Topic(**new_topic)
session.add(created_new_topic)
new_topics_to_link.append(created_new_topic)
if len(new_topics) > 0:
session.commit()
for new_topic_to_link in new_topics_to_link:
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
session.add(created_unlinked_topic)
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0]
existing_topic_to_link_ids = [
existing_topic_input["id"]
for existing_topic_input in existing_topics_input
if existing_topic_input["id"] not in [topic.id for topic in shout.topics]
]
for existing_topic_to_link_id in existing_topic_to_link_ids:
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id)
session.add(created_unlinked_topic)
topic_to_unlink_ids = [
topic.id
for topic in shout.topics
if topic.id not in [topic_input["id"] for topic_input in existing_topics_input]
]
shout_topics_to_remove = session.query(ShoutTopic).filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.topic.in_(topic_to_unlink_ids),
) )
.filter(Shout.id == shout_id)
.first()
) )
for shout_topic_to_remove in shout_topics_to_remove: if not shout:
session.delete(shout_topic_to_remove) return {"error": "shout not found"}
shout_input["mainTopic"] = shout_input["mainTopic"]["slug"] if shout.created_by != author.id:
if shout_input["mainTopic"] == "": return {"error": "access denied"}
del shout_input["mainTopic"] updated = False
# Replace datetime with Unix timestamp if shout_input is not None:
current_time = int(time.time()) topics_input = shout_input["topics"]
shout_input["updated_at"] = current_time # Set updated_at as Unix timestamp del shout_input["topics"]
Shout.update(shout, shout_input) new_topics_to_link = []
session.add(shout) new_topics = [topic_input for topic_input in topics_input if topic_input["id"] < 0]
updated = True for new_topic in new_topics:
# TODO: use visibility setting del new_topic["id"]
if publish and shout.visibility == ShoutVisibility.AUTHORS: created_new_topic = Topic(**new_topic)
shout.visibility = ShoutVisibility.COMMUNITY session.add(created_new_topic)
shout.published_at = current_time # Set published_at as Unix timestamp new_topics_to_link.append(created_new_topic)
session.add(shout) if len(new_topics) > 0:
updated = True session.commit()
# notify on publish for new_topic_to_link in new_topics_to_link:
await notify_shout(shout.dict(), "public") created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
if updated: session.add(created_unlinked_topic)
session.commit() existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0]
if not publish: existing_topic_to_link_ids = [
await notify_shout(shout.dict(), "update") existing_topic_input["id"]
return {"shout": shout} for existing_topic_input in existing_topics_input
if existing_topic_input["id"] not in [topic.id for topic in shout.topics]
]
for existing_topic_to_link_id in existing_topic_to_link_ids:
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id)
session.add(created_unlinked_topic)
topic_to_unlink_ids = [
topic.id
for topic in shout.topics
if topic.id not in [topic_input["id"] for topic_input in existing_topics_input]
]
shout_topics_to_remove = session.query(ShoutTopic).filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.topic.in_(topic_to_unlink_ids),
)
)
for shout_topic_to_remove in shout_topics_to_remove:
session.delete(shout_topic_to_remove)
shout_input["mainTopic"] = shout_input["mainTopic"]["slug"]
if shout_input["mainTopic"] == "":
del shout_input["mainTopic"]
# Replace datetime with Unix timestamp
current_time = int(time.time())
shout_input["updated_at"] = current_time # Set updated_at as Unix timestamp
Shout.update(shout, shout_input)
session.add(shout)
updated = True
# TODO: use visibility setting
if publish and shout.visibility == ShoutVisibility.AUTHORS:
shout.visibility = ShoutVisibility.COMMUNITY
shout.published_at = current_time # Set published_at as Unix timestamp
session.add(shout)
updated = True
# notify on publish
await notify_shout(shout.dict(), "public")
if updated:
session.commit()
if not publish:
await notify_shout(shout.dict(), "update")
return {"shout": shout}
@mutation.field("deleteShout") @mutation.field("deleteShout")