2022-07-21 11:58:50 +00:00
|
|
|
from datetime import datetime
|
2022-06-19 17:54:39 +00:00
|
|
|
from orm.base import local_session
|
2022-07-10 18:52:57 +00:00
|
|
|
from orm.shout import Shout
|
|
|
|
from orm.user import User
|
2022-07-21 11:58:50 +00:00
|
|
|
from resolvers.base import mutation
|
2022-06-19 17:54:39 +00:00
|
|
|
from auth.authenticate import login_required
|
2022-06-22 05:28:42 +00:00
|
|
|
|
|
|
|
@mutation.field("inviteAuthor")
|
|
|
|
@login_required
|
2022-07-10 18:52:57 +00:00
|
|
|
async def invite_author(_, info, author, shout):
|
2022-06-22 05:28:42 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == shout).first()
|
|
|
|
if not shout:
|
|
|
|
return {"error": "invalid shout slug"}
|
2022-07-10 18:52:57 +00:00
|
|
|
authors = [a.id for a in shout.authors]
|
2022-06-22 05:28:42 +00:00
|
|
|
if user_id not in authors:
|
|
|
|
return {"error": "access denied"}
|
2022-07-10 18:52:57 +00:00
|
|
|
author = session.query(User).filter(User.slug == author).first()
|
2022-06-22 05:28:42 +00:00
|
|
|
if author.id in authors:
|
|
|
|
return {"error": "already added"}
|
|
|
|
shout.authors.append(author)
|
2022-07-21 11:58:50 +00:00
|
|
|
shout.updated_at = datetime.now()
|
|
|
|
shout.save()
|
2022-06-22 05:28:42 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
# TODO: email notify
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@mutation.field("removeAuthor")
|
|
|
|
@login_required
|
2022-07-10 18:52:57 +00:00
|
|
|
async def remove_author(_, info, author, shout):
|
2022-06-22 05:28:42 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == shout).first()
|
|
|
|
if not shout:
|
|
|
|
return {"error": "invalid shout slug"}
|
|
|
|
authors = [author.id for author in shout.authors]
|
|
|
|
if user_id not in authors:
|
|
|
|
return {"error": "access denied"}
|
2022-07-10 18:52:57 +00:00
|
|
|
author = session.query(User).filter(User.slug == author).first()
|
2022-06-22 05:28:42 +00:00
|
|
|
if author.id not in authors:
|
|
|
|
return {"error": "not in authors"}
|
|
|
|
shout.authors.remove(author)
|
2022-07-21 11:58:50 +00:00
|
|
|
shout.updated_at = datetime.now()
|
|
|
|
shout.save()
|
2022-06-22 05:28:42 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
# result = Result("INVITED")
|
|
|
|
# FIXME: await ShoutStorage.put(result)
|
|
|
|
|
|
|
|
# TODO: email notify
|
|
|
|
|
2022-06-23 08:39:00 +00:00
|
|
|
return {}
|