core/resolvers/editor.py

98 lines
2.7 KiB
Python
Raw Normal View History

from datetime import datetime
from auth.authenticate import login_required
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import mutation
from orm import Shout
2022-07-10 18:52:57 +00:00
from orm.rbac import Resource
from orm.shout import ShoutAuthor, ShoutTopic
from orm.user import User
from resolvers.reactions import reactions_follow, reactions_unfollow
2022-08-11 09:09:57 +00:00
from services.zine.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
async def create_shout(_, info, inp):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
topic_slugs = inp.get("topic_slugs", [])
2022-09-03 10:50:14 +00:00
if topic_slugs:
del inp["topic_slugs"]
2022-09-03 10:50:14 +00:00
new_shout = Shout.create(**inp)
2022-09-03 10:50:14 +00:00
ShoutAuthor.create(shout=new_shout.slug, user=user.slug)
reactions_follow(user, new_shout.slug, True)
if "mainTopic" in inp:
topic_slugs.append(inp["mainTopic"])
2022-09-03 10:50:14 +00:00
for slug in topic_slugs:
2022-09-04 17:20:38 +00:00
ShoutTopic.create(shout=new_shout.slug, topic=slug)
2022-09-03 10:50:14 +00:00
new_shout.topic_slugs = topic_slugs
GitTask(inp, user.username, user.email, "new shout %s" % (new_shout.slug))
2022-09-03 10:50:14 +00:00
# await ShoutCommentsStorage.send_shout(new_shout)
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
async def update_shout(_, info, inp):
2022-09-03 10:50:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
slug = inp["slug"]
2022-09-03 10:50:14 +00:00
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]
2022-09-04 17:20:38 +00:00
if user_id not in authors:
2022-09-03 10:50:14 +00:00
scopes = auth.scopes
print(scopes)
2022-09-04 17:20:38 +00:00
if Resource.shout_id not in scopes:
2022-09-03 10:50:14 +00:00
return {"error": "access denied"}
shout.update(inp)
2022-09-03 10:50:14 +00:00
shout.updatedAt = datetime.now()
session.add(shout)
2022-09-03 10:50:14 +00:00
session.commit()
session.close()
for topic in inp.get("topic_slugs", []):
2022-09-03 10:50:14 +00:00
ShoutTopic.create(shout=slug, topic=topic)
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)
shout.deletedAt = datetime.now()
session.add(shout)
2022-09-03 10:50:14 +00:00
session.commit()
return {}