2020-08-12 17:08:37 -04:00
|
|
|
const moment = require("moment");
|
|
|
|
const knex = require("../knex");
|
2017-12-24 15:04:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} userId
|
2019-06-09 13:04:40 -04:00
|
|
|
* @returns {Promise<{ isBlocked: boolean, expiresAt: string }>}
|
2017-12-24 15:04:08 -05:00
|
|
|
*/
|
2019-06-09 13:04:40 -04:00
|
|
|
async function getBlockStatus(userId) {
|
2020-08-12 17:08:37 -04:00
|
|
|
const row = await knex("blocked_users")
|
|
|
|
.where("user_id", userId)
|
2017-12-24 15:04:08 -05:00
|
|
|
.first();
|
|
|
|
|
2019-06-09 13:04:40 -04:00
|
|
|
return {
|
|
|
|
isBlocked: !! row,
|
|
|
|
expiresAt: row && row.expires_at
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether userId is blocked
|
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise<Boolean>}
|
|
|
|
*/
|
|
|
|
async function isBlocked(userId) {
|
|
|
|
return (await getBlockStatus(userId)).isBlocked;
|
2017-12-24 15:04:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks the given userId
|
|
|
|
* @param {String} userId
|
2017-12-31 19:16:05 -05:00
|
|
|
* @param {String} userName
|
|
|
|
* @param {String} blockedBy
|
2017-12-24 15:04:08 -05:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2020-08-12 17:08:37 -04:00
|
|
|
async function block(userId, userName = "", blockedBy = null, expiresAt = null) {
|
2017-12-24 15:04:08 -05:00
|
|
|
if (await isBlocked(userId)) return;
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
return knex("blocked_users")
|
2017-12-24 15:04:08 -05:00
|
|
|
.insert({
|
|
|
|
user_id: userId,
|
|
|
|
user_name: userName,
|
|
|
|
blocked_by: blockedBy,
|
2020-08-12 17:08:37 -04:00
|
|
|
blocked_at: moment.utc().format("YYYY-MM-DD HH:mm:ss"),
|
2019-06-09 13:04:40 -04:00
|
|
|
expires_at: expiresAt
|
2017-12-24 15:04:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks the given userId
|
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async function unblock(userId) {
|
2020-08-12 17:08:37 -04:00
|
|
|
return knex("blocked_users")
|
|
|
|
.where("user_id", userId)
|
2017-12-24 15:04:08 -05:00
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:04:40 -04:00
|
|
|
/**
|
|
|
|
* Updates the expiry time of the block for the given userId
|
|
|
|
* @param {String} userId
|
|
|
|
* @param {String} expiresAt
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function updateExpiryTime(userId, expiresAt) {
|
2020-08-12 17:08:37 -04:00
|
|
|
return knex("blocked_users")
|
|
|
|
.where("user_id", userId)
|
2019-06-09 13:04:40 -04:00
|
|
|
.update({
|
|
|
|
expires_at: expiresAt
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {String[]}
|
|
|
|
*/
|
|
|
|
async function getExpiredBlocks() {
|
2020-08-12 17:08:37 -04:00
|
|
|
const now = moment.utc().format("YYYY-MM-DD HH:mm:ss");
|
2019-06-09 13:04:40 -04:00
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
const blocks = await knex("blocked_users")
|
|
|
|
.whereNotNull("expires_at")
|
|
|
|
.where("expires_at", "<=", now)
|
2019-06-09 13:04:40 -04:00
|
|
|
.select();
|
|
|
|
|
|
|
|
return blocks.map(block => block.user_id);
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
module.exports = {
|
2019-06-09 13:04:40 -04:00
|
|
|
getBlockStatus,
|
2017-12-24 15:04:08 -05:00
|
|
|
isBlocked,
|
|
|
|
block,
|
|
|
|
unblock,
|
2019-06-09 13:04:40 -04:00
|
|
|
updateExpiryTime,
|
|
|
|
getExpiredBlocks,
|
2017-12-24 15:04:08 -05:00
|
|
|
};
|