2022-11-23 14:09:35 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-09-17 18:12:14 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
from sqlalchemy import and_, select
|
2023-05-11 11:03:14 +00:00
|
|
|
from sqlalchemy.orm import joinedload
|
2022-11-29 12:36:46 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
from services.auth import login_required
|
2023-10-23 14:51:13 +00:00
|
|
|
from services.db import local_session
|
|
|
|
from services.schema import mutation, query
|
2022-09-22 10:31:44 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
2023-05-14 17:02:26 +00:00
|
|
|
from orm.topic import Topic
|
2023-10-23 14:47:11 +00:00
|
|
|
from reaction import reactions_follow, reactions_unfollow
|
2023-10-16 15:18:29 +00:00
|
|
|
from services.presence import notify_shout
|
2022-07-10 18:52:57 +00:00
|
|
|
|
2022-06-19 11:11:14 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
@query.field("loadDrafts")
|
|
|
|
async def get_drafts(_, info):
|
|
|
|
author = info.context["request"].author
|
|
|
|
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.where(and_(Shout.deletedAt.is_(None), Shout.createdBy == author.id))
|
|
|
|
)
|
|
|
|
|
|
|
|
q = q.group_by(Shout.id)
|
|
|
|
|
|
|
|
shouts = []
|
|
|
|
with local_session() as session:
|
|
|
|
for [shout] in session.execute(q).unique():
|
|
|
|
shouts.append(shout)
|
|
|
|
|
|
|
|
return shouts
|
|
|
|
|
|
|
|
|
2022-06-19 17:54:39 +00:00
|
|
|
@mutation.field("createShout")
|
2022-06-19 11:11:14 +00:00
|
|
|
@login_required
|
2022-09-17 18:12:14 +00:00
|
|
|
async def create_shout(_, info, inp):
|
2023-10-23 14:47:11 +00:00
|
|
|
author_id = info.context["author_id"]
|
2022-09-22 10:31:44 +00:00
|
|
|
with local_session() as session:
|
2023-10-05 18:46:18 +00:00
|
|
|
topics = (
|
|
|
|
session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all()
|
|
|
|
)
|
|
|
|
|
|
|
|
new_shout = Shout.create(
|
|
|
|
**{
|
|
|
|
"title": inp.get("title"),
|
|
|
|
"subtitle": inp.get("subtitle"),
|
|
|
|
"lead": inp.get("lead"),
|
|
|
|
"description": inp.get("description"),
|
|
|
|
"body": inp.get("body", ""),
|
|
|
|
"layout": inp.get("layout"),
|
|
|
|
"authors": inp.get("authors", []),
|
|
|
|
"slug": inp.get("slug"),
|
|
|
|
"mainTopic": inp.get("mainTopic"),
|
2023-10-23 14:47:11 +00:00
|
|
|
"visibility": "authors",
|
|
|
|
"createdBy": author_id,
|
2023-10-05 18:46:18 +00:00
|
|
|
}
|
|
|
|
)
|
2023-03-27 14:46:14 +00:00
|
|
|
|
|
|
|
for topic in topics:
|
|
|
|
t = ShoutTopic.create(topic=topic.id, shout=new_shout.id)
|
|
|
|
session.add(t)
|
|
|
|
|
2022-09-22 10:31:44 +00:00
|
|
|
# NOTE: shout made by one first author
|
2023-10-23 14:47:11 +00:00
|
|
|
sa = ShoutAuthor.create(shout=new_shout.id, author=author_id)
|
2022-09-22 10:31:44 +00:00
|
|
|
session.add(sa)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-01-23 15:25:53 +00:00
|
|
|
session.add(new_shout)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
reactions_follow(author_id, new_shout.id, True)
|
2023-01-23 15:25:53 +00:00
|
|
|
|
2022-09-22 10:31:44 +00:00
|
|
|
session.commit()
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-05-03 15:47:09 +00:00
|
|
|
# TODO
|
|
|
|
# GitTask(inp, user.username, user.email, "new shout %s" % new_shout.slug)
|
|
|
|
|
|
|
|
if new_shout.slug is None:
|
|
|
|
new_shout.slug = f"draft-{new_shout.id}"
|
|
|
|
session.commit()
|
2023-10-23 14:47:11 +00:00
|
|
|
else:
|
|
|
|
notify_shout(new_shout.dict(), "create")
|
2022-09-03 10:50:14 +00:00
|
|
|
|
|
|
|
return {"shout": new_shout}
|
|
|
|
|
2022-06-19 11:11:14 +00:00
|
|
|
|
2022-06-19 17:54:39 +00:00
|
|
|
@mutation.field("updateShout")
|
2022-06-19 11:11:14 +00:00
|
|
|
@login_required
|
2023-05-08 17:06:01 +00:00
|
|
|
async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
2023-10-23 14:47:11 +00:00
|
|
|
author_id = info.context["author_id"]
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-09-22 10:31:44 +00:00
|
|
|
with local_session() as session:
|
2023-10-05 18:46:18 +00:00
|
|
|
shout = (
|
|
|
|
session.query(Shout)
|
|
|
|
.options(
|
|
|
|
joinedload(Shout.authors),
|
|
|
|
joinedload(Shout.topics),
|
|
|
|
)
|
|
|
|
.filter(Shout.id == shout_id)
|
|
|
|
.first()
|
|
|
|
)
|
2023-05-03 15:47:09 +00:00
|
|
|
|
|
|
|
if not shout:
|
|
|
|
return {"error": "shout not found"}
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
if shout.createdBy != author_id:
|
2023-05-08 12:28:17 +00:00
|
|
|
return {"error": "access denied"}
|
|
|
|
|
2023-05-08 17:06:01 +00:00
|
|
|
updated = False
|
|
|
|
|
2023-05-08 12:28:17 +00:00
|
|
|
if shout_input is not None:
|
2023-05-11 11:03:14 +00:00
|
|
|
topics_input = shout_input["topics"]
|
|
|
|
del shout_input["topics"]
|
|
|
|
|
|
|
|
new_topics_to_link = []
|
2023-10-05 18:46:18 +00:00
|
|
|
new_topics = [
|
|
|
|
topic_input for topic_input in topics_input if topic_input["id"] < 0
|
|
|
|
]
|
2023-05-11 11:03:14 +00:00
|
|
|
|
|
|
|
for new_topic in new_topics:
|
|
|
|
del new_topic["id"]
|
|
|
|
created_new_topic = Topic.create(**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:
|
2023-10-05 18:46:18 +00:00
|
|
|
created_unlinked_topic = ShoutTopic.create(
|
|
|
|
shout=shout.id, topic=new_topic_to_link.id
|
|
|
|
)
|
2023-05-11 11:03:14 +00:00
|
|
|
session.add(created_unlinked_topic)
|
|
|
|
|
2023-10-05 18:46:18 +00:00
|
|
|
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]
|
|
|
|
]
|
2023-05-11 11:03:14 +00:00
|
|
|
|
|
|
|
for existing_topic_to_link_id in existing_topic_to_link_ids:
|
2023-10-05 18:46:18 +00:00
|
|
|
created_unlinked_topic = ShoutTopic.create(
|
|
|
|
shout=shout.id, topic=existing_topic_to_link_id
|
|
|
|
)
|
2023-05-11 11:03:14 +00:00
|
|
|
session.add(created_unlinked_topic)
|
|
|
|
|
2023-10-05 18:46:18 +00:00
|
|
|
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]
|
|
|
|
]
|
2023-05-11 11:03:14 +00:00
|
|
|
|
|
|
|
shout_topics_to_remove = session.query(ShoutTopic).filter(
|
|
|
|
and_(
|
|
|
|
ShoutTopic.shout == shout.id,
|
2023-10-05 18:46:18 +00:00
|
|
|
ShoutTopic.topic.in_(topic_to_unlink_ids),
|
2023-05-11 11:03:14 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for shout_topic_to_remove in shout_topics_to_remove:
|
|
|
|
session.delete(shout_topic_to_remove)
|
|
|
|
|
|
|
|
shout_input["mainTopic"] = shout_input["mainTopic"]["slug"]
|
|
|
|
|
2023-10-05 18:46:18 +00:00
|
|
|
if shout_input["mainTopic"] == "":
|
2023-05-11 11:03:14 +00:00
|
|
|
del shout_input["mainTopic"]
|
|
|
|
|
2023-05-08 12:28:17 +00:00
|
|
|
shout.update(shout_input)
|
2023-05-08 17:06:01 +00:00
|
|
|
updated = True
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
# TODO: use visibility setting
|
|
|
|
|
|
|
|
if publish and shout.visibility == "authors":
|
2023-05-08 17:06:01 +00:00
|
|
|
shout.visibility = "community"
|
|
|
|
shout.publishedAt = datetime.now(tz=timezone.utc)
|
|
|
|
updated = True
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
# notify on publish
|
2023-10-16 15:18:29 +00:00
|
|
|
notify_shout(shout.dict())
|
2023-05-08 17:06:01 +00:00
|
|
|
|
|
|
|
if updated:
|
|
|
|
shout.updatedAt = datetime.now(tz=timezone.utc)
|
2023-05-08 12:28:17 +00:00
|
|
|
|
|
|
|
session.commit()
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-05-08 17:06:01 +00:00
|
|
|
# GitTask(inp, user.username, user.email, "update shout %s" % slug)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
notify_shout(shout.dict(), "update")
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
return {"shout": shout}
|
|
|
|
|
2022-06-19 11:11:14 +00:00
|
|
|
|
2022-06-19 17:54:39 +00:00
|
|
|
@mutation.field("deleteShout")
|
2022-06-19 11:11:14 +00:00
|
|
|
@login_required
|
2023-05-08 12:28:17 +00:00
|
|
|
async def delete_shout(_, info, shout_id):
|
2023-10-23 14:47:11 +00:00
|
|
|
author_id = info.context["author_id"]
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
2023-05-08 12:28:17 +00:00
|
|
|
shout = session.query(Shout).filter(Shout.id == shout_id).first()
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
if not shout:
|
2023-05-08 12:28:17 +00:00
|
|
|
return {"error": "invalid shout id"}
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
if author_id != shout.createdBy:
|
2022-09-03 10:50:14 +00:00
|
|
|
return {"error": "access denied"}
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2023-05-08 12:28:17 +00:00
|
|
|
for author_id in shout.authors:
|
|
|
|
reactions_unfollow(author_id, shout_id)
|
2023-05-08 17:06:01 +00:00
|
|
|
|
2022-11-23 14:09:35 +00:00
|
|
|
shout.deletedAt = datetime.now(tz=timezone.utc)
|
2022-09-03 10:50:14 +00:00
|
|
|
session.commit()
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
notify_shout(shout.dict(), "delete")
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
return {}
|