granian+precommit
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import logging
|
||||
import time
|
||||
from typing import List
|
||||
import logging
|
||||
|
||||
from sqlalchemy import and_, case, distinct, func, literal, select, cast, Integer
|
||||
from sqlalchemy import and_, distinct, func, select
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from orm.author import Author, AuthorFollower, AuthorRating
|
||||
from orm.community import Community
|
||||
from orm.reaction import Reaction, ReactionKind
|
||||
from orm.shout import ShoutAuthor, ShoutTopic, Shout
|
||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||
from orm.topic import Topic
|
||||
from resolvers.community import followed_communities
|
||||
from resolvers.reaction import reacted_shouts_updates as followed_reactions
|
||||
@@ -19,25 +19,26 @@ from services.schema import mutation, query
|
||||
from services.unread import get_total_unread_counter
|
||||
from services.viewed import ViewedStorage
|
||||
|
||||
|
||||
logging.basicConfig()
|
||||
logger = logging.getLogger("\t[resolvers.author]\t")
|
||||
logger = logging.getLogger('\t[resolvers.author]\t')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def add_author_stat_columns(q):
|
||||
shout_author_aliased = aliased(ShoutAuthor)
|
||||
q = q.outerjoin(shout_author_aliased, shout_author_aliased.author == Author.id).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")
|
||||
func.count(distinct(followers_table.author)).label('followings_stat')
|
||||
)
|
||||
|
||||
q = q.group_by(Author.id)
|
||||
@@ -49,10 +50,10 @@ async def get_authors_from_query(q):
|
||||
with local_session() as session:
|
||||
for [author, shouts_stat, followers_stat, followings_stat] in session.execute(q):
|
||||
author.stat = {
|
||||
"shouts": shouts_stat,
|
||||
"followers": followers_stat,
|
||||
"followings": followings_stat,
|
||||
"viewed": await ViewedStorage.get_author(author.slug),
|
||||
'shouts': shouts_stat,
|
||||
'followers': followers_stat,
|
||||
'followings': followings_stat,
|
||||
'viewed': await ViewedStorage.get_author(author.slug),
|
||||
}
|
||||
authors.append(author)
|
||||
return authors
|
||||
@@ -61,24 +62,24 @@ async def get_authors_from_query(q):
|
||||
async def author_followings(author_id: int):
|
||||
# NOTE: topics, authors, shout-reactions and communities slugs list
|
||||
return {
|
||||
"unread": await get_total_unread_counter(author_id),
|
||||
"topics": [t.slug for t in await followed_topics(author_id)],
|
||||
"authors": [a.slug for a in await followed_authors(author_id)],
|
||||
"reactions": [s.slug for s in await followed_reactions(author_id)],
|
||||
"communities": [c.slug for c in [followed_communities(author_id)] if isinstance(c, Community)],
|
||||
'unread': await get_total_unread_counter(author_id),
|
||||
'topics': [t.slug for t in await followed_topics(author_id)],
|
||||
'authors': [a.slug for a in await followed_authors(author_id)],
|
||||
'reactions': [s.slug for s in await followed_reactions(author_id)],
|
||||
'communities': [c.slug for c in [followed_communities(author_id)] if isinstance(c, Community)],
|
||||
}
|
||||
|
||||
|
||||
@mutation.field("update_profile")
|
||||
@mutation.field('update_profile')
|
||||
@login_required
|
||||
async def update_profile(_, 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}
|
||||
|
||||
|
||||
# for mutation.field("follow")
|
||||
@@ -111,7 +112,7 @@ def author_unfollow(follower_id, slug):
|
||||
|
||||
|
||||
# TODO: caching query
|
||||
@query.field("get_authors_all")
|
||||
@query.field('get_authors_all')
|
||||
async def get_authors_all(_, _info):
|
||||
with local_session() as session:
|
||||
return session.query(Author).all()
|
||||
@@ -122,40 +123,59 @@ def count_author_comments_rating(session, author_id) -> int:
|
||||
replies_likes = (
|
||||
session.query(replied_alias)
|
||||
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
||||
.where(and_(replied_alias.created_by == author_id, replied_alias.kind == ReactionKind.COMMENT.value))
|
||||
.where(
|
||||
and_(
|
||||
replied_alias.created_by == author_id,
|
||||
replied_alias.kind == ReactionKind.COMMENT.value,
|
||||
)
|
||||
)
|
||||
.filter(replied_alias.kind == ReactionKind.LIKE.value)
|
||||
.count()
|
||||
) or 0
|
||||
replies_dislikes = (
|
||||
session.query(replied_alias)
|
||||
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
||||
.where(and_(replied_alias.created_by == author_id, replied_alias.kind == ReactionKind.COMMENT.value))
|
||||
.where(
|
||||
and_(
|
||||
replied_alias.created_by == author_id,
|
||||
replied_alias.kind == ReactionKind.COMMENT.value,
|
||||
)
|
||||
)
|
||||
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
||||
.count()
|
||||
) or 0
|
||||
|
||||
return replies_likes - replies_dislikes
|
||||
return replies_likes - replies_dislikes
|
||||
|
||||
|
||||
def count_author_shouts_rating(session, author_id) -> int:
|
||||
shouts_likes = (
|
||||
session.query(Reaction, Shout)
|
||||
.join(Shout, Shout.id == Reaction.shout)
|
||||
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.LIKE.value))
|
||||
.filter(
|
||||
and_(
|
||||
Shout.authors.any(id=author_id),
|
||||
Reaction.kind == ReactionKind.LIKE.value,
|
||||
)
|
||||
)
|
||||
.count()
|
||||
or 0
|
||||
)
|
||||
shouts_dislikes = (
|
||||
session.query(Reaction, Shout)
|
||||
.join(Shout, Shout.id == Reaction.shout)
|
||||
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.DISLIKE.value))
|
||||
.filter(
|
||||
and_(
|
||||
Shout.authors.any(id=author_id),
|
||||
Reaction.kind == ReactionKind.DISLIKE.value,
|
||||
)
|
||||
)
|
||||
.count()
|
||||
or 0
|
||||
)
|
||||
return shouts_likes - shouts_dislikes
|
||||
|
||||
|
||||
|
||||
async def load_author_with_stats(q):
|
||||
q = add_author_stat_columns(q)
|
||||
|
||||
@@ -175,25 +195,25 @@ async def load_author_with_stats(q):
|
||||
)
|
||||
.count()
|
||||
)
|
||||
ratings_sum = (
|
||||
session.query(
|
||||
func.sum(
|
||||
case((AuthorRating.plus == True, cast(1, Integer)),
|
||||
else_=cast(-1, Integer))).label("rating")
|
||||
)
|
||||
.filter(AuthorRating.author == author.id)
|
||||
.scalar()
|
||||
likes_count = (
|
||||
session.query(AuthorRating)
|
||||
.filter(and_(AuthorRating.author == author.id, AuthorRating.plus == True))
|
||||
.count()
|
||||
)
|
||||
|
||||
author.stat["rating"] = ratings_sum or 0
|
||||
author.stat["rating_shouts"] = count_author_shouts_rating(session, author.id)
|
||||
author.stat["rating_comments"] = count_author_comments_rating(session, author.id)
|
||||
author.stat["commented"] = comments_count
|
||||
dislikes_count = (
|
||||
session.query(AuthorRating)
|
||||
.filter(and_(AuthorRating.author == author.id, AuthorRating.plus != True))
|
||||
.count()
|
||||
)
|
||||
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(session, author.id)
|
||||
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):
|
||||
@@ -204,38 +224,37 @@ async def get_author(_, _info, slug="", author_id=None):
|
||||
return await load_author_with_stats(q)
|
||||
|
||||
|
||||
@query.field("get_author_id")
|
||||
@query.field('get_author_id')
|
||||
async def get_author_id(_, _info, user: str):
|
||||
with local_session() as session:
|
||||
logger.info(f"getting author id for {user}")
|
||||
q = select(Author).filter(Author.user == user)
|
||||
return await load_author_with_stats(q)
|
||||
logger.info(f'getting author id for {user}')
|
||||
q = select(Author).filter(Author.user == user)
|
||||
return await load_author_with_stats(q)
|
||||
|
||||
|
||||
@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"):
|
||||
q = q.join(ShoutAuthor).join(ShoutTopic).join(Topic).where(Topic.slug == by["topic"])
|
||||
elif by.get('topic'):
|
||||
q = q.join(ShoutAuthor).join(ShoutTopic).join(Topic).where(Topic.slug == by['topic'])
|
||||
|
||||
if by.get("last_seen"): # in unixtime
|
||||
before = int(time.time()) - by["last_seen"]
|
||||
if by.get('last_seen'): # in unixtime
|
||||
before = int(time.time()) - by['last_seen']
|
||||
q = q.filter(Author.last_seen > before)
|
||||
elif by.get("created_at"): # in unixtime
|
||||
before = int(time.time()) - by["created_at"]
|
||||
elif by.get('created_at'): # in unixtime
|
||||
before = int(time.time()) - by['created_at']
|
||||
q = q.filter(Author.created_at > before)
|
||||
|
||||
q = q.order_by(by.get("order", Author.created_at)).limit(limit).offset(offset)
|
||||
q = q.order_by(by.get('order', Author.created_at)).limit(limit).offset(offset)
|
||||
return await get_authors_from_query(q)
|
||||
|
||||
|
||||
@query.field("get_author_followed")
|
||||
async def get_author_followed(_, _info, slug="", user=None, author_id=None) -> List[Author]:
|
||||
@query.field('get_author_followed')
|
||||
async def get_author_followed(_, _info, slug='', user=None, author_id=None) -> List[Author]:
|
||||
author_id_query = None
|
||||
if slug:
|
||||
author_id_query = select(Author.id).where(Author.slug == slug)
|
||||
@@ -246,12 +265,12 @@ async def get_author_followed(_, _info, slug="", user=None, author_id=None) -> L
|
||||
author_id = session.execute(author_id_query).scalar()
|
||||
|
||||
if author_id is None:
|
||||
raise ValueError("Author not found")
|
||||
raise ValueError('Author not found')
|
||||
else:
|
||||
return await followed_authors(author_id) # Author[]
|
||||
|
||||
|
||||
@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)
|
||||
@@ -274,10 +293,10 @@ async def followed_authors(follower_id):
|
||||
return await get_authors_from_query(q)
|
||||
|
||||
|
||||
@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()
|
||||
@@ -285,7 +304,12 @@ async def rate_author(_, info, rated_slug, value):
|
||||
if rater and rated_author:
|
||||
rating: AuthorRating = (
|
||||
session.query(AuthorRating)
|
||||
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.author == rated_author.id))
|
||||
.filter(
|
||||
and_(
|
||||
AuthorRating.rater == rater.id,
|
||||
AuthorRating.author == rated_author.id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if rating:
|
||||
@@ -299,13 +323,13 @@ 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()}')
|
||||
|
Reference in New Issue
Block a user