@@ -12,7 +12,7 @@ from orm.topic import Topic
|
||||
from orm.author import AuthorFollower, Author, AuthorRating
|
||||
from community import followed_communities
|
||||
from topic import followed_topics
|
||||
from reaction import load_followed_reactions
|
||||
from reaction import reacted_shouts_updates as followed_reactions
|
||||
|
||||
|
||||
def add_author_stat_columns(q):
|
||||
@@ -28,16 +28,13 @@ def add_author_stat_columns(q):
|
||||
func.count(distinct(followers_table.follower)).label("followers_stat")
|
||||
)
|
||||
|
||||
q = q.outerjoin(
|
||||
followings_table, followings_table.follower == Author.id
|
||||
).add_columns(
|
||||
q = q.outerjoin(followings_table, followings_table.follower == Author.id).add_columns(
|
||||
func.count(distinct(followings_table.author)).label("followings_stat")
|
||||
)
|
||||
|
||||
q = q.add_columns(literal(0).label("rating_stat"))
|
||||
# FIXME
|
||||
# q = q.outerjoin(author_rating_aliased, author_rating_aliased.user == Author.id).add_columns(
|
||||
# # TODO: check
|
||||
# func.sum(author_rating_aliased.value).label('rating_stat')
|
||||
# )
|
||||
|
||||
@@ -83,16 +80,10 @@ 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": await load_followed_reactions(author_id),
|
||||
"communities": [
|
||||
c.slug for c in await followed_communities(author_id)
|
||||
], # communities
|
||||
"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
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +93,8 @@ async def update_profile(_, info, profile):
|
||||
author_id = info.context["author_id"]
|
||||
with local_session() as session:
|
||||
author = session.query(Author).where(Author.id == author_id).first()
|
||||
author.update(profile)
|
||||
Author.update(author, profile)
|
||||
session.add(author)
|
||||
session.commit()
|
||||
return {"error": None, "author": author}
|
||||
|
||||
@@ -112,7 +104,7 @@ def author_follow(follower_id, slug):
|
||||
try:
|
||||
with local_session() as session:
|
||||
author = session.query(Author).where(Author.slug == slug).one()
|
||||
af = AuthorFollower.create(follower=follower_id, author=author.id)
|
||||
af = AuthorFollower(follower=follower_id, author=author.id)
|
||||
session.add(af)
|
||||
session.commit()
|
||||
return True
|
||||
@@ -163,13 +155,8 @@ async def load_authors_by(_, _info, by, limit, offset):
|
||||
elif by.get("name"):
|
||||
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
|
||||
elif by.get("topic"):
|
||||
q = (
|
||||
q.join(ShoutAuthor)
|
||||
.join(ShoutTopic)
|
||||
.join(Topic)
|
||||
.where(Topic.slug == by["topic"])
|
||||
)
|
||||
|
||||
q = q.join(ShoutAuthor).join(ShoutTopic).join(Topic).where(Topic.slug == by["topic"])
|
||||
|
||||
if by.get("last_seen"): # in unixtime
|
||||
before = int(time.time()) - by["last_seen"]
|
||||
q = q.filter(Author.last_seen > before)
|
||||
@@ -211,9 +198,7 @@ async def author_followers(_, _info, slug) -> List[Author]:
|
||||
async def followed_authors(follower_id):
|
||||
q = select(Author)
|
||||
q = add_author_stat_columns(q)
|
||||
q = q.join(AuthorFollower, AuthorFollower.author == Author.id).where(
|
||||
AuthorFollower.follower == follower_id
|
||||
)
|
||||
q = q.join(AuthorFollower, AuthorFollower.author == Author.id).where(AuthorFollower.follower == follower_id)
|
||||
# Pass the query to the get_authors_from_query function and return the results
|
||||
return get_authors_from_query(q)
|
||||
|
||||
@@ -226,20 +211,19 @@ async def rate_author(_, info, rated_user_id, value):
|
||||
with local_session() as session:
|
||||
rating = (
|
||||
session.query(AuthorRating)
|
||||
.filter(
|
||||
and_(
|
||||
AuthorRating.rater == author_id,
|
||||
AuthorRating.user == rated_user_id
|
||||
)
|
||||
)
|
||||
.filter(and_(AuthorRating.rater == author_id, AuthorRating.user == rated_user_id))
|
||||
.first()
|
||||
)
|
||||
if rating:
|
||||
rating.value = value
|
||||
session.add(rating)
|
||||
session.commit()
|
||||
return {}
|
||||
try:
|
||||
AuthorRating.create(rater=author_id, user=rated_user_id, value=value)
|
||||
except Exception as err:
|
||||
return {"error": err}
|
||||
else:
|
||||
try:
|
||||
rating = AuthorRating(rater=author_id, user=rated_user_id, value=value)
|
||||
session.add(rating)
|
||||
session.commit()
|
||||
except Exception as err:
|
||||
return {"error": err}
|
||||
return {}
|
||||
|
Reference in New Issue
Block a user