core/resolvers/create/collab.py

111 lines
3.5 KiB
Python
Raw Normal View History

from auth.authenticate import login_required
2022-12-01 14:45:19 +00:00
from auth.credentials import AuthCredentials
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import query, mutation
2022-12-01 08:12:48 +00:00
from base.exceptions import ObjectNotExist, BaseHttpException
2023-01-13 11:04:45 +00:00
from orm.draft import DraftCollab, CollabAuthor
2022-07-10 18:52:57 +00:00
from orm.shout import Shout
from orm.user import User
2022-09-03 10:50:14 +00:00
2023-01-13 11:04:45 +00:00
# TODO: use updatedAt
@query.field("loadDrafts")
2022-09-02 10:23:33 +00:00
@login_required
2023-01-13 11:04:45 +00:00
async def get_drafts(_, info):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2023-01-13 11:04:45 +00:00
drafts = []
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-01-13 11:04:45 +00:00
drafts = session.query(DraftCollab).filter(auth.user_id in DraftCollab.authors)
return {
"drafts": drafts
}
2022-09-02 10:23:33 +00:00
2023-01-13 11:04:45 +00:00
@mutation.field("createDraft") # TODO
@login_required
2023-01-13 11:04:45 +00:00
async def create_draft(_, info):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-01-13 11:04:45 +00:00
pass
2023-01-13 11:04:45 +00:00
@mutation.field("deleteDraft") # TODO
@login_required
async def delete_draft(_, info, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
pass
2023-01-13 11:04:45 +00:00
@mutation.field("updateDraft") # TODO
@login_required
2023-01-13 11:04:45 +00:00
async def update_draft(_, info, author: int = 0, draft: int = 0):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-01-13 11:04:45 +00:00
s = session.query(DraftCollab).where(DraftCollab.id == draft).one() # raises Error when not found
2022-12-23 14:45:00 +00:00
if auth.user_id not in s.authors:
2023-01-13 11:04:45 +00:00
# raise BaseHttpException("only owner can remove coauthors")
return {
"error": "Only authors can update draft"
}
elif not s:
return {
"error": "There is no draft with this id"
}
2022-11-24 17:53:39 +00:00
else:
2023-01-13 11:04:45 +00:00
c = session.query(DraftCollab).where(DraftCollab.id == draft).one()
ca = session.query(CollabAuthor).join(User).where(c.id == draft).filter(User.id == author).one()
2022-11-24 17:53:39 +00:00
session.remve(ca)
2022-12-23 14:45:00 +00:00
c.invites = filter(lambda x: x.id == author, c.invites)
c.authors = filter(lambda x: x.id == author, c.authors)
2022-11-24 17:53:39 +00:00
session.add(c)
session.commit()
2022-09-03 10:50:14 +00:00
# TODO: email notify
return {}
2022-11-24 17:53:39 +00:00
2023-01-13 11:04:45 +00:00
@mutation.field("inviteAuthor")
@login_required
async def invite_coauthor(_, info, author: int = 0, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
2022-11-24 17:53:39 +00:00
2023-01-13 11:04:45 +00:00
with local_session() as session:
c = session.query(DraftCollab).where(DraftCollab.id == draft).one()
if auth.user_id not in c.authors:
# raise BaseHttpException("you are not in authors list")
return {
"error": "You are not in authors list"
}
else:
invited_user = session.query(User).where(User.id == author).one()
c.invites.append(invited_user)
session.add(c)
session.commit()
# TODO: email notify
return {}
@mutation.field("inviteAccept")
2022-11-24 17:53:39 +00:00
@login_required
2023-01-13 11:04:45 +00:00
async def accept_coauthor(_, info, draft: int):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-11-24 17:53:39 +00:00
with local_session() as session:
2023-01-13 11:04:45 +00:00
c = session.query(DraftCollab).where(DraftCollab.id == draft).one()
accepted = filter(lambda x: x.id == auth.user_id, c.invites).pop()
if accepted:
c.authors.append(accepted)
session.add(c)
session.commit()
return {}
2022-11-24 17:53:39 +00:00
else:
2023-01-13 11:04:45 +00:00
# raise BaseHttpException("only invited can accept")
return {
"error": "You don't have an invitation yet"
}