diff --git a/core/server/api/canary/utils/serializers/output/utils/post-gating.js b/core/server/api/canary/utils/serializers/output/utils/post-gating.js index 4ffb68d6da..465cc22b23 100644 --- a/core/server/api/canary/utils/serializers/output/utils/post-gating.js +++ b/core/server/api/canary/utils/serializers/output/utils/post-gating.js @@ -1,37 +1,11 @@ -const _ = require('lodash'); +const membersService = require('../../../../../../services/members'); const labs = require('../../../../../../services/labs'); -// Checks if request should hide members only content -function hideMembersOnlyContent({visibility}, frame) { - const PERMIT_CONTENT = false; - const BLOCK_CONTENT = true; - - if (visibility === 'public') { - return PERMIT_CONTENT; - } - - const requestFromMember = frame.original.context.member; - - if (!requestFromMember) { - return BLOCK_CONTENT; - } else if (visibility === 'members') { - return PERMIT_CONTENT; - } - - const memberHasPlan = !!(_.get(frame, 'original.context.member.stripe.subscriptions', [])).length; - - if (visibility === 'paid' && memberHasPlan) { - return PERMIT_CONTENT; - } - - return BLOCK_CONTENT; -} - const forPost = (attrs, frame) => { if (labs.isSet('members')) { - const hideFormatsData = hideMembersOnlyContent(attrs, frame); + const memberHasAccess = membersService.contentGating.checkPostAccess(attrs, frame.original.context.member); - if (hideFormatsData) { + if (!memberHasAccess) { ['plaintext', 'html'].forEach((field) => { attrs[field] = ''; }); diff --git a/core/server/api/v2/utils/serializers/output/utils/post-gating.js b/core/server/api/v2/utils/serializers/output/utils/post-gating.js index 4ffb68d6da..465cc22b23 100644 --- a/core/server/api/v2/utils/serializers/output/utils/post-gating.js +++ b/core/server/api/v2/utils/serializers/output/utils/post-gating.js @@ -1,37 +1,11 @@ -const _ = require('lodash'); +const membersService = require('../../../../../../services/members'); const labs = require('../../../../../../services/labs'); -// Checks if request should hide members only content -function hideMembersOnlyContent({visibility}, frame) { - const PERMIT_CONTENT = false; - const BLOCK_CONTENT = true; - - if (visibility === 'public') { - return PERMIT_CONTENT; - } - - const requestFromMember = frame.original.context.member; - - if (!requestFromMember) { - return BLOCK_CONTENT; - } else if (visibility === 'members') { - return PERMIT_CONTENT; - } - - const memberHasPlan = !!(_.get(frame, 'original.context.member.stripe.subscriptions', [])).length; - - if (visibility === 'paid' && memberHasPlan) { - return PERMIT_CONTENT; - } - - return BLOCK_CONTENT; -} - const forPost = (attrs, frame) => { if (labs.isSet('members')) { - const hideFormatsData = hideMembersOnlyContent(attrs, frame); + const memberHasAccess = membersService.contentGating.checkPostAccess(attrs, frame.original.context.member); - if (hideFormatsData) { + if (!memberHasAccess) { ['plaintext', 'html'].forEach((field) => { attrs[field] = ''; });