2018-04-19 16:24:36 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-10-03 23:31:42 -04:00
|
|
|
const db = require('../database');
|
2020-05-08 20:42:02 +02:00
|
|
|
const plugins = require('../plugins');
|
2022-08-10 15:26:07 -04:00
|
|
|
const cacheCreate = require('../cache/lru');
|
2018-04-19 16:24:36 -04:00
|
|
|
|
|
|
|
|
module.exports = function (User) {
|
2018-07-03 18:43:29 -04:00
|
|
|
User.blocks = {
|
2021-10-16 13:09:14 -04:00
|
|
|
_cache: cacheCreate({
|
|
|
|
|
name: 'user:blocks',
|
2018-07-03 18:43:29 -04:00
|
|
|
max: 100,
|
2022-08-10 13:24:16 -04:00
|
|
|
ttl: 0,
|
2018-07-03 18:43:29 -04:00
|
|
|
}),
|
|
|
|
|
};
|
2018-04-19 16:24:36 -04:00
|
|
|
|
2021-10-16 13:09:14 -04:00
|
|
|
User.blocks.is = async function (targetUid, uids) {
|
|
|
|
|
const isArray = Array.isArray(uids);
|
|
|
|
|
uids = isArray ? uids : [uids];
|
|
|
|
|
const blocks = await User.blocks.list(uids);
|
|
|
|
|
const isBlocked = uids.map((uid, index) => blocks[index] && blocks[index].includes(parseInt(targetUid, 10)));
|
|
|
|
|
return isArray ? isBlocked : isBlocked[0];
|
2018-04-19 16:24:36 -04:00
|
|
|
};
|
|
|
|
|
|
2022-04-17 18:15:49 -04:00
|
|
|
User.blocks.can = async function (callerUid, blockerUid, blockeeUid, type) {
|
2018-12-10 14:14:11 -05:00
|
|
|
// Guests can't block
|
|
|
|
|
if (blockerUid === 0 || blockeeUid === 0) {
|
2019-07-13 17:23:31 -04:00
|
|
|
throw new Error('[[error:cannot-block-guest]]');
|
2018-12-10 14:14:11 -05:00
|
|
|
} else if (blockerUid === blockeeUid) {
|
2019-07-13 17:23:31 -04:00
|
|
|
throw new Error('[[error:cannot-block-self]]');
|
2018-12-10 14:14:11 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-04 12:39:00 -04:00
|
|
|
// Administrators and global moderators cannot be blocked
|
2018-12-10 14:14:11 -05:00
|
|
|
// Only admins/mods can block users as another user
|
2019-07-13 17:23:31 -04:00
|
|
|
const [isCallerAdminOrMod, isBlockeeAdminOrMod] = await Promise.all([
|
|
|
|
|
User.isAdminOrGlobalMod(callerUid),
|
|
|
|
|
User.isAdminOrGlobalMod(blockeeUid),
|
|
|
|
|
]);
|
2022-04-17 18:15:49 -04:00
|
|
|
if (isBlockeeAdminOrMod && type === 'block') {
|
2019-07-13 17:23:31 -04:00
|
|
|
throw new Error('[[error:cannot-block-privileged]]');
|
|
|
|
|
}
|
|
|
|
|
if (parseInt(callerUid, 10) !== parseInt(blockerUid, 10) && !isCallerAdminOrMod) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:no-privileges]]');
|
2019-07-13 17:23:31 -04:00
|
|
|
}
|
2018-05-04 12:39:00 -04:00
|
|
|
};
|
|
|
|
|
|
2021-10-16 13:09:14 -04:00
|
|
|
User.blocks.list = async function (uids) {
|
|
|
|
|
const isArray = Array.isArray(uids);
|
|
|
|
|
uids = (isArray ? uids : [uids]).map(uid => parseInt(uid, 10));
|
|
|
|
|
const cachedData = {};
|
|
|
|
|
const unCachedUids = User.blocks._cache.getUnCachedKeys(uids, cachedData);
|
|
|
|
|
if (unCachedUids.length) {
|
|
|
|
|
const unCachedData = await db.getSortedSetsMembers(unCachedUids.map(uid => `uid:${uid}:blocked_uids`));
|
|
|
|
|
unCachedUids.forEach((uid, index) => {
|
|
|
|
|
cachedData[uid] = (unCachedData[index] || []).map(uid => parseInt(uid, 10));
|
|
|
|
|
User.blocks._cache.set(uid, cachedData[uid]);
|
|
|
|
|
});
|
2018-07-03 18:43:29 -04:00
|
|
|
}
|
2021-10-16 13:09:14 -04:00
|
|
|
const result = uids.map(uid => cachedData[uid] || []);
|
|
|
|
|
return isArray ? result.slice() : result[0];
|
2018-04-19 16:24:36 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-13 17:23:31 -04:00
|
|
|
User.blocks.add = async function (targetUid, uid) {
|
|
|
|
|
await User.blocks.applyChecks('block', targetUid, uid);
|
2021-02-03 23:59:08 -07:00
|
|
|
await db.sortedSetAdd(`uid:${uid}:blocked_uids`, Date.now(), targetUid);
|
2019-07-13 17:23:31 -04:00
|
|
|
await User.incrementUserFieldBy(uid, 'blocksCount', 1);
|
|
|
|
|
User.blocks._cache.del(parseInt(uid, 10));
|
2020-11-20 16:06:26 -05:00
|
|
|
plugins.hooks.fire('action:user.blocks.add', { uid: uid, targetUid: targetUid });
|
2018-04-19 16:24:36 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-13 17:23:31 -04:00
|
|
|
User.blocks.remove = async function (targetUid, uid) {
|
|
|
|
|
await User.blocks.applyChecks('unblock', targetUid, uid);
|
2021-02-03 23:59:08 -07:00
|
|
|
await db.sortedSetRemove(`uid:${uid}:blocked_uids`, targetUid);
|
2019-07-13 17:23:31 -04:00
|
|
|
await User.decrementUserFieldBy(uid, 'blocksCount', 1);
|
|
|
|
|
User.blocks._cache.del(parseInt(uid, 10));
|
2020-11-20 16:06:26 -05:00
|
|
|
plugins.hooks.fire('action:user.blocks.remove', { uid: uid, targetUid: targetUid });
|
2018-04-19 16:24:36 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-13 17:23:31 -04:00
|
|
|
User.blocks.applyChecks = async function (type, targetUid, uid) {
|
|
|
|
|
await User.blocks.can(uid, uid, targetUid);
|
|
|
|
|
const isBlock = type === 'block';
|
|
|
|
|
const is = await User.blocks.is(targetUid, uid);
|
|
|
|
|
if (is === isBlock) {
|
2021-02-03 23:59:08 -07:00
|
|
|
throw new Error(`[[error:already-${isBlock ? 'blocked' : 'unblocked'}]]`);
|
2019-07-13 17:23:31 -04:00
|
|
|
}
|
2018-04-27 12:51:04 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-13 17:23:31 -04:00
|
|
|
User.blocks.filterUids = async function (targetUid, uids) {
|
2021-10-16 13:09:14 -04:00
|
|
|
const isBlocked = await User.blocks.is(targetUid, uids);
|
|
|
|
|
return uids.filter((uid, index) => !isBlocked[index]);
|
2018-05-30 16:29:40 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-13 17:23:31 -04:00
|
|
|
User.blocks.filter = async function (uid, property, set) {
|
2018-04-20 14:48:10 -04:00
|
|
|
// Given whatever is passed in, iterates through it, and removes entries made by blocked uids
|
2018-04-20 13:49:23 -04:00
|
|
|
// property is optional
|
2019-07-13 17:23:31 -04:00
|
|
|
if (Array.isArray(property) && typeof set === 'undefined') {
|
2018-04-20 13:49:23 -04:00
|
|
|
set = property;
|
|
|
|
|
property = 'uid';
|
|
|
|
|
}
|
2018-07-03 18:43:29 -04:00
|
|
|
|
2020-10-29 22:33:28 -04:00
|
|
|
if (!Array.isArray(set) || !set.length) {
|
2019-07-13 17:23:31 -04:00
|
|
|
return set;
|
2018-04-19 16:24:36 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-20 13:49:23 -04:00
|
|
|
const isPlain = typeof set[0] !== 'object';
|
2019-07-13 17:23:31 -04:00
|
|
|
const blocked_uids = await User.blocks.list(uid);
|
|
|
|
|
const blockedSet = new Set(blocked_uids);
|
2018-07-03 18:43:29 -04:00
|
|
|
|
2021-02-04 00:01:39 -07:00
|
|
|
set = set.filter(item => !blockedSet.has(parseInt(isPlain ? item : (item && item[property]), 10)));
|
2020-11-20 16:06:26 -05:00
|
|
|
const data = await plugins.hooks.fire('filter:user.blocks.filter', { set: set, property: property, uid: uid, blockedSet: blockedSet });
|
2019-07-13 17:23:31 -04:00
|
|
|
|
2020-05-08 20:42:02 +02:00
|
|
|
return data.set;
|
2018-04-19 16:24:36 -04:00
|
|
|
};
|
|
|
|
|
};
|