Files
core/resolvers/author.py

218 lines
6.9 KiB
Python
Raw Normal View History

2024-02-21 17:37:58 +03:00
import json
2023-11-03 13:10:22 +03:00
import time
2023-12-17 23:30:20 +03:00
2024-02-25 17:58:09 +03:00
from sqlalchemy import select, or_, and_, text, desc
2023-10-23 17:47:11 +03:00
from sqlalchemy.orm import aliased
2024-02-25 16:43:04 +03:00
from sqlalchemy_searchable import search
2023-10-23 17:47:11 +03:00
2024-02-24 21:45:38 +03:00
from orm.author import Author, AuthorFollower
2024-02-24 19:23:53 +03:00
from orm.shout import ShoutAuthor, ShoutTopic
2023-10-23 17:47:11 +03:00
from orm.topic import Topic
2024-02-25 11:27:08 +03:00
from resolvers.stat import get_with_stat, author_follows_authors, author_follows_topics
2024-02-25 14:58:16 +03:00
from services.event_listeners import update_author_cache
2023-12-17 23:30:20 +03:00
from services.auth import login_required
from services.db import local_session
2024-02-21 10:27:16 +03:00
from services.rediscache import redis
2023-12-17 23:30:20 +03:00
from services.schema import mutation, query
2024-02-20 19:19:46 +03:00
from services.logger import root_logger as logger
2024-01-13 11:49:12 +03:00
2023-10-23 17:47:11 +03:00
2024-02-21 19:14:58 +03:00
@mutation.field('update_author')
2023-10-23 17:47:11 +03:00
@login_required
2024-02-23 02:08:43 +03:00
def update_author(_, info, profile):
2024-02-21 19:14:58 +03:00
user_id = info.context['user_id']
2023-10-23 17:47:11 +03:00
with local_session() as session:
2023-11-24 02:00:28 +03:00
author = session.query(Author).where(Author.user == user_id).first()
2023-11-22 19:38:39 +03:00
Author.update(author, profile)
session.add(author)
2023-10-23 17:47:11 +03:00
session.commit()
2024-02-21 19:14:58 +03:00
return {'error': None, 'author': author}
2023-10-23 17:47:11 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_authors_all')
2024-02-23 02:08:43 +03:00
def get_authors_all(_, _info):
2023-12-19 11:09:50 +03:00
with local_session() as session:
2024-02-03 17:31:00 +03:00
authors = session.query(Author).all()
2024-02-24 13:22:35 +03:00
return authors
2023-10-23 17:47:11 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_author')
2024-02-25 14:58:16 +03:00
async def get_author(_, _info, slug='', author_id=None):
2023-11-29 11:00:00 +03:00
q = None
2024-02-24 13:22:35 +03:00
author = None
try:
if slug or author_id:
if bool(slug):
q = select(Author).where(Author.slug == slug)
if author_id:
2024-02-24 21:15:11 +03:00
q = select(Author).where(Author.id == author_id)
2023-12-28 01:05:52 +03:00
2024-02-25 11:27:08 +03:00
[author] = get_with_stat(q)
2024-02-25 14:58:16 +03:00
if author:
await update_author_cache(author)
2024-02-24 13:22:35 +03:00
except Exception as exc:
logger.error(exc)
return author
2023-12-28 01:37:54 +03:00
2024-02-25 11:27:08 +03:00
async def get_author_by_user_id(user_id: str):
2024-02-25 14:39:26 +03:00
logger.info(f'getting author id for {user_id}')
2024-02-21 19:14:58 +03:00
redis_key = f'user:{user_id}:author'
2024-02-24 13:22:35 +03:00
author = None
try:
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}')
return author
q = select(Author).filter(Author.user == user_id)
2024-02-25 11:27:08 +03:00
[author] = get_with_stat(q)
2024-02-25 14:58:16 +03:00
if author:
await update_author_cache(author)
2024-02-24 13:22:35 +03:00
except Exception as exc:
logger.error(exc)
return author
2024-02-21 10:27:16 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_author_id')
2023-12-28 01:37:54 +03:00
async def get_author_id(_, _info, user: str):
2024-02-25 11:27:08 +03:00
return await get_author_by_user_id(user)
2023-10-23 17:47:11 +03:00
2024-02-21 19:14:58 +03:00
@query.field('load_authors_by')
2024-02-23 02:08:43 +03:00
def load_authors_by(_, _info, by, limit, offset):
2024-02-25 14:39:26 +03:00
logger.debug(f'loading authors by {by}')
2023-10-23 17:47:11 +03:00
q = select(Author)
2024-02-21 19:14:58 +03:00
if by.get('slug'):
2023-10-23 17:47:11 +03:00
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
2024-02-21 19:14:58 +03:00
elif by.get('name'):
2023-10-23 17:47:11 +03:00
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
2024-02-21 19:14:58 +03:00
elif by.get('topic'):
2024-02-21 10:27:16 +03:00
q = (
q.join(ShoutAuthor)
.join(ShoutTopic)
.join(Topic)
2024-02-24 13:22:35 +03:00
.where(Topic.slug == str(by['topic']))
2024-02-21 10:27:16 +03:00
)
2023-11-22 19:38:39 +03:00
2024-02-21 19:14:58 +03:00
if by.get('last_seen'): # in unix time
before = int(time.time()) - by['last_seen']
2023-11-03 13:10:22 +03:00
q = q.filter(Author.last_seen > before)
2024-02-21 19:14:58 +03:00
elif by.get('created_at'): # in unix time
before = int(time.time()) - by['created_at']
2023-11-03 13:10:22 +03:00
q = q.filter(Author.created_at > before)
2023-10-23 17:47:11 +03:00
2024-02-21 19:14:58 +03:00
order = by.get('order')
2024-02-25 17:49:15 +03:00
if order in ['likes', 'shouts', 'followers']:
2024-02-25 17:58:09 +03:00
q = q.order_by(desc(text(f'{order}_stat')))
2024-02-14 12:07:55 +03:00
2024-02-14 10:47:54 +03:00
q = q.limit(limit).offset(offset)
2024-02-24 19:12:35 +03:00
2024-02-25 11:27:08 +03:00
authors = get_with_stat(q)
2024-02-14 10:47:54 +03:00
return authors
2023-10-23 17:47:11 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_author_follows')
2024-02-23 19:35:40 +03:00
def get_author_follows(_, _info, slug='', user=None, author_id=None):
2024-02-21 19:14:58 +03:00
with local_session() as session:
2024-02-23 21:10:11 +03:00
if user or slug:
2024-02-24 21:45:38 +03:00
author_id_result = (
session.query(Author.id)
.filter(or_(Author.user == user, Author.slug == slug))
.first()
)
2024-02-23 21:10:11 +03:00
author_id = author_id_result[0] if author_id_result else None
if author_id:
2024-02-25 14:39:26 +03:00
logger.debug(f'getting {author_id} follows')
2024-02-25 11:27:08 +03:00
topics = author_follows_topics(author_id)
authors = author_follows_authors(author_id)
return {
'topics': topics,
'authors': authors,
2024-02-25 13:29:57 +03:00
'communities': [
{'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''}
],
2024-02-25 11:27:08 +03:00
}
2024-02-23 21:10:11 +03:00
else:
raise ValueError('Author not found')
2024-02-24 13:22:35 +03:00
2024-02-23 21:10:11 +03:00
@query.field('get_author_follows_topics')
def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
with local_session() as session:
if user or slug:
2024-02-24 21:45:38 +03:00
author_id_result = (
session.query(Author.id)
.filter(or_(Author.user == user, Author.slug == slug))
.first()
)
2024-02-23 21:10:11 +03:00
author_id = author_id_result[0] if author_id_result else None
if author_id:
2024-02-25 14:39:26 +03:00
logger.debug(f'getting {author_id} follows topics')
2024-02-25 11:27:08 +03:00
follows = author_follows_topics(author_id)
2024-02-23 21:10:11 +03:00
return follows
else:
raise ValueError('Author not found')
@query.field('get_author_follows_authors')
def get_author_follows_authors(_, _info, slug='', user=None, author_id=None):
with local_session() as session:
if user or slug:
2024-02-24 21:45:38 +03:00
author_id_result = (
session.query(Author.id)
.filter(or_(Author.user == user, Author.slug == slug))
.first()
)
2024-02-23 21:10:11 +03:00
author_id = author_id_result[0] if author_id_result else None
if author_id:
2024-02-25 14:39:26 +03:00
logger.debug(f'getting {author_id} follows authors')
2024-02-25 11:27:08 +03:00
follows = author_follows_authors(author_id)
2024-02-21 19:14:58 +03:00
return follows
else:
raise ValueError('Author not found')
2023-10-23 17:47:11 +03:00
2024-02-23 02:08:43 +03:00
def create_author(user_id: str, slug: str, name: str = ''):
2023-11-28 22:07:53 +03:00
with local_session() as session:
2023-12-16 19:59:43 +03:00
new_author = Author(user=user_id, slug=slug, name=name)
2023-11-28 22:07:53 +03:00
session.add(new_author)
session.commit()
2024-02-21 19:14:58 +03:00
logger.info(f'author created by webhook {new_author.dict()}')
2024-02-21 12:34:12 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_author_followers')
2024-02-24 13:22:35 +03:00
def get_author_followers(_, _info, slug: str):
2024-02-25 14:39:26 +03:00
logger.debug(f'getting followers for @{slug}')
2024-02-25 11:27:08 +03:00
try:
with local_session() as session:
2024-02-25 14:41:04 +03:00
author_alias = aliased(Author)
2024-02-25 11:27:08 +03:00
author_id_result = (
2024-02-25 14:41:04 +03:00
session.query(author_alias.id).filter(author_alias.slug == slug).first()
2024-02-25 11:27:08 +03:00
)
author_id = author_id_result[0] if author_id_result else None
2024-02-23 03:28:46 +03:00
2024-02-25 11:27:08 +03:00
author_follower_alias = aliased(AuthorFollower, name='af')
2024-02-25 14:41:04 +03:00
q = select(Author).join(
2024-02-25 11:27:08 +03:00
author_follower_alias,
and_(
author_follower_alias.author == author_id,
2024-02-25 14:41:04 +03:00
author_follower_alias.follower == Author.id,
2024-02-25 11:27:08 +03:00
),
)
return get_with_stat(q)
except Exception as exc:
logger.error(exc)
return []
2024-02-25 15:22:48 +03:00
@query.field('search_authors')
def search_authors(_, info, text: str):
2024-02-25 16:43:04 +03:00
q = search(select(Author), text)
2024-02-25 15:22:48 +03:00
return get_with_stat(q)