core/resolvers/editor.py

119 lines
2.5 KiB
Python
Raw Normal View History

2022-07-10 18:52:57 +00:00
from orm import Shout
2022-08-11 05:53:14 +00:00
from base.orm import local_session
2022-07-10 18:52:57 +00:00
from orm.rbac import Resource
from orm.shout import ShoutAuthor, ShoutTopic
from orm.user import User
2022-08-11 05:53:14 +00:00
from base.resolvers import mutation
from resolvers.reactions import reactions_follow, reactions_unfollow
2022-06-19 11:11:14 +00:00
from auth.authenticate import login_required
from datetime import datetime
from storages.gittask import GitTask
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-06-19 17:54:39 +00:00
async def create_shout(_, info, input):
user = info.context["request"].user
2022-06-19 11:11:14 +00:00
2022-06-19 17:54:39 +00:00
topic_slugs = input.get("topic_slugs", [])
if topic_slugs:
del input["topic_slugs"]
2022-06-19 11:11:14 +00:00
2022-06-19 17:54:39 +00:00
new_shout = Shout.create(**input)
ShoutAuthor.create(
shout = new_shout.slug,
2022-07-10 18:52:57 +00:00
user = user.slug
)
2022-06-29 13:05:04 +00:00
reactions_follow(user, new_shout.slug, True)
2022-06-29 13:05:04 +00:00
2022-06-19 17:54:39 +00:00
if "mainTopic" in input:
topic_slugs.append(input["mainTopic"])
for slug in topic_slugs:
topic = ShoutTopic.create(
shout = new_shout.slug,
topic = slug)
new_shout.topic_slugs = topic_slugs
task = GitTask(
input,
user.username,
user.email,
"new shout %s" % (new_shout.slug)
)
2022-07-10 18:52:57 +00:00
# await ShoutCommentsStorage.send_shout(new_shout)
2022-06-19 11:11:14 +00:00
2022-06-19 17:54:39 +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-06-19 17:54:39 +00:00
async def update_shout(_, info, input):
2022-06-19 11:11:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
2022-06-19 17:54:39 +00:00
slug = input["slug"]
session = local_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"
}
authors = [author.id for author in shout.authors]
if not user_id in authors:
scopes = auth.scopes
print(scopes)
if not Resource.shout_id in scopes:
return {
"error" : "access denied"
}
shout.update(input)
shout.updatedAt = datetime.now()
session.commit()
session.close()
for topic in input.get("topic_slugs", []):
ShoutTopic.create(
shout = slug,
topic = topic)
task = GitTask(
input,
user.username,
user.email,
"update shout %s" % (slug)
)
2022-06-19 11:11:14 +00:00
2022-06-19 17:54:39 +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-06-19 11:11:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
2022-06-19 17:54:39 +00:00
shout = session.query(Shout).filter(Shout.slug == slug).first()
2022-07-10 18:52:57 +00:00
authors = [a.id for a in shout.authors]
if not shout:
2022-06-19 17:54:39 +00:00
return {"error": "invalid shout slug"}
if user_id not in authors:
2022-06-19 11:11:14 +00:00
return {"error": "access denied"}
for a in authors:
reactions_unfollow(a.slug, slug, True)
2022-06-19 17:54:39 +00:00
shout.deletedAt = datetime.now()
2022-06-19 11:11:14 +00:00
session.commit()
2022-06-29 13:05:04 +00:00
return {}