core/resolvers/profile.py

200 lines
6.0 KiB
Python
Raw Normal View History

from typing import List
2022-10-04 12:07:41 +00:00
from sqlalchemy import and_, desc, func
from sqlalchemy.orm import selectinload
from auth.authenticate import login_required
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import mutation, query
from orm.reaction import Reaction
from orm.shout import Shout
from orm.topic import Topic, TopicFollower
from orm.user import User, UserRole, Role, UserRating, AuthorFollower
2022-09-19 13:50:43 +00:00
from .community import get_followed_communities
from .inbox import get_unread_counter
from .reactions import get_shout_reactions
from .topics import get_topic_stat
from services.auth.users import UserStorage
2022-04-13 12:11:06 +00:00
2022-09-03 10:50:14 +00:00
2022-10-04 12:07:41 +00:00
async def get_user_subscriptions(slug):
2022-09-19 13:50:43 +00:00
return {
"unread": await get_unread_counter(slug), # unread inbox messages counter
"topics": [t.slug for t in get_followed_topics(0, slug)], # followed topics slugs
"authors": [a.slug for a in get_followed_authors(0, slug)], # followed authors slugs
"reactions": [r.shout for r in get_shout_reactions(0, slug)], # followed reacted shouts slugs
"communities": [c.slug for c in get_followed_communities(0, slug)], # followed communities slugs
}
async def get_author_stat(slug):
# TODO: implement author stat
2022-10-04 12:07:41 +00:00
with local_session() as session:
return {
"followers": session.query(AuthorFollower).where(AuthorFollower.author == slug).count(),
"rating": session.query(func.sum(UserRating.value)).where(UserRating.user == slug).first()
# TODO: debug
}
2022-09-19 13:50:43 +00:00
@query.field("userReactedShouts")
2022-10-04 12:07:41 +00:00
async def get_user_reacted_shouts(_, slug, offset, limit) -> List[Shout]:
2022-09-03 10:50:14 +00:00
user = await UserStorage.get_user_by_slug(slug)
if not user:
return []
with local_session() as session:
shouts = (
session.query(Shout)
.join(Reaction)
.where(Reaction.createdBy == user.slug)
.order_by(desc(Reaction.createdAt))
2022-09-14 09:45:31 +00:00
.limit(limit)
.offset()
2022-09-03 10:50:14 +00:00
.all()
)
return shouts
2022-05-04 13:53:23 +00:00
@query.field("userFollowedTopics")
@login_required
2022-10-04 12:07:41 +00:00
async def get_followed_topics(_, info, slug) -> List[Topic]:
2022-09-19 13:50:43 +00:00
topics = []
2022-09-03 10:50:14 +00:00
with local_session() as session:
2022-09-19 13:50:43 +00:00
topics = (
2022-09-03 10:50:14 +00:00
session.query(Topic)
.join(TopicFollower)
.where(TopicFollower.follower == slug)
.all()
)
2022-09-19 13:50:43 +00:00
for topic in topics:
topic.stat = await get_topic_stat(topic.slug)
return topics
@query.field("userFollowedAuthors")
2022-10-04 12:07:41 +00:00
async def get_followed_authors(_, _info, slug) -> List[User]:
2022-09-03 10:50:14 +00:00
authors = []
with local_session() as session:
authors = (
session.query(User)
.join(AuthorFollower, User.slug == AuthorFollower.author)
.where(AuthorFollower.follower == slug)
.all()
)
2022-09-19 13:50:43 +00:00
for author in authors:
author.stat = await get_author_stat(author.slug)
2022-09-03 10:50:14 +00:00
return authors
@query.field("userFollowers")
2022-10-04 12:07:41 +00:00
async def user_followers(_, _info, slug) -> List[User]:
2022-09-03 10:50:14 +00:00
with local_session() as session:
users = (
session.query(User)
.join(AuthorFollower, User.slug == AuthorFollower.follower)
.where(AuthorFollower.author == slug)
.all()
)
return users
2022-08-12 08:13:18 +00:00
@query.field("getUsersBySlugs")
2022-09-14 09:45:31 +00:00
async def get_users_by_slugs(_, _info, slugs):
2022-09-03 10:50:14 +00:00
with local_session() as session:
users = (
session.query(User)
.options(selectinload(User.ratings))
.filter(User.slug in slugs)
.all()
)
return users
2021-11-25 02:45:22 +00:00
2021-12-11 13:18:40 +00:00
@query.field("getUserRoles")
2022-09-14 09:45:31 +00:00
async def get_user_roles(_, _info, slug):
2022-09-03 10:50:14 +00:00
with local_session() as session:
user = session.query(User).where(User.slug == slug).first()
roles = (
session.query(Role)
.options(selectinload(Role.permissions))
.join(UserRole)
.where(UserRole.user_id == user.id)
.all()
)
return roles
2021-11-24 09:09:47 +00:00
@mutation.field("updateProfile")
@login_required
async def update_profile(_, info, profile):
2022-09-03 10:50:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
if user:
User.update(user, **profile)
session.add(user)
session.commit()
2022-09-03 10:50:14 +00:00
return {}
2022-02-22 09:55:02 +00:00
2022-02-16 12:38:05 +00:00
@mutation.field("rateUser")
@login_required
2022-10-04 12:07:41 +00:00
async def rate_user(_, info, rated_userslug, value):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
with local_session() as session:
rating = (
session.query(UserRating)
2022-10-04 12:07:41 +00:00
.filter(and_(UserRating.rater == user.slug, UserRating.user == rated_userslug))
2022-09-03 10:50:14 +00:00
.first()
)
if rating:
rating.value = value
session.commit()
return {}
try:
2022-10-04 12:07:41 +00:00
UserRating.create(rater=user.slug, user=rated_userslug, value=value)
2022-09-03 10:50:14 +00:00
except Exception as err:
return {"error": err}
return {}
# for mutation.field("follow")
def author_follow(user, slug):
2022-09-18 14:29:21 +00:00
with local_session() as session:
af = AuthorFollower.create(follower=user.slug, author=slug)
session.add(af)
session.commit()
2022-09-03 10:50:14 +00:00
# for mutation.field("unfollow")
def author_unfollow(user, slug):
2022-09-03 10:50:14 +00:00
with local_session() as session:
flw = (
session.query(AuthorFollower)
.filter(
and_(
AuthorFollower.follower == user.slug, AuthorFollower.author == slug
)
)
.first()
)
if not flw:
raise Exception("[resolvers.profile] follower not exist, cant unfollow")
else:
session.delete(flw)
session.commit()
2022-07-28 03:44:56 +00:00
@query.field("authorsAll")
2022-09-30 08:48:38 +00:00
async def get_authors_all(_, _info):
2022-09-29 17:35:04 +00:00
authors = await UserStorage.get_all_users()
for author in authors:
author.stat = await get_author_stat(author.slug)
return authors
2022-09-14 08:27:44 +00:00
2022-09-30 08:48:38 +00:00
2022-09-14 08:27:44 +00:00
@query.field("topAuthors")
2022-09-14 09:45:31 +00:00
def get_top_authors(_, _info, offset, limit):
return list(UserStorage.get_top_users())[offset : offset + limit] # type: ignore