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
|
|
|
|
|
|
|
from sqlalchemy import and_, case, distinct, func, literal, select
|
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-11-28 07:53:48 +00:00
|
|
|
from orm.community import Community
|
2023-12-24 17:46:50 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2023-12-24 18:38:16 +00:00
|
|
|
from orm.shout import ShoutAuthor, ShoutTopic, Shout
|
2023-10-23 14:47:11 +00:00
|
|
|
from orm.topic import Topic
|
2023-11-22 18:23:15 +00:00
|
|
|
from resolvers.community import followed_communities
|
|
|
|
from resolvers.reaction import reacted_shouts_updates as followed_reactions
|
2023-12-17 20:30:20 +00:00
|
|
|
from resolvers.topic import followed_topics
|
|
|
|
from services.auth import login_required
|
|
|
|
from services.db import local_session
|
|
|
|
from services.schema import mutation, query
|
|
|
|
from services.unread import get_total_unread_counter
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_author_stat_columns(q):
|
2023-12-02 19:45:41 +00:00
|
|
|
shout_author_aliased = aliased(ShoutAuthor)
|
2023-12-24 15:34:06 +00:00
|
|
|
q = q.outerjoin(shout_author_aliased, shout_author_aliased.author == Author.id).add_columns(
|
2023-12-02 19:45:41 +00:00
|
|
|
func.count(distinct(shout_author_aliased.shout)).label("shouts_stat")
|
2023-10-23 14:47:11 +00:00
|
|
|
)
|
2023-12-02 19:33:00 +00:00
|
|
|
|
2023-12-02 19:45:41 +00:00
|
|
|
followers_table = aliased(AuthorFollower)
|
2023-10-23 14:47:11 +00:00
|
|
|
q = q.outerjoin(followers_table, followers_table.author == Author.id).add_columns(
|
2023-12-02 19:45:41 +00:00
|
|
|
func.count(distinct(followers_table.follower)).label("followers_stat")
|
2023-10-23 14:47:11 +00:00
|
|
|
)
|
|
|
|
|
2023-12-02 19:45:41 +00:00
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
q = q.group_by(Author.id)
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
|
|
|
def get_authors_from_query(q):
|
|
|
|
authors = []
|
|
|
|
with local_session() as session:
|
2023-12-24 17:46:50 +00:00
|
|
|
for [author, shouts_stat, followers_stat, followings_stat] in session.execute(q):
|
2023-12-24 15:34:06 +00:00
|
|
|
author.stat = {
|
|
|
|
"shouts": shouts_stat,
|
|
|
|
"followers": followers_stat,
|
|
|
|
"followings": followings_stat,
|
|
|
|
}
|
2023-10-23 14:47:11 +00:00
|
|
|
authors.append(author)
|
2023-12-16 15:24:30 +00:00
|
|
|
# print(f"[resolvers.author] get_authors_from_query {authors}")
|
2023-10-23 14:47:11 +00:00
|
|
|
return authors
|
|
|
|
|
|
|
|
|
|
|
|
async def author_followings(author_id: int):
|
2023-12-24 15:34:06 +00:00
|
|
|
# NOTE: topics, authors, shout-reactions and communities slugs list
|
2023-10-23 14:47:11 +00:00
|
|
|
return {
|
2023-11-28 07:53:48 +00:00
|
|
|
"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 followed_reactions(author_id)],
|
|
|
|
"communities": [c.slug for c in [followed_communities(author_id)] if isinstance(c, Community)],
|
2023-10-23 14:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@mutation.field("update_profile")
|
2023-10-23 14:47:11 +00:00
|
|
|
@login_required
|
|
|
|
async def update_profile(_, info, profile):
|
2023-11-23 23:00:28 +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()
|
|
|
|
return {"error": None, "author": author}
|
|
|
|
|
|
|
|
|
|
|
|
# for mutation.field("follow")
|
|
|
|
def author_follow(follower_id, slug):
|
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).where(Author.slug == slug).one()
|
2023-11-22 16:38:39 +00:00
|
|
|
af = AuthorFollower(follower=follower_id, author=author.id)
|
2023-10-23 14:47:11 +00:00
|
|
|
session.add(af)
|
|
|
|
session.commit()
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# for mutation.field("unfollow")
|
|
|
|
def author_unfollow(follower_id, slug):
|
|
|
|
with local_session() as session:
|
|
|
|
flw = (
|
|
|
|
session.query(AuthorFollower)
|
|
|
|
.join(Author, Author.id == AuthorFollower.author)
|
|
|
|
.filter(and_(AuthorFollower.follower == follower_id, Author.slug == slug))
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if flw:
|
|
|
|
session.delete(flw)
|
|
|
|
session.commit()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-12-19 08:09:50 +00:00
|
|
|
# TODO: caching query
|
|
|
|
@query.field("get_authors_all")
|
|
|
|
async def get_authors_all(_, _info):
|
|
|
|
with local_session() as session:
|
2023-12-24 14:25:57 +00:00
|
|
|
return session.query(Author).all()
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2023-12-13 19:59:21 +00:00
|
|
|
@query.field("get_author_id")
|
|
|
|
async def get_author_id(_, _info, user: str):
|
|
|
|
with local_session() as session:
|
2023-12-15 13:48:47 +00:00
|
|
|
print(f"[resolvers.author] getting author id for {user}")
|
2023-12-15 13:59:03 +00:00
|
|
|
a = session.query(Author).filter(Author.user == user).first()
|
2023-12-17 12:22:07 +00:00
|
|
|
if a:
|
|
|
|
print(f"[resolvers.author] got @{a.slug}")
|
2023-12-15 13:59:03 +00:00
|
|
|
return a
|
2023-12-13 19:59:21 +00:00
|
|
|
|
|
|
|
|
2023-12-24 18:38:16 +00:00
|
|
|
def count_author_rating(session, author_id) -> int:
|
|
|
|
shouts_likes = (
|
|
|
|
session.query(Reaction, Shout)
|
|
|
|
.join(Shout, Shout.id == Reaction.shout)
|
2023-12-24 22:06:27 +00:00
|
|
|
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.LIKE.value))
|
2023-12-24 18:38:16 +00:00
|
|
|
.count()
|
|
|
|
or 0
|
|
|
|
)
|
|
|
|
shouts_dislikes = (
|
|
|
|
session.query(Reaction, Shout)
|
|
|
|
.join(Shout, Shout.id == Reaction.shout)
|
2023-12-24 22:06:27 +00:00
|
|
|
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.DISLIKE.value))
|
2023-12-24 18:38:16 +00:00
|
|
|
.count()
|
|
|
|
or 0
|
|
|
|
)
|
|
|
|
replied_alias = aliased(Reaction)
|
|
|
|
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))
|
|
|
|
.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))
|
|
|
|
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
|
|
|
.count()
|
|
|
|
) or 0
|
|
|
|
|
|
|
|
return shouts_likes - shouts_dislikes + replies_likes - replies_dislikes
|
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("get_author")
|
2023-12-13 19:59:21 +00:00
|
|
|
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-11-27 16:03:47 +00:00
|
|
|
elif author_id:
|
|
|
|
q = select(Author).where(Author.id == author_id)
|
2023-11-24 01:53:30 +00:00
|
|
|
q = add_author_stat_columns(q)
|
|
|
|
authors = get_authors_from_query(q)
|
2023-12-07 18:29:25 +00:00
|
|
|
if authors:
|
2023-12-24 17:46:50 +00:00
|
|
|
author = authors[0]
|
2023-12-24 21:02: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),
|
2023-12-24 18:38:16 +00:00
|
|
|
)
|
|
|
|
)
|
2023-12-24 21:02:54 +00:00
|
|
|
.count()
|
|
|
|
)
|
|
|
|
author.stat["commented"] = comments_count
|
2023-12-07 18:29:25 +00:00
|
|
|
else:
|
|
|
|
return {"error": "cant find author"}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +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)
|
|
|
|
q = add_author_stat_columns(q)
|
|
|
|
if by.get("slug"):
|
|
|
|
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
|
|
|
|
elif by.get("name"):
|
|
|
|
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
|
|
|
|
elif by.get("topic"):
|
2023-11-22 16:38:39 +00:00
|
|
|
q = q.join(ShoutAuthor).join(ShoutTopic).join(Topic).where(Topic.slug == by["topic"])
|
|
|
|
|
2023-11-03 10:10:22 +00:00
|
|
|
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"]
|
|
|
|
q = q.filter(Author.created_at > before)
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-11-03 10:10:22 +00:00
|
|
|
q = q.order_by(by.get("order", Author.created_at)).limit(limit).offset(offset)
|
2023-10-23 14:47:11 +00:00
|
|
|
return get_authors_from_query(q)
|
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("get_author_followed")
|
|
|
|
async def get_author_followed(_, _info, slug="", user=None, author_id=None) -> List[Author]:
|
2023-12-07 18:29:25 +00:00
|
|
|
author_id_query = None
|
|
|
|
if slug:
|
2023-10-23 14:47:11 +00:00
|
|
|
author_id_query = select(Author.id).where(Author.slug == slug)
|
2023-12-07 18:29:25 +00:00
|
|
|
elif user:
|
|
|
|
author_id_query = select(Author.id).where(Author.user == user)
|
|
|
|
if not author_id:
|
|
|
|
with local_session() as session:
|
|
|
|
author_id = session.execute(author_id_query).scalar()
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
if author_id is None:
|
|
|
|
raise ValueError("Author not found")
|
2023-12-07 18:29:25 +00:00
|
|
|
else:
|
|
|
|
return await followed_authors(author_id) # Author[]
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("get_author_followers")
|
|
|
|
async def get_author_followers(_, _info, slug) -> List[Author]:
|
2023-10-23 14:47:11 +00:00
|
|
|
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 get_authors_from_query(q)
|
|
|
|
|
|
|
|
|
|
|
|
async def followed_authors(follower_id):
|
|
|
|
q = select(Author)
|
|
|
|
q = add_author_stat_columns(q)
|
2023-11-22 16:38:39 +00:00
|
|
|
q = q.join(AuthorFollower, AuthorFollower.author == Author.id).where(AuthorFollower.follower == follower_id)
|
2023-10-23 14:47:11 +00:00
|
|
|
# Pass the query to the get_authors_from_query function and return the results
|
|
|
|
return get_authors_from_query(q)
|
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +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):
|
2023-11-23 23:00:28 +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-11-28 07:53:48 +00:00
|
|
|
rating = (
|
|
|
|
session.query(AuthorRating)
|
2023-11-28 19:07:53 +00:00
|
|
|
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.author == rated_author.id))
|
2023-11-28 07:53:48 +00:00
|
|
|
.first()
|
|
|
|
)
|
2023-11-29 20:22:39 +00:00
|
|
|
if value > 0:
|
|
|
|
rating.plus = True
|
2023-11-22 16:38:39 +00:00
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
2023-11-28 07:53:48 +00:00
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
try:
|
2023-11-29 20:22:39 +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:
|
|
|
|
return {"error": err}
|
2023-10-23 14:47:11 +00:00
|
|
|
return {}
|
2023-11-28 19:07:53 +00:00
|
|
|
|
|
|
|
|
2023-12-16 16:59:43 +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()
|
2023-12-15 15:28:44 +00:00
|
|
|
print(f"[resolvers.author] created by webhook {new_author.dict()}")
|