stats refactored
This commit is contained in:
@@ -5,6 +5,7 @@ from resolvers.auth import (
|
||||
register,
|
||||
confirm_email,
|
||||
auth_send_link,
|
||||
get_current_user,
|
||||
)
|
||||
from resolvers.collab import remove_author, invite_author
|
||||
from resolvers.community import (
|
||||
@@ -18,7 +19,6 @@ from resolvers.community import (
|
||||
from resolvers.editor import create_shout, delete_shout, update_shout
|
||||
from resolvers.profile import (
|
||||
get_users_by_slugs,
|
||||
get_current_user,
|
||||
get_user_reacted_shouts,
|
||||
get_user_roles,
|
||||
get_top_authors,
|
||||
@@ -44,7 +44,7 @@ from resolvers.zine import (
|
||||
get_shout_by_slug,
|
||||
follow,
|
||||
unfollow,
|
||||
view_shout,
|
||||
increment_view,
|
||||
top_month,
|
||||
top_overall,
|
||||
recent_published,
|
||||
@@ -65,8 +65,8 @@ __all__ = [
|
||||
"confirm_email",
|
||||
"auth_send_link",
|
||||
"sign_out",
|
||||
# profile
|
||||
"get_current_user",
|
||||
# profile
|
||||
"get_users_by_slugs",
|
||||
"get_user_roles",
|
||||
"get_top_authors",
|
||||
@@ -80,7 +80,7 @@ __all__ = [
|
||||
"top_month",
|
||||
"top_overall",
|
||||
"top_viewed",
|
||||
"view_shout",
|
||||
"increment_view",
|
||||
"get_shout_by_slug",
|
||||
# editor
|
||||
"create_shout",
|
||||
|
@@ -1,9 +1,10 @@
|
||||
from urllib.parse import quote_plus
|
||||
from datetime import datetime
|
||||
|
||||
from auth.tokenstorage import TokenStorage
|
||||
from graphql.type import GraphQLResolveInfo
|
||||
from transliterate import translit
|
||||
|
||||
from auth.tokenstorage import TokenStorage
|
||||
from auth.authenticate import login_required
|
||||
from auth.email import send_auth_email
|
||||
from auth.identity import Identity, Password
|
||||
@@ -20,6 +21,22 @@ from resolvers.profile import get_user_info
|
||||
from settings import SESSION_TOKEN_HEADER
|
||||
|
||||
|
||||
@mutation.field("refreshSession")
|
||||
@login_required
|
||||
async def get_current_user(_, info):
|
||||
user = info.context["request"].user
|
||||
user.lastSeen = datetime.now()
|
||||
with local_session() as session:
|
||||
session.add(user)
|
||||
session.commit()
|
||||
token = await TokenStorage.create_session(user)
|
||||
return {
|
||||
"token": token,
|
||||
"user": user,
|
||||
"info": await get_user_info(user.slug),
|
||||
}
|
||||
|
||||
|
||||
@mutation.field("confirmEmail")
|
||||
async def confirm_email(*_, confirm_token):
|
||||
"""confirm owning email address"""
|
||||
|
@@ -99,3 +99,6 @@ async def get_my_collections(_, info):
|
||||
session.query(Collection).when(Collection.createdBy == user_id).all()
|
||||
)
|
||||
return collections
|
||||
|
||||
|
||||
# TODO: get shouts list by collection
|
||||
|
@@ -13,7 +13,7 @@ from services.zine.shoutscache import prepare_shouts
|
||||
|
||||
@query.field("shoutsForFeed")
|
||||
@login_required
|
||||
def get_user_feed(_, info, offset, limit) -> List[Shout]:
|
||||
async def get_user_feed(_, info, offset, limit) -> List[Shout]:
|
||||
user = info.context["request"].user
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
|
@@ -1,23 +1,39 @@
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
from sqlalchemy import and_, desc
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from auth.authenticate import login_required
|
||||
from auth.tokenstorage import TokenStorage
|
||||
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
|
||||
from resolvers.community import get_followed_communities
|
||||
from resolvers.inbox import get_unread_counter
|
||||
from resolvers.reactions import get_shout_reactions
|
||||
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
|
||||
|
||||
|
||||
async def get_user_info(slug):
|
||||
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
|
||||
return {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@query.field("userReactedShouts")
|
||||
async def get_user_reacted_shouts(_, _info, slug, offset, limit) -> List[Shout]:
|
||||
user = await UserStorage.get_user_by_slug(slug)
|
||||
@@ -38,20 +54,22 @@ async def get_user_reacted_shouts(_, _info, slug, offset, limit) -> List[Shout]:
|
||||
|
||||
@query.field("userFollowedTopics")
|
||||
@login_required
|
||||
def get_followed_topics(_, slug) -> List[Topic]:
|
||||
rows = []
|
||||
async def get_followed_topics(_, slug) -> List[Topic]:
|
||||
topics = []
|
||||
with local_session() as session:
|
||||
rows = (
|
||||
topics = (
|
||||
session.query(Topic)
|
||||
.join(TopicFollower)
|
||||
.where(TopicFollower.follower == slug)
|
||||
.all()
|
||||
)
|
||||
return rows
|
||||
for topic in topics:
|
||||
topic.stat = await get_topic_stat(topic.slug)
|
||||
return topics
|
||||
|
||||
|
||||
@query.field("userFollowedAuthors")
|
||||
def get_followed_authors(_, slug) -> List[User]:
|
||||
async def get_followed_authors(_, slug) -> List[User]:
|
||||
authors = []
|
||||
with local_session() as session:
|
||||
authors = (
|
||||
@@ -60,6 +78,8 @@ def get_followed_authors(_, slug) -> List[User]:
|
||||
.where(AuthorFollower.follower == slug)
|
||||
.all()
|
||||
)
|
||||
for author in authors:
|
||||
author.stat = await get_author_stat(author.slug)
|
||||
return authors
|
||||
|
||||
|
||||
@@ -75,33 +95,6 @@ async def user_followers(_, slug) -> List[User]:
|
||||
return users
|
||||
|
||||
|
||||
# for mutation.field("refreshSession")
|
||||
async def get_user_info(slug):
|
||||
return {
|
||||
"unread": await get_unread_counter(slug),
|
||||
"topics": [t.slug for t in get_followed_topics(0, slug)],
|
||||
"authors": [a.slug for a in get_followed_authors(0, slug)],
|
||||
"reactions": [r.shout for r in get_shout_reactions(0, slug)],
|
||||
"communities": [c.slug for c in get_followed_communities(0, slug)],
|
||||
}
|
||||
|
||||
|
||||
@mutation.field("refreshSession")
|
||||
@login_required
|
||||
async def get_current_user(_, info):
|
||||
user = info.context["request"].user
|
||||
user.lastSeen = datetime.now()
|
||||
with local_session() as session:
|
||||
session.add(user)
|
||||
session.commit()
|
||||
token = await TokenStorage.create_session(user)
|
||||
return {
|
||||
"token": token,
|
||||
"user": user,
|
||||
"info": await get_user_info(user.slug),
|
||||
}
|
||||
|
||||
|
||||
@query.field("getUsersBySlugs")
|
||||
async def get_users_by_slugs(_, _info, slugs):
|
||||
with local_session() as session:
|
||||
|
@@ -10,6 +10,16 @@ from orm.shout import ShoutReactionsFollower
|
||||
from orm.user import User
|
||||
from services.auth.users import UserStorage
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.viewed import ViewedStorage
|
||||
|
||||
|
||||
async def get_reaction_stat(reaction_id):
|
||||
return {
|
||||
"viewed": await ViewedStorage.get_reaction(reaction_id),
|
||||
"reacted": len(await ReactedStorage.get_reaction(reaction_id)),
|
||||
"rating": await ReactedStorage.get_reaction_rating(reaction_id),
|
||||
"commented": len(await ReactedStorage.get_reaction_comments(reaction_id)),
|
||||
}
|
||||
|
||||
|
||||
def reactions_follow(user, slug, auto=False):
|
||||
@@ -61,6 +71,8 @@ async def create_reaction(_, info, inp):
|
||||
except Exception as e:
|
||||
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
|
||||
|
||||
reaction.stat = await get_reaction_stat(reaction.id)
|
||||
|
||||
return {"reaction": reaction}
|
||||
|
||||
|
||||
@@ -86,6 +98,8 @@ async def update_reaction(_, info, inp):
|
||||
reaction.range = inp.get("range")
|
||||
session.commit()
|
||||
|
||||
reaction.stat = await get_reaction_stat(reaction.id)
|
||||
|
||||
return {"reaction": reaction}
|
||||
|
||||
|
||||
@@ -118,6 +132,7 @@ async def get_shout_reactions(_, info, slug, offset, limit):
|
||||
.all()
|
||||
)
|
||||
for r in reactions:
|
||||
r.stat = await get_reaction_stat(r.id)
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
return reactions
|
||||
|
||||
@@ -137,6 +152,7 @@ async def get_reactions_for_shouts(_, info, shouts, offset, limit):
|
||||
.all()
|
||||
)
|
||||
for r in reactions:
|
||||
r.stat = await get_reaction_stat(r.id)
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
return reactions
|
||||
|
||||
@@ -152,5 +168,6 @@ async def get_reactions_by_author(_, info, slug, limit=50, offset=0):
|
||||
.offset(offset)
|
||||
)
|
||||
for r in reactions:
|
||||
r.stat = await get_reaction_stat(r.id)
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
return reactions
|
||||
|
@@ -6,16 +6,30 @@ from auth.authenticate import login_required
|
||||
from base.orm import local_session
|
||||
from base.resolvers import mutation, query
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from services.stat.topicstat import TopicStat
|
||||
from services.zine.shoutscache import ShoutsCache
|
||||
from services.zine.topics import TopicStorage
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.topicstat import TopicStat
|
||||
from services.stat.viewed import ViewedStorage
|
||||
|
||||
|
||||
async def get_topic_stat(slug):
|
||||
return {
|
||||
"shouts": len(TopicStat.shouts_by_topic.get(slug, [])),
|
||||
"authors": len(TopicStat.authors_by_topic.get(slug, [])),
|
||||
"followers": len(TopicStat.followers_by_topic.get(slug, [])),
|
||||
"viewed": await ViewedStorage.get_topic(slug),
|
||||
"reacted": len(await ReactedStorage.get_topic(slug)),
|
||||
"commented": len(await ReactedStorage.get_topic_comments(slug)),
|
||||
"rating": await ReactedStorage.get_topic_rating(slug)
|
||||
}
|
||||
|
||||
|
||||
@query.field("topicsAll")
|
||||
async def topics_all(_, _info):
|
||||
topics = await TopicStorage.get_topics_all()
|
||||
for topic in topics:
|
||||
topic.stat = await TopicStat.get_stat(topic.slug)
|
||||
topic.stat = await get_topic_stat(topic.slug)
|
||||
return topics
|
||||
|
||||
|
||||
@@ -23,18 +37,19 @@ async def topics_all(_, _info):
|
||||
async def topics_by_community(_, info, community):
|
||||
topics = await TopicStorage.get_topics_by_community(community)
|
||||
for topic in topics:
|
||||
topic.stat = await TopicStat.get_stat(topic.slug)
|
||||
topic.stat = await get_topic_stat(topic.slug)
|
||||
return topics
|
||||
|
||||
|
||||
@query.field("topicsByAuthor")
|
||||
async def topics_by_author(_, _info, author):
|
||||
topics = ShoutsCache.by_author.get(author)
|
||||
shouts = ShoutsCache.by_author.get(author)
|
||||
author_topics = set()
|
||||
for tpc in topics:
|
||||
tpc = await TopicStorage.topics[tpc.slug]
|
||||
tpc.stat = await TopicStat.get_stat(tpc.slug)
|
||||
author_topics.add(tpc)
|
||||
for s in shouts:
|
||||
for tpc in s.topics:
|
||||
tpc = await TopicStorage.topics[tpc.slug]
|
||||
tpc.stat = await get_topic_stat(tpc.slug)
|
||||
author_topics.add(tpc)
|
||||
return list(author_topics)
|
||||
|
||||
|
||||
@@ -91,11 +106,8 @@ async def topics_random(_, info, amount=12):
|
||||
topics = await TopicStorage.get_topics_all()
|
||||
normalized_topics = []
|
||||
for topic in topics:
|
||||
topic_stat = await TopicStat.get_stat(topic.slug)
|
||||
# FIXME: expects topicstat fix
|
||||
# #if topic_stat["shouts"] > 2:
|
||||
# normalized_topics.append(topic)
|
||||
topic.stat = topic_stat
|
||||
normalized_topics.append(topic)
|
||||
topic.stat = await get_topic_stat(topic.slug)
|
||||
if topic.stat["shouts"] > 2:
|
||||
normalized_topics.append(topic)
|
||||
sample_length = min(len(normalized_topics), amount)
|
||||
return random.sample(normalized_topics, sample_length)
|
||||
|
@@ -64,12 +64,6 @@ async def recent_reacted(_, _info, offset, limit):
|
||||
return ShoutsCache.recent_reacted[offset : offset + limit]
|
||||
|
||||
|
||||
@mutation.field("viewShout")
|
||||
async def view_shout(_, _info, slug):
|
||||
await ViewedStorage.increment(slug)
|
||||
return {"error": ""}
|
||||
|
||||
|
||||
@query.field("getShoutBySlug")
|
||||
async def get_shout_by_slug(_, info, slug):
|
||||
all_fields = [
|
||||
|
Reference in New Issue
Block a user