2023-12-17 20:30:20 +00:00
|
|
|
from orm.author import Author
|
|
|
|
from orm.invite import Invite, InviteStatus
|
|
|
|
from orm.shout import Shout
|
2023-11-28 10:46:06 +00:00
|
|
|
from services.auth import login_required
|
|
|
|
from services.db import local_session
|
|
|
|
from services.schema import mutation
|
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('accept_invite')
|
2023-11-28 10:46:06 +00:00
|
|
|
@login_required
|
|
|
|
async def accept_invite(_, info, invite_id: int):
|
2024-02-21 16:14:58 +00:00
|
|
|
user_id = info.context['user_id']
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
# Check if the user exists
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
|
|
|
# Check if the invite exists
|
|
|
|
invite = session.query(Invite).filter(Invite.id == invite_id).first()
|
2024-02-21 07:27:16 +00:00
|
|
|
if (
|
|
|
|
invite
|
2024-02-23 18:27:38 +00:00
|
|
|
and invite.author_d is author.id
|
2024-02-21 07:27:16 +00:00
|
|
|
and invite.status is InviteStatus.PENDING.value
|
|
|
|
):
|
2023-11-28 10:46:06 +00:00
|
|
|
# Add the user to the shout authors
|
|
|
|
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
|
|
|
|
if shout:
|
2023-11-30 08:40:27 +00:00
|
|
|
if author not in shout.authors:
|
|
|
|
shout.authors.append(author)
|
|
|
|
session.delete(invite)
|
|
|
|
session.add(shout)
|
|
|
|
session.commit()
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'success': True, 'message': 'Invite accepted'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Shout not found'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Invalid invite or already accepted/rejected'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'User not found'}
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('reject_invite')
|
2023-11-28 10:46:06 +00:00
|
|
|
@login_required
|
|
|
|
async def reject_invite(_, info, invite_id: int):
|
2024-02-21 16:14:58 +00:00
|
|
|
user_id = info.context['user_id']
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
# Check if the user exists
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
|
|
|
# Check if the invite exists
|
|
|
|
invite = session.query(Invite).filter(Invite.id == invite_id).first()
|
2024-02-21 07:27:16 +00:00
|
|
|
if (
|
|
|
|
invite
|
|
|
|
and invite.author_id is author.id
|
|
|
|
and invite.status is InviteStatus.PENDING.value
|
|
|
|
):
|
2023-11-28 10:46:06 +00:00
|
|
|
# Delete the invite
|
|
|
|
session.delete(invite)
|
|
|
|
session.commit()
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'success': True, 'message': 'Invite rejected'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Invalid invite or already accepted/rejected'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'User not found'}
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('create_invite')
|
2023-11-28 10:46:06 +00:00
|
|
|
@login_required
|
2024-02-21 16:14:58 +00:00
|
|
|
async def create_invite(_, info, slug: str = '', author_id: int = 0):
|
|
|
|
user_id = info.context['user_id']
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
# Check if the inviter is the owner of the shout
|
|
|
|
with local_session() as session:
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == slug).first()
|
2023-11-28 12:56:32 +00:00
|
|
|
inviter = session.query(Author).filter(Author.user == user_id).first()
|
2024-01-23 13:04:38 +00:00
|
|
|
if inviter and shout and shout.authors and inviter.id is shout.created_by:
|
2023-11-28 10:55:05 +00:00
|
|
|
# Check if the author is a valid author
|
|
|
|
author = session.query(Author).filter(Author.id == author_id).first()
|
|
|
|
if author:
|
2023-11-28 10:46:06 +00:00
|
|
|
# Check if an invite already exists
|
|
|
|
existing_invite = (
|
|
|
|
session.query(Invite)
|
|
|
|
.filter(
|
2023-11-28 12:56:32 +00:00
|
|
|
Invite.inviter_id == inviter.id,
|
2023-11-28 10:55:05 +00:00
|
|
|
Invite.author_id == author_id,
|
2023-11-28 10:46:06 +00:00
|
|
|
Invite.shout_id == shout.id,
|
2023-11-30 07:38:41 +00:00
|
|
|
Invite.status == InviteStatus.PENDING.value,
|
2023-11-28 10:46:06 +00:00
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if existing_invite:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Invite already sent'}
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
# Create a new invite
|
|
|
|
new_invite = Invite(
|
2024-02-21 07:27:16 +00:00
|
|
|
inviter_id=user_id,
|
|
|
|
author_id=author_id,
|
|
|
|
shout_id=shout.id,
|
|
|
|
status=InviteStatus.PENDING.value,
|
2023-11-28 10:46:06 +00:00
|
|
|
)
|
|
|
|
session.add(new_invite)
|
|
|
|
session.commit()
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': None, 'invite': new_invite}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Invalid author'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Access denied'}
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('remove_author')
|
2023-11-28 10:46:06 +00:00
|
|
|
@login_required
|
2024-02-21 16:14:58 +00:00
|
|
|
async def remove_author(_, info, slug: str = '', author_id: int = 0):
|
|
|
|
user_id = info.context['user_id']
|
2023-11-28 10:46:06 +00:00
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == slug).first()
|
|
|
|
# NOTE: owner should be first in a list
|
2024-01-23 13:04:38 +00:00
|
|
|
if shout and author.id is shout.created_by:
|
2024-02-21 07:27:16 +00:00
|
|
|
shout.authors = [
|
|
|
|
author for author in shout.authors if author.id != author_id
|
|
|
|
]
|
2023-11-28 10:46:06 +00:00
|
|
|
session.commit()
|
|
|
|
return {}
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Access denied'}
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('remove_invite')
|
2023-11-28 10:46:06 +00:00
|
|
|
@login_required
|
|
|
|
async def remove_invite(_, info, invite_id: int):
|
2024-02-21 16:14:58 +00:00
|
|
|
user_id = info.context['user_id']
|
2023-11-28 10:46:06 +00:00
|
|
|
|
|
|
|
# Check if the user exists
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
|
|
|
# Check if the invite exists
|
|
|
|
invite = session.query(Invite).filter(Invite.id == invite_id).first()
|
2024-01-23 13:04:38 +00:00
|
|
|
if isinstance(invite, Invite):
|
|
|
|
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
|
|
|
|
if shout and shout.deleted_at is None and invite:
|
|
|
|
if invite.inviter_id is author.id or author.id is shout.created_by:
|
|
|
|
if invite.status is InviteStatus.PENDING.value:
|
|
|
|
# Delete the invite
|
|
|
|
session.delete(invite)
|
|
|
|
session.commit()
|
|
|
|
return {}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Invalid invite or already accepted/rejected'}
|
2023-11-28 10:46:06 +00:00
|
|
|
else:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'Author not found'}
|