This commit is contained in:
@@ -18,10 +18,10 @@ from services.search import search_service
|
||||
from services.logger import root_logger as logger
|
||||
|
||||
|
||||
@query.field('get_shouts_drafts')
|
||||
@query.field("get_shouts_drafts")
|
||||
@login_required
|
||||
async def get_shouts_drafts(_, info):
|
||||
user_id = info.context['user_id']
|
||||
user_id = info.context["user_id"]
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
@@ -40,28 +40,28 @@ async def get_shouts_drafts(_, info):
|
||||
return shouts
|
||||
|
||||
|
||||
@mutation.field('create_shout')
|
||||
@mutation.field("create_shout")
|
||||
@login_required
|
||||
async def create_shout(_, info, inp):
|
||||
user_id = info.context['user_id']
|
||||
user_id = info.context["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}'
|
||||
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
|
||||
"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
|
||||
}
|
||||
|
||||
new_shout = Shout(**shout_dict)
|
||||
@@ -75,7 +75,11 @@ async def create_shout(_, info, inp):
|
||||
sa = ShoutAuthor(shout=shout.id, author=author.id)
|
||||
session.add(sa)
|
||||
|
||||
topics = session.query(Topic).filter(Topic.slug.in_(inp.get('topics', []))).all()
|
||||
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)
|
||||
@@ -85,9 +89,9 @@ async def create_shout(_, info, inp):
|
||||
# notifier
|
||||
# await notify_shout(shout_dict, 'create')
|
||||
|
||||
return { 'shout': shout.dict() }
|
||||
return {"shout": shout.dict()}
|
||||
|
||||
return {'error': 'cant create shout'}
|
||||
return {"error": "cant create shout"}
|
||||
|
||||
|
||||
def patch_main_topic(session, main_topic, shout):
|
||||
@@ -117,15 +121,17 @@ def patch_main_topic(session, main_topic, shout):
|
||||
)
|
||||
|
||||
if old_main_topic and new_main_topic and old_main_topic is not new_main_topic:
|
||||
ShoutTopic.update(old_main_topic, {'main': False})
|
||||
ShoutTopic.update(old_main_topic, {"main": False})
|
||||
session.add(old_main_topic)
|
||||
|
||||
ShoutTopic.update(new_main_topic, {'main': True})
|
||||
ShoutTopic.update(new_main_topic, {"main": True})
|
||||
session.add(new_main_topic)
|
||||
|
||||
|
||||
def patch_topics(session, shout, topics_input):
|
||||
new_topics_to_link = [Topic(**new_topic) for new_topic in topics_input if new_topic['id'] < 0]
|
||||
new_topics_to_link = [
|
||||
Topic(**new_topic) for new_topic in topics_input if new_topic["id"] < 0
|
||||
]
|
||||
if new_topics_to_link:
|
||||
session.add_all(new_topics_to_link)
|
||||
session.commit()
|
||||
@@ -134,21 +140,25 @@ def patch_topics(session, shout, topics_input):
|
||||
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
|
||||
session.add(created_unlinked_topic)
|
||||
|
||||
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0]
|
||||
existing_topics_input = [
|
||||
topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0
|
||||
]
|
||||
existing_topic_to_link_ids = [
|
||||
existing_topic_input['id']
|
||||
existing_topic_input["id"]
|
||||
for existing_topic_input in existing_topics_input
|
||||
if existing_topic_input['id'] not in [topic.id for topic in shout.topics]
|
||||
if existing_topic_input["id"] not in [topic.id for topic in shout.topics]
|
||||
]
|
||||
|
||||
for existing_topic_to_link_id in existing_topic_to_link_ids:
|
||||
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id)
|
||||
created_unlinked_topic = ShoutTopic(
|
||||
shout=shout.id, topic=existing_topic_to_link_id
|
||||
)
|
||||
session.add(created_unlinked_topic)
|
||||
|
||||
topic_to_unlink_ids = [
|
||||
topic.id
|
||||
for topic in shout.topics
|
||||
if topic.id not in [topic_input['id'] for topic_input in existing_topics_input]
|
||||
if topic.id not in [topic_input["id"] for topic_input in existing_topics_input]
|
||||
]
|
||||
|
||||
session.query(ShoutTopic).filter(
|
||||
@@ -159,16 +169,16 @@ def patch_topics(session, shout, topics_input):
|
||||
).delete(synchronize_session=False)
|
||||
|
||||
|
||||
@mutation.field('update_shout')
|
||||
@mutation.field("update_shout")
|
||||
@login_required
|
||||
async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||
user_id = info.context['user_id']
|
||||
roles = info.context['roles']
|
||||
user_id = info.context["user_id"]
|
||||
roles = info.context["roles"]
|
||||
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')
|
||||
shout_id = shout_id or shout_input.get("id")
|
||||
if isinstance(author, Author) and isinstance(shout_id, int):
|
||||
shout = (
|
||||
session.query(Shout)
|
||||
@@ -181,23 +191,27 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||
)
|
||||
|
||||
if not shout:
|
||||
return {'error': 'shout not found'}
|
||||
if shout.created_by is not author.id and author.id not in shout.authors and 'editor' not in roles:
|
||||
return {'error': 'access denied'}
|
||||
return {"error": "shout not found"}
|
||||
if (
|
||||
shout.created_by is not author.id
|
||||
and author.id not in shout.authors
|
||||
and "editor" not in roles
|
||||
):
|
||||
return {"error": "access denied"}
|
||||
|
||||
# topics patch
|
||||
topics_input = shout_input.get('topics')
|
||||
topics_input = shout_input.get("topics")
|
||||
if topics_input:
|
||||
patch_topics(session, shout, topics_input)
|
||||
del shout_input['topics']
|
||||
del shout_input["topics"]
|
||||
|
||||
# main topic
|
||||
main_topic = shout_input.get('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_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()
|
||||
@@ -205,50 +219,61 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||
shout_dict = shout.dict()
|
||||
|
||||
if not publish:
|
||||
await notify_shout(shout_dict, 'update')
|
||||
await notify_shout(shout_dict, "update")
|
||||
else:
|
||||
await notify_shout(shout_dict, 'published')
|
||||
await notify_shout(shout_dict, "published")
|
||||
# search service indexing
|
||||
search_service.index(shout)
|
||||
|
||||
return {'shout': shout_dict}
|
||||
logger.debug(f' cannot update with data: {shout_input}')
|
||||
return { 'error': 'not enough data' }
|
||||
return {'error': 'cannot update'}
|
||||
return {"shout": shout_dict}
|
||||
logger.debug(f" cannot update with data: {shout_input}")
|
||||
return {"error": "cant update shout"}
|
||||
|
||||
|
||||
@mutation.field('delete_shout')
|
||||
@mutation.field("delete_shout")
|
||||
@login_required
|
||||
async def delete_shout(_, info, shout_id):
|
||||
user_id = info.context['user_id']
|
||||
roles = info.context['roles']
|
||||
user_id = info.context["user_id"]
|
||||
roles = info.context["roles"]
|
||||
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'}
|
||||
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'}
|
||||
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_dict["deleted_at"] = int(time.time())
|
||||
Shout.update(shout, shout_dict)
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
await notify_shout(shout_dict, 'delete')
|
||||
await notify_shout(shout_dict, "delete")
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
def handle_proposing(session, r, shout):
|
||||
if is_positive(r.kind):
|
||||
replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to, Reaction.shout == r.shout).first()
|
||||
replied_reaction = (
|
||||
session.query(Reaction)
|
||||
.filter(Reaction.id == r.reply_to, Reaction.shout == r.shout)
|
||||
.first()
|
||||
)
|
||||
|
||||
if replied_reaction and replied_reaction.kind is ReactionKind.PROPOSE.value and replied_reaction.quote:
|
||||
if (
|
||||
replied_reaction
|
||||
and replied_reaction.kind is ReactionKind.PROPOSE.value
|
||||
and replied_reaction.quote
|
||||
):
|
||||
# patch all the proposals' quotes
|
||||
proposals = (
|
||||
session.query(Reaction)
|
||||
@@ -265,13 +290,15 @@ def handle_proposing(session, r, shout):
|
||||
if proposal.quote:
|
||||
proposal_diff = get_diff(shout.body, proposal.quote)
|
||||
proposal_dict = proposal.dict()
|
||||
proposal_dict['quote'] = apply_diff(replied_reaction.quote, proposal_diff)
|
||||
proposal_dict["quote"] = apply_diff(
|
||||
replied_reaction.quote, proposal_diff
|
||||
)
|
||||
Reaction.update(proposal, proposal_dict)
|
||||
session.add(proposal)
|
||||
|
||||
# patch shout's body
|
||||
shout_dict = shout.dict()
|
||||
shout_dict['body'] = replied_reaction.quote
|
||||
shout_dict["body"] = replied_reaction.quote
|
||||
Shout.update(shout, shout_dict)
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
|
Reference in New Issue
Block a user