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