0.2.16-resolvers-revision
All checks were successful
deploy / deploy (push) Successful in 2m22s

This commit is contained in:
2023-11-28 10:53:48 +03:00
parent 3cf86d9e6e
commit 20f7c22441
15 changed files with 266 additions and 196 deletions

View File

@@ -1,62 +1,67 @@
from resolvers.editor import create_shout, delete_shout, update_shout
from resolvers.author import load_authors_by, update_profile, get_authors_all, rate_author
from resolvers.author import (
get_author,
get_authors_all,
get_author_followers,
get_author_followed,
load_authors_by,
update_profile,
rate_author,
)
from resolvers.reaction import (
create_reaction,
delete_reaction,
update_reaction,
reactions_unfollow,
reactions_follow,
delete_reaction,
load_reactions_by,
load_shouts_followed,
)
from resolvers.topic import (
topic_follow,
topic_unfollow,
topics_by_author,
topics_by_community,
topics_all,
get_topics_by_author,
get_topics_by_community,
get_topics_all,
get_topic,
)
from resolvers.follower import follow, unfollow
from resolvers.reader import load_shout, load_shouts_by, search, load_my_subscriptions
from resolvers.follower import follow, unfollow, get_my_followed
from resolvers.reader import get_shout, load_shouts_by, load_shouts_feed, load_shouts_search
from resolvers.community import get_community, get_communities_all
__all__ = [
# author
"load_authors_by",
"update_profile",
"get_author",
"get_authors_all",
"get_author_followers",
"get_author_followed",
"load_authors_by",
"rate_author",
"update_profile",
# community
"get_community",
"get_communities_all",
# topic
"get_topic",
"get_topics_all",
"get_topics_by_community",
"get_topics_by_author",
# reader
"load_shout",
"get_shout",
"load_shouts_by",
"rate_author",
"load_my_subscriptions",
"search",
"load_shouts_feed",
"load_shouts_search",
"load_shouts_followed",
# follower
"follow",
"unfollow",
"get_my_followed",
# editor
"create_shout",
"update_shout",
"delete_shout",
# topic
"topics_all",
"topics_by_community",
"topics_by_author",
"topic_follow",
"topic_unfollow",
"get_topic",
# reaction
"reactions_follow",
"reactions_unfollow",
"create_reaction",
"update_reaction",
"delete_reaction",
"load_reactions_by",
# community
"get_community",
"get_communities_all",
]

View File

@@ -7,6 +7,7 @@ from services.auth import login_required
from services.db import local_session
from services.unread import get_total_unread_counter
from services.schema import mutation, query
from orm.community import Community
from orm.shout import ShoutAuthor, ShoutTopic
from orm.topic import Topic
from orm.author import AuthorFollower, Author, AuthorRating
@@ -79,15 +80,15 @@ def get_authors_from_query(q):
async def author_followings(author_id: int):
return {
"unread": await get_total_unread_counter(author_id), # unread inbox messages counter
"topics": [t.slug for t in await followed_topics(author_id)], # followed topics slugs
"authors": [a.slug for a in await followed_authors(author_id)], # followed authors slugs
"reactions": [s.slug for s in await followed_reactions(author_id)], # fresh reacted shouts slugs
"communities": [c.slug for c in await followed_communities(author_id)], # communities
"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)],
}
@mutation.field("updateProfile")
@mutation.field("update_profile")
@login_required
async def update_profile(_, info, profile):
user_id = info.context["user_id"]
@@ -128,7 +129,7 @@ def author_unfollow(follower_id, slug):
return False
@query.field("authorsAll")
@query.field("get_authors_all")
async def get_authors_all(_, _info):
q = select(Author)
q = add_author_stat_columns(q)
@@ -137,7 +138,7 @@ async def get_authors_all(_, _info):
return get_authors_from_query(q)
@query.field("getAuthor")
@query.field("get_author")
async def get_author(_, _info, slug="", user=None, author_id=None):
if slug or user or author_id:
if slug:
@@ -152,7 +153,7 @@ async def get_author(_, _info, slug="", user=None, author_id=None):
return authors[0]
@query.field("loadAuthorsBy")
@query.field("load_authors_by")
async def load_authors_by(_, _info, by, limit, offset):
q = select(Author)
q = add_author_stat_columns(q)
@@ -175,7 +176,8 @@ async def load_authors_by(_, _info, by, limit, offset):
return get_authors_from_query(q)
async def get_followed_authors(_, _info, slug) -> List[Author]:
@query.field("get_author_followed")
async def get_author_followed(_, _info, slug="", user=None, author_id=None) -> List[Author]:
# First, we need to get the author_id for the given slug
with local_session() as session:
author_id_query = select(Author.id).where(Author.slug == slug)
@@ -187,7 +189,8 @@ async def get_followed_authors(_, _info, slug) -> List[Author]:
return await followed_authors(author_id)
async def author_followers(_, _info, slug) -> List[Author]:
@query.field("get_author_followers")
async def get_author_followers(_, _info, slug) -> List[Author]:
q = select(Author)
q = add_author_stat_columns(q)
@@ -209,28 +212,29 @@ async def followed_authors(follower_id):
return get_authors_from_query(q)
@mutation.field("rateAuthor")
@mutation.field("rate_author")
@login_required
async def rate_author(_, info, rated_user_id, value):
async def rate_author(_, info, rated_slug, value):
user_id = info.context["user_id"]
with local_session() as session:
rater = session.query(Author).filter(Author.user == user_id).first()
rating = (
session.query(AuthorRating)
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.user == rated_user_id))
.first()
)
if rating:
rating.value = value
session.add(rating)
session.commit()
return {}
else:
try:
rating = AuthorRating(rater=rater.id, user=rated_user_id, value=value)
rater = session.query(Author).filter(Author.slug == rated_slug).first()
if rater:
rating = (
session.query(AuthorRating)
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.user == rated_user_id))
.first()
)
if rating:
rating.value = value
session.add(rating)
session.commit()
except Exception as err:
return {"error": err}
return {}
else:
try:
rating = AuthorRating(rater=rater.id, user=rated_user_id, value=value)
session.add(rating)
session.commit()
except Exception as err:
return {"error": err}
return {}

