2024-02-02 20:38:16 +00:00
|
|
|
|
import time
|
2023-12-17 20:30:20 +00:00
|
|
|
|
|
2024-04-08 07:38:58 +00:00
|
|
|
|
from sqlalchemy import and_, desc, select
|
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
|
|
|
|
|
2025-01-18 07:57:34 +00:00
|
|
|
|
from cache.cache import cache_author, cache_topic, invalidate_shouts_cache
|
2024-05-06 21:06:31 +00:00
|
|
|
|
from orm.author import Author
|
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-06-05 15:29:15 +00:00
|
|
|
|
from resolvers.follower import follow, unfollow
|
2024-05-05 15:46:16 +00:00
|
|
|
|
from resolvers.stat import get_with_stat
|
2023-12-17 20:30:20 +00:00
|
|
|
|
from services.auth import login_required
|
|
|
|
|
from services.db import local_session
|
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-08-09 06:37:06 +00:00
|
|
|
|
from utils.logger import root_logger as logger
|
2024-02-17 18:04:01 +00:00
|
|
|
|
|
|
|
|
|
|
2024-06-05 18:04:48 +00:00
|
|
|
|
async def cache_by_id(entity, entity_id: int, cache_method):
|
2024-05-18 11:15:05 +00:00
|
|
|
|
caching_query = select(entity).filter(entity.id == entity_id)
|
2024-12-11 21:20:43 +00:00
|
|
|
|
result = get_with_stat(caching_query)
|
|
|
|
|
if not result or not result[0]:
|
2024-12-11 21:21:51 +00:00
|
|
|
|
logger.warning(f"{entity.__name__} with id {entity_id} not found")
|
2024-05-05 15:46:16 +00:00
|
|
|
|
return
|
2024-12-11 21:20:43 +00:00
|
|
|
|
x = result[0]
|
2024-05-05 15:46:16 +00:00
|
|
|
|
d = x.dict() # convert object to dictionary
|
2024-06-05 18:04:48 +00:00
|
|
|
|
cache_method(d)
|
2024-05-05 15:46:16 +00:00
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
@query.field("get_my_shout")
|
2024-03-05 17:12:17 +00:00
|
|
|
|
@login_required
|
|
|
|
|
async def get_my_shout(_, info, shout_id: int):
|
2024-10-23 08:22:07 +00:00
|
|
|
|
# logger.debug(info)
|
2024-04-30 23:42:25 +00:00
|
|
|
|
user_id = info.context.get("user_id", "")
|
|
|
|
|
author_dict = info.context.get("author", {})
|
|
|
|
|
author_id = author_dict.get("id")
|
2024-05-01 00:29:25 +00:00
|
|
|
|
roles = info.context.get("roles", [])
|
|
|
|
|
shout = None
|
2024-04-30 23:42:25 +00:00
|
|
|
|
if not user_id or not author_id:
|
|
|
|
|
return {"error": "unauthorized", "shout": None}
|
2024-03-28 12:56:32 +00:00
|
|
|
|
with local_session() as session:
|
|
|
|
|
shout = (
|
|
|
|
|
session.query(Shout)
|
|
|
|
|
.filter(Shout.id == shout_id)
|
|
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
2024-04-26 08:06:13 +00:00
|
|
|
|
.filter(Shout.deleted_at.is_(None))
|
2024-03-28 12:56:32 +00:00
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-03-07 11:42:48 +00:00
|
|
|
|
if not shout:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": "no shout found", "shout": None}
|
2024-05-01 00:29:25 +00:00
|
|
|
|
|
2024-10-23 08:22:07 +00:00
|
|
|
|
logger.debug(f"got {len(shout.authors)} shout authors, created by {shout.created_by}")
|
2024-05-01 00:29:25 +00:00
|
|
|
|
is_editor = "editor" in roles
|
2024-05-01 00:35:31 +00:00
|
|
|
|
logger.debug(f'viewer is{'' if is_editor else ' not'} editor')
|
2024-05-01 00:29:25 +00:00
|
|
|
|
is_creator = author_id == shout.created_by
|
2024-05-01 00:35:31 +00:00
|
|
|
|
logger.debug(f'viewer is{'' if is_creator else ' not'} creator')
|
2024-05-30 04:12:00 +00:00
|
|
|
|
is_author = bool(list(filter(lambda x: x.id == int(author_id), [x for x in shout.authors])))
|
2024-05-01 00:35:31 +00:00
|
|
|
|
logger.debug(f'viewer is{'' if is_creator else ' not'} author')
|
2024-05-01 00:29:25 +00:00
|
|
|
|
can_edit = is_editor or is_author or is_creator
|
|
|
|
|
|
|
|
|
|
if not can_edit:
|
|
|
|
|
return {"error": "forbidden", "shout": None}
|
|
|
|
|
|
2024-05-01 01:01:21 +00:00
|
|
|
|
logger.debug("got shout editor with data")
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": None, "shout": shout}
|
2024-03-05 17:12:17 +00:00
|
|
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +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-04-30 11:10:01 +00:00
|
|
|
|
# user_id = info.context.get("user_id")
|
|
|
|
|
author_dict = info.context.get("author")
|
|
|
|
|
if not author_dict:
|
2024-05-06 18:14:17 +00:00
|
|
|
|
return {"error": "author profile was not found"}
|
2024-04-19 15:22:07 +00:00
|
|
|
|
author_id = author_dict.get("id")
|
2024-02-03 14:31:00 +00:00
|
|
|
|
shouts = []
|
2023-10-23 14:47:11 +00:00
|
|
|
|
with local_session() as session:
|
2024-04-19 15:22:07 +00:00
|
|
|
|
if author_id:
|
2023-11-27 16:15:34 +00:00
|
|
|
|
q = (
|
|
|
|
|
select(Shout)
|
2024-02-21 16:14:58 +00:00
|
|
|
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
2024-05-30 04:12:00 +00:00
|
|
|
|
.filter(and_(Shout.deleted_at.is_(None), Shout.created_by == int(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-05-06 21:06:31 +00:00
|
|
|
|
return {"shouts": shouts}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
2023-11-22 16:38:39 +00:00
|
|
|
|
|
2024-04-17 15:32:23 +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):
|
2025-01-16 02:34:43 +00:00
|
|
|
|
logger.info(f"Starting create_shout with input: {inp}")
|
2024-04-17 15:32:23 +00:00
|
|
|
|
user_id = info.context.get("user_id")
|
2024-05-06 17:59:56 +00:00
|
|
|
|
author_dict = info.context.get("author")
|
2025-01-16 02:34:43 +00:00
|
|
|
|
logger.debug(f"Context user_id: {user_id}, author: {author_dict}")
|
|
|
|
|
|
2024-05-06 17:59:56 +00:00
|
|
|
|
if not author_dict:
|
2025-01-21 14:50:02 +00:00
|
|
|
|
logger.error("Author profile not found in context")
|
2024-05-06 17:59:56 +00:00
|
|
|
|
return {"error": "author profile was not found"}
|
2025-01-16 02:34:43 +00:00
|
|
|
|
|
2025-01-21 14:50:02 +00:00
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
|
if user_id and author_id:
|
|
|
|
|
try:
|
|
|
|
|
with local_session() as session:
|
|
|
|
|
author_id = int(author_id)
|
|
|
|
|
current_time = int(time.time())
|
|
|
|
|
slug = inp.get("slug") or f"draft-{current_time}"
|
2025-01-21 15:28:03 +00:00
|
|
|
|
|
2025-01-21 16:58:20 +00:00
|
|
|
|
logger.info(f"Creating shout with input: {inp}")
|
|
|
|
|
|
2025-01-21 15:28:03 +00:00
|
|
|
|
new_shout = Shout(
|
2025-01-21 16:58:20 +00:00
|
|
|
|
slug=slug,
|
|
|
|
|
published_at=None,
|
|
|
|
|
body=inp.get("body", ""),
|
|
|
|
|
layout=inp.get("layout", "article"),
|
|
|
|
|
title=inp.get("title", ""),
|
|
|
|
|
topics=inp.get("topics", []),
|
2025-01-21 15:28:03 +00:00
|
|
|
|
created_by=author_id,
|
|
|
|
|
created_at=current_time,
|
2025-01-21 16:33:28 +00:00
|
|
|
|
community=1
|
2025-01-21 15:28:03 +00:00
|
|
|
|
)
|
2025-01-21 14:50:02 +00:00
|
|
|
|
|
|
|
|
|
# Check for duplicate slug
|
|
|
|
|
logger.debug(f"Checking for existing slug: {slug}")
|
|
|
|
|
same_slug_shout = session.query(Shout).filter(Shout.slug == new_shout.slug).first()
|
|
|
|
|
c = 1
|
|
|
|
|
while same_slug_shout is not None:
|
|
|
|
|
logger.debug(f"Found duplicate slug, trying iteration {c}")
|
|
|
|
|
new_shout.slug = f"{slug}-{c}"
|
|
|
|
|
same_slug_shout = session.query(Shout).filter(Shout.slug == new_shout.slug).first()
|
|
|
|
|
c += 1
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
logger.info("Creating new shout object")
|
|
|
|
|
session.add(new_shout)
|
|
|
|
|
session.commit()
|
|
|
|
|
logger.info(f"Created shout with ID: {new_shout.id}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error creating shout object: {e}", exc_info=True)
|
|
|
|
|
return {"error": f"Database error: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
# Get created shout
|
|
|
|
|
try:
|
|
|
|
|
logger.debug(f"Retrieving created shout with slug: {slug}")
|
|
|
|
|
shout = session.query(Shout).where(Shout.slug == slug).first()
|
|
|
|
|
if not shout:
|
|
|
|
|
logger.error("Created shout not found in database")
|
|
|
|
|
return {"error": "Shout creation failed - not found after commit"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error retrieving created shout: {e}", exc_info=True)
|
|
|
|
|
return {"error": f"Error retrieving created shout: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
# Link author
|
|
|
|
|
try:
|
|
|
|
|
logger.debug(f"Linking author {author_id} to shout {shout.id}")
|
|
|
|
|
existing_sa = session.query(ShoutAuthor).filter_by(shout=shout.id, author=author_id).first()
|
|
|
|
|
if not existing_sa:
|
|
|
|
|
sa = ShoutAuthor(shout=shout.id, author=author_id)
|
|
|
|
|
session.add(sa)
|
|
|
|
|
logger.info(f"Added author {author_id} to shout {shout.id}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error linking author: {e}", exc_info=True)
|
|
|
|
|
return {"error": f"Error linking author: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
# Link topics
|
|
|
|
|
try:
|
|
|
|
|
logger.debug(f"Linking topics: {inp.get('topics', [])}")
|
|
|
|
|
topics = session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all()
|
|
|
|
|
for topic in topics:
|
|
|
|
|
existing_st = session.query(ShoutTopic).filter_by(shout=shout.id, topic=topic.id).first()
|
|
|
|
|
if not existing_st:
|
|
|
|
|
t = ShoutTopic(topic=topic.id, shout=shout.id)
|
|
|
|
|
session.add(t)
|
|
|
|
|
logger.info(f"Added topic {topic.id} to shout {shout.id}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error linking topics: {e}", exc_info=True)
|
|
|
|
|
return {"error": f"Error linking topics: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
try:
|
2025-01-16 02:34:43 +00:00
|
|
|
|
session.commit()
|
2025-01-21 14:50:02 +00:00
|
|
|
|
logger.info("Final commit successful")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error in final commit: {e}", exc_info=True)
|
|
|
|
|
return {"error": f"Error in final commit: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
logger.debug("Following created shout")
|
|
|
|
|
await follow(None, info, "shout", shout.slug)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f"Error following shout: {e}", exc_info=True)
|
|
|
|
|
# Don't return error as this is not critical
|
|
|
|
|
|
|
|
|
|
# После успешного создания обновляем статистику автора
|
|
|
|
|
try:
|
|
|
|
|
author = session.query(Author).filter(Author.id == author_id).first()
|
|
|
|
|
if author and author.stat:
|
2025-01-21 15:28:03 +00:00
|
|
|
|
author.stat["shouts"] = author.stat.get("shouts", 0) + 1
|
2025-01-21 14:50:02 +00:00
|
|
|
|
session.add(author)
|
|
|
|
|
session.commit()
|
|
|
|
|
await cache_author(author.dict())
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f"Error updating author stats: {e}", exc_info=True)
|
|
|
|
|
# Не возвращаем ошибку, так как это некритично
|
|
|
|
|
|
|
|
|
|
logger.info(f"Successfully created shout {shout.id}")
|
2025-01-21 10:11:15 +00:00
|
|
|
|
return {"shout": shout}
|
|
|
|
|
|
2025-01-21 14:50:02 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Unexpected error in create_shout: {e}", exc_info=True)
|
|
|
|
|
return {"error": f"Unexpected error: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
error_msg = "cant create shout" if user_id else "unauthorized"
|
|
|
|
|
logger.error(f"Create shout failed: {error_msg}")
|
|
|
|
|
return {"error": error_msg}
|
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):
|
2024-03-07 08:55:23 +00:00
|
|
|
|
with session.begin():
|
2024-05-30 04:12:00 +00:00
|
|
|
|
shout = session.query(Shout).options(joinedload(Shout.topics)).filter(Shout.id == shout.id).first()
|
2024-03-07 08:55:23 +00:00
|
|
|
|
if not shout:
|
|
|
|
|
return
|
|
|
|
|
old_main_topic = (
|
2024-05-30 04:12:00 +00:00
|
|
|
|
session.query(ShoutTopic).filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main.is_(True))).first()
|
2024-02-02 20:59:42 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-03-07 08:55:23 +00:00
|
|
|
|
main_topic = session.query(Topic).filter(Topic.slug == main_topic).first()
|
|
|
|
|
|
|
|
|
|
if main_topic:
|
|
|
|
|
new_main_topic = (
|
|
|
|
|
session.query(ShoutTopic)
|
2024-05-30 04:12:00 +00:00
|
|
|
|
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id))
|
2024-03-07 08:55:23 +00:00
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-30 04:12:00 +00:00
|
|
|
|
if old_main_topic and new_main_topic and old_main_topic is not new_main_topic:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
ShoutTopic.update(old_main_topic, {"main": False})
|
2024-03-07 08:55:23 +00:00
|
|
|
|
session.add(old_main_topic)
|
2024-02-02 20:59:42 +00:00
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
ShoutTopic.update(new_main_topic, {"main": True})
|
2024-03-07 08:55:23 +00:00
|
|
|
|
session.add(new_main_topic)
|
2024-02-02 20:59:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def patch_topics(session, shout, topics_input):
|
2024-05-30 04:12:00 +00:00
|
|
|
|
new_topics_to_link = [Topic(**new_topic) for new_topic in topics_input if new_topic["id"] < 0]
|
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-05-30 04:12:00 +00:00
|
|
|
|
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0]
|
2024-02-02 20:59:42 +00:00
|
|
|
|
existing_topic_to_link_ids = [
|
2024-04-17 15:32:23 +00:00
|
|
|
|
existing_topic_input["id"]
|
2024-02-02 20:59:42 +00:00
|
|
|
|
for existing_topic_input in existing_topics_input
|
2024-04-17 15:32:23 +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-05-30 04:12:00 +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-04-17 15:32:23 +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-04-17 15:32:23 +00:00
|
|
|
|
@mutation.field("update_shout")
|
2022-06-19 11:11:14 +00:00
|
|
|
|
@login_required
|
2024-03-05 15:13:39 +00:00
|
|
|
|
async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
|
2024-04-17 15:32:23 +00:00
|
|
|
|
user_id = info.context.get("user_id")
|
|
|
|
|
roles = info.context.get("roles", [])
|
2024-05-06 18:01:10 +00:00
|
|
|
|
author_dict = info.context.get("author")
|
|
|
|
|
if not author_dict:
|
|
|
|
|
return {"error": "author profile was not found"}
|
2024-04-19 15:22:07 +00:00
|
|
|
|
author_id = author_dict.get("id")
|
2024-03-05 13:59:55 +00:00
|
|
|
|
shout_input = shout_input or {}
|
2024-03-05 14:53:49 +00:00
|
|
|
|
current_time = int(time.time())
|
2024-04-17 15:32:23 +00:00
|
|
|
|
shout_id = shout_id or shout_input.get("id", shout_id)
|
|
|
|
|
slug = shout_input.get("slug")
|
2024-03-05 13:59:55 +00:00
|
|
|
|
if not user_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": "unauthorized"}
|
2024-02-27 13:28:54 +00:00
|
|
|
|
try:
|
|
|
|
|
with local_session() as session:
|
2024-04-19 15:22:07 +00:00
|
|
|
|
if author_id:
|
|
|
|
|
logger.info(f"author for shout#{shout_id} detected author #{author_id}")
|
2024-02-27 13:28:54 +00:00
|
|
|
|
shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first()
|
2024-12-11 21:32:27 +00:00
|
|
|
|
|
2024-03-05 14:53:49 +00:00
|
|
|
|
if not shout_by_id:
|
2024-12-11 21:32:27 +00:00
|
|
|
|
logger.error(f"shout#{shout_id} not found")
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": "shout not found"}
|
2024-12-11 21:32:27 +00:00
|
|
|
|
logger.info(f"shout#{shout_id} found")
|
|
|
|
|
|
2024-03-11 08:16:12 +00:00
|
|
|
|
if slug != shout_by_id.slug:
|
2025-01-21 14:50:02 +00:00
|
|
|
|
same_slug_shout = session.query(Shout).filter(Shout.slug == slug).first()
|
|
|
|
|
c = 1
|
|
|
|
|
while same_slug_shout is not None:
|
|
|
|
|
c += 1
|
|
|
|
|
slug = f"{slug}-{c}"
|
|
|
|
|
same_slug_shout = session.query(Shout).filter(Shout.slug == slug).first()
|
|
|
|
|
shout_input["slug"] = slug
|
2024-12-11 21:32:27 +00:00
|
|
|
|
logger.info(f"shout#{shout_id} slug patched")
|
2024-12-11 22:04:11 +00:00
|
|
|
|
|
2024-05-30 04:12:00 +00:00
|
|
|
|
if filter(lambda x: x.id == author_id, [x for x in shout_by_id.authors]) or "editor" in roles:
|
2024-12-11 21:32:27 +00:00
|
|
|
|
logger.info(f"shout#{shout_id} is author or editor")
|
2024-03-06 07:44:08 +00:00
|
|
|
|
# topics patch
|
2024-04-17 15:32:23 +00:00
|
|
|
|
topics_input = shout_input.get("topics")
|
2024-03-06 07:44:08 +00:00
|
|
|
|
if topics_input:
|
2024-12-11 21:32:27 +00:00
|
|
|
|
logger.info(f"topics_input: {topics_input}")
|
2024-03-06 07:44:08 +00:00
|
|
|
|
patch_topics(session, shout_by_id, topics_input)
|
2024-04-17 15:32:23 +00:00
|
|
|
|
del shout_input["topics"]
|
2024-05-05 15:46:16 +00:00
|
|
|
|
for tpc in topics_input:
|
2024-06-05 18:04:48 +00:00
|
|
|
|
await cache_by_id(Topic, tpc["id"], cache_topic)
|
2024-03-06 07:44:08 +00:00
|
|
|
|
|
|
|
|
|
# main topic
|
2024-04-17 15:32:23 +00:00
|
|
|
|
main_topic = shout_input.get("main_topic")
|
2024-03-06 07:44:08 +00:00
|
|
|
|
if main_topic:
|
|
|
|
|
patch_main_topic(session, main_topic, shout_by_id)
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
shout_input["updated_at"] = current_time
|
|
|
|
|
shout_input["published_at"] = current_time if publish else None
|
2024-03-06 07:44:08 +00:00
|
|
|
|
Shout.update(shout_by_id, shout_input)
|
|
|
|
|
session.add(shout_by_id)
|
|
|
|
|
session.commit()
|
2024-02-27 13:28:54 +00:00
|
|
|
|
|
2024-03-06 07:44:08 +00:00
|
|
|
|
shout_dict = shout_by_id.dict()
|
2024-02-27 13:28:54 +00:00
|
|
|
|
|
2025-01-16 02:42:53 +00:00
|
|
|
|
# Инвалидация кэша после обновления
|
|
|
|
|
try:
|
|
|
|
|
logger.info("Invalidating cache after shout update")
|
2025-01-21 15:28:03 +00:00
|
|
|
|
|
2025-01-16 02:53:37 +00:00
|
|
|
|
cache_keys = [
|
2025-01-16 02:42:53 +00:00
|
|
|
|
"feed", # лента
|
|
|
|
|
f"author_{author_id}", # публикации автора
|
|
|
|
|
"random_top", # случайные топовые
|
|
|
|
|
"unrated", # неоцененные
|
2025-01-16 02:53:37 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Добавляем ключи для старых тем (до обновления)
|
|
|
|
|
for topic in shout_by_id.topics:
|
|
|
|
|
cache_keys.append(f"topic_{topic.id}")
|
|
|
|
|
cache_keys.append(f"topic_shouts_{topic.id}")
|
2025-01-21 15:28:03 +00:00
|
|
|
|
|
2025-01-16 02:53:37 +00:00
|
|
|
|
# Добавляем ключи для новых тем (если есть в shout_input)
|
|
|
|
|
if shout_input.get("topics"):
|
|
|
|
|
for topic in shout_input["topics"]:
|
|
|
|
|
if topic.get("id"):
|
|
|
|
|
cache_keys.append(f"topic_{topic.id}")
|
|
|
|
|
cache_keys.append(f"topic_shouts_{topic.id}")
|
|
|
|
|
|
|
|
|
|
await invalidate_shouts_cache(cache_keys)
|
2025-01-21 15:28:03 +00:00
|
|
|
|
|
2025-01-16 02:53:37 +00:00
|
|
|
|
# Обновляем кэш тем и авторов
|
2025-01-16 02:42:53 +00:00
|
|
|
|
for topic in shout_by_id.topics:
|
2025-01-16 02:53:37 +00:00
|
|
|
|
await cache_by_id(Topic, topic.id, cache_topic)
|
|
|
|
|
for author in shout_by_id.authors:
|
|
|
|
|
await cache_author(author.dict())
|
2025-01-21 15:28:03 +00:00
|
|
|
|
|
2025-01-16 02:42:53 +00:00
|
|
|
|
logger.info("Cache invalidated successfully")
|
|
|
|
|
except Exception as cache_error:
|
|
|
|
|
logger.warning(f"Cache invalidation error: {cache_error}", exc_info=True)
|
|
|
|
|
|
2024-03-06 07:44:08 +00:00
|
|
|
|
if not publish:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
await notify_shout(shout_dict, "update")
|
2024-03-06 07:44:08 +00:00
|
|
|
|
else:
|
2024-04-17 15:32:23 +00:00
|
|
|
|
await notify_shout(shout_dict, "published")
|
2024-03-06 07:44:08 +00:00
|
|
|
|
# search service indexing
|
|
|
|
|
search_service.index(shout_by_id)
|
2024-05-05 15:46:16 +00:00
|
|
|
|
for a in shout_by_id.authors:
|
2024-06-05 18:04:48 +00:00
|
|
|
|
await cache_by_id(Author, a.id, cache_author)
|
2024-12-11 21:32:27 +00:00
|
|
|
|
logger.info(f"shout#{shout_id} updated")
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"shout": shout_dict, "error": None}
|
2024-02-27 13:28:54 +00:00
|
|
|
|
else:
|
2025-01-21 14:50:02 +00:00
|
|
|
|
logger.warning(f"updater for shout#{shout_id} is not author or editor")
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": "access denied", "shout": None}
|
2024-02-27 13:28:54 +00:00
|
|
|
|
|
|
|
|
|
except Exception as exc:
|
2024-03-11 08:16:12 +00:00
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
traceback.print_exc()
|
2024-02-27 13:28:54 +00:00
|
|
|
|
logger.error(exc)
|
2024-04-17 15:32:23 +00:00
|
|
|
|
logger.error(f" cannot update with data: {shout_input}")
|
2024-02-17 06:35:11 +00:00
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": "cant update shout"}
|
2022-09-03 10:50:14 +00:00
|
|
|
|
|
2023-11-22 16:38:39 +00:00
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
|
@mutation.field("delete_shout")
|
2022-06-19 11:11:14 +00:00
|
|
|
|
@login_required
|
2024-03-06 07:44:08 +00:00
|
|
|
|
async def delete_shout(_, info, shout_id: int):
|
2024-04-17 15:32:23 +00:00
|
|
|
|
user_id = info.context.get("user_id")
|
2024-04-19 15:22:07 +00:00
|
|
|
|
roles = info.context.get("roles", [])
|
2024-05-06 18:01:10 +00:00
|
|
|
|
author_dict = info.context.get("author")
|
|
|
|
|
if not author_dict:
|
|
|
|
|
return {"error": "author profile was not found"}
|
2024-04-19 15:22:07 +00:00
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
|
if user_id and author_id:
|
|
|
|
|
author_id = int(author_id)
|
2024-02-27 11:29:28 +00:00
|
|
|
|
with local_session() as session:
|
|
|
|
|
shout = session.query(Shout).filter(Shout.id == shout_id).first()
|
2024-04-19 15:22:07 +00:00
|
|
|
|
if not isinstance(shout, Shout):
|
2024-04-17 15:32:23 +00:00
|
|
|
|
return {"error": "invalid shout id"}
|
2024-04-19 15:22:07 +00:00
|
|
|
|
shout_dict = shout.dict()
|
|
|
|
|
# NOTE: only owner and editor can mark the shout as deleted
|
|
|
|
|
if shout_dict["created_by"] == author_id or "editor" in roles:
|
|
|
|
|
shout_dict["deleted_at"] = int(time.time())
|
|
|
|
|
Shout.update(shout, shout_dict)
|
|
|
|
|
session.add(shout)
|
|
|
|
|
session.commit()
|
2024-05-05 15:46:16 +00:00
|
|
|
|
|
2024-05-06 17:41:34 +00:00
|
|
|
|
for author in shout.authors:
|
2024-06-05 18:04:48 +00:00
|
|
|
|
await cache_by_id(Author, author.id, cache_author)
|
2024-06-05 15:29:15 +00:00
|
|
|
|
info.context["author"] = author.dict()
|
|
|
|
|
info.context["user_id"] = author.user
|
|
|
|
|
unfollow(None, info, "shout", shout.slug)
|
2024-05-05 15:46:16 +00:00
|
|
|
|
|
2024-05-06 17:41:34 +00:00
|
|
|
|
for topic in shout.topics:
|
2024-06-05 18:04:48 +00:00
|
|
|
|
await cache_by_id(Topic, topic.id, cache_topic)
|
2024-05-05 15:46:16 +00:00
|
|
|
|
|
2024-04-19 15:22:07 +00:00
|
|
|
|
await notify_shout(shout_dict, "delete")
|
|
|
|
|
return {"error": None}
|
|
|
|
|
else:
|
|
|
|
|
return {"error": "access denied"}
|