2022-09-17 18:12:14 +00:00
|
|
|
from typing import List
|
2022-11-23 14:09:35 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2022-11-15 02:36:30 +00:00
|
|
|
from sqlalchemy import and_, func
|
2022-09-17 18:12:14 +00:00
|
|
|
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
|
2022-09-17 18:12:14 +00:00
|
|
|
from orm.reaction import Reaction
|
2022-11-24 17:19:43 +00:00
|
|
|
from orm.shout import ShoutAuthor
|
2022-09-17 18:12:14 +00:00
|
|
|
from orm.topic import Topic, TopicFollower
|
2022-11-13 15:24:29 +00:00
|
|
|
from orm.user import AuthorFollower, Role, User, UserRating, UserRole
|
2022-11-15 02:36:30 +00:00
|
|
|
from services.stat.topicstat import TopicStat
|
2022-11-13 15:24:29 +00:00
|
|
|
|
2022-11-15 02:36:30 +00:00
|
|
|
# from .community import followed_communities
|
2022-11-21 08:13:57 +00:00
|
|
|
from resolvers.inbox.unread import get_total_unread_counter
|
2022-09-19 13:50:43 +00:00
|
|
|
from .topics import get_topic_stat
|
2022-04-13 12:11:06 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-10-23 09:33:28 +00:00
|
|
|
async def user_subscriptions(slug: str):
|
2022-09-19 13:50:43 +00:00
|
|
|
return {
|
2022-11-02 09:46:25 +00:00
|
|
|
"unread": await get_total_unread_counter(slug), # unread inbox messages counter
|
2022-10-23 09:33:28 +00:00
|
|
|
"topics": [t.slug for t in await followed_topics(slug)], # followed topics slugs
|
|
|
|
"authors": [a.slug for a in await followed_authors(slug)], # followed authors slugs
|
2022-11-26 00:55:45 +00:00
|
|
|
"reactions": await followed_reactions(slug)
|
2022-11-15 02:36:30 +00:00
|
|
|
# "communities": [c.slug for c in followed_communities(slug)], # communities
|
2022-09-19 13:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def get_author_stat(slug):
|
2022-10-04 12:07:41 +00:00
|
|
|
with local_session() as session:
|
|
|
|
return {
|
2022-11-19 11:35:34 +00:00
|
|
|
"shouts": session.query(ShoutAuthor).where(ShoutAuthor.user == slug).count(),
|
2022-10-04 12:07:41 +00:00
|
|
|
"followers": session.query(AuthorFollower).where(AuthorFollower.author == slug).count(),
|
2022-11-11 21:27:17 +00:00
|
|
|
"followings": session.query(AuthorFollower).where(AuthorFollower.follower == slug).count(),
|
2022-11-13 15:24:29 +00:00
|
|
|
"rating": session.query(func.sum(UserRating.value)).where(UserRating.user == slug).first(),
|
|
|
|
"commented": session.query(
|
|
|
|
Reaction.id
|
|
|
|
).where(
|
|
|
|
Reaction.createdBy == slug
|
|
|
|
).filter(
|
2022-11-26 00:55:45 +00:00
|
|
|
Reaction.body.is_not(None)
|
2022-11-13 15:24:29 +00:00
|
|
|
).count()
|
2022-10-04 12:07:41 +00:00
|
|
|
}
|
2022-09-19 13:50:43 +00:00
|
|
|
|
|
|
|
|
2022-11-26 00:55:45 +00:00
|
|
|
# @query.field("userFollowedDiscussions")
|
|
|
|
@login_required
|
|
|
|
async def followed_discussions(_, info, slug) -> List[Topic]:
|
|
|
|
return await followed_reactions(slug)
|
|
|
|
|
|
|
|
|
|
|
|
async def followed_reactions(slug):
|
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).where(User.slug == slug).first()
|
|
|
|
return session.query(
|
|
|
|
Reaction.shout
|
|
|
|
).where(
|
|
|
|
Reaction.author == slug
|
|
|
|
).filter(
|
|
|
|
Reaction.createdAt > user.lastSeen
|
|
|
|
).all()
|
|
|
|
|
|
|
|
|
2022-07-21 11:58:50 +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-10-23 09:33:28 +00:00
|
|
|
return await followed_topics(slug)
|
|
|
|
|
|
|
|
|
|
|
|
async def followed_topics(slug):
|
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
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@query.field("userFollowedAuthors")
|
2022-10-04 12:07:41 +00:00
|
|
|
async def get_followed_authors(_, _info, slug) -> List[User]:
|
2022-10-23 09:33:28 +00:00
|
|
|
return await followed_authors(slug)
|
|
|
|
|
|
|
|
|
|
|
|
async def followed_authors(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
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
2022-11-15 02:36:30 +00:00
|
|
|
async def get_user_roles(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
|
|
|
|
2021-12-08 12:51:30 +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)
|
2022-09-17 18:12:14 +00:00
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
2022-09-03 10:50:14 +00:00
|
|
|
return {}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
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 {}
|
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2022-07-21 11:58:50 +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-11-21 04:36:40 +00:00
|
|
|
with local_session() as session:
|
|
|
|
authors = session.query(User).join(ShoutAuthor).all()
|
|
|
|
for author in authors:
|
|
|
|
author.stat = await get_author_stat(author.slug)
|
2022-09-29 17:35:04 +00:00
|
|
|
return authors
|
2022-09-14 08:27:44 +00:00
|
|
|
|
2022-09-30 08:48:38 +00:00
|
|
|
|
2022-11-21 04:36:40 +00:00
|
|
|
@query.field("getAuthor")
|
|
|
|
async def get_author(_, _info, slug):
|
|
|
|
with local_session() as session:
|
2022-11-25 11:52:14 +00:00
|
|
|
author = session.query(User).where(User.slug == slug).first()
|
2022-11-24 15:19:58 +00:00
|
|
|
author.stat = await get_author_stat(author.slug)
|
2022-11-21 04:36:40 +00:00
|
|
|
return author
|
|
|
|
|
|
|
|
|
2022-11-15 02:36:30 +00:00
|
|
|
@query.field("loadAuthorsBy")
|
2022-11-16 07:32:24 +00:00
|
|
|
async def load_authors_by(_, info, by, limit, offset):
|
2022-11-15 02:36:30 +00:00
|
|
|
authors = []
|
|
|
|
with local_session() as session:
|
|
|
|
aq = session.query(User)
|
|
|
|
if by.get("slug"):
|
|
|
|
aq = aq.filter(User.slug.ilike(f"%{by['slug']}%"))
|
|
|
|
elif by.get("name"):
|
|
|
|
aq = aq.filter(User.name.ilike(f"%{by['name']}%"))
|
|
|
|
elif by.get("topic"):
|
|
|
|
aaa = list(map(lambda a: a.slug, TopicStat.authors_by_topic.get(by["topic"])))
|
|
|
|
aq = aq.filter(User.name._in(aaa))
|
|
|
|
if by.get("lastSeen"): # in days
|
2022-11-23 14:09:35 +00:00
|
|
|
days_before = datetime.now(tz=timezone.utc) - timedelta(days=by["lastSeen"])
|
2022-11-15 02:36:30 +00:00
|
|
|
aq = aq.filter(User.lastSeen > days_before)
|
|
|
|
elif by.get("createdAt"): # in days
|
2022-11-23 14:09:35 +00:00
|
|
|
days_before = datetime.now(tz=timezone.utc) - timedelta(days=by["createdAt"])
|
2022-11-15 02:36:30 +00:00
|
|
|
aq = aq.filter(User.createdAt > days_before)
|
|
|
|
aq = aq.group_by(
|
|
|
|
User.id
|
|
|
|
).order_by(
|
|
|
|
by.get("order") or "createdAt"
|
2022-11-16 07:32:24 +00:00
|
|
|
).limit(limit).offset(offset)
|
2022-11-17 06:25:26 +00:00
|
|
|
print(aq)
|
2022-11-15 02:36:30 +00:00
|
|
|
authors = list(map(lambda r: r.User, session.execute(aq)))
|
|
|
|
if by.get("stat"):
|
|
|
|
for a in authors:
|
|
|
|
a.stat = await get_author_stat(a.slug)
|
2022-11-17 08:11:30 +00:00
|
|
|
authors = list(set(authors))
|
2022-11-18 02:19:10 +00:00
|
|
|
# authors = sorted(authors, key=lambda a: a["stat"].get(by.get("stat")))
|
2022-11-15 02:36:30 +00:00
|
|
|
return authors
|