View File

@@ -51,13 +51,16 @@ def get_communities_from_query(q):
return ccc
SINGLE_COMMUNITY = True
def followed_communities(follower_id):
amount = select(Community).count()
if amount < 2:
# no need to run long query most of the cases
return [
select(Community).first(),
]
if SINGLE_COMMUNITY:
with local_session() as session:
c = session.query(Community).first()
return [
c,
]
else:
q = select(Community)
q = add_community_stat_columns(q)
@@ -97,7 +100,7 @@ def community_unfollow(follower_id, slug):
return False
@query.field("communitiesAll")
@query.field("get_communities_all")
async def get_communities_all(_, _info):
q = select(Author)
q = add_community_stat_columns(q)
@@ -105,7 +108,7 @@ async def get_communities_all(_, _info):
return get_communities_from_query(q)
@query.field("getCommunity")
@query.field("get_community")
async def get_community(_, _info, slug):
q = select(Community).where(Community.slug == slug)
q = add_community_stat_columns(q)

View File

@@ -12,9 +12,9 @@ from resolvers.reaction import reactions_follow, reactions_unfollow
from services.notify import notify_shout
@query.field("loadDrafts")
@query.field("get_shouts_drafts")
@login_required
async def get_drafts(_, info):
async def get_shouts_drafts(_, info):
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
@@ -34,7 +34,7 @@ async def get_drafts(_, info):
return shouts
@mutation.field("createShout")
@mutation.field("create_shout")
@login_required
async def create_shout(_, info, inp):
user_id = info.context["user_id"]
@@ -79,7 +79,7 @@ async def create_shout(_, info, inp):
return {"shout": new_shout}
@mutation.field("updateShout")
@mutation.field("update_shout")
@login_required
async def update_shout(_, info, shout_id, shout_input=None, publish=False):
user_id = info.context["user_id"]
@@ -161,7 +161,7 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
return {"shout": shout}
@mutation.field("deleteShout")
@mutation.field("delete_shout")
@login_required
async def delete_shout(_, info, shout_id):
user_id = info.context["user_id"]

View File

@@ -1,3 +1,7 @@
from sqlalchemy import select
from orm.community import Community, CommunityAuthor as CommunityFollower
from orm.topic import Topic, TopicFollower
from services.auth import login_required
from resolvers.author import author_follow, author_unfollow
from resolvers.reaction import reactions_follow, reactions_unfollow
@@ -5,9 +9,9 @@ from resolvers.topic import topic_follow, topic_unfollow
from resolvers.community import community_follow, community_unfollow
from services.following import FollowingManager, FollowingResult
from services.db import local_session
from orm.author import Author
from orm.author import Author, AuthorFollower
from services.notify import notify_follower
from services.schema import mutation
from services.schema import mutation, query
@login_required
@@ -77,3 +81,36 @@ async def unfollow(_, info, what, slug):
return {"error": str(e)}
return {}
@query.field("get_my_followed")
@login_required
async def get_my_followed(_, info):
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
authors_query = (
select(Author)
.join(AuthorFollower, AuthorFollower.author == Author.id)
.where(AuthorFollower.follower == author.id)
)
topics_query = select(Topic).join(TopicFollower).where(TopicFollower.follower == author.id)
communities_query = (
select(Community)
.join(CommunityFollower, CommunityFollower.author == Author.id)
.where(CommunityFollower.follower == author.id)
)
topics = []
authors = []
communities = []
for [author] in session.execute(authors_query):
authors.append(author)
for [topic] in session.execute(topics_query):
topics.append(topic)
for [c] in session.execute(communities_query):
communities.append(c)
return {"topics": topics, "authors": authors, "communities": communities}

