This commit is contained in:
parent
3bc7946ab3
commit
a05072fd71
|
@ -1,6 +1,8 @@
|
||||||
from resolvers.author import (
|
from resolvers.author import (
|
||||||
get_author,
|
get_author,
|
||||||
get_author_follows,
|
get_author_follows,
|
||||||
|
get_author_follows_topics,
|
||||||
|
get_author_follows_authors,
|
||||||
get_author_followers,
|
get_author_followers,
|
||||||
get_author_id,
|
get_author_id,
|
||||||
get_authors_all,
|
get_authors_all,
|
||||||
|
@ -45,6 +47,8 @@ __all__ = [
|
||||||
'get_author',
|
'get_author',
|
||||||
'get_author_id',
|
'get_author_id',
|
||||||
'get_author_follows',
|
'get_author_follows',
|
||||||
|
'get_author_follows_topics',
|
||||||
|
'get_author_follows_authors',
|
||||||
'get_authors_all',
|
'get_authors_all',
|
||||||
'load_authors_by',
|
'load_authors_by',
|
||||||
'rate_author',
|
'rate_author',
|
||||||
|
|
|
@ -9,7 +9,7 @@ from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from resolvers.follower import query_follows
|
from resolvers.follower import query_follows
|
||||||
from resolvers.stat import get_authors_with_stat, execute_with_ministat
|
from resolvers.stat import get_authors_with_stat, execute_with_ministat, author_follows_authors, author_follows_topics
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
@ -210,15 +210,36 @@ def load_authors_by(_, _info, by, limit, offset):
|
||||||
@query.field('get_author_follows')
|
@query.field('get_author_follows')
|
||||||
def get_author_follows(_, _info, slug='', user=None, author_id=None):
|
def get_author_follows(_, _info, slug='', user=None, author_id=None):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
if not user and (author_id or slug):
|
if user or slug:
|
||||||
user_query_result = (
|
author_id_result = session.query(Author.id).filter(or_(Author.user == user, Author.slug == slug)).first()
|
||||||
session.query(Author.user)
|
author_id = author_id_result[0] if author_id_result else None
|
||||||
.where(or_(Author.id == author_id, Author.slug == slug))
|
if author_id:
|
||||||
.first()
|
follows = query_follows(author_id)
|
||||||
)
|
return follows
|
||||||
user = user_query_result[0] if user_query_result else None
|
else:
|
||||||
if user:
|
raise ValueError('Author not found')
|
||||||
follows = query_follows(user)
|
|
||||||
|
@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:
|
||||||
|
author_id_result = session.query(Author.id).filter(or_(Author.user == user, Author.slug == slug)).first()
|
||||||
|
author_id = author_id_result[0] if author_id_result else None
|
||||||
|
if author_id:
|
||||||
|
follows = author_follows_authors(author_id)
|
||||||
|
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:
|
||||||
|
author_id_result = session.query(Author.id).filter(or_(Author.user == user, Author.slug == slug)).first()
|
||||||
|
author_id = author_id_result[0] if author_id_result else None
|
||||||
|
if author_id:
|
||||||
|
follows = author_follows_topics(author_id)
|
||||||
return follows
|
return follows
|
||||||
else:
|
else:
|
||||||
raise ValueError('Author not found')
|
raise ValueError('Author not found')
|
||||||
|
|
|
@ -91,7 +91,8 @@ async def get_follows_by_user_id(user_id: str):
|
||||||
follows = DEFAULT_FOLLOWS
|
follows = DEFAULT_FOLLOWS
|
||||||
day_old = int(time.time()) - author.get('last_seen', 0) > 24 * 60 * 60
|
day_old = int(time.time()) - author.get('last_seen', 0) > 24 * 60 * 60
|
||||||
if day_old:
|
if day_old:
|
||||||
follows = query_follows(user_id)
|
author_id = json.loads(str(author)).get('id')
|
||||||
|
follows = query_follows(author_id)
|
||||||
else:
|
else:
|
||||||
logger.debug(f'getting follows for {user_id} from redis')
|
logger.debug(f'getting follows for {user_id} from redis')
|
||||||
res = await redis.execute('GET', f'user:{user_id}:follows')
|
res = await redis.execute('GET', f'user:{user_id}:follows')
|
||||||
|
|
|
@ -5,6 +5,7 @@ from orm.topic import TopicFollower, Topic
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from orm.author import AuthorFollower, Author
|
from orm.author import AuthorFollower, Author
|
||||||
from orm.shout import ShoutTopic, ShoutAuthor
|
from orm.shout import ShoutTopic, ShoutAuthor
|
||||||
|
from services.logger import root_logger as logger
|
||||||
|
|
||||||
|
|
||||||
def add_topic_stat_columns(q):
|
def add_topic_stat_columns(q):
|
||||||
|
@ -90,40 +91,42 @@ def get_topics_with_stat(q):
|
||||||
return execute_with_ministat(q)
|
return execute_with_ministat(q)
|
||||||
|
|
||||||
|
|
||||||
def query_follows(author_id: int):
|
def author_follows_authors(author_id: int):
|
||||||
|
aliased_shout_authors = aliased(ShoutAuthor)
|
||||||
subquery_shout_author = (
|
subquery_shout_author = (
|
||||||
select(
|
select(
|
||||||
[
|
[
|
||||||
ShoutAuthor.author,
|
aliased_shout_authors.author,
|
||||||
func.count(distinct(ShoutAuthor.shout)).label('shouts_stat'),
|
func.count(distinct(aliased_shout_authors.shout)).label('shouts_stat'),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
.group_by(ShoutAuthor.author)
|
.group_by(aliased_shout_authors.author)
|
||||||
.where(ShoutAuthor.author == author_id)
|
.where(aliased_shout_authors.author == author_id)
|
||||||
.alias()
|
.alias()
|
||||||
)
|
)
|
||||||
|
alias_author_authors = aliased(AuthorFollower)
|
||||||
subquery_author_authors = (
|
subquery_author_authors = (
|
||||||
select(
|
select(
|
||||||
[
|
[
|
||||||
AuthorFollower.author,
|
alias_author_authors.author,
|
||||||
func.count(distinct(AuthorFollower.author)).label('authors_stat'),
|
func.count(distinct(alias_author_authors.author)).label('authors_stat'),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
.group_by(AuthorFollower.author)
|
.group_by(alias_author_authors.author)
|
||||||
.where(AuthorFollower.author == author_id)
|
.where(alias_author_authors.follower == author_id)
|
||||||
.alias()
|
.alias()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
alias_author_followers = aliased(AuthorFollower)
|
||||||
subquery_author_followers = (
|
subquery_author_followers = (
|
||||||
select(
|
select(
|
||||||
[
|
[
|
||||||
AuthorFollower.follower,
|
alias_author_followers.follower,
|
||||||
func.count(distinct(AuthorFollower.follower)).label('followers_stat'),
|
func.count(distinct(alias_author_followers.follower)).label('followers_stat'),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
.group_by(AuthorFollower.follower)
|
.group_by(alias_author_followers.follower)
|
||||||
.where(AuthorFollower.follower == author_id)
|
.where(alias_author_followers.author == author_id)
|
||||||
.alias()
|
.alias()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -138,7 +141,7 @@ def query_follows(author_id: int):
|
||||||
authors_query = (
|
authors_query = (
|
||||||
select(
|
select(
|
||||||
[
|
[
|
||||||
Author.id,
|
Author,
|
||||||
subq_shout_author_alias.shouts_stat,
|
subq_shout_author_alias.shouts_stat,
|
||||||
subq_author_authors_alias.authors_stat,
|
subq_author_authors_alias.authors_stat,
|
||||||
subq_author_followers_alias.followers_stat,
|
subq_author_followers_alias.followers_stat,
|
||||||
|
@ -159,6 +162,10 @@ def query_follows(author_id: int):
|
||||||
|
|
||||||
authors = execute_with_ministat(authors_query)
|
authors = execute_with_ministat(authors_query)
|
||||||
|
|
||||||
|
return authors
|
||||||
|
|
||||||
|
|
||||||
|
def author_follows_topics(author_id: int):
|
||||||
subquery_shout_topic = (
|
subquery_shout_topic = (
|
||||||
select(
|
select(
|
||||||
[
|
[
|
||||||
|
@ -203,7 +210,7 @@ def query_follows(author_id: int):
|
||||||
topics_query = (
|
topics_query = (
|
||||||
select(
|
select(
|
||||||
[
|
[
|
||||||
Topic.id,
|
Topic,
|
||||||
subq_shout_topic_alias.columns.shouts_stat,
|
subq_shout_topic_alias.columns.shouts_stat,
|
||||||
subq_shout_topic_authors_alias.columns.authors_stat,
|
subq_shout_topic_authors_alias.columns.authors_stat,
|
||||||
subq_topic_followers_alias.columns.followers_stat,
|
subq_topic_followers_alias.columns.followers_stat,
|
||||||
|
@ -224,9 +231,18 @@ def query_follows(author_id: int):
|
||||||
)
|
)
|
||||||
|
|
||||||
topics = execute_with_ministat(topics_query)
|
topics = execute_with_ministat(topics_query)
|
||||||
|
return topics
|
||||||
|
|
||||||
return {
|
|
||||||
'topics': topics,
|
def query_follows(author_id: int):
|
||||||
'authors': authors,
|
try:
|
||||||
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
|
topics = author_follows_topics(author_id)
|
||||||
}
|
authors = author_follows_authors(author_id)
|
||||||
|
return {
|
||||||
|
'topics': topics,
|
||||||
|
'authors': authors,
|
||||||
|
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"An error occurred while executing query_follows: {e}")
|
||||||
|
raise Exception("An error occurred while executing query_follows") from e
|
||||||
|
|
|
@ -17,6 +17,8 @@ type Query {
|
||||||
get_topic_followers(slug: String, topic_id: Int): [Author]
|
get_topic_followers(slug: String, topic_id: Int): [Author]
|
||||||
get_author_followers(slug: String, user: String, author_id: Int): [Author]
|
get_author_followers(slug: String, user: String, author_id: Int): [Author]
|
||||||
get_author_follows(slug: String, user: String, author_id: Int): AuthorFollows!
|
get_author_follows(slug: String, user: String, author_id: Int): AuthorFollows!
|
||||||
|
get_author_follows_topics(slug: String, user: String, author_id: Int): [Topic]
|
||||||
|
get_author_follows_authors(slug: String, user: String, author_id: Int): [Author]
|
||||||
load_shouts_followed(follower_id: Int!, limit: Int, offset: Int): [Shout] # userReactedShouts
|
load_shouts_followed(follower_id: Int!, limit: Int, offset: Int): [Shout] # userReactedShouts
|
||||||
|
|
||||||
# reaction
|
# reaction
|
||||||
|
|
Loading…
Reference in New Issue
Block a user