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

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

View File

@ -1,5 +1,7 @@
[0.2.16]
- schema: Reaction.range -> Reaction.quote
- resolvers: queries and mutations revision and renaming
- resolvers: delete_topic(slug) implemented
[0.2.15]
- schema: Shout.created_by removed

View File

@ -29,7 +29,7 @@ class ReactionKind(Enumeration):
class Reaction(Base):
__tablename__ = "reaction"
body = Column(String, default='', comment="Reaction Body")
body = Column(String, default="", comment="Reaction Body")
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
created_by = Column(ForeignKey("author.id"), nullable=False, index=True)
updated_at = Column(Integer, nullable=True, comment="Updated at")

View File

@ -83,4 +83,3 @@ class Shout(Base):
lang = Column(String, nullable=False, default="ru", comment="Language")
version_of = Column(ForeignKey("shout.id"), nullable=True)
oid = Column(String, nullable=True)

View File

@ -18,6 +18,9 @@ gql = {git = "https://github.com/graphql-python/gql.git", rev = "master"}
starlette = {git = "https://github.com/encode/starlette.git", rev = "master"}
ariadne = {git = "https://github.com/tonyrewin/ariadne.git", rev = "master"}
[tool.poetry.group.dev.dependencies]
setuptools = "^69.0.2"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

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,13 +212,14 @@ 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()
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))

View File

@ -51,12 +51,15 @@ 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
if SINGLE_COMMUNITY:
with local_session() as session:
c = session.query(Community).first()
return [
select(Community).first(),
c,
]
else:
q = select(Community)
@ -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,7 +293,8 @@ 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]:
@ -305,9 +306,11 @@ async def delete_reaction(_, info, rid):
await notify_reaction(r.dict(), "delete")
return {"reaction": r}
else:
return {"error": "access denied"}
@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)
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)

View File

@ -291,50 +291,68 @@ type Result {
# Мутации
type Mutation {
createShout(inp: ShoutInput!): Result!
updateShout(shout_id: Int!, shout_input: ShoutInput, publish: Boolean): Result!
deleteShout(shout_id: Int!): Result!
rateAuthor(slug: String!, value: Int!): Result!
updateOnlineStatus: Result!
updateProfile(profile: ProfileInput!): Result!
createTopic(input: TopicInput!): Result!
updateTopic(input: TopicInput!): Result!
destroyTopic(slug: String!): Result!
createReaction(reaction: ReactionInput!): Result!
updateReaction(id: Int!, reaction: ReactionInput!): Result!
deleteReaction(id: Int!): Result!
# author
rate_author(rated_slug: String!, value: Int!): Result!
update_profile(profile: ProfileInput!): Result!
# editor
create_shout(inp: ShoutInput!): Result!
update_shout(shout_id: Int!, shout_input: ShoutInput, publish: Boolean): Result!
delete_shout(shout_id: Int!): Result!
# follower
follow(what: FollowingEntity!, slug: String!): Result!
unfollow(what: FollowingEntity!, slug: String!): Result!
# FIXME!
updateOnlineStatus: Result!
# topic
create_topic(input: TopicInput!): Result!
update_topic(input: TopicInput!): Result!
delete_topic(slug: String!): Result!
# reaction
create_reaction(reaction: ReactionInput!): Result!
update_reaction(id: Int!, reaction: ReactionInput!): Result!
delete_reaction(id: Int!): Result!
}
# Запросы
type Query {
loadShout(slug: String, shout_id: Int): Shout
loadShouts(options: LoadShoutsOptions): [Shout]
loadFeed(options: LoadShoutsOptions): [Shout]
loadMySubscriptions: Result
loadDrafts: [Shout]
# author
get_author(slug: String, user: String, author_id: Int): Author
get_authors_all: [Author]
get_author_followers(slug: String, user: String, author_id: Int): [Author]
get_author_followed(slug: String, user: String, author_id: Int): [Author]
load_authors_by(by: AuthorsBy, limit: Int, offset: Int): [Author]
search(text: String!, limit: Int, offset: Int): [Shout]
# community
get_community: Community
get_communities_all: [Community]
loadReactionsBy(by: ReactionBy!, limit: Int, offset: Int): [Reaction]
followedReactions(follower_id: Int!): [Shout]
# editor
get_shouts_drafts: [Shout]
authorsAll: [Author]
authorFollowers(slug: String!): [Author]
authorFollowedAuthors(slug: String!): [Author]
authorFollowedTopics(slug: String!): [Topic]
loadAuthorsBy(by: AuthorsBy, limit: Int, offset: Int): [Author]
getAuthor(slug: String, user: String, author: Int): Author
# follower
get_my_followed: Result # { authors topics communities }
topicsAll: [Topic]
getTopic(slug: String!): Topic
topicsRandom(amount: Int): [Topic]
topicsByCommunity(community: String!): [Topic]
topicsByAuthor(author_id: Int!): [Topic]
# reaction
load_reactions_by(by: ReactionBy!, limit: Int, offset: Int): [Reaction]
communitiesAll: [Community]
getCommunity: Community
# reader
get_shout(slug: String, shout_id: Int): Shout
load_shouts_followed(follower_id: Int!, limit: Int, offset: Int): [Shout]
load_shouts_by(options: LoadShoutsOptions): [Shout]
load_shouts_search(text: String!, limit: Int, offset: Int): [Shout]
load_shouts_feed(options: LoadShoutsOptions): [Shout]
# topic
get_topic(slug: String!): Topic
get_topics_all: [Topic]
get_topics_random(amount: Int): [Topic]
get_topics_by_author(slug: String, user: String, author_id: Int): [Topic]
get_topics_by_community(slug: String, community_id: Int): [Topic]
}

View File

@ -57,11 +57,6 @@ if __name__ == "__main__":
sys.excepthook = exception_handler
if "dev" in sys.argv:
import os
os.environ.set("MODE", "development")
uvicorn.run(
"main:app",
host="0.0.0.0",
port=PORT,
proxy_headers=True,
server_header=True
)
uvicorn.run("main:app", host="0.0.0.0", port=PORT, proxy_headers=True, server_header=True)

View File

@ -18,12 +18,7 @@ class Following:
class FollowingManager:
lock = asyncio.Lock()
data = {
'author': [],
'topic': [],
'shout': [],
'community': []
}
data = {"author": [], "topic": [], "shout": [], "community": []}
@staticmethod
async def register(kind, uid):
@ -40,7 +35,7 @@ class FollowingManager:
try:
async with FollowingManager.lock:
for entity in FollowingManager[kind]:
if payload.shout['created_by'] == entity.uid:
if payload.shout["created_by"] == entity.uid:
entity.queue.put_nowait(payload)
except Exception as e:
print(Exception(e))