Files
core/resolvers/create/editor.py

143 lines
4.8 KiB
Python
Raw Normal View History

2022-11-23 17:09:35 +03:00
from datetime import datetime, timezone
2022-11-29 13:36:46 +01:00
from sqlalchemy import and_
from auth.authenticate import login_required
2022-12-01 15:45:19 +01:00
from auth.credentials import AuthCredentials
2022-08-11 08:53:14 +03:00
from base.orm import local_session
from base.resolvers import mutation
2022-07-10 21:52:57 +03:00
from orm.rbac import Resource
2022-09-22 13:31:44 +03:00
from orm.shout import Shout, ShoutAuthor, ShoutTopic
2022-11-29 13:36:46 +01:00
from orm.topic import TopicFollower, Topic
2022-07-10 21:52:57 +03:00
from orm.user import User
2022-11-21 11:13:57 +03:00
from resolvers.zine.reactions import reactions_follow, reactions_unfollow
2022-08-11 12:09:57 +03:00
from services.zine.gittask import GitTask
2022-11-24 19:26:12 +03:00
from resolvers.inbox.chats import create_chat
2022-12-04 17:03:55 +03:00
from services.inbox.storage import MessagesStorage
2023-01-16 11:32:36 +03:00
from orm.draft import DraftCollab
2022-07-10 21:52:57 +03:00
2022-06-19 14:11:14 +03:00
2022-06-19 20:54:39 +03:00
@mutation.field("createShout")
2022-06-19 14:11:14 +03:00
@login_required
async def create_shout(_, info, inp):
2022-12-01 15:45:19 +01:00
auth: AuthCredentials = info.context["request"].auth
2022-09-03 13:50:14 +03:00
topic_slugs = inp.get("topic_slugs", [])
2022-09-03 13:50:14 +03:00
if topic_slugs:
del inp["topic_slugs"]
2022-11-24 19:26:12 +03:00
body = inp.get("body")
2022-09-22 13:31:44 +03:00
with local_session() as session:
2022-11-24 19:26:12 +03:00
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,
2022-11-24 20:53:39 +03:00
"authors": authors,
"topics": inp.get("topics", []),
"mainTopic": inp.get("topics", []).pop(),
"visibility": "authors"
2022-11-24 19:26:12 +03:00
})
2022-12-01 15:45:19 +01:00
authors.remove(auth.user_id)
2022-11-24 19:26:12 +03:00
if authors:
chat = create_chat(None, info, new_shout.title, members=authors)
# create a cooperative chatroom
2022-12-01 15:45:19 +01:00
await MessagesStorage.register_chat(chat)
2022-11-24 19:26:12 +03:00
# now we should create a collab
new_collab = Collab.create({
"shout": new_shout.id,
2022-12-01 15:45:19 +01:00
"authors": [auth.user_id, ],
2022-11-24 19:26:12 +03:00
"invites": authors
})
session.add(new_collab)
2022-09-03 13:50:14 +03:00
2022-09-22 13:31:44 +03:00
# NOTE: shout made by one first author
2022-12-01 15:45:19 +01:00
sa = ShoutAuthor.create(shout=new_shout.id, user=auth.user_id)
2022-09-22 13:31:44 +03:00
session.add(sa)
2022-09-03 13:50:14 +03:00
2022-12-01 15:45:19 +01:00
reactions_follow(auth.user_id, new_shout.slug, True)
2022-09-03 13:50:14 +03:00
2022-09-22 13:31:44 +03:00
if "mainTopic" in inp:
topic_slugs.append(inp["mainTopic"])
2022-09-03 13:50:14 +03:00
2022-09-22 13:31:44 +03:00
for slug in topic_slugs:
2022-11-29 13:36:46 +01:00
topic = session.query(Topic).where(Topic.slug == slug).one()
2022-11-30 09:27:12 +03:00
st = ShoutTopic.create(shout=new_shout.id, topic=topic.id)
2022-09-22 13:31:44 +03:00
session.add(st)
2022-11-29 13:36:46 +01:00
tf = session.query(TopicFollower).where(
2022-12-01 15:45:19 +01:00
and_(TopicFollower.follower == auth.user_id, TopicFollower.topic == topic.id)
2022-11-29 13:36:46 +01:00
)
2022-09-22 13:31:44 +03:00
if not tf:
2022-12-01 15:45:19 +01:00
tf = TopicFollower.create(follower=auth.user_id, topic=topic.id, auto=True)
2022-09-22 13:31:44 +03:00
session.add(tf)
new_shout.topic_slugs = topic_slugs
session.add(new_shout)
session.commit()
2022-09-03 13:50:14 +03:00
2022-12-01 15:45:19 +01:00
# TODO
# GitTask(inp, user.username, user.email, "new shout %s" % new_shout.slug)
2022-09-03 13:50:14 +03:00
return {"shout": new_shout}
2022-06-19 14:11:14 +03:00
2022-06-19 20:54:39 +03:00
@mutation.field("updateShout")
2022-06-19 14:11:14 +03:00
@login_required
async def update_shout(_, info, inp):
2022-12-01 15:45:19 +01:00
auth: AuthCredentials = info.context["request"].auth
slug = inp["slug"]
2022-09-03 13:50:14 +03:00
2022-09-22 13:31:44 +03:00
with local_session() as session:
2022-12-01 15:45:19 +01:00
user = session.query(User).filter(User.id == auth.user_id).first()
2022-09-22 13:31:44 +03:00
shout = session.query(Shout).filter(Shout.slug == slug).first()
if not shout:
return {"error": "shout not found"}
2022-09-03 13:50:14 +03:00
2022-09-22 13:31:44 +03:00
authors = [author.id for author in shout.authors]
2022-12-01 15:45:19 +01:00
if auth.user_id not in authors:
2022-09-22 13:31:44 +03:00
scopes = auth.scopes
print(scopes)
2022-11-30 09:27:12 +03:00
if Resource.shout not in scopes:
2022-09-22 13:31:44 +03:00
return {"error": "access denied"}
else:
shout.update(inp)
2022-11-23 17:09:35 +03:00
shout.updatedAt = datetime.now(tz=timezone.utc)
2022-09-22 13:31:44 +03:00
session.add(shout)
2022-11-15 05:36:30 +03:00
if inp.get("topics"):
# remove old links
2022-11-30 09:27:12 +03:00
links = session.query(ShoutTopic).where(ShoutTopic.shout == shout.id).all()
2022-11-15 05:36:30 +03:00
for topiclink in links:
session.delete(topiclink)
# add new topic links
for topic in inp.get("topics", []):
ShoutTopic.create(shout=slug, topic=topic)
2022-09-22 13:31:44 +03:00
session.commit()
2022-09-03 13:50:14 +03:00
2022-12-01 15:45:19 +01:00
GitTask(inp, user.username, user.email, "update shout %s" % slug)
2022-09-03 13:50:14 +03:00
return {"shout": shout}
2022-06-19 14:11:14 +03:00
2022-06-19 20:54:39 +03:00
@mutation.field("deleteShout")
2022-06-19 14:11:14 +03:00
@login_required
2022-06-19 20:54:39 +03:00
async def delete_shout(_, info, slug):
2022-12-01 15:45:19 +01:00
auth: AuthCredentials = info.context["request"].auth
2022-09-03 13:50:14 +03:00
with local_session() as session:
shout = session.query(Shout).filter(Shout.slug == slug).first()
authors = [a.id for a in shout.authors]
if not shout:
return {"error": "invalid shout slug"}
2022-12-01 15:45:19 +01:00
if auth.user_id not in authors:
2022-09-03 13:50:14 +03:00
return {"error": "access denied"}
for a in authors:
2022-12-01 15:45:19 +01:00
reactions_unfollow(a.id, slug)
2022-11-23 17:09:35 +03:00
shout.deletedAt = datetime.now(tz=timezone.utc)
session.add(shout)
2022-09-03 13:50:14 +03:00
session.commit()
return {}