CyTube/src/account.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

import db from './database';
import Promise from 'bluebird';
const dbGetGlobalRank = Promise.promisify(db.users.getGlobalRank);
const dbMultiGetGlobalRank = Promise.promisify(db.users.getGlobalRanks);
const dbGetChannelRank = Promise.promisify(db.channels.getRank);
const dbMultiGetChannelRank = Promise.promisify(db.channels.getRanks);
const dbGetAliases = Promise.promisify(db.getAliases);
2014-05-21 02:30:14 +00:00
const DEFAULT_PROFILE = Object.freeze({ image: '', text: '' });
2014-05-21 02:30:14 +00:00
class Account {
constructor(ip, user, aliases) {
this.ip = ip;
this.user = user;
this.aliases = aliases;
this.channelRank = -1;
this.guestName = null;
2014-05-21 02:30:14 +00:00
this.update();
2014-05-21 02:30:14 +00:00
}
update() {
if (this.user !== null) {
this.name = this.user.name;
this.globalRank = this.user.global_rank;
} else if (this.guestName !== null) {
this.name = this.guestName;
this.globalRank = 0;
2014-05-21 02:30:14 +00:00
} else {
this.name = '';
this.globalRank = -1;
2014-05-21 02:30:14 +00:00
}
this.lowername = this.name.toLowerCase();
this.effectiveRank = Math.max(this.channelRank, this.globalRank);
this.profile = (this.user === null) ? DEFAULT_PROFILE : this.user.profile;
}
}
module.exports.Account = Account;
2014-05-21 02:30:14 +00:00
module.exports.rankForName = async function rankForNameAsync(name, channel) {
const [globalRank, channelRank] = await Promise.all([
dbGetGlobalRank(name),
dbGetChannelRank(channel, name)
]);
2014-05-21 02:30:14 +00:00
return Math.max(globalRank, channelRank);
2014-05-21 02:30:14 +00:00
};
module.exports.rankForIP = async function rankForIP(ip, channel) {
const aliases = await dbGetAliases(ip);
const [globalRanks, channelRanks] = await Promise.all([
dbMultiGetGlobalRank(aliases),
dbMultiGetChannelRank(channel, aliases)
]);
2014-05-21 02:30:14 +00:00
return Math.max.apply(Math, globalRanks.concat(channelRanks));
2014-05-21 02:30:14 +00:00
};