2024-02-02 20:38:16 +00:00
|
|
|
import time
|
2023-12-17 20:30:20 +00:00
|
|
|
|
2024-02-29 12:50:26 +00:00
|
|
|
from sqlalchemy import and_, select, desc
|
2023-05-11 11:03:14 +00:00
|
|
|
from sqlalchemy.orm import joinedload
|
2024-02-29 12:52:36 +00:00
|
|
|
from sqlalchemy.sql.functions import coalesce
|
2023-11-23 23:00:28 +00:00
|
|
|
|
|
|
|
from orm.author import Author
|
2024-02-19 11:45:55 +00:00
|
|
|
from orm.rating import is_negative, is_positive
|
2024-02-02 12:03:44 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2024-02-02 16:36:30 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
2023-05-14 17:02:26 +00:00
|
|
|
from orm.topic import Topic
|
2024-02-02 12:03:44 +00:00
|
|
|
from resolvers.follower import reactions_follow, reactions_unfollow
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.auth import login_required
|
|
|
|
from services.db import local_session
|
2024-02-02 12:03:44 +00:00
|
|
|
from services.diff import apply_diff, get_diff
|
2023-10-25 18:33:53 +00:00
|
|
|
from services.notify import notify_shout
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.schema import mutation, query
|
2024-01-29 03:42:02 +00:00
|
|
|
from services.search import search_service
|
2024-02-20 16:19:46 +00:00
|
|
|
from services.logger import root_logger as logger
|
2024-02-17 18:04:01 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('get_shouts_drafts')
|
2023-11-23 23:00:28 +00:00
|
|
|
@login_required
|
2023-11-28 07:53:48 +00:00
|
|
|
async def get_shouts_drafts(_, info):
|
2024-02-21 16:14:58 +00:00
|
|
|
user_id = info.context['user_id']
|
2024-02-03 14:31:00 +00:00
|
|
|
shouts = []
|
2023-10-23 14:47:11 +00:00
|
|
|
with local_session() as session:
|
2023-11-23 23:00:28 +00:00
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
2023-11-27 16:15:34 +00:00
|
|
|
if author:
|
|
|
|
q = (
|
|
|
|
select(Shout)
|
2024-02-21 16:14:58 +00:00
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
2023-11-30 11:04:55 +00:00
|
|
|
.filter(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id))
|
2024-02-05 09:47:26 +00:00
|
|
|
.filter(Shout.published_at.is_(None))
|
2024-02-29 12:52:36 +00:00
|
|
|
.order_by(desc(coalesce(Shout.updated_at, Shout.created_at)))
|
2024-02-02 20:38:16 +00:00
|
|
|
.group_by(Shout.id)
|
2023-11-23 23:00:28 +00:00
|
|
|
)
|
2024-02-02 20:38:16 +00:00
|
|
|
shouts = [shout for [shout] in session.execute(q).unique()]
|
2024-02-03 14:31:00 +00:00
|
|
|
return shouts
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('create_shout')
|
2022-06-19 11:11:14 +00:00
|
|
|
@login_required
|
2024-02-27 10:07:14 +00:00
|
|
|
async def create_shout(_, info, inp):
|
2024-02-26 09:22:55 +00:00
|
|
|
user_id = info.context.get('user_id')
|
|
|
|
if user_id:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if isinstance(author, Author):
|
|
|
|
current_time = int(time.time())
|
|
|
|
slug = inp.get('slug') or f'draft-{current_time}'
|
|
|
|
shout_dict = {
|
|
|
|
'title': inp.get('title', ''),
|
|
|
|
'subtitle': inp.get('subtitle', ''),
|
|
|
|
'lead': inp.get('lead', ''),
|
|
|
|
'description': inp.get('description', ''),
|
|
|
|
'body': inp.get('body', ''),
|
|
|
|
'layout': inp.get('layout', 'article'),
|
|
|
|
'created_by': author.id,
|
|
|
|
'authors': [],
|
|
|
|
'slug': slug,
|
|
|
|
'topics': inp.get('topics', []),
|
|
|
|
'published_at': None,
|
|
|
|
'created_at': current_time, # Set created_at as Unix timestamp
|
|
|
|
}
|
2024-02-23 16:35:40 +00:00
|
|
|
same_slug_shout = (
|
|
|
|
session.query(Shout)
|
|
|
|
.filter(Shout.slug == shout_dict.get('slug'))
|
|
|
|
.first()
|
|
|
|
)
|
2024-02-26 09:22:55 +00:00
|
|
|
c = 1
|
|
|
|
while same_slug_shout is not None:
|
|
|
|
same_slug_shout = (
|
|
|
|
session.query(Shout)
|
|
|
|
.filter(Shout.slug == shout_dict.get('slug'))
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
c += 1
|
|
|
|
shout_dict['slug'] += f'-{c}'
|
|
|
|
new_shout = Shout(**shout_dict)
|
|
|
|
session.add(new_shout)
|
2024-02-25 21:06:37 +00:00
|
|
|
session.commit()
|
|
|
|
|
2024-02-26 09:22:55 +00:00
|
|
|
# NOTE: requesting new shout back
|
|
|
|
shout = session.query(Shout).where(Shout.slug == slug).first()
|
|
|
|
if shout:
|
|
|
|
sa = ShoutAuthor(shout=shout.id, author=author.id)
|
|
|
|
session.add(sa)
|
|
|
|
|
|
|
|
topics = (
|
|
|
|
session.query(Topic)
|
|
|
|
.filter(Topic.slug.in_(inp.get('topics', [])))
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
for topic in topics:
|
|
|
|
t = ShoutTopic(topic=topic.id, shout=shout.id)
|
|
|
|
session.add(t)
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
reactions_follow(author.id, shout.id, True)
|
2024-01-22 16:57:48 +00:00
|
|
|
|
2024-02-26 09:22:55 +00:00
|
|
|
# notifier
|
|
|
|
# await notify_shout(shout_dict, 'create')
|
2024-02-02 20:38:16 +00:00
|
|
|
|
2024-02-26 09:22:55 +00:00
|
|
|
return {'shout': shout}
|
2024-02-02 20:38:16 +00:00
|
|
|
|
2024-02-26 09:22:55 +00:00
|
|
|
return {'error': 'cant create shout' if user_id else 'unauthorized'}
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2024-02-02 20:59:42 +00:00
|
|
|
def patch_main_topic(session, main_topic, shout):
|
|
|
|
old_main_topic = (
|
|
|
|
session.query(ShoutTopic)
|
2024-02-21 16:14:58 +00:00
|
|
|
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main.is_(True)))
|
2024-02-02 20:59:42 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
main_topic = session.query(Topic).filter(Topic.slug == main_topic).first()
|
|
|
|
|
|
|
|
if main_topic:
|
|
|
|
new_main_topic = (
|
|
|
|
session.query(ShoutTopic)
|
|
|
|
.filter(
|
2024-02-21 16:14:58 +00:00
|
|
|
and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id)
|
2024-02-02 20:59:42 +00:00
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if old_main_topic and new_main_topic and old_main_topic is not new_main_topic:
|
2024-02-21 16:14:58 +00:00
|
|
|
ShoutTopic.update(old_main_topic, {'main': False})
|
2024-02-02 20:59:42 +00:00
|
|
|
session.add(old_main_topic)
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
ShoutTopic.update(new_main_topic, {'main': True})
|
2024-02-02 20:59:42 +00:00
|
|
|
session.add(new_main_topic)
|
|
|
|
|
|
|
|
|
|
|
|
def patch_topics(session, shout, topics_input):
|
2024-02-21 07:27:16 +00:00
|
|
|
new_topics_to_link = [
|
2024-02-21 16:14:58 +00:00
|
|
|
Topic(**new_topic) for new_topic in topics_input if new_topic['id'] < 0
|
2024-02-21 07:27:16 +00:00
|
|
|
]
|
2024-02-02 20:59:42 +00:00
|
|
|
if new_topics_to_link:
|
|
|
|
session.add_all(new_topics_to_link)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
for new_topic_to_link in new_topics_to_link:
|
|
|
|
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
|
|
|
|
session.add(created_unlinked_topic)
|
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
existing_topics_input = [
|
2024-02-21 16:14:58 +00:00
|
|
|
topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0
|
2024-02-21 07:27:16 +00:00
|
|
|
]
|
2024-02-02 20:59:42 +00:00
|
|
|
existing_topic_to_link_ids = [
|
2024-02-21 16:14:58 +00:00
|
|
|
existing_topic_input['id']
|
2024-02-02 20:59:42 +00:00
|
|
|
for existing_topic_input in existing_topics_input
|
2024-02-21 16:14:58 +00:00
|
|
|
if existing_topic_input['id'] not in [topic.id for topic in shout.topics]
|
2024-02-02 20:59:42 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for existing_topic_to_link_id in existing_topic_to_link_ids:
|
2024-02-21 07:27:16 +00:00
|
|
|
created_unlinked_topic = ShoutTopic(
|
|
|
|
shout=shout.id, topic=existing_topic_to_link_id
|
|
|
|
)
|
2024-02-02 20:59:42 +00:00
|
|
|
session.add(created_unlinked_topic)
|
|
|
|
|
|
|
|
topic_to_unlink_ids = [
|
|
|
|
topic.id
|
|
|
|
for topic in shout.topics
|
2024-02-21 16:14:58 +00:00
|
|
|
if topic.id not in [topic_input['id'] for topic_input in existing_topics_input]
|
2024-02-02 20:59:42 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
session.query(ShoutTopic).filter(
|
2024-02-21 16:14:58 +00:00
|
|
|
and_(ShoutTopic.shout == shout.id, ShoutTopic.topic.in_(topic_to_unlink_ids))
|
2024-02-02 20:59:42 +00:00
|
|
|
).delete(synchronize_session=False)
|
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('update_shout')
|
2022-06-19 11:11:14 +00:00
|
|
|
@login_required
|
2024-02-17 18:55:50 +00:00
|
|
|
async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
2024-02-27 13:28:54 +00:00
|
|
|
try:
|
|
|
|
user_id = info.context.get('user_id')
|
|
|
|
if not user_id:
|
|
|
|
return {"error": "unauthorized"}
|
2024-02-27 13:33:25 +00:00
|
|
|
roles = info.context.get('roles', [])
|
2024-02-27 13:28:54 +00:00
|
|
|
shout_input = shout_input or {}
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
current_time = int(time.time())
|
|
|
|
shout_id = shout_id or shout_input.get('id')
|
|
|
|
slug = shout_input.get('slug')
|
|
|
|
if slug:
|
|
|
|
|
|
|
|
shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first()
|
|
|
|
if shout_by_id and slug != shout_by_id.slug:
|
2024-02-23 16:35:40 +00:00
|
|
|
same_slug_shout = (
|
|
|
|
session.query(Shout)
|
|
|
|
.filter(Shout.slug == shout_input.get('slug'))
|
|
|
|
.first()
|
|
|
|
)
|
2024-02-27 13:28:54 +00:00
|
|
|
c = 1
|
|
|
|
while same_slug_shout is not None:
|
2024-02-29 10:14:14 +00:00
|
|
|
c += 1
|
|
|
|
slug += f'-{c}'
|
2024-02-27 13:28:54 +00:00
|
|
|
same_slug_shout = (
|
|
|
|
session.query(Shout)
|
2024-02-29 10:14:14 +00:00
|
|
|
.filter(Shout.slug == slug) # Use the updated slug value here
|
2024-02-27 13:28:54 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
shout_input['slug'] = slug
|
|
|
|
|
|
|
|
if isinstance(author, Author) and isinstance(shout_id, int):
|
|
|
|
shout = (
|
|
|
|
session.query(Shout)
|
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
|
|
|
.filter(Shout.id == shout_id)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if not shout:
|
|
|
|
return {'error': 'shout not found'}
|
|
|
|
if (
|
2024-02-29 17:00:35 +00:00
|
|
|
shout.created_by != author.id
|
|
|
|
and not shout.authors.contains(author.id)
|
2024-02-27 13:28:54 +00:00
|
|
|
and 'editor' not in roles
|
|
|
|
):
|
|
|
|
return {'error': 'access denied'}
|
|
|
|
|
|
|
|
# topics patch
|
|
|
|
topics_input = shout_input.get('topics')
|
|
|
|
if topics_input:
|
|
|
|
patch_topics(session, shout, topics_input)
|
|
|
|
del shout_input['topics']
|
|
|
|
|
|
|
|
# main topic
|
|
|
|
main_topic = shout_input.get('main_topic')
|
|
|
|
if main_topic:
|
|
|
|
patch_main_topic(session, main_topic, shout)
|
|
|
|
|
|
|
|
shout_input['updated_at'] = current_time
|
|
|
|
shout_input['published_at'] = current_time if publish else None
|
|
|
|
Shout.update(shout, shout_input)
|
|
|
|
session.add(shout)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
shout_dict = shout.dict()
|
|
|
|
|
|
|
|
if not publish:
|
|
|
|
await notify_shout(shout_dict, 'update')
|
|
|
|
else:
|
|
|
|
await notify_shout(shout_dict, 'published')
|
|
|
|
# search service indexing
|
|
|
|
search_service.index(shout)
|
|
|
|
|
|
|
|
return {'shout': shout_dict}
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error(exc)
|
|
|
|
logger.error(f' cannot update with data: {shout_input}')
|
2024-02-17 06:35:11 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': 'cant update shout'}
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('delete_shout')
|
2022-06-19 11:11:14 +00:00
|
|
|
@login_required
|
2023-05-08 12:28:17 +00:00
|
|
|
async def delete_shout(_, info, shout_id):
|
2024-02-27 11:29:28 +00:00
|
|
|
user_id = info.context.get('user_id')
|
|
|
|
roles = info.context.get('roles')
|
|
|
|
if user_id:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
shout = session.query(Shout).filter(Shout.id == shout_id).first()
|
|
|
|
if not shout:
|
|
|
|
return {'error': 'invalid shout id'}
|
|
|
|
if author and shout:
|
|
|
|
if (
|
|
|
|
shout.created_by is not author.id
|
|
|
|
and author.id not in shout.authors
|
|
|
|
and 'editor' not in roles
|
|
|
|
):
|
|
|
|
return {'error': 'access denied'}
|
|
|
|
|
|
|
|
for author_id in shout.authors:
|
|
|
|
reactions_unfollow(author_id, shout_id)
|
|
|
|
|
|
|
|
shout_dict = shout.dict()
|
|
|
|
shout_dict['deleted_at'] = int(time.time())
|
|
|
|
Shout.update(shout, shout_dict)
|
|
|
|
session.add(shout)
|
|
|
|
session.commit()
|
|
|
|
await notify_shout(shout_dict, 'delete')
|
2024-02-02 20:38:16 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
return {}
|
2024-02-02 12:03:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handle_proposing(session, r, shout):
|
|
|
|
if is_positive(r.kind):
|
2024-02-21 07:27:16 +00:00
|
|
|
replied_reaction = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(Reaction.id == r.reply_to, Reaction.shout == r.shout)
|
|
|
|
.first()
|
|
|
|
)
|
2024-02-02 20:38:16 +00:00
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
if (
|
|
|
|
replied_reaction
|
|
|
|
and replied_reaction.kind is ReactionKind.PROPOSE.value
|
|
|
|
and replied_reaction.quote
|
|
|
|
):
|
2024-02-02 12:03:44 +00:00
|
|
|
# patch all the proposals' quotes
|
2024-02-02 12:59:22 +00:00
|
|
|
proposals = (
|
|
|
|
session.query(Reaction)
|
2024-02-02 20:38:16 +00:00
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Reaction.shout == r.shout,
|
|
|
|
Reaction.kind == ReactionKind.PROPOSE.value,
|
|
|
|
)
|
|
|
|
)
|
2024-02-02 12:59:22 +00:00
|
|
|
.all()
|
|
|
|
)
|
2024-02-02 20:38:16 +00:00
|
|
|
|
2024-02-02 12:03:44 +00:00
|
|
|
for proposal in proposals:
|
|
|
|
if proposal.quote:
|
|
|
|
proposal_diff = get_diff(shout.body, proposal.quote)
|
|
|
|
proposal_dict = proposal.dict()
|
2024-02-21 16:14:58 +00:00
|
|
|
proposal_dict['quote'] = apply_diff(
|
2024-02-21 07:27:16 +00:00
|
|
|
replied_reaction.quote, proposal_diff
|
|
|
|
)
|
2024-02-02 12:03:44 +00:00
|
|
|
Reaction.update(proposal, proposal_dict)
|
|
|
|
session.add(proposal)
|
|
|
|
|
|
|
|
# patch shout's body
|
|
|
|
shout_dict = shout.dict()
|
2024-02-21 16:14:58 +00:00
|
|
|
shout_dict['body'] = replied_reaction.quote
|
2024-02-02 12:03:44 +00:00
|
|
|
Shout.update(shout, shout_dict)
|
|
|
|
session.add(shout)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
if is_negative(r.kind):
|
|
|
|
# TODO: rejection logic
|
|
|
|
pass
|