2022-09-17 18:12:14 +00:00
|
|
|
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
|
2022-09-17 18:12:14 +00:00
|
|
|
from base.resolvers import query, mutation
|
2022-12-01 08:12:48 +00:00
|
|
|
from base.exceptions import ObjectNotExist, BaseHttpException
|
2022-11-24 17:53:39 +00:00
|
|
|
from orm.collab import Collab, CollabAuthor
|
2022-07-10 18:52:57 +00:00
|
|
|
from orm.shout import Shout
|
|
|
|
from orm.user import User
|
2022-06-22 05:28:42 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-09-02 10:23:33 +00:00
|
|
|
@query.field("getCollabs")
|
|
|
|
@login_required
|
|
|
|
async def get_collabs(_, 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:
|
2022-12-01 14:45:19 +00:00
|
|
|
collabs = session.query(Collab).filter(auth.user_id in Collab.authors)
|
2022-09-17 18:12:14 +00:00
|
|
|
return collabs
|
2022-09-02 10:23:33 +00:00
|
|
|
|
|
|
|
|
2022-11-24 17:53:39 +00:00
|
|
|
@mutation.field("inviteCoauthor")
|
2022-06-22 05:28:42 +00:00
|
|
|
@login_required
|
2022-11-24 17:53:39 +00:00
|
|
|
async def invite_coauthor(_, info, author: str, shout: int):
|
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:
|
2022-11-24 17:53:39 +00:00
|
|
|
s = session.query(Shout).where(Shout.id == shout).one()
|
|
|
|
if not s:
|
|
|
|
raise ObjectNotExist("invalid shout id")
|
|
|
|
else:
|
|
|
|
c = session.query(Collab).where(Collab.shout == shout).one()
|
2022-12-01 14:45:19 +00:00
|
|
|
if auth.user_id not in c.authors:
|
2022-12-01 08:12:48 +00:00
|
|
|
raise BaseHttpException("you are not in authors list")
|
2022-11-24 17:53:39 +00:00
|
|
|
else:
|
2022-12-01 14:45:19 +00:00
|
|
|
invited_user = session.query(User).where(User.id == author).one()
|
2022-11-24 17:53:39 +00:00
|
|
|
c.invites.append(invited_user)
|
|
|
|
session.add(c)
|
|
|
|
session.commit()
|
2022-06-22 05:28:42 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
# TODO: email notify
|
|
|
|
return {}
|
2022-06-22 05:28:42 +00:00
|
|
|
|
|
|
|
|
2022-11-24 17:53:39 +00:00
|
|
|
@mutation.field("removeCoauthor")
|
2022-06-22 05:28:42 +00:00
|
|
|
@login_required
|
2022-11-24 17:53:39 +00:00
|
|
|
async def remove_coauthor(_, info, author: str, shout: int):
|
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:
|
2022-11-24 17:53:39 +00:00
|
|
|
s = session.query(Shout).where(Shout.id == shout).one()
|
|
|
|
if not s:
|
|
|
|
raise ObjectNotExist("invalid shout id")
|
2022-12-01 14:45:19 +00:00
|
|
|
if auth.user_id != s.createdBy:
|
|
|
|
raise BaseHttpException("only owner can remove coauthors")
|
2022-11-24 17:53:39 +00:00
|
|
|
else:
|
|
|
|
c = session.query(Collab).where(Collab.shout == shout).one()
|
2022-12-01 14:45:19 +00:00
|
|
|
ca = session.query(CollabAuthor).join(User).where(c.shout == shout, User.slug == author).one()
|
2022-11-24 17:53:39 +00:00
|
|
|
session.remve(ca)
|
|
|
|
c.invites = filter(lambda x: x.slug == author, c.invites)
|
|
|
|
c.authors = filter(lambda x: x.slug == author, c.authors)
|
|
|
|
session.add(c)
|
|
|
|
session.commit()
|
2022-06-22 05:28:42 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
# TODO: email notify
|
|
|
|
return {}
|
2022-11-24 17:53:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("acceptCoauthor")
|
|
|
|
@login_required
|
|
|
|
async def accept_coauthor(_, info, shout: 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:
|
|
|
|
s = session.query(Shout).where(Shout.id == shout).one()
|
|
|
|
if not s:
|
|
|
|
raise ObjectNotExist("invalid shout id")
|
|
|
|
else:
|
|
|
|
c = session.query(Collab).where(Collab.shout == shout).one()
|
2022-12-01 14:45:19 +00:00
|
|
|
accepted = filter(lambda x: x.id == auth.user_id, c.invites).pop()
|
2022-11-24 17:53:39 +00:00
|
|
|
if accepted:
|
|
|
|
c.authors.append(accepted)
|
|
|
|
s.authors.append(accepted)
|
|
|
|
session.add(s)
|
|
|
|
session.add(c)
|
|
|
|
session.commit()
|
|
|
|
return {}
|
|
|
|
else:
|
2022-12-01 08:12:48 +00:00
|
|
|
raise BaseHttpException("only invited can accept")
|