This commit is contained in:
2024-02-21 19:14:58 +03:00
parent 88cd6e1060
commit 214af0cf51
33 changed files with 750 additions and 773 deletions

View File

@@ -42,43 +42,43 @@ from resolvers.topic import (
__all__ = [
# author
"get_author",
"get_author_id",
"get_author_follows",
"get_authors_all",
"load_authors_by",
"rate_author",
"update_author",
'get_author',
'get_author_id',
'get_author_follows',
'get_authors_all',
'load_authors_by',
'rate_author',
'update_author',
# community
"get_community",
"get_communities_all",
'get_community',
'get_communities_all',
# topic
"get_topic",
"get_topics_all",
"get_topics_by_community",
"get_topics_by_author",
'get_topic',
'get_topics_all',
'get_topics_by_community',
'get_topics_by_author',
# reader
"get_shout",
"load_shouts_by",
"load_shouts_feed",
"load_shouts_search",
"load_shouts_followed",
"load_shouts_unrated",
"load_shouts_random_top",
"load_shouts_random_topic",
'get_shout',
'load_shouts_by',
'load_shouts_feed',
'load_shouts_search',
'load_shouts_followed',
'load_shouts_unrated',
'load_shouts_random_top',
'load_shouts_random_topic',
# follower
"follow",
"unfollow",
"get_topic_followers",
"get_shout_followers",
"get_author_followers",
'follow',
'unfollow',
'get_topic_followers',
'get_shout_followers',
'get_author_followers',
# editor
"create_shout",
"update_shout",
"delete_shout",
'create_shout',
'update_shout',
'delete_shout',
# reaction
"create_reaction",
"update_reaction",
"delete_reaction",
"load_reactions_by",
'create_reaction',
'update_reaction',
'delete_reaction',
'load_reactions_by',
]

View File

@@ -21,18 +21,18 @@ from services.logger import root_logger as logger
def add_author_stat_columns(q):
shout_author_aliased = aliased(ShoutAuthor)
q = q.outerjoin(shout_author_aliased).add_columns(
func.count(distinct(shout_author_aliased.shout)).label("shouts_stat")
func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
)
followers_table = aliased(AuthorFollower)
q = q.outerjoin(followers_table, followers_table.author == Author.id).add_columns(
func.count(distinct(followers_table.follower)).label("followers_stat")
func.count(distinct(followers_table.follower)).label('followers_stat')
)
followings_table = aliased(AuthorFollower)
q = q.outerjoin(
followings_table, followings_table.follower == Author.id
).add_columns(func.count(distinct(followers_table.author)).label("followings_stat"))
).add_columns(func.count(distinct(followers_table.author)).label('followings_stat'))
q = q.group_by(Author.id)
return q
@@ -45,29 +45,29 @@ async def get_authors_from_query(q):
q
):
author.stat = {
"shouts": shouts_stat,
"viewed": await ViewedStorage.get_author(author.slug),
"followers": followers_stat,
"followings": followings_stat,
'shouts': shouts_stat,
'viewed': await ViewedStorage.get_author(author.slug),
'followers': followers_stat,
'followings': followings_stat,
}
authors.append(author)
return authors
@mutation.field("update_author")
@mutation.field('update_author')
@login_required
async def update_author(_, info, profile):
user_id = info.context["user_id"]
user_id = info.context['user_id']
with local_session() as session:
author = session.query(Author).where(Author.user == user_id).first()
Author.update(author, profile)
session.add(author)
session.commit()
return {"error": None, "author": author}
return {'error': None, 'author': author}
# TODO: caching query
@query.field("get_authors_all")
@query.field('get_authors_all')
async def get_authors_all(_, _info):
authors = []
with local_session() as session:
@@ -168,19 +168,19 @@ async def load_author_with_stats(q):
)
.count()
)
author.stat["rating"] = likes_count - dislikes_count
author.stat["rating_shouts"] = count_author_shouts_rating(
author.stat['rating'] = likes_count - dislikes_count
author.stat['rating_shouts'] = count_author_shouts_rating(
session, author.id
)
author.stat["rating_comments"] = count_author_comments_rating(
author.stat['rating_comments'] = count_author_comments_rating(
session, author.id
)
author.stat["commented"] = comments_count
author.stat['commented'] = comments_count
return author
@query.field("get_author")
async def get_author(_, _info, slug="", author_id=None):
@query.field('get_author')
async def get_author(_, _info, slug='', author_id=None):
q = None
if slug or author_id:
if bool(slug):
@@ -192,64 +192,64 @@ async def get_author(_, _info, slug="", author_id=None):
async def get_author_by_user_id(user_id: str):
redis_key = f"user:{user_id}:author"
res = await redis.execute("GET", redis_key)
redis_key = f'user:{user_id}:author'
res = await redis.execute('GET', redis_key)
if isinstance(res, str):
author = json.loads(res)
if author.get("id"):
logger.debug(f"got cached author: {author}")
if author.get('id'):
logger.debug(f'got cached author: {author}')
return author
logger.info(f"getting author id for {user_id}")
logger.info(f'getting author id for {user_id}')
q = select(Author).filter(Author.user == user_id)
author = await load_author_with_stats(q)
if author:
await redis.execute(
"set",
'set',
redis_key,
json.dumps(
{
"id": author.id,
"name": author.name,
"slug": author.slug,
"pic": author.pic,
'id': author.id,
'name': author.name,
'slug': author.slug,
'pic': author.pic,
}
),
)
return author
@query.field("get_author_id")
@query.field('get_author_id')
async def get_author_id(_, _info, user: str):
return await get_author_by_user_id(user)
@query.field("load_authors_by")
@query.field('load_authors_by')
async def load_authors_by(_, _info, by, limit, offset):
q = select(Author)
q = add_author_stat_columns(q)
if by.get("slug"):
if by.get('slug'):
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
elif by.get("name"):
elif by.get('name'):
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
elif by.get("topic"):
elif by.get('topic'):
q = (
q.join(ShoutAuthor)
.join(ShoutTopic)
.join(Topic)
.where(Topic.slug == by["topic"])
.where(Topic.slug == by['topic'])
)
if by.get("last_seen"): # in unix time
before = int(time.time()) - by["last_seen"]
if by.get('last_seen'): # in unix time
before = int(time.time()) - by['last_seen']
q = q.filter(Author.last_seen > before)
elif by.get("created_at"): # in unix time
before = int(time.time()) - by["created_at"]
elif by.get('created_at'): # in unix time
before = int(time.time()) - by['created_at']
q = q.filter(Author.created_at > before)
order = by.get("order")
if order == "followers" or order == "shouts":
q = q.order_by(desc(f"{order}_stat"))
order = by.get('order')
if order == 'followers' or order == 'shouts':
q = q.order_by(desc(f'{order}_stat'))
q = q.limit(limit).offset(offset)
@@ -258,32 +258,24 @@ async def load_authors_by(_, _info, by, limit, offset):
return authors
@query.field("get_author_follows")
@query.field('get_author_follows')
async def get_author_follows(
_, _info, slug="", user=None, author_id=None
_, _info, slug='', user=None, author_id=None
) -> List[Author]:
user_id = user
if not user_id and author_id or slug:
with local_session() as session:
author = (
session.query(Author)
.where(or_(Author.id == author_id, Author.slug == slug))
.first()
)
user_id = author.user
if user_id:
follows = await get_follows_by_user_id(user_id)
return follows
else:
raise ValueError("Author not found")
with local_session() as session:
if not user and (author_id or slug):
user = session.query(Author.user).where(or_(Author.id == author_id, Author.slug == slug)).first()
if user:
follows = await get_follows_by_user_id(user)
return follows
else:
raise ValueError('Author not found')
@mutation.field("rate_author")
@mutation.field('rate_author')
@login_required
async def rate_author(_, info, rated_slug, value):
user_id = info.context["user_id"]
user_id = info.context['user_id']
with local_session() as session:
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
@@ -312,19 +304,19 @@ async def rate_author(_, info, rated_slug, value):
session.add(rating)
session.commit()
except Exception as err:
return {"error": err}
return {'error': err}
return {}
async def create_author(user_id: str, slug: str, name: str = ""):
async def create_author(user_id: str, slug: str, name: str = ''):
with local_session() as session:
new_author = Author(user=user_id, slug=slug, name=name)
session.add(new_author)
session.commit()
logger.info(f"author created by webhook {new_author.dict()}")
logger.info(f'author created by webhook {new_author.dict()}')
@query.field("get_author_followers")
@query.field('get_author_followers')
async def get_author_followers(_, _info, slug) -> List[Author]:
q = select(Author)
q = add_author_stat_columns(q)

View File

@@ -6,10 +6,10 @@ from services.db import local_session
from services.schema import mutation
@mutation.field("accept_invite")
@mutation.field('accept_invite')
@login_required
async def accept_invite(_, info, invite_id: int):
user_id = info.context["user_id"]
user_id = info.context['user_id']
# Check if the user exists
with local_session() as session:
@@ -30,19 +30,19 @@ async def accept_invite(_, info, invite_id: int):
session.delete(invite)
session.add(shout)
session.commit()
return {"success": True, "message": "Invite accepted"}
return {'success': True, 'message': 'Invite accepted'}
else:
return {"error": "Shout not found"}
return {'error': 'Shout not found'}
else:
return {"error": "Invalid invite or already accepted/rejected"}
return {'error': 'Invalid invite or already accepted/rejected'}
else:
return {"error": "User not found"}
return {'error': 'User not found'}
@mutation.field("reject_invite")
@mutation.field('reject_invite')
@login_required
async def reject_invite(_, info, invite_id: int):
user_id = info.context["user_id"]
user_id = info.context['user_id']
# Check if the user exists
with local_session() as session:
@@ -58,17 +58,17 @@ async def reject_invite(_, info, invite_id: int):
# Delete the invite
session.delete(invite)
session.commit()
return {"success": True, "message": "Invite rejected"}
return {'success': True, 'message': 'Invite rejected'}
else:
return {"error": "Invalid invite or already accepted/rejected"}
return {'error': 'Invalid invite or already accepted/rejected'}
else:
return {"error": "User not found"}
return {'error': 'User not found'}
@mutation.field("create_invite")
@mutation.field('create_invite')
@login_required
async def create_invite(_, info, slug: str = "", author_id: int = 0):
user_id = info.context["user_id"]
async def create_invite(_, info, slug: str = '', author_id: int = 0):
user_id = info.context['user_id']
# Check if the inviter is the owner of the shout
with local_session() as session:
@@ -90,7 +90,7 @@ async def create_invite(_, info, slug: str = "", author_id: int = 0):
.first()
)
if existing_invite:
return {"error": "Invite already sent"}
return {'error': 'Invite already sent'}
# Create a new invite
new_invite = Invite(
@@ -102,17 +102,17 @@ async def create_invite(_, info, slug: str = "", author_id: int = 0):
session.add(new_invite)
session.commit()
return {"error": None, "invite": new_invite}
return {'error': None, 'invite': new_invite}
else:
return {"error": "Invalid author"}
return {'error': 'Invalid author'}
else:
return {"error": "Access denied"}
return {'error': 'Access denied'}
@mutation.field("remove_author")
@mutation.field('remove_author')
@login_required
async def remove_author(_, info, slug: str = "", author_id: int = 0):
user_id = info.context["user_id"]
async def remove_author(_, info, slug: str = '', author_id: int = 0):
user_id = info.context['user_id']
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
@@ -124,13 +124,13 @@ async def remove_author(_, info, slug: str = "", author_id: int = 0):
]
session.commit()
return {}
return {"error": "Access denied"}
return {'error': 'Access denied'}
@mutation.field("remove_invite")
@mutation.field('remove_invite')
@login_required
async def remove_invite(_, info, invite_id: int):
user_id = info.context["user_id"]
user_id = info.context['user_id']
# Check if the user exists
with local_session() as session:
@@ -148,6 +148,6 @@ async def remove_invite(_, info, invite_id: int):
session.commit()
return {}
else:
return {"error": "Invalid invite or already accepted/rejected"}
return {'error': 'Invalid invite or already accepted/rejected'}
else:
return {"error": "Author not found"}
return {'error': 'Author not found'}

View File

@@ -14,12 +14,12 @@ def add_community_stat_columns(q):
shout_community_aliased = aliased(ShoutCommunity)
q = q.outerjoin(shout_community_aliased).add_columns(
func.count(distinct(shout_community_aliased.shout)).label("shouts_stat")
func.count(distinct(shout_community_aliased.shout)).label('shouts_stat')
)
q = q.outerjoin(
community_followers, community_followers.author == Author.id
).add_columns(
func.count(distinct(community_followers.follower)).label("followers_stat")
func.count(distinct(community_followers.follower)).label('followers_stat')
)
q = q.group_by(Author.id)
@@ -32,8 +32,8 @@ def get_communities_from_query(q):
with local_session() as session:
for [c, shouts_stat, followers_stat] in session.execute(q):
c.stat = {
"shouts": shouts_stat,
"followers": followers_stat,
'shouts': shouts_stat,
'followers': followers_stat,
# "commented": commented_stat,
}
ccc.append(c)
@@ -72,7 +72,7 @@ def community_unfollow(follower_id, slug):
return False
@query.field("get_communities_all")
@query.field('get_communities_all')
async def get_communities_all(_, _info):
q = select(Author)
q = add_community_stat_columns(q)
@@ -80,7 +80,7 @@ async def get_communities_all(_, _info):
return get_communities_from_query(q)
@query.field("get_community")
@query.field('get_community')
async def get_community(_, _info, slug):
q = select(Community).where(Community.slug == slug)
q = add_community_stat_columns(q)

View File

@@ -18,20 +18,17 @@ 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()
if author:
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.filter(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id))
.filter(Shout.published_at.is_(None))
.group_by(Shout.id)
@@ -40,28 +37,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)
@@ -77,7 +74,7 @@ async def create_shout(_, info, inp):
topics = (
session.query(Topic)
.filter(Topic.slug.in_(inp.get("topics", [])))
.filter(Topic.slug.in_(inp.get('topics', [])))
.all()
)
for topic in topics:
@@ -89,20 +86,15 @@ 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):
old_main_topic = (
session.query(ShoutTopic)
.filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.main.is_(True),
)
)
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main.is_(True)))
.first()
)
@@ -112,25 +104,22 @@ def patch_main_topic(session, main_topic, shout):
new_main_topic = (
session.query(ShoutTopic)
.filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.topic == main_topic.id,
)
and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id)
)
.first()
)
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
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)
@@ -141,12 +130,12 @@ def patch_topics(session, shout, topics_input):
session.add(created_unlinked_topic)
existing_topics_input = [
topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0
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:
@@ -158,60 +147,54 @@ def patch_topics(session, shout, topics_input):
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(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.topic.in_(topic_to_unlink_ids),
)
and_(ShoutTopic.shout == shout.id, ShoutTopic.topic.in_(topic_to_unlink_ids))
).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)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.filter(Shout.id == shout_id)
.first()
)
if not shout:
return {"error": "shout not found"}
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
and 'editor' not in roles
):
return {"error": "access denied"}
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()
@@ -219,44 +202,44 @@ 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": "cant update shout"}
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
and 'editor' not in roles
):
return {"error": "access denied"}
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 {}
@@ -290,7 +273,7 @@ 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(
proposal_dict['quote'] = apply_diff(
replied_reaction.quote, proposal_diff
)
Reaction.update(proposal, proposal_dict)
@@ -298,7 +281,7 @@ def handle_proposing(session, r, shout):
# 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()

View File

@@ -11,6 +11,7 @@ from orm.author import Author, AuthorFollower
from orm.reaction import Reaction
from orm.shout import Shout, ShoutReactionsFollower
from orm.topic import Topic, TopicFollower
from resolvers.author import add_author_stat_columns
from resolvers.community import community_follow, community_unfollow
from resolvers.topic import (
topic_follow,
@@ -26,16 +27,16 @@ from services.logger import root_logger as logger
from services.rediscache import redis
@mutation.field("follow")
@mutation.field('follow')
@login_required
async def follow(_, info, what, slug):
try:
user_id = info.context["user_id"]
user_id = info.context['user_id']
with local_session() as session:
actor = session.query(Author).filter(Author.user == user_id).first()
if actor:
follower_id = actor.id
if what == "AUTHOR":
if what == 'AUTHOR':
if author_follow(follower_id, slug):
author = (
session.query(Author.id).where(Author.slug == slug).one()
@@ -44,30 +45,30 @@ async def follow(_, info, what, slug):
session.query(Author).where(Author.id == follower_id).one()
)
await notify_follower(follower.dict(), author.id)
elif what == "TOPIC":
elif what == 'TOPIC':
topic_follow(follower_id, slug)
elif what == "COMMUNITY":
elif what == 'COMMUNITY':
community_follow(follower_id, slug)
elif what == "REACTIONS":
elif what == 'REACTIONS':
reactions_follow(follower_id, slug)
except Exception as e:
logger.debug(info, what, slug)
logger.error(e)
return {"error": str(e)}
return {'error': str(e)}
return {}
@mutation.field("unfollow")
@mutation.field('unfollow')
@login_required
async def unfollow(_, info, what, slug):
user_id = info.context["user_id"]
user_id = info.context['user_id']
try:
with local_session() as session:
actor = session.query(Author).filter(Author.user == user_id).first()
if actor:
follower_id = actor.id
if what == "AUTHOR":
if what == 'AUTHOR':
if author_unfollow(follower_id, slug):
author = (
session.query(Author.id).where(Author.slug == slug).one()
@@ -75,15 +76,15 @@ async def unfollow(_, info, what, slug):
follower = (
session.query(Author).where(Author.id == follower_id).one()
)
await notify_follower(follower.dict(), author.id, "unfollow")
elif what == "TOPIC":
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
topic_unfollow(follower_id, slug)
elif what == "COMMUNITY":
elif what == 'COMMUNITY':
community_unfollow(follower_id, slug)
elif what == "REACTIONS":
elif what == 'REACTIONS':
reactions_unfollow(follower_id, slug)
except Exception as e:
return {"error": str(e)}
return {'error': str(e)}
return {}
@@ -97,22 +98,45 @@ def query_follows(user_id: str):
if isinstance(author, Author):
author_id = author.id
authors_query = (
select(column("name"), column("id"), column("slug"), column("pic"))
select(column('name'), column('id'), column('slug'), column('pic'), column('bio'))
.select_from(Author)
.join(AuthorFollower, AuthorFollower.follower == author_id)
.filter(AuthorFollower.author == Author.id)
)
authors_query = add_author_stat_columns(authors_query)
topics_query = (
select(column("title"), column("id"), column("slug"), column("pic"))
select(column('title'), column('id'), column('slug'), column('body'))
.select_from(Topic)
.join(TopicFollower, TopicFollower.follower == author_id)
.filter(TopicFollower.topic == Topic.id)
)
topics_query = add_topic_stat_columns(topics_query)
# Convert query results to lists of dictionaries
authors = [author.dict() for author in session.execute(authors_query)]
topics = [topic.dict() for topic in session.execute(topics_query)]
authors = [{
'id': author.id,
'name': author.name,
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
'stat': {
'shouts': shouts_stat,
'followers': followers_stat,
'followings': followings_stat,
}
} for [author, shouts_stat, followers_stat, followings_stat] in session.execute(authors_query)]
topics = [{
'id': topic.id,
'title': topic.title,
'slug': topic.slug,
'body': topic.body,
'stat': {
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
}
} for [topic, shouts_stat, authors_stat, followers_stat] in session.execute(topics_query)]
# shouts_query = (
# session.query(Shout)
# .join(ShoutReactionsFollower, ShoutReactionsFollower.follower == author_id)
@@ -124,24 +148,24 @@ def query_follows(user_id: str):
# communities = session.query(Community).all()
return {
"topics": topics,
"authors": authors,
'topics': topics,
'authors': authors,
# "shouts": shouts,
"communities": [{"id": 1, "name": "Дискурс", "slug": "discours"}],
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
}
async def get_follows_by_user_id(user_id: str):
if user_id:
redis_key = f"user:{user_id}:follows"
res = await redis.execute("GET", redis_key)
redis_key = f'user:{user_id}:follows'
res = await redis.execute('GET', redis_key)
if isinstance(res, str):
follows = json.loads(res)
return follows
logger.debug(f"getting follows for {user_id}")
logger.debug(f'getting follows for {user_id}')
follows = query_follows(user_id)
await redis.execute("SET", redis_key, json.dumps(follows))
await redis.execute('SET', redis_key, json.dumps(follows))
return follows
@@ -227,7 +251,7 @@ def author_unfollow(follower_id, slug):
return False
@query.field("get_topic_followers")
@query.field('get_topic_followers')
async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
q = select(Author)
q = add_topic_stat_columns(q)
@@ -241,9 +265,9 @@ async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author
return await get_topics_from_query(q)
@query.field("get_shout_followers")
@query.field('get_shout_followers')
def get_shout_followers(
_, _info, slug: str = "", shout_id: int | None = None
_, _info, slug: str = '', shout_id: int | None = None
) -> List[Author]:
followers = []
with local_session() as session:

View File

@@ -21,22 +21,22 @@ from services.logger import root_logger as logger
def add_stat_columns(q, aliased_reaction):
q = q.outerjoin(aliased_reaction).add_columns(
func.sum(aliased_reaction.id).label("reacted_stat"),
func.sum(aliased_reaction.id).label('reacted_stat'),
func.sum(
case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)
).label("comments_stat"),
).label('comments_stat'),
func.sum(
case((aliased_reaction.kind == ReactionKind.LIKE.value, 1), else_=0)
).label("likes_stat"),
).label('likes_stat'),
func.sum(
case((aliased_reaction.kind == ReactionKind.DISLIKE.value, 1), else_=0)
).label("dislikes_stat"),
).label('dislikes_stat'),
func.max(
case(
(aliased_reaction.kind != ReactionKind.COMMENT.value, None),
else_=aliased_reaction.created_at,
)
).label("last_comment"),
).label('last_comment'),
)
return q
@@ -101,7 +101,7 @@ def check_to_unfeature(session, rejecter_id, reaction):
async def set_featured(session, shout_id):
s = session.query(Shout).where(Shout.id == shout_id).first()
s.featured_at = int(time.time())
Shout.update(s, {"featured_at": int(time.time())})
Shout.update(s, {'featured_at': int(time.time())})
author = session.query(Author).filter(Author.id == s.created_by).first()
if author:
await add_user_role(str(author.user))
@@ -111,7 +111,7 @@ async def set_featured(session, shout_id):
def set_unfeatured(session, shout_id):
s = session.query(Shout).where(Shout.id == shout_id).first()
Shout.update(s, {"featured_at": None})
Shout.update(s, {'featured_at': None})
session.add(s)
session.commit()
@@ -124,7 +124,7 @@ async def _create_reaction(session, shout, author, reaction):
# collaborative editing
if (
rdict.get("reply_to")
rdict.get('reply_to')
and r.kind in RATING_REACTIONS
and author.id in shout.authors
):
@@ -137,42 +137,42 @@ async def _create_reaction(session, shout, author, reaction):
await set_featured(session, shout.id)
# reactions auto-following
reactions_follow(author.id, reaction["shout"], True)
reactions_follow(author.id, reaction['shout'], True)
rdict["shout"] = shout.dict()
rdict["created_by"] = author.dict()
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
rdict['shout'] = shout.dict()
rdict['created_by'] = author.dict()
rdict['stat'] = {'commented': 0, 'reacted': 0, 'rating': 0}
# notifications call
await notify_reaction(rdict, "create")
await notify_reaction(rdict, 'create')
return rdict
@mutation.field("create_reaction")
@mutation.field('create_reaction')
@login_required
async def create_reaction(_, info, reaction):
user_id = info.context["user_id"]
user_id = info.context['user_id']
shout_id = reaction.get("shout")
shout_id = reaction.get('shout')
if not shout_id:
return {"error": "Shout ID is required to create a reaction."}
return {'error': 'Shout ID is required to create a reaction.'}
try:
with local_session() as session:
shout = session.query(Shout).filter(Shout.id == shout_id).first()
author = session.query(Author).filter(Author.user == user_id).first()
if shout and author:
reaction["created_by"] = author.id
kind = reaction.get("kind")
reaction['created_by'] = author.id
kind = reaction.get('kind')
shout_id = shout.id
if not kind and isinstance(reaction.get("body"), str):
if not kind and isinstance(reaction.get('body'), str):
kind = ReactionKind.COMMENT.value
if not kind:
return {"error": "cannot create reaction without a kind"}
return {'error': 'cannot create reaction without a kind'}
if kind in RATING_REACTIONS:
opposite_kind = (
@@ -188,7 +188,7 @@ async def create_reaction(_, info, reaction):
Reaction.kind.in_(RATING_REACTIONS),
)
)
reply_to = reaction.get("reply_to")
reply_to = reaction.get('reply_to')
if reply_to:
q = q.filter(Reaction.reply_to == reply_to)
rating_reactions = session.execute(q).all()
@@ -201,31 +201,31 @@ async def create_reaction(_, info, reaction):
rating_reactions,
)
if same_rating:
return {"error": "You can't rate the same thing twice"}
return {'error': "You can't rate the same thing twice"}
elif opposite_rating:
return {"error": "Remove opposite vote first"}
return {'error': 'Remove opposite vote first'}
elif filter(lambda r: r.created_by == author.id, rating_reactions):
return {"error": "You can't rate your own thing"}
return {'error': "You can't rate your own thing"}
rdict = await _create_reaction(session, shout, author, reaction)
return {"reaction": rdict}
return {'reaction': rdict}
except Exception as e:
import traceback
traceback.print_exc()
logger.error(f"{type(e).__name__}: {e}")
logger.error(f'{type(e).__name__}: {e}')
return {"error": "Cannot create reaction."}
return {'error': 'Cannot create reaction.'}
@mutation.field("update_reaction")
@mutation.field('update_reaction')
@login_required
async def update_reaction(_, info, reaction):
user_id = info.context.get("user_id")
roles = info.context.get("roles")
rid = reaction.get("id")
user_id = info.context.get('user_id')
roles = info.context.get('roles')
rid = reaction.get('id')
if rid and user_id and roles:
del reaction["id"]
del reaction['id']
with local_session() as session:
reaction_query = select(Reaction).filter(Reaction.id == int(rid))
aliased_reaction = aliased(Reaction)
@@ -238,19 +238,19 @@ async def update_reaction(_, info, reaction):
)
if not r:
return {"error": "invalid reaction id"}
return {'error': 'invalid reaction id'}
author = session.query(Author).filter(Author.user == user_id).first()
if author:
if r.created_by != author.id and "editor" not in roles:
return {"error": "access denied"}
if r.created_by != author.id and 'editor' not in roles:
return {'error': 'access denied'}
body = reaction.get("body")
body = reaction.get('body')
if body:
r.body = body
r.updated_at = int(time.time())
if r.kind != reaction["kind"]:
if r.kind != reaction['kind']:
# Определение изменения мнения может быть реализовано здесь
pass
@@ -259,79 +259,79 @@ async def update_reaction(_, info, reaction):
session.commit()
r.stat = {
"reacted": reacted_stat,
"commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
'reacted': reacted_stat,
'commented': commented_stat,
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
}
await notify_reaction(r.dict(), "update")
await notify_reaction(r.dict(), 'update')
return {"reaction": r}
return {'reaction': r}
else:
return {"error": "not authorized"}
return {'error': 'not authorized'}
except Exception:
import traceback
traceback.print_exc()
return {"error": "cannot create reaction"}
return {'error': 'cannot create reaction'}
@mutation.field("delete_reaction")
@mutation.field('delete_reaction')
@login_required
async def delete_reaction(_, info, reaction_id: int):
user_id = info.context["user_id"]
roles = info.context["roles"]
user_id = info.context['user_id']
roles = info.context['roles']
if isinstance(reaction_id, int) and user_id and isinstance(roles, list):
with local_session() as session:
try:
author = session.query(Author).filter(Author.user == user_id).one()
r = session.query(Reaction).filter(Reaction.id == reaction_id).one()
if r and author:
if r.created_by is author.id and "editor" not in roles:
return {"error": "access denied"}
if r.created_by is author.id and 'editor' not in roles:
return {'error': 'access denied'}
if r.kind in [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]:
session.delete(r)
session.commit()
await notify_reaction(r.dict(), "delete")
await notify_reaction(r.dict(), 'delete')
except Exception as exc:
return {"error": f"cannot delete reaction: {exc}"}
return {"error": "cannot delete reaction"}
return {'error': f'cannot delete reaction: {exc}'}
return {'error': 'cannot delete reaction'}
def apply_reaction_filters(by, q):
shout_slug = by.get("shout", None)
shout_slug = by.get('shout', None)
if shout_slug:
q = q.filter(Shout.slug == shout_slug)
elif by.get("shouts"):
q = q.filter(Shout.slug.in_(by.get("shouts", [])))
elif by.get('shouts'):
q = q.filter(Shout.slug.in_(by.get('shouts', [])))
created_by = by.get("created_by", None)
created_by = by.get('created_by', None)
if created_by:
q = q.filter(Author.id == created_by)
topic = by.get("topic", None)
topic = by.get('topic', None)
if topic:
q = q.filter(Shout.topics.contains(topic))
if by.get("comment", False):
if by.get('comment', False):
q = q.filter(Reaction.kind == ReactionKind.COMMENT.value)
if by.get("rating", False):
if by.get('rating', False):
q = q.filter(Reaction.kind.in_(RATING_REACTIONS))
by_search = by.get("search", "")
by_search = by.get('search', '')
if len(by_search) > 2:
q = q.filter(Reaction.body.ilike(f"%{by_search}%"))
q = q.filter(Reaction.body.ilike(f'%{by_search}%'))
after = by.get("after", None)
after = by.get('after', None)
if isinstance(after, int):
q = q.filter(Reaction.created_at > after)
return q
@query.field("load_reactions_by")
@query.field('load_reactions_by')
async def load_reactions_by(_, info, by, limit=50, offset=0):
"""
:param info: graphql meta
@@ -368,7 +368,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
q = q.group_by(Reaction.id, Author.id, Shout.id, aliased_reaction.id)
# order by
q = q.order_by(desc("created_at"))
q = q.order_by(desc('created_at'))
# pagination
q = q.limit(limit).offset(offset)
@@ -389,19 +389,19 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
reaction.created_by = author
reaction.shout = shout
reaction.stat = {
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"reacted": reacted_stat,
"commented": commented_stat,
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
'reacted': reacted_stat,
'commented': commented_stat,
}
reactions.add(reaction)
# sort if by stat is present
stat_sort = by.get("stat")
stat_sort = by.get('stat')
if stat_sort:
reactions = sorted(
reactions,
key=lambda r: r.stat.get(stat_sort) or r.created_at,
reverse=stat_sort.startswith("-"),
reverse=stat_sort.startswith('-'),
)
return reactions
@@ -440,7 +440,7 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
# Sort shouts by the `last_comment` field
combined_query = (
union(q1, q2).order_by(desc("last_comment")).limit(limit).offset(offset)
union(q1, q2).order_by(desc('last_comment')).limit(limit).offset(offset)
)
results = session.execute(combined_query).scalars()
with local_session() as session:
@@ -453,26 +453,26 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
last_comment,
] in results:
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"reacted": reacted_stat,
"commented": commented_stat,
"last_comment": last_comment,
'viewed': await ViewedStorage.get_shout(shout.slug),
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
'reacted': reacted_stat,
'commented': commented_stat,
'last_comment': last_comment,
}
shouts.append(shout)
return shouts
@query.field("load_shouts_followed")
@query.field('load_shouts_followed')
@login_required
async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
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 author:
try:
author_id: int = author.dict()["id"]
author_id: int = author.dict()['id']
shouts = await reacted_shouts_updates(author_id, limit, offset)
return shouts
except Exception as error:

View File

@@ -18,22 +18,22 @@ from services.logger import root_logger as logger
def apply_filters(q, filters, author_id=None):
if filters.get("reacted") and author_id:
if filters.get('reacted') and author_id:
q.join(Reaction, Reaction.created_by == author_id)
by_featured = filters.get("featured")
by_featured = filters.get('featured')
if by_featured:
q = q.filter(Shout.featured_at.is_not(None))
by_layouts = filters.get("layouts")
by_layouts = filters.get('layouts')
if by_layouts:
q = q.filter(Shout.layout.in_(by_layouts))
by_author = filters.get("author")
by_author = filters.get('author')
if by_author:
q = q.filter(Shout.authors.any(slug=by_author))
by_topic = filters.get("topic")
by_topic = filters.get('topic')
if by_topic:
q = q.filter(Shout.topics.any(slug=by_topic))
by_after = filters.get("after")
by_after = filters.get('after')
if by_after:
ts = int(by_after)
q = q.filter(Shout.created_at > ts)
@@ -41,13 +41,10 @@ def apply_filters(q, filters, author_id=None):
return q
@query.field("get_shout")
@query.field('get_shout')
async def get_shout(_, _info, slug=None, shout_id=None):
with local_session() as session:
q = select(Shout).options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
q = select(Shout).options(joinedload(Shout.authors), joinedload(Shout.topics))
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
@@ -72,10 +69,10 @@ async def get_shout(_, _info, slug=None, shout_id=None):
] = results
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
"reacted": reacted_stat,
"commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
'viewed': await ViewedStorage.get_shout(shout.slug),
'reacted': reacted_stat,
'commented': commented_stat,
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
}
for author_caption in (
@@ -102,11 +99,11 @@ async def get_shout(_, _info, slug=None, shout_id=None):
return shout
except Exception:
raise HTTPException(
status_code=404, detail=f"shout {slug or shout_id} not found"
status_code=404, detail=f'shout {slug or shout_id} not found'
)
@query.field("load_shouts_by")
@query.field('load_shouts_by')
async def load_shouts_by(_, _info, options):
"""
:param options: {
@@ -130,10 +127,7 @@ async def load_shouts_by(_, _info, options):
# base
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.where(and_(Shout.deleted_at.is_(None), Shout.layout.is_not(None)))
)
@@ -142,7 +136,7 @@ async def load_shouts_by(_, _info, options):
q = add_stat_columns(q, aliased_reaction)
# filters
filters = options.get("filters", {})
filters = options.get('filters', {})
q = apply_filters(q, filters)
# group
@@ -150,16 +144,16 @@ async def load_shouts_by(_, _info, options):
# order
order_by = options.get(
"order_by", Shout.featured_at if filters.get("featured") else Shout.published_at
'order_by', Shout.featured_at if filters.get('featured') else Shout.published_at
)
query_order_by = (
desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
)
q = q.order_by(nulls_last(query_order_by))
# limit offset
offset = options.get("offset", 0)
limit = options.get("limit", 10)
offset = options.get('offset', 0)
limit = options.get('limit', 10)
q = q.limit(limit).offset(offset)
shouts = []
@@ -188,27 +182,24 @@ async def load_shouts_by(_, _info, options):
if main_topic:
shout.main_topic = main_topic[0]
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
"reacted": reacted_stat,
"commented": commented_stat,
"rating": int(likes_stat) - int(dislikes_stat),
'viewed': await ViewedStorage.get_shout(shout.slug),
'reacted': reacted_stat,
'commented': commented_stat,
'rating': int(likes_stat) - int(dislikes_stat),
}
shouts.append(shout)
return shouts
@query.field("load_shouts_drafts")
@query.field('load_shouts_drafts')
@login_required
async def load_shouts_drafts(_, info):
user_id = info.context["user_id"]
user_id = info.context['user_id']
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.filter(and_(Shout.deleted_at.is_(None), Shout.published_at.is_(None)))
)
@@ -239,10 +230,10 @@ async def load_shouts_drafts(_, info):
return shouts
@query.field("load_shouts_feed")
@query.field('load_shouts_feed')
@login_required
async def load_shouts_feed(_, info, options):
user_id = info.context["user_id"]
user_id = info.context['user_id']
shouts = []
with local_session() as session:
@@ -267,10 +258,7 @@ async def load_shouts_feed(_, info, options):
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.where(
and_(
Shout.published_at.is_not(None),
@@ -282,19 +270,19 @@ async def load_shouts_feed(_, info, options):
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
filters = options.get("filters", {})
filters = options.get('filters', {})
q = apply_filters(q, filters, reader.id)
order_by = options.get(
"order_by",
Shout.featured_at if filters.get("featured") else Shout.published_at,
'order_by',
Shout.featured_at if filters.get('featured') else Shout.published_at,
)
query_order_by = (
desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
)
offset = options.get("offset", 0)
limit = options.get("limit", 10)
offset = options.get('offset', 0)
limit = options.get('limit', 10)
q = (
q.group_by(Shout.id)
@@ -329,17 +317,17 @@ async def load_shouts_feed(_, info, options):
if main_topic:
shout.main_topic = main_topic[0]
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
"reacted": reacted_stat,
"commented": commented_stat,
"rating": likes_stat - dislikes_stat,
'viewed': await ViewedStorage.get_shout(shout.slug),
'reacted': reacted_stat,
'commented': commented_stat,
'rating': likes_stat - dislikes_stat,
}
shouts.append(shout)
return shouts
@query.field("load_shouts_search")
@query.field('load_shouts_search')
async def load_shouts_search(_, _info, text, limit=50, offset=0):
if isinstance(text, str) and len(text) > 2:
results = await search_text(text, limit, offset)
@@ -349,14 +337,11 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
@login_required
@query.field("load_shouts_unrated")
@query.field('load_shouts_unrated')
async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
q = (
select(Shout)
.options(
selectinload(Shout.authors),
selectinload(Shout.topics),
)
.options(selectinload(Shout.authors), selectinload(Shout.topics))
.outerjoin(
Reaction,
and_(
@@ -367,7 +352,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
),
),
)
.outerjoin(Author, Author.user == bindparam("user_id"))
.outerjoin(Author, Author.user == bindparam('user_id'))
.where(
and_(
Shout.deleted_at.is_(None),
@@ -384,7 +369,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
q = add_stat_columns(q, aliased_reaction)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit).offset(offset)
user_id = info.context.get("user_id")
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()
@@ -404,20 +389,20 @@ async def get_shouts_from_query(q, author_id=None):
likes_stat,
dislikes_stat,
last_comment,
] in session.execute(q, {"author_id": author_id}).unique():
] in session.execute(q, {'author_id': author_id}).unique():
shouts.append(shout)
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout_slug=shout.slug),
"reacted": reacted_stat,
"commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"last_comment": last_comment,
'viewed': await ViewedStorage.get_shout(shout_slug=shout.slug),
'reacted': reacted_stat,
'commented': commented_stat,
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
'last_comment': last_comment,
}
return shouts
@query.field("load_shouts_random_top")
@query.field('load_shouts_random_top')
async def load_shouts_random_top(_, _info, options):
"""
:param _
@@ -440,7 +425,7 @@ async def load_shouts_random_top(_, _info, options):
select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
)
subquery = apply_filters(subquery, options.get("filters", {}))
subquery = apply_filters(subquery, options.get('filters', {}))
subquery = subquery.group_by(Shout.id).order_by(
desc(
func.sum(
@@ -455,36 +440,33 @@ async def load_shouts_random_top(_, _info, options):
)
)
random_limit = options.get("random_limit")
random_limit = options.get('random_limit')
if random_limit:
subquery = subquery.limit(random_limit)
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.where(Shout.id.in_(subquery))
)
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
limit = options.get("limit", 10)
limit = options.get('limit', 10)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)
return await get_shouts_from_query(q)
@query.field("load_shouts_random_topic")
@query.field('load_shouts_random_topic')
async def load_shouts_random_topic(_, info, limit: int = 10):
topic = get_random_topic()
if topic:
shouts = fetch_shouts_by_topic(topic, limit)
if shouts:
return {"topic": topic, "shouts": shouts}
return {'topic': topic, 'shouts': shouts}
return {
"error": "failed to get random topic after few retries",
'error': 'failed to get random topic after few retries',
shouts: [],
topic: {},
}
@@ -493,10 +475,7 @@ async def load_shouts_random_topic(_, info, limit: int = 10):
def fetch_shouts_by_topic(topic, limit):
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.filter(
and_(
Shout.deleted_at.is_(None),

View File

@@ -17,15 +17,15 @@ def add_topic_stat_columns(q):
q = (
q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic)
.add_columns(func.count(distinct(ShoutTopic.shout)).label("shouts_stat"))
.add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat'))
.outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout)
.add_columns(
func.count(distinct(aliased_shout_author.author)).label("authors_stat")
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
)
.outerjoin(aliased_topic_follower)
.add_columns(
func.count(distinct(aliased_topic_follower.follower)).label(
"followers_stat"
'followers_stat'
)
)
)
@@ -40,17 +40,17 @@ async def get_topics_from_query(q):
with local_session() as session:
for [topic, shouts_stat, authors_stat, followers_stat] in session.execute(q):
topic.stat = {
"shouts": shouts_stat,
"authors": authors_stat,
"followers": followers_stat,
"viewed": await ViewedStorage.get_topic(topic.slug),
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
'viewed': await ViewedStorage.get_topic(topic.slug),
}
topics.append(topic)
return topics
@query.field("get_topics_all")
@query.field('get_topics_all')
async def get_topics_all(_, _info):
q = select(Topic)
q = add_topic_stat_columns(q)
@@ -66,7 +66,7 @@ async def topics_followed_by(author_id):
return await get_topics_from_query(q)
@query.field("get_topics_by_community")
@query.field('get_topics_by_community')
async def get_topics_by_community(_, _info, community_id: int):
q = select(Topic).where(Topic.community == community_id)
q = add_topic_stat_columns(q)
@@ -74,8 +74,8 @@ async def get_topics_by_community(_, _info, community_id: int):
return await get_topics_from_query(q)
@query.field("get_topics_by_author")
async def get_topics_by_author(_, _info, author_id=None, slug="", user=""):
@query.field('get_topics_by_author')
async def get_topics_by_author(_, _info, author_id=None, slug='', user=''):
q = select(Topic)
q = add_topic_stat_columns(q)
if author_id:
@@ -88,7 +88,7 @@ async def get_topics_by_author(_, _info, author_id=None, slug="", user=""):
return await get_topics_from_query(q)
@query.field("get_topic")
@query.field('get_topic')
async def get_topic(_, _info, slug):
q = select(Topic).where(Topic.slug == slug)
q = add_topic_stat_columns(q)
@@ -98,7 +98,7 @@ async def get_topic(_, _info, slug):
return topics[0]
@mutation.field("create_topic")
@mutation.field('create_topic')
@login_required
async def create_topic(_, _info, inp):
with local_session() as session:
@@ -108,43 +108,43 @@ async def create_topic(_, _info, inp):
session.add(new_topic)
session.commit()
return {"topic": new_topic}
return {'topic': new_topic}
@mutation.field("update_topic")
@mutation.field('update_topic')
@login_required
async def update_topic(_, _info, inp):
slug = inp["slug"]
slug = inp['slug']
with local_session() as session:
topic = session.query(Topic).filter(Topic.slug == slug).first()
if not topic:
return {"error": "topic not found"}
return {'error': 'topic not found'}
else:
Topic.update(topic, inp)
session.add(topic)
session.commit()
return {"topic": topic}
return {'topic': topic}
@mutation.field("delete_topic")
@mutation.field('delete_topic')
@login_required
async def delete_topic(_, info, slug: str):
user_id = info.context["user_id"]
user_id = info.context['user_id']
with local_session() as session:
t: Topic = session.query(Topic).filter(Topic.slug == slug).first()
if not t:
return {"error": "invalid topic slug"}
return {'error': 'invalid topic slug'}
author = session.query(Author).filter(Author.user == user_id).first()
if author:
if t.created_by != author.id:
return {"error": "access denied"}
return {'error': 'access denied'}
session.delete(t)
session.commit()
return {}
return {"error": "access denied"}
return {'error': 'access denied'}
def topic_follow(follower_id, slug):
@@ -175,7 +175,7 @@ def topic_unfollow(follower_id, slug):
return False
@query.field("get_topics_random")
@query.field('get_topics_random')
async def get_topics_random(_, info, amount=12):
q = select(Topic)
q = q.join(ShoutTopic)