get-my-shout-resolver
This commit is contained in:
parent
5d8c46e76c
commit
b09ea39668
|
@ -19,10 +19,34 @@ from services.search import search_service
|
|||
from services.logger import root_logger as logger
|
||||
|
||||
|
||||
@query.field('get_my_shout')
|
||||
@login_required
|
||||
async def get_my_shout(_, info, shout_id: int):
|
||||
user_id = info.context.get('user_id')
|
||||
shout = None
|
||||
error = None
|
||||
with local_session() as session:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
shout = session.query(Shout).filter(Shout.id == shout_id).first()
|
||||
if shout and author:
|
||||
if not shout.published_at:
|
||||
user_id = info.context.get('user_id', '')
|
||||
roles = info.context.get('roles', [])
|
||||
if not user_id:
|
||||
error = 'user is not logged in'
|
||||
elif shout.created_by != author.id:
|
||||
error = 'author cannot edit this post'
|
||||
elif 'editor' not in roles:
|
||||
error = 'user has no editor role'
|
||||
elif not any([x.id == author.id for x in shout.authors]):
|
||||
error = 'author have no permissions to read this not published shout'
|
||||
return {"error": error, "shout": shout}
|
||||
|
||||
|
||||
@query.field('get_shouts_drafts')
|
||||
@login_required
|
||||
async def get_shouts_drafts(_, info):
|
||||
user_id = info.context['user_id']
|
||||
user_id = info.context.get('user_id')
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
from sqlalchemy import bindparam, distinct, or_, text
|
||||
from sqlalchemy.orm import aliased, joinedload, selectinload
|
||||
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from orm.author import Author, AuthorFollower
|
||||
from orm.reaction import Reaction, ReactionKind
|
||||
|
@ -42,19 +41,12 @@ def apply_filters(q, filters, author_id=None):
|
|||
|
||||
|
||||
@query.field('get_shout')
|
||||
@login_required
|
||||
async def get_shout(_, info, slug=None, shout_id=None):
|
||||
async def get_shout(_, info, slug: str):
|
||||
with local_session() as session:
|
||||
q = select(Shout).options(joinedload(Shout.authors), joinedload(Shout.topics))
|
||||
aliased_reaction = aliased(Reaction)
|
||||
q = add_reaction_stat_columns(q, aliased_reaction)
|
||||
|
||||
if slug is not None:
|
||||
q = q.filter(Shout.slug == slug)
|
||||
|
||||
if shout_id is not None:
|
||||
q = q.filter(Shout.id == shout_id)
|
||||
|
||||
q = q.filter(Shout.deleted_at.is_(None)).group_by(Shout.id)
|
||||
|
||||
results = session.execute(q).first()
|
||||
|
@ -68,37 +60,6 @@ async def get_shout(_, info, slug=None, shout_id=None):
|
|||
_last_comment,
|
||||
] = results
|
||||
|
||||
if not shout.published_at:
|
||||
user_id = info.context.get('user_id', '')
|
||||
if not user_id:
|
||||
logger.warn('user is not logged in')
|
||||
logger.debug(info)
|
||||
raise HTTPException(
|
||||
status_code=401, detail='shout is not published yet'
|
||||
)
|
||||
roles = info.context.get('roles', [])
|
||||
logger.debug(f'{user_id} is getting shout which is not published yet')
|
||||
logger.debug(f'roles: {roles}')
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
logger.debug(author)
|
||||
if not author:
|
||||
logger.warn('author is not found')
|
||||
raise HTTPException(
|
||||
status_code=401, detail='shout is not published yet'
|
||||
)
|
||||
|
||||
author_id = author.id if author else None
|
||||
if (
|
||||
author_id is not None
|
||||
and shout.created_by != author_id
|
||||
and not any(x == author_id for x in [a.id for a in shout.authors])
|
||||
and 'editor' not in roles
|
||||
):
|
||||
logger.warn('author have no permissions to read this not published shout')
|
||||
raise HTTPException(
|
||||
status_code=401, detail='shout is not published yet'
|
||||
)
|
||||
|
||||
shout.stat = {
|
||||
'viewed': await ViewedStorage.get_shout(shout.slug),
|
||||
'reacted': reacted_stat,
|
||||
|
|
|
@ -30,6 +30,9 @@ type Query {
|
|||
load_shouts_unrated(limit: Int, offset: Int): [Shout]
|
||||
load_shouts_random_top(options: LoadShoutsOptions): [Shout]
|
||||
load_shouts_random_topic(limit: Int!): CommonResult! # { topic shouts }
|
||||
|
||||
# editor
|
||||
get_my_shout(shout_id: Int!): CommonResult
|
||||
get_shouts_drafts: [Shout]
|
||||
|
||||
# topic
|
||||
|
|
Loading…
Reference in New Issue
Block a user