2022-03-09 13:02:17 +00:00
|
|
|
const nql = require('@tryghost/nql');
|
2021-05-10 19:32:11 +01:00
|
|
|
|
2019-11-05 09:47:12 +07:00
|
|
|
// @ts-check
|
|
|
|
/** @typedef { boolean } AccessFlag */
|
|
|
|
|
|
|
|
const PERMIT_ACCESS = true;
|
|
|
|
const BLOCK_ACCESS = false;
|
|
|
|
|
2021-05-10 19:32:11 +01:00
|
|
|
// TODO: better place to store this?
|
|
|
|
const MEMBER_NQL_EXPANSIONS = [{
|
|
|
|
key: 'labels',
|
|
|
|
replacement: 'labels.slug'
|
|
|
|
}, {
|
|
|
|
key: 'label',
|
|
|
|
replacement: 'labels.slug'
|
|
|
|
}, {
|
|
|
|
key: 'products',
|
|
|
|
replacement: 'products.slug'
|
|
|
|
}, {
|
|
|
|
key: 'product',
|
|
|
|
replacement: 'products.slug'
|
|
|
|
}];
|
|
|
|
|
2019-11-05 09:47:12 +07:00
|
|
|
/**
|
|
|
|
* @param {object} post - A post object to check access to
|
|
|
|
* @param {object} member - The member whos access should be checked
|
|
|
|
*
|
|
|
|
* @returns {AccessFlag}
|
|
|
|
*/
|
|
|
|
function checkPostAccess(post, member) {
|
|
|
|
if (post.visibility === 'public') {
|
|
|
|
return PERMIT_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
return BLOCK_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post.visibility === 'members') {
|
|
|
|
return PERMIT_ACCESS;
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:53:56 +05:30
|
|
|
let visibility = post.visibility === 'paid' ? 'status:-free' : post.visibility;
|
|
|
|
if (visibility === 'tiers') {
|
2022-02-11 12:26:19 +05:30
|
|
|
if (!post.tiers) {
|
|
|
|
return BLOCK_ACCESS;
|
|
|
|
}
|
2022-01-26 16:53:56 +05:30
|
|
|
visibility = post.tiers.map((product) => {
|
|
|
|
return `product:${product.slug}`;
|
|
|
|
}).join(',');
|
|
|
|
}
|
2021-05-10 19:32:11 +01:00
|
|
|
|
|
|
|
if (visibility && member.status && nql(visibility, {expansions: MEMBER_NQL_EXPANSIONS}).queryJSON(member)) {
|
2019-11-05 09:47:12 +07:00
|
|
|
return PERMIT_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BLOCK_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
checkPostAccess,
|
|
|
|
PERMIT_ACCESS,
|
|
|
|
BLOCK_ACCESS
|
|
|
|
};
|