import { COMMENT_TYPE , USER_STATE } from 'common/enums' ; import { AuthenticationError , ForbiddenByStateError , ForbiddenError , } from 'common/errors' ; import { fromGlobalId } from 'common/utils' ; import { MutationToUnvoteCommentResolver } from 'definitions' ; const resolver = async ( _ , { input : { id } } , { viewer , dataSources : { atomService , articleService , paymentService , commentService , } , knex , } ) => { if ( ! viewer . id ) { throw new AuthenticationError ( 'visitor has no permission' ) ; } const { id : dbId } = fromGlobalId ( id ) ; const comment = await commentService . dataloader . load ( dbId ) ; let article ; let circle ; let targetAuthor ; if ( comment . type === COMMENT_TYPE . article ) { article = await articleService . dataloader . load ( comment . targetId ) ; targetAuthor = article . authorId ; } else { circle = await atomService . circleIdLoader . load ( comment . targetId ) ; targetAuthor = circle . owner ; } const isTargetAuthor = targetAuthor === viewer . id ; const isOnboarding = viewer . state === USER_STATE . onboarding ; const isInactive = [ USER_STATE . banned , USER_STATE . archived , USER_STATE . frozen , ] . includes ( viewer . state ) ; if ( ( isOnboarding && ! isTargetAuthor ) || isInactive ) { throw new ForbiddenByStateError ( `${ viewer . state } user has no permission` ) ; } if ( circle && ! isTargetAuthor ) { const isCircleMember = await paymentService . isCircleMember ( { userId : viewer . id , circleId : circle . id , } ) ; if ( ! isCircleMember ) { throw new ForbiddenError ( 'only circle members have the permission' ) ; } } await commentService . unvote ( { commentId : dbId , userId : viewer . id } ) ; return comment ; } ; export default resolver ;