View File

@@ -17,7 +17,7 @@ def add_reaction_stat_columns(q):
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns(
func.sum(aliased_reaction.id).label("reacted_stat"),
func.sum(case((aliased_reaction.body.is_not(''), 1), else_=0)).label("commented_stat"),
func.sum(case((aliased_reaction.body.is_not(""), 1), else_=0)).label("commented_stat"),
func.sum(
case(
(aliased_reaction.kind == ReactionKind.AGREE, 1),
@@ -91,7 +91,7 @@ def is_published_author(session, author_id):
return (
session.query(Shout)
.where(Shout.authors.contains(author_id))
.filter(and_(Shout.published_at.is_not(''), Shout.deleted_at.is_(None)))
.filter(and_(Shout.published_at.is_not(""), Shout.deleted_at.is_(None)))
.count()
> 0
)
@@ -156,7 +156,7 @@ def set_hidden(session, shout_id):
session.commit()
@mutation.field("createReaction")
@mutation.field("create_reaction")
@login_required
async def create_reaction(_, info, reaction):
user_id = info.context["user_id"]
@@ -241,7 +241,7 @@ async def create_reaction(_, info, reaction):
return {"reaction": rdict}
@mutation.field("updateReaction")
@mutation.field("update_reaction")
@login_required
async def update_reaction(_, info, rid, reaction):
user_id = info.context["user_id"]
@@ -284,7 +284,7 @@ async def update_reaction(_, info, rid, reaction):
return {"error": "user"}
@mutation.field("deleteReaction")
@mutation.field("delete_reaction")
@login_required
async def delete_reaction(_, info, rid):
user_id = info.context["user_id"]
@@ -293,21 +293,24 @@ async def delete_reaction(_, info, rid):
if not r:
return {"error": "invalid reaction id"}
author = session.query(Author).filter(Author.user == user_id).first()
if not author or r.created_by != author.id:
if author:
if r.created_by != author.id:
return {"error": "access denied"}
if r.kind in [ReactionKind.LIKE, ReactionKind.DISLIKE]:
session.delete(r)
else:
r.deleted_at = int(time.time())
session.commit()
await notify_reaction(r.dict(), "delete")
return {"reaction": r}
else:
return {"error": "access denied"}
if r.kind in [ReactionKind.LIKE, ReactionKind.DISLIKE]:
session.delete(r)
else:
r.deleted_at = int(time.time())
session.commit()
await notify_reaction(r.dict(), "delete")
return {"reaction": r}
@query.field("loadReactionsBy")
@query.field("load_reactions_by")
async def load_reactions_by(_, info, by, limit=50, offset=0):
"""
:param info: graphql meta
@@ -387,29 +390,31 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
return reactions
def reacted_shouts_updates(follower_id):
shouts = []
def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
shouts: List[Shout] = []
with local_session() as session:
author = session.query(Author).where(Author.id == follower_id).first()
if author:
shouts = (
session.query(Reaction.shout)
.join(Shout)
session.query(Shout)
.join(Reaction)
.filter(Reaction.created_by == author.id)
.filter(Reaction.created_at > author.last_seen)
.all()
.limit(limit)
.offset(offset)
)
return shouts
# @query.field("followedReactions")
@login_required
@query.field("followedReactions")
async def get_reacted_shouts(_, info) -> List[Shout]:
@query.field("load_shouts_followed")
async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
shouts = reacted_shouts_updates(author.id)
shouts = reacted_shouts_updates(author.id, limit, offset)
return shouts
else:
return []

View File

@@ -70,8 +70,8 @@ def apply_filters(q, filters, author_id=None):
return q
@query.field("loadShout")
async def load_shout(_, _info, slug=None, shout_id=None):
@query.field("get_shout")
async def get_shout(_, _info, slug=None, shout_id=None):
with local_session() as session:
q = select(Shout).options(
joinedload(Shout.authors),
@@ -95,7 +95,9 @@ async def load_shout(_, _info, slug=None, shout_id=None):
commented_stat,
rating_stat,
_last_comment,
] = session.execute(q).first() or []
] = (
session.execute(q).first() or []
)
if shout:
shout.stat = {
"viewed": viewed_stat,
@@ -113,7 +115,7 @@ async def load_shout(_, _info, slug=None, shout_id=None):
return None
@query.field("loadShouts")
@query.field("load_shouts_by")
async def load_shouts_by(_, info, options):
"""
:param _:
@@ -186,8 +188,8 @@ async def load_shouts_by(_, info, options):
@login_required
@query.field("loadFeed")
async def get_my_feed(_, info, options):
@query.field("load_shouts_feed")
async def load_shouts_feed(_, info, options):
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
@@ -199,7 +201,9 @@ async def get_my_feed(_, info, options):
select(Shout.id)
.where(Shout.id == ShoutAuthor.shout)
.where(Shout.id == ShoutTopic.shout)
.where((ShoutAuthor.author.in_(author_followed_authors)) | (ShoutTopic.topic.in_(author_followed_topics)))
.where(
(ShoutAuthor.author.in_(author_followed_authors)) | (ShoutTopic.topic.in_(author_followed_topics))
)
)
q = (
@@ -247,36 +251,9 @@ async def get_my_feed(_, info, options):
return []
@query.field("search")
async def search(_, info, text, limit=50, offset=0):
@query.field("load_shouts_search")
async def load_shouts_search(_, info, text, limit=50, offset=0):
if text and len(text) > 2:
return SearchService.search(text, limit, offset)
else:
return []
@query.field("loadMySubscriptions")
@login_required
async def load_my_subscriptions(_, info):
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
authors_query = (
select(Author)
.join(AuthorFollower, AuthorFollower.author == Author.id)
.where(AuthorFollower.follower == author.id)
)
topics_query = select(Topic).join(TopicFollower).where(TopicFollower.follower == author.id)
topics = []
authors = []
for [author] in session.execute(authors_query):
authors.append(author)
for [topic] in session.execute(topics_query):
topics.append(topic)
return {"topics": topics, "authors": authors}

View File

@@ -64,32 +64,37 @@ def topics_followed_by(author_id):
return get_topics_from_query(q)
@query.field("topicsAll")
async def topics_all(_, _info):
@query.field("get_topics_all")
async def get_topics_all(_, _info):
q = select(Topic)
q = add_topic_stat_columns(q)
return get_topics_from_query(q)
@query.field("topicsByCommunity")
async def topics_by_community(_, info, community):
q = select(Topic).where(Topic.community == community)
@query.field("get_topics_by_community")
async def get_topics_by_community(_, _info, community_id: int):
q = select(Topic).where(Topic.community == community_id)
q = add_topic_stat_columns(q)
return get_topics_from_query(q)
@query.field("topicsByAuthor")
async def topics_by_author(_, _info, author_id):
@query.field("get_topics_by_author")
async def get_topics_by_author(_, _info, author_id=None, slug="", user=""):
q = select(Topic)
q = add_topic_stat_columns(q)
q = q.join(Author).where(Author.id == author_id)
if author_id:
q = q.join(Author).where(Author.id == author_id)
elif slug:
q = q.join(Author).where(Author.slug == slug)
elif user:
q = q.join(Author).where(Author.user == user)
return get_topics_from_query(q)
@query.field("getTopic")
@query.field("get_topic")
async def get_topic(_, _info, slug):
q = select(Topic).where(Topic.slug == slug)
q = add_topic_stat_columns(q)
@@ -98,7 +103,7 @@ async def get_topic(_, _info, slug):
return topics[0]
@mutation.field("createTopic")
@mutation.field("create_topic")
@login_required
async def create_topic(_, _info, inp):
with local_session() as session:
@@ -110,6 +115,7 @@ async def create_topic(_, _info, inp):
return {"topic": new_topic}
@mutation.field("update_topic")
@login_required
async def update_topic(_, _info, inp):
slug = inp["slug"]
@@ -125,6 +131,27 @@ async def update_topic(_, _info, inp):
return {"topic": topic}
@mutation.field("delete_topic")
@login_required
async def delete_topic(_, info, slug: str):
user_id = info.context["user_id"]
with local_session() as session:
t: Topic = session.query(Topic).filter(Topic.slug == slug).first()
if not t:
return {"error": "invalid topic slug"}
author = session.query(Author).filter(Author.user == user_id).first()
if author:
if t.created_by != author.id:
return {"error": "access denied"}
session.delete(t)
session.commit()
return {}
else:
return {"error": "access denied"}
def topic_follow(follower_id, slug):
try:
with local_session() as session:
@@ -153,8 +180,8 @@ def topic_unfollow(follower_id, slug):
return False
@query.field("topicsRandom")
async def topics_random(_, info, amount=12):
@query.field("get_topics_random")
async def get_topics_random(_, info, amount=12):
q = select(Topic)
q = q.join(ShoutTopic)
q = q.group_by(Topic.id)