reviewed resolvers

This commit is contained in:
tonyrewin 2022-09-22 13:31:44 +03:00
parent c292d7da55
commit 64914028fb
8 changed files with 77 additions and 68 deletions

View File

@ -75,7 +75,7 @@ async def get_user_collections(_, _info, userslug):
collections = (
session.query(Collection)
.where(
and_(Collection.createdBy == userslug, bool(Collection.publishedAt))
and_(Collection.createdBy == userslug, Collection.publishedAt.is_not(None))
)
.all()
)

View File

@ -3,9 +3,9 @@ from datetime import datetime
from auth.authenticate import login_required
from base.orm import local_session
from base.resolvers import mutation
from orm import Shout
from orm.rbac import Resource
from orm.shout import ShoutAuthor, ShoutTopic
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import TopicFollower
from orm.user import User
from resolvers.reactions import reactions_follow, reactions_unfollow
from services.zine.gittask import GitTask
@ -20,8 +20,12 @@ async def create_shout(_, info, inp):
if topic_slugs:
del inp["topic_slugs"]
with local_session() as session:
new_shout = Shout.create(**inp)
ShoutAuthor.create(shout=new_shout.slug, user=user.slug)
# NOTE: shout made by one first author
sa = ShoutAuthor.create(shout=new_shout.slug, user=user.slug)
session.add(sa)
reactions_follow(user, new_shout.slug, True)
@ -29,13 +33,20 @@ async def create_shout(_, info, inp):
topic_slugs.append(inp["mainTopic"])
for slug in topic_slugs:
ShoutTopic.create(shout=new_shout.slug, topic=slug)
st = ShoutTopic.create(shout=new_shout.slug, topic=slug)
session.add(st)
tf = session.query(TopicFollower).where(follower=user.slug, topic=slug)
if not tf:
tf = TopicFollower.create(follower=user.slug, topic=slug, auto=True)
session.add(tf)
new_shout.topic_slugs = topic_slugs
session.add(new_shout)
session.commit()
GitTask(inp, user.username, user.email, "new shout %s" % (new_shout.slug))
# await ShoutCommentsStorage.send_shout(new_shout)
return {"shout": new_shout}
@ -44,13 +55,11 @@ async def create_shout(_, info, inp):
async def update_shout(_, info, inp):
auth = info.context["request"].auth
user_id = auth.user_id
slug = inp["slug"]
session = local_session()
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
shout = session.query(Shout).filter(Shout.slug == slug).first()
if not shout:
return {"error": "shout not found"}
@ -60,15 +69,14 @@ async def update_shout(_, info, inp):
print(scopes)
if Resource.shout_id not in scopes:
return {"error": "access denied"}
else:
shout.update(inp)
shout.updatedAt = datetime.now()
session.add(shout)
session.commit()
session.close()
for topic in inp.get("topic_slugs", []):
ShoutTopic.create(shout=slug, topic=topic)
st = ShoutTopic.create(shout=slug, topic=topic)
session.add(st)
session.commit()
GitTask(inp, user.username, user.email, "update shout %s" % (slug))

View File

@ -35,7 +35,7 @@ async def get_user_feed(_, info, offset, limit) -> List[Shout]:
return shouts
@query.field("myCandidates")
@query.field("recentCandidates")
@login_required
async def user_unpublished_shouts(_, info, offset, limit) -> List[Shout]:
user = info.context["request"].user
@ -43,7 +43,7 @@ async def user_unpublished_shouts(_, info, offset, limit) -> List[Shout]:
shouts = prepare_shouts(
session.query(Shout)
.join(ShoutAuthor)
.where(and_(not bool(Shout.publishedAt), ShoutAuthor.user == user.slug))
.where(and_(Shout.publishedAt.is_(None), ShoutAuthor.user == user.slug))
.order_by(desc(Shout.createdAt))
.limit(limit)
.offset(offset)

View File

@ -1,6 +1,6 @@
from datetime import datetime
from sqlalchemy import desc
from sqlalchemy import desc, and_
from auth.authenticate import login_required
from base.orm import local_session
@ -22,14 +22,14 @@ async def get_reaction_stat(reaction_id):
}
def reactions_follow(user, slug, auto=False):
def reactions_follow(user: User, slug: str, auto=False):
with local_session() as session:
following = (
session.query(ShoutReactionsFollower)
.filter(
.where(and_(
ShoutReactionsFollower.follower == user.slug,
ShoutReactionsFollower.shout == slug
)
))
.first()
)
if not following:
@ -46,10 +46,10 @@ def reactions_unfollow(user, slug):
with local_session() as session:
following = (
session.query(ShoutReactionsFollower)
.filter(
.where(and_(
ShoutReactionsFollower.follower == user.slug,
ShoutReactionsFollower.shout == slug,
)
ShoutReactionsFollower.shout == slug
))
.first()
)
if following:
@ -83,7 +83,7 @@ async def update_reaction(_, info, inp):
user_id = auth.user_id
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
user = session.query(User).where(User.id == user_id).first()
reaction = session.query(Reaction).filter(Reaction.id == inp.id).first()
if not reaction:
return {"error": "invalid reaction id"}
@ -109,7 +109,7 @@ async def delete_reaction(_, info, rid):
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()
user = session.query(User).where(User.id == user_id).first()
reaction = session.query(Reaction).filter(Reaction.id == rid).first()
if not reaction:
return {"error": "invalid reaction id"}
@ -145,7 +145,7 @@ async def get_reactions_for_shouts(_, info, shouts, offset, limit):
reactions += (
session.query(Reaction)
.filter(Reaction.shout == slug)
.where(not bool(Reaction.deletedAt))
.where(Reaction.deletedAt.is_not(None))
.order_by(desc("createdAt"))
.offset(offset)
.limit(limit)
@ -163,7 +163,7 @@ async def get_reactions_by_author(_, info, slug, limit=50, offset=0):
with local_session() as session:
reactions = (
session.query(Reaction)
.filter(Reaction.createdBy == slug)
.where(Reaction.createdBy == slug)
.limit(limit)
.offset(offset)
)

View File

@ -56,7 +56,11 @@ async def topics_by_author(_, _info, author):
@mutation.field("createTopic")
@login_required
async def create_topic(_, _info, inp):
with local_session() as session:
# TODO: check user permissions to create topic for exact community
new_topic = Topic.create(**inp)
session.add(new_topic)
session.commit()
await TopicStorage.update_topic(new_topic)
return {"topic": new_topic}

View File

@ -97,14 +97,14 @@ async def get_shout_by_slug(_, info, slug):
@query.field("searchQuery")
async def get_search_results(_, _info, query, offset, limit):
async def get_search_results(_, _info, searchtext, offset, limit):
# TODO: remove the copy of searchByTopics
# with search ranking query
with local_session() as session:
shouts = (
session.query(Shout)
.join(ShoutTopic)
.where(and_(ShoutTopic.topic.in_(query), bool(Shout.publishedAt)))
.where(and_(ShoutTopic.topic.in_(searchtext), Shout.publishedAt.is_not(None)))
.order_by(desc(Shout.publishedAt))
.limit(limit)
.offset(offset)
@ -123,7 +123,7 @@ async def shouts_by_topics(_, _info, slugs, offset, limit):
shouts = (
session.query(Shout)
.join(ShoutTopic)
.where(and_(ShoutTopic.topic.in_(slugs), bool(Shout.publishedAt)))
.where(and_(ShoutTopic.topic.in_(slugs), Shout.publishedAt.is_not(None)))
.order_by(desc(Shout.publishedAt))
.limit(limit)
.offset(offset)
@ -141,8 +141,8 @@ async def shouts_by_collection(_, _info, collection, offset, limit):
shouts = (
session.query(Shout)
.join(ShoutCollection, ShoutCollection.collection == collection)
.where(and_(ShoutCollection.shout == Shout.slug, bool(Shout.publishedAt)))
.order_by(desc(Shout.publishedAt))
.where(and_(ShoutCollection.shout == Shout.slug, Shout.publishedAt.is_not(None)))
.order_by(desc("publishedAt"))
.limit(limit)
.offset(offset)
)
@ -158,7 +158,7 @@ async def shouts_by_authors(_, _info, slugs, offset, limit):
shouts = (
session.query(Shout)
.join(ShoutAuthor)
.where(and_(ShoutAuthor.user.in_(slugs), bool(Shout.publishedAt)))
.where(and_(ShoutAuthor.user.in_(slugs), Shout.publishedAt.is_not(None)))
.order_by(desc(Shout.publishedAt))
.limit(limit)
.offset(offset)
@ -186,7 +186,7 @@ async def shouts_by_communities(_, info, slugs, offset, limit):
.join(ShoutTopic)
.where(
and_(
bool(Shout.publishedAt),
Shout.publishedAt.is_not(None),
ShoutTopic.topic.in_(
select(Topic.slug).where(Topic.community.in_(slugs))
),

View File

@ -228,7 +228,6 @@ type Query {
shoutsByTopics(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
shoutsByAuthors(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
shoutsByCommunities(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
myCandidates(offset: Int!, limit: Int!): [Shout]! # test
# topReacted(offset: Int!, limit: Int!): [Shout]!
topAuthors(offset: Int!, limit: Int!): [Author]! # by User.rating
topPublished(daysago: Int!, offset: Int!, limit: Int!): [Shout]!
@ -239,6 +238,7 @@ type Query {
recentReacted(offset: Int!, limit: Int!): [Shout]! # TODO: use in design!
recentCommented(offset: Int!, limit: Int!): [Shout]!
recentAll(offset: Int!, limit: Int!): [Shout]!
recentCandidates(offset: Int!, limit: Int!): [Shout]!
# reactons
reactionsByAuthor(slug: String!, offset: Int!, limit: Int!): [Reaction]!

View File

@ -123,7 +123,7 @@ class ShoutsCache:
reactions = session.query(Reaction).order_by(Reaction.createdAt).limit(ShoutsCache.limit)
commented_slugs = set([])
for r in reactions:
if bool(r.body):
if len(r.body) > 0:
commented_slugs.add(r.shout)
shouts = await prepare_shouts(
session,
@ -156,18 +156,18 @@ class ShoutsCache:
(
select(
Shout,
func.sum(int(bool(Reaction.kind == ReactionKind.LIKE))).label('liked')
func.sum(Reaction.id).label('reacted')
)
.options(
selectinload(Shout.authors),
selectinload(Shout.topics),
selectinload(Shout.reactions),
)
.join(Reaction)
.join(Reaction, Reaction.kind == ReactionKind.LIKE)
.where(Shout.deletedAt.is_(None))
.filter(Shout.publishedAt.is_not(None))
.group_by(Shout.slug)
.order_by(desc("liked"))
.order_by(desc("reacted"))
.limit(ShoutsCache.limit)
),
)
@ -183,10 +183,7 @@ class ShoutsCache:
shouts = await prepare_shouts(
session,
(
select(
Shout,
func.sum(int(bool(Reaction.kind == ReactionKind.LIKE))).label('liked')
)
select(Shout)
.options(
selectinload(Shout.authors),
selectinload(Shout.topics),
@ -196,7 +193,6 @@ class ShoutsCache:
.where(Shout.deletedAt.is_(None))
.filter(Shout.publishedAt > month_ago)
.group_by(Shout.slug)
.order_by(desc("liked"))
.limit(ShoutsCache.limit)
),
)
@ -214,14 +210,14 @@ class ShoutsCache:
(
select(
Shout,
func.sum(int(bool(Reaction.body))).label("commented")
func.sum(Reaction.id).label("commented")
)
.options(
selectinload(Shout.authors),
selectinload(Shout.topics),
selectinload(Shout.reactions)
)
.join(Reaction)
.join(Reaction, func.length(Reaction.body) > 0)
.where(Shout.deletedAt.is_(None))
.filter(Shout.publishedAt > month_ago)
.group_by(Shout.slug)
@ -255,6 +251,7 @@ class ShoutsCache:
for s in ShoutsCache.recent_published:
if s.publishedAt >= before:
shouts_by_rating.append(s)
shouts_by_rating.sort(lambda s: s.stat["rating"], reverse=True)
return shouts_by_rating
@staticmethod