This commit is contained in:
70
resolvers/bookmark.py
Normal file
70
resolvers/bookmark.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from graphql import GraphQLError
|
||||
from sqlalchemy import delete, insert
|
||||
|
||||
from orm.author import AuthorBookmark
|
||||
from orm.shout import Shout
|
||||
from services.common_result import CommonResult
|
||||
from services.db import local_session
|
||||
from services.schema import mutation, query
|
||||
|
||||
|
||||
@query.field("load_shouts_bookmarked")
|
||||
def load_shouts_bookmarked(_, info, limit=50, offset=0):
|
||||
"""
|
||||
Load bookmarked shouts for the authenticated user.
|
||||
|
||||
Args:
|
||||
limit (int): Maximum number of shouts to return.
|
||||
offset (int): Number of shouts to skip.
|
||||
|
||||
Returns:
|
||||
list: List of bookmarked shouts.
|
||||
"""
|
||||
author_dict = info.context.get("author", {})
|
||||
author_id = author_dict.get("id")
|
||||
if not author_id:
|
||||
raise GraphQLError("User not authenticated")
|
||||
result = []
|
||||
with local_session() as db:
|
||||
result = db.query(AuthorBookmark).where(AuthorBookmark.author == author_id).offset(offset).limit(limit).all()
|
||||
return result
|
||||
|
||||
|
||||
@mutation.field("toggle_bookmark_shout")
|
||||
def toggle_bookmark_shout(_, info, slug: str) -> CommonResult:
|
||||
"""
|
||||
Toggle bookmark status for a specific shout.
|
||||
|
||||
Args:
|
||||
slug (str): Unique identifier of the shout.
|
||||
|
||||
Returns:
|
||||
CommonResult: Result of the operation with bookmark status.
|
||||
"""
|
||||
author_dict = info.context.get("author", {})
|
||||
author_id = author_dict.get("id")
|
||||
if not author_id:
|
||||
raise GraphQLError("User not authenticated")
|
||||
|
||||
with local_session() as db:
|
||||
shout = db.query(Shout).filter(Shout.slug == slug).first()
|
||||
if not shout:
|
||||
raise GraphQLError("Shout not found")
|
||||
|
||||
existing_bookmark = (
|
||||
db.query(AuthorBookmark)
|
||||
.filter(AuthorBookmark.author == author_id, AuthorBookmark.shout == shout.id)
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing_bookmark:
|
||||
db.execute(
|
||||
delete(AuthorBookmark).where(AuthorBookmark.author == author_id, AuthorBookmark.shout == shout.id)
|
||||
)
|
||||
result = False
|
||||
else:
|
||||
db.execute(insert(AuthorBookmark).values(author=author_id, shout=shout.id))
|
||||
result = True
|
||||
|
||||
db.commit()
|
||||
return result
|
@@ -1,36 +1,31 @@
|
||||
from sqlalchemy import select
|
||||
|
||||
from orm.author import Author
|
||||
from orm.community import Community
|
||||
from orm.community import Community, CommunityFollower
|
||||
from services.db import local_session
|
||||
from services.schema import query
|
||||
|
||||
|
||||
def get_communities_from_query(q):
|
||||
ccc = []
|
||||
with local_session() as session:
|
||||
for [c, shouts_stat, followers_stat] in session.execute(q):
|
||||
c.stat = {
|
||||
"shouts": shouts_stat,
|
||||
"followers": followers_stat,
|
||||
# "authors": session.execute(select(func.count(distinct(ShoutCommunity.shout))).filter(ShoutCommunity.community == c.id)),
|
||||
# "commented": commented_stat,
|
||||
}
|
||||
ccc.append(c)
|
||||
|
||||
return ccc
|
||||
|
||||
|
||||
@query.field("get_communities_all")
|
||||
async def get_communities_all(_, _info):
|
||||
q = select(Author)
|
||||
|
||||
return get_communities_from_query(q)
|
||||
return local_session().query(Community).all()
|
||||
|
||||
|
||||
@query.field("get_community")
|
||||
async def get_community(_, _info, slug: str):
|
||||
q = select(Community).where(Community.slug == slug)
|
||||
q = local_session().query(Community).where(Community.slug == slug)
|
||||
return q.first()
|
||||
|
||||
communities = get_communities_from_query(q)
|
||||
return communities[0]
|
||||
|
||||
@query.field("get_communities_by_author")
|
||||
async def get_communities_by_author(_, _info, slug="", user="", author_id=0):
|
||||
with local_session() as session:
|
||||
q = session.query(Community).join(CommunityFollower)
|
||||
if slug:
|
||||
author_id = session.query(Author).where(Author.slug == slug).first().id
|
||||
q = q.where(CommunityFollower.author == author_id)
|
||||
if user:
|
||||
author_id = session.query(Author).where(Author.user == user).first().id
|
||||
q = q.where(CommunityFollower.author == author_id)
|
||||
if author_id:
|
||||
q = q.where(CommunityFollower.author == author_id)
|
||||
return q.all()
|
||||
return []
|
||||
|
@@ -430,11 +430,12 @@ def apply_reaction_filters(by, q):
|
||||
if isinstance(topic, int):
|
||||
q = q.filter(Shout.topics.any(id=topic))
|
||||
|
||||
if by.get("comment"):
|
||||
q = q.filter(Reaction.kind == ReactionKind.COMMENT.value)
|
||||
kinds = by.get("kinds")
|
||||
if isinstance(kinds, list):
|
||||
q = q.filter(Reaction.kind.in_(kinds))
|
||||
|
||||
if by.get("rating"):
|
||||
q = q.filter(Reaction.kind.in_(RATING_REACTIONS))
|
||||
if by.get("reply_to"):
|
||||
q = q.filter(Reaction.reply_to == by.get("reply_to"))
|
||||
|
||||
by_search = by.get("search", "")
|
||||
if len(by_search) > 2:
|
||||
|
Reference in New Issue
Block a user