2017-09-22 15:52:27 +03:30
|
|
|
'use strict';
|
|
|
|
|
2017-09-25 09:56:39 +03:30
|
|
|
// Utils
|
|
|
|
const { logError } = require('../utils/log');
|
2019-01-31 20:19:39 +01:00
|
|
|
const { strip } = require('../utils/parse');
|
2017-09-25 09:56:39 +03:30
|
|
|
|
2017-09-22 15:52:27 +03:30
|
|
|
const Datastore = require('nedb-promise');
|
2018-01-27 23:13:42 +01:00
|
|
|
const R = require('ramda');
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
const User = new Datastore({
|
|
|
|
autoload: true,
|
|
|
|
filename: 'data/User.db'
|
|
|
|
});
|
|
|
|
|
|
|
|
User.ensureIndex({
|
|
|
|
fieldName: 'id',
|
|
|
|
unique: true
|
|
|
|
});
|
|
|
|
|
2017-09-27 00:11:20 +03:30
|
|
|
User.ensureIndex({
|
|
|
|
fieldName: 'status',
|
|
|
|
});
|
|
|
|
|
2018-02-06 15:03:37 +01:00
|
|
|
// Migration
|
|
|
|
User.update(
|
|
|
|
{ username: '' },
|
|
|
|
{ $unset: { username: true } },
|
|
|
|
{ multi: true }
|
|
|
|
).then(() =>
|
|
|
|
User.ensureIndex({ fieldName: 'username', sparse: true, unique: true }));
|
|
|
|
|
2018-01-27 23:13:42 +01:00
|
|
|
const normalizeTgUser = R.pipe(
|
|
|
|
R.pick([ 'first_name', 'id', 'last_name', 'username' ]),
|
|
|
|
R.evolve({ username: R.toLower }),
|
2018-02-06 15:03:37 +01:00
|
|
|
R.merge({ first_name: '', last_name: '' }),
|
2018-01-27 23:13:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const getUpdatedDocument = R.prop(1);
|
|
|
|
|
2017-09-22 15:52:27 +03:30
|
|
|
const addUser = ({ id, first_name = '', last_name = '', username = '' }) =>
|
2017-09-27 00:11:20 +03:30
|
|
|
User.update(
|
|
|
|
{ id },
|
2017-11-20 16:03:47 +03:30
|
|
|
{
|
|
|
|
first_name,
|
|
|
|
id,
|
|
|
|
last_name,
|
|
|
|
status: 'member',
|
|
|
|
username: username.toLowerCase(),
|
|
|
|
warns: []
|
|
|
|
},
|
2017-10-31 23:08:22 +01:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
2017-09-27 23:12:12 +03:30
|
|
|
.catch(logError);
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
|
|
const isUser = ({ id }) =>
|
|
|
|
User.findOne({ id });
|
|
|
|
|
2017-09-27 00:11:20 +03:30
|
|
|
const getUser = user =>
|
2017-09-25 12:40:04 +03:30
|
|
|
User.findOne(user);
|
|
|
|
|
2018-01-27 23:13:42 +01:00
|
|
|
const updateUser = async (rawTgUser) => {
|
|
|
|
const tgUser = normalizeTgUser(rawTgUser);
|
|
|
|
|
2018-02-06 15:03:37 +01:00
|
|
|
const { id, username } = tgUser;
|
2018-01-27 23:13:42 +01:00
|
|
|
|
2018-02-06 15:03:37 +01:00
|
|
|
const [ rawDbUser ] = await Promise.all([
|
|
|
|
getUser({ id }),
|
|
|
|
User.update({ $not: { id }, username }, { $unset: { username: true } }),
|
|
|
|
]);
|
2018-01-27 23:13:42 +01:00
|
|
|
|
|
|
|
if (rawDbUser === null) {
|
|
|
|
return User.update(
|
|
|
|
{ id },
|
|
|
|
{ status: 'member', warns: [], ...tgUser },
|
|
|
|
{ returnUpdatedDocs: true, upsert: true }
|
|
|
|
).then(getUpdatedDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbUser = rawDbUser;
|
|
|
|
|
|
|
|
if (!R.whereEq(tgUser, dbUser)) {
|
|
|
|
return User.update(
|
|
|
|
{ id },
|
|
|
|
{ $set: tgUser },
|
|
|
|
{ returnUpdatedDocs: true }
|
|
|
|
).then(getUpdatedDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbUser;
|
|
|
|
};
|
|
|
|
|
2017-09-27 12:34:26 +03:30
|
|
|
const admin = ({ id }) =>
|
2017-09-27 00:11:20 +03:30
|
|
|
User.update(
|
|
|
|
{ id },
|
2019-01-28 21:46:29 +01:00
|
|
|
{ $set: { status: 'admin', warns: [] } }
|
2017-10-31 23:08:22 +01:00
|
|
|
);
|
2017-09-27 00:11:20 +03:30
|
|
|
|
|
|
|
const getAdmins = () =>
|
|
|
|
User.find({ status: 'admin' });
|
|
|
|
|
|
|
|
const unadmin = ({ id }) =>
|
|
|
|
User.update({ id }, { $set: { status: 'member' } });
|
|
|
|
|
2019-01-24 19:53:10 +01:00
|
|
|
const isAdmin = (user) => {
|
|
|
|
if (!user) return false;
|
|
|
|
|
|
|
|
if (user.status) return user.status === 'admin';
|
|
|
|
|
|
|
|
return User.findOne({ id: user.id, status: 'admin' });
|
|
|
|
};
|
2017-09-27 00:11:20 +03:30
|
|
|
|
2018-04-20 21:45:06 +02:00
|
|
|
const ban = ({ id }, ban_details) => {
|
|
|
|
const ban_reason = ban_details.reason;
|
|
|
|
return User.update(
|
|
|
|
{ id },
|
2019-01-28 21:46:29 +01:00
|
|
|
{ $set: { ban_details, ban_reason, status: 'banned' } },
|
|
|
|
{ upsert: true }
|
2018-04-20 21:45:06 +02:00
|
|
|
);
|
|
|
|
};
|
2017-09-27 00:11:20 +03:30
|
|
|
|
2019-01-31 20:19:39 +01:00
|
|
|
const batchBan = (users, ban_details) => {
|
|
|
|
const ban_reason = ban_details.reason;
|
|
|
|
return User.update(
|
|
|
|
{ $or: users.map(strip), $not: { status: 'admin' } },
|
|
|
|
{ $set: { ban_details, ban_reason, status: 'banned' } },
|
|
|
|
{ multi: true, returnUpdatedDocs: true }
|
|
|
|
).then(getUpdatedDocument);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ensureExists = ({ id }) =>
|
|
|
|
id && User.insert({ id, status: 'member', warns: [] }).catch(R.F);
|
|
|
|
|
2017-09-27 00:11:20 +03:30
|
|
|
const unban = ({ id }) =>
|
2017-10-06 19:05:44 +02:00
|
|
|
User.update(
|
|
|
|
{ id },
|
2018-04-20 21:45:06 +02:00
|
|
|
{
|
|
|
|
$set: { status: 'member', warns: [] },
|
|
|
|
$unset: { ban_details: true, ban_reason: true },
|
|
|
|
}
|
2017-10-31 23:08:22 +01:00
|
|
|
);
|
2017-09-27 00:11:20 +03:30
|
|
|
|
|
|
|
const isBanned = ({ id }) =>
|
|
|
|
User.findOne({ id, status: 'banned' })
|
2017-09-27 12:34:26 +03:30
|
|
|
.then(user => user ? user.ban_reason : null);
|
|
|
|
|
|
|
|
const warn = ({ id }, reason) =>
|
2018-02-05 19:17:52 +01:00
|
|
|
User.update(
|
|
|
|
{ id },
|
|
|
|
{ $push: { warns: reason } },
|
|
|
|
{ returnUpdatedDocs: true }
|
|
|
|
).then(getUpdatedDocument);
|
2017-09-27 12:34:26 +03:30
|
|
|
|
|
|
|
const unwarn = ({ id }) =>
|
2017-11-17 19:13:44 +01:00
|
|
|
User.update(
|
|
|
|
{ id },
|
2018-04-20 21:45:06 +02:00
|
|
|
{
|
|
|
|
$pop: { warns: 1 },
|
|
|
|
$set: { status: 'member' },
|
|
|
|
$unset: { ban_details: true, ban_reason: true },
|
|
|
|
}
|
2017-11-17 19:13:44 +01:00
|
|
|
);
|
2017-09-27 12:34:26 +03:30
|
|
|
|
2017-11-17 18:53:51 +01:00
|
|
|
const nowarns = unban;
|
2017-09-27 12:34:26 +03:30
|
|
|
|
|
|
|
const getWarns = ({ id }) =>
|
|
|
|
User.findOne({ id })
|
|
|
|
.then(user => user && user.warns.length > 0
|
|
|
|
? user.warns
|
|
|
|
: null);
|
2017-09-27 00:11:20 +03:30
|
|
|
|
2017-09-22 15:52:27 +03:30
|
|
|
module.exports = {
|
|
|
|
addUser,
|
2017-09-27 00:11:20 +03:30
|
|
|
admin,
|
|
|
|
ban,
|
2019-01-31 20:19:39 +01:00
|
|
|
batchBan,
|
|
|
|
ensureExists,
|
2017-09-27 00:11:20 +03:30
|
|
|
getAdmins,
|
2017-09-25 12:40:04 +03:30
|
|
|
getUser,
|
2017-09-27 12:34:26 +03:30
|
|
|
getWarns,
|
2017-09-27 00:11:20 +03:30
|
|
|
isAdmin,
|
|
|
|
isBanned,
|
|
|
|
isUser,
|
2017-09-27 12:34:26 +03:30
|
|
|
nowarns,
|
2017-09-27 00:11:20 +03:30
|
|
|
unadmin,
|
2017-09-27 12:34:26 +03:30
|
|
|
unban,
|
|
|
|
unwarn,
|
2018-01-27 23:13:42 +01:00
|
|
|
updateUser,
|
2017-09-27 12:34:26 +03:30
|
|
|
warn
|
2017-09-22 15:52:27 +03:30
|
|
|
};
|