2024-02-21 14:37:58 +00:00
|
|
|
import json
|
2023-11-03 10:10:22 +00:00
|
|
|
import time
|
2023-10-23 14:47:11 +00:00
|
|
|
from typing import List
|
2023-12-17 20:30:20 +00:00
|
|
|
|
2024-02-21 17:38:12 +00:00
|
|
|
from sqlalchemy import and_, desc, select, or_
|
2023-10-23 14:47:11 +00:00
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
2023-12-17 20:30:20 +00:00
|
|
|
from orm.author import Author, AuthorFollower, AuthorRating
|
2023-12-24 17:46:50 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2024-01-25 19:41:27 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
2023-10-23 14:47:11 +00:00
|
|
|
from orm.topic import Topic
|
2024-02-21 07:27:16 +00:00
|
|
|
from resolvers.follower import get_follows_by_user_id
|
2024-02-21 17:38:12 +00:00
|
|
|
from resolvers.stat import get_authors_from_query, add_author_stat_columns
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.auth import login_required
|
|
|
|
from services.db import local_session
|
2024-02-21 07:27:16 +00:00
|
|
|
from services.rediscache import redis
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.schema import mutation, query
|
2024-02-20 16:19:46 +00:00
|
|
|
from services.logger import root_logger as logger
|
2024-01-13 08:49:12 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('update_author')
|
2023-10-23 14:47:11 +00:00
|
|
|
@login_required
|
2024-02-03 14:44:28 +00:00
|
|
|
async def update_author(_, info, profile):
|
2024-02-21 16:14:58 +00:00
|
|
|
user_id = info.context['user_id']
|
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).where(Author.user == user_id).first()
|
2023-11-22 16:38:39 +00:00
|
|
|
Author.update(author, profile)
|
|
|
|
session.add(author)
|
2023-10-23 14:47:11 +00:00
|
|
|
session.commit()
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': None, 'author': author}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2023-12-19 08:09:50 +00:00
|
|
|
# TODO: caching query
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('get_authors_all')
|
2023-12-19 08:09:50 +00:00
|
|
|
async def get_authors_all(_, _info):
|
2024-02-03 14:31:00 +00:00
|
|
|
authors = []
|
2023-12-19 08:09:50 +00:00
|
|
|
with local_session() as session:
|
2024-02-03 14:31:00 +00:00
|
|
|
authors = session.query(Author).all()
|
|
|
|
return authors
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2023-12-27 22:05:52 +00:00
|
|
|
def count_author_comments_rating(session, author_id) -> int:
|
2023-12-24 18:38:16 +00:00
|
|
|
replied_alias = aliased(Reaction)
|
|
|
|
replies_likes = (
|
|
|
|
session.query(replied_alias)
|
|
|
|
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
2024-01-25 19:41:27 +00:00
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
replied_alias.created_by == author_id,
|
|
|
|
replied_alias.kind == ReactionKind.COMMENT.value,
|
|
|
|
)
|
|
|
|
)
|
2023-12-24 18:38:16 +00:00
|
|
|
.filter(replied_alias.kind == ReactionKind.LIKE.value)
|
|
|
|
.count()
|
|
|
|
) or 0
|
|
|
|
replies_dislikes = (
|
|
|
|
session.query(replied_alias)
|
|
|
|
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
2024-01-25 19:41:27 +00:00
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
replied_alias.created_by == author_id,
|
|
|
|
replied_alias.kind == ReactionKind.COMMENT.value,
|
|
|
|
)
|
|
|
|
)
|
2023-12-24 18:38:16 +00:00
|
|
|
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
|
|
|
.count()
|
|
|
|
) or 0
|
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
return replies_likes - replies_dislikes
|
2023-12-27 22:05:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def count_author_shouts_rating(session, author_id) -> int:
|
|
|
|
shouts_likes = (
|
|
|
|
session.query(Reaction, Shout)
|
|
|
|
.join(Shout, Shout.id == Reaction.shout)
|
2024-01-25 19:41:27 +00:00
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Shout.authors.any(id=author_id),
|
|
|
|
Reaction.kind == ReactionKind.LIKE.value,
|
|
|
|
)
|
|
|
|
)
|
2023-12-27 22:05:52 +00:00
|
|
|
.count()
|
|
|
|
or 0
|
|
|
|
)
|
|
|
|
shouts_dislikes = (
|
|
|
|
session.query(Reaction, Shout)
|
|
|
|
.join(Shout, Shout.id == Reaction.shout)
|
2024-01-25 19:41:27 +00:00
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Shout.authors.any(id=author_id),
|
|
|
|
Reaction.kind == ReactionKind.DISLIKE.value,
|
|
|
|
)
|
|
|
|
)
|
2023-12-27 22:05:52 +00:00
|
|
|
.count()
|
|
|
|
or 0
|
|
|
|
)
|
|
|
|
return shouts_likes - shouts_dislikes
|
2023-12-24 18:38:16 +00:00
|
|
|
|
|
|
|
|
2024-01-22 18:20:17 +00:00
|
|
|
async def load_author_with_stats(q):
|
2023-12-27 22:37:54 +00:00
|
|
|
q = add_author_stat_columns(q)
|
|
|
|
|
2024-01-22 18:20:17 +00:00
|
|
|
result = await get_authors_from_query(q)
|
2023-12-27 22:37:54 +00:00
|
|
|
|
2023-12-28 23:31:44 +00:00
|
|
|
if result:
|
|
|
|
[author] = result
|
2023-12-27 22:37:54 +00:00
|
|
|
with local_session() as session:
|
|
|
|
comments_count = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Reaction.created_by == author.id,
|
|
|
|
Reaction.kind == ReactionKind.COMMENT.value,
|
|
|
|
Reaction.deleted_at.is_(None),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.count()
|
|
|
|
)
|
2024-01-25 19:41:27 +00:00
|
|
|
likes_count = (
|
|
|
|
session.query(AuthorRating)
|
2024-02-21 07:27:16 +00:00
|
|
|
.filter(
|
|
|
|
and_(AuthorRating.author == author.id, AuthorRating.plus.is_(True))
|
|
|
|
)
|
2024-01-25 19:41:27 +00:00
|
|
|
.count()
|
2023-12-27 22:37:54 +00:00
|
|
|
)
|
2024-01-25 19:41:27 +00:00
|
|
|
dislikes_count = (
|
|
|
|
session.query(AuthorRating)
|
2024-02-21 07:27:16 +00:00
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
AuthorRating.author == author.id, AuthorRating.plus.is_not(True)
|
|
|
|
)
|
|
|
|
)
|
2024-01-25 19:41:27 +00:00
|
|
|
.count()
|
|
|
|
)
|
2024-02-21 16:14:58 +00:00
|
|
|
author.stat['rating'] = likes_count - dislikes_count
|
|
|
|
author.stat['rating_shouts'] = count_author_shouts_rating(
|
2024-02-21 07:27:16 +00:00
|
|
|
session, author.id
|
|
|
|
)
|
2024-02-21 16:14:58 +00:00
|
|
|
author.stat['rating_comments'] = count_author_comments_rating(
|
2024-02-21 07:27:16 +00:00
|
|
|
session, author.id
|
|
|
|
)
|
2024-02-21 16:14:58 +00:00
|
|
|
author.stat['commented'] = comments_count
|
2023-12-27 22:37:54 +00:00
|
|
|
return author
|
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('get_author')
|
|
|
|
async def get_author(_, _info, slug='', author_id=None):
|
2023-11-29 08:00:00 +00:00
|
|
|
q = None
|
2023-12-13 19:59:21 +00:00
|
|
|
if slug or author_id:
|
2023-12-24 18:38:16 +00:00
|
|
|
if bool(slug):
|
2023-11-24 01:53:30 +00:00
|
|
|
q = select(Author).where(Author.slug == slug)
|
2023-12-27 22:37:54 +00:00
|
|
|
if author_id:
|
2023-11-27 16:03:47 +00:00
|
|
|
q = select(Author).where(Author.id == author_id)
|
2023-12-27 22:05:52 +00:00
|
|
|
|
2024-01-22 18:20:17 +00:00
|
|
|
return await load_author_with_stats(q)
|
2023-12-27 22:37:54 +00:00
|
|
|
|
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
async def get_author_by_user_id(user_id: str):
|
2024-02-21 16:14:58 +00:00
|
|
|
redis_key = f'user:{user_id}:author'
|
|
|
|
res = await redis.execute('GET', redis_key)
|
2024-02-21 14:37:58 +00:00
|
|
|
if isinstance(res, str):
|
|
|
|
author = json.loads(res)
|
2024-02-21 16:14:58 +00:00
|
|
|
if author.get('id'):
|
|
|
|
logger.debug(f'got cached author: {author}')
|
2024-02-21 14:37:58 +00:00
|
|
|
return author
|
2024-02-21 07:27:16 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
logger.info(f'getting author id for {user_id}')
|
2024-02-21 07:27:16 +00:00
|
|
|
q = select(Author).filter(Author.user == user_id)
|
|
|
|
author = await load_author_with_stats(q)
|
2024-02-21 14:37:58 +00:00
|
|
|
if author:
|
2024-02-21 16:11:49 +00:00
|
|
|
await redis.execute(
|
2024-02-21 16:14:58 +00:00
|
|
|
'set',
|
2024-02-21 16:11:49 +00:00
|
|
|
redis_key,
|
|
|
|
json.dumps(
|
|
|
|
{
|
2024-02-21 16:14:58 +00:00
|
|
|
'id': author.id,
|
|
|
|
'name': author.name,
|
|
|
|
'slug': author.slug,
|
|
|
|
'pic': author.pic,
|
2024-02-21 17:38:12 +00:00
|
|
|
'bio': author.bio,
|
2024-02-21 16:11:49 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
2024-02-21 14:37:58 +00:00
|
|
|
return author
|
2024-02-21 07:27:16 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('get_author_id')
|
2023-12-27 22:37:54 +00:00
|
|
|
async def get_author_id(_, _info, user: str):
|
2024-02-21 10:16:39 +00:00
|
|
|
return await get_author_by_user_id(user)
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('load_authors_by')
|
2023-10-23 14:47:11 +00:00
|
|
|
async def load_authors_by(_, _info, by, limit, offset):
|
|
|
|
q = select(Author)
|
2024-02-14 09:07:55 +00:00
|
|
|
q = add_author_stat_columns(q)
|
2024-02-21 16:14:58 +00:00
|
|
|
if by.get('slug'):
|
2023-10-23 14:47:11 +00:00
|
|
|
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
|
2024-02-21 16:14:58 +00:00
|
|
|
elif by.get('name'):
|
2023-10-23 14:47:11 +00:00
|
|
|
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
|
2024-02-21 16:14:58 +00:00
|
|
|
elif by.get('topic'):
|
2024-02-21 07:27:16 +00:00
|
|
|
q = (
|
|
|
|
q.join(ShoutAuthor)
|
|
|
|
.join(ShoutTopic)
|
|
|
|
.join(Topic)
|
2024-02-21 16:14:58 +00:00
|
|
|
.where(Topic.slug == by['topic'])
|
2024-02-21 07:27:16 +00:00
|
|
|
)
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
if by.get('last_seen'): # in unix time
|
|
|
|
before = int(time.time()) - by['last_seen']
|
2023-11-03 10:10:22 +00:00
|
|
|
q = q.filter(Author.last_seen > before)
|
2024-02-21 16:14:58 +00:00
|
|
|
elif by.get('created_at'): # in unix time
|
|
|
|
before = int(time.time()) - by['created_at']
|
2023-11-03 10:10:22 +00:00
|
|
|
q = q.filter(Author.created_at > before)
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
order = by.get('order')
|
|
|
|
if order == 'followers' or order == 'shouts':
|
|
|
|
q = q.order_by(desc(f'{order}_stat'))
|
2024-02-14 09:07:55 +00:00
|
|
|
|
2024-02-14 07:47:54 +00:00
|
|
|
q = q.limit(limit).offset(offset)
|
|
|
|
|
|
|
|
authors = await get_authors_from_query(q)
|
|
|
|
|
|
|
|
return authors
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('get_author_follows')
|
2024-02-21 07:27:16 +00:00
|
|
|
async def get_author_follows(
|
2024-02-21 16:14:58 +00:00
|
|
|
_, _info, slug='', user=None, author_id=None
|
2024-02-21 07:27:16 +00:00
|
|
|
) -> List[Author]:
|
2024-02-21 16:14:58 +00:00
|
|
|
with local_session() as session:
|
|
|
|
if not user and (author_id or slug):
|
2024-02-21 16:48:33 +00:00
|
|
|
user = (
|
|
|
|
session.query(Author.user)
|
|
|
|
.where(or_(Author.id == author_id, Author.slug == slug))
|
|
|
|
.first()
|
|
|
|
)
|
2024-02-21 16:14:58 +00:00
|
|
|
if user:
|
|
|
|
follows = await get_follows_by_user_id(user)
|
|
|
|
return follows
|
|
|
|
else:
|
|
|
|
raise ValueError('Author not found')
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@mutation.field('rate_author')
|
2023-10-23 14:47:11 +00:00
|
|
|
@login_required
|
2023-11-28 07:53:48 +00:00
|
|
|
async def rate_author(_, info, rated_slug, value):
|
2024-02-21 16:14:58 +00:00
|
|
|
user_id = info.context['user_id']
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
with local_session() as session:
|
2023-11-28 19:07:53 +00:00
|
|
|
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
|
|
|
|
rater = session.query(Author).filter(Author.slug == user_id).first()
|
|
|
|
if rater and rated_author:
|
2023-12-27 22:05:52 +00:00
|
|
|
rating: AuthorRating = (
|
2023-11-28 07:53:48 +00:00
|
|
|
session.query(AuthorRating)
|
2024-01-25 19:41:27 +00:00
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
AuthorRating.rater == rater.id,
|
|
|
|
AuthorRating.author == rated_author.id,
|
|
|
|
)
|
|
|
|
)
|
2023-11-28 07:53:48 +00:00
|
|
|
.first()
|
|
|
|
)
|
2023-12-27 22:05:52 +00:00
|
|
|
if rating:
|
|
|
|
rating.plus = value > 0
|
2023-11-22 16:38:39 +00:00
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
2023-11-28 07:53:48 +00:00
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
try:
|
2024-02-21 07:27:16 +00:00
|
|
|
rating = AuthorRating(
|
|
|
|
rater=rater.id, author=rated_author.id, plus=value > 0
|
|
|
|
)
|
2023-11-28 07:53:48 +00:00
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
|
|
|
except Exception as err:
|
2024-02-21 16:14:58 +00:00
|
|
|
return {'error': err}
|
2023-10-23 14:47:11 +00:00
|
|
|
return {}
|
2023-11-28 19:07:53 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
async def create_author(user_id: str, slug: str, name: str = ''):
|
2023-11-28 19:07:53 +00:00
|
|
|
with local_session() as session:
|
2023-12-16 16:59:43 +00:00
|
|
|
new_author = Author(user=user_id, slug=slug, name=name)
|
2023-11-28 19:07:53 +00:00
|
|
|
session.add(new_author)
|
|
|
|
session.commit()
|
2024-02-21 16:14:58 +00:00
|
|
|
logger.info(f'author created by webhook {new_author.dict()}')
|
2024-02-21 09:34:12 +00:00
|
|
|
|
|
|
|
|
2024-02-21 16:14:58 +00:00
|
|
|
@query.field('get_author_followers')
|
2024-02-21 09:34:12 +00:00
|
|
|
async def get_author_followers(_, _info, slug) -> List[Author]:
|
|
|
|
q = select(Author)
|
|
|
|
q = add_author_stat_columns(q)
|
|
|
|
|
|
|
|
aliased_author = aliased(Author)
|
|
|
|
q = (
|
|
|
|
q.join(AuthorFollower, AuthorFollower.follower == Author.id)
|
|
|
|
.join(aliased_author, aliased_author.id == AuthorFollower.author)
|
|
|
|
.where(aliased_author.slug == slug)
|
|
|
|
)
|
|
|
|
|
|
|
|
return await get_authors_from_query(q)
|