ramirez/src/data/blocked.js

52 lines
1003 B
JavaScript
Raw Normal View History

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;
}
/**
* Blocks the given userId
* @param {String} userId
2017-12-31 19:16:05 -05:00
* @param {String} userName
* @param {String} blockedBy
* @returns {Promise}
*/
2017-12-31 19:16:05 -05:00
async function block(userId, userName = '', blockedBy = null) {
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,
};