2017-12-24 15:04:08 -05:00
|
|
|
const moment = require('moment');
|
|
|
|
const knex = require('../knex');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether userId is blocked
|
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise<Boolean>}
|
|
|
|
*/
|
|
|
|
async function isBlocked(userId) {
|
|
|
|
const row = await knex('blocked_users')
|
|
|
|
.where('user_id', userId)
|
|
|
|
.first();
|
|
|
|
|
2017-12-31 19:16:05 -05:00
|
|
|
return !! row;
|
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}
|
|
|
|
*/
|
2017-12-31 19:16:05 -05:00
|
|
|
async function block(userId, userName = '', blockedBy = 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,
|
|
|
|
blocked_at: moment.utc().format('YYYY-MM-DD HH:mm:ss')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks the given userId
|
|
|
|
* @param {String} userId
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async function unblock(userId) {
|
|
|
|
return knex('blocked_users')
|
|
|
|
.where('user_id', userId)
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
isBlocked,
|
|
|
|
block,
|
|
|
|
unblock,
|
|
|
|
};
|