2017-12-24 15:04:08 -05:00
|
|
|
const moment = require('moment');
|
|
|
|
const knex = require('../knex');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2017-12-24 15:04:08 -05:00
|
|
|
const row = await knex('blocked_users')
|
|
|
|
.where('user_id', userId)
|
|
|
|
.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}
|
|
|
|
*/
|
2019-06-09 13:04:40 -04:00
|
|
|
async function block(userId, userName = '', blockedBy = null, expiresAt = null) {
|
2017-12-24 15:04:08 -05:00
|
|
|
if (await isBlocked(userId)) return;
|
|
|
|
|
|
|
|
return knex('blocked_users')
|
|
|
|
.insert({
|
|
|
|
user_id: userId,
|
|
|
|
user_name: userName,
|
|
|
|
blocked_by: blockedBy,
|
2019-06-09 13:04:40 -04:00
|
|
|
blocked_at: moment.utc().format('YYYY-MM-DD HH:mm:ss'),
|
|
|
|
expires_at: expiresAt
|
2017-12-24 15:04:08 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks the given userId
|
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async function unblock(userId) {
|
|
|
|
return knex('blocked_users')
|
|
|
|
.where('user_id', userId)
|
|
|
|
.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) {
|
|
|
|
return knex('blocked_users')
|
|
|
|
.where('user_id', userId)
|
|
|
|
.update({
|
|
|
|
expires_at: expiresAt
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {String[]}
|
|
|
|
*/
|
|
|
|
async function getExpiredBlocks() {
|
|
|
|
const now = moment.utc().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
|
|
|
const blocks = await knex('blocked_users')
|
|
|
|
.whereNotNull('expires_at')
|
|
|
|
.where('expires_at', '<=', now)
|
|
|
|
.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
|
|
|
};
|