Update README.md
[KisSync.git] / src / account.js
blob2aa3e5bde2f03d9008f123275f133d22123da84a
1 import db from './database';
2 import Promise from 'bluebird';
4 const dbGetGlobalRank = Promise.promisify(db.users.getGlobalRank);
5 const dbMultiGetGlobalRank = Promise.promisify(db.users.getGlobalRanks);
6 const dbGetChannelRank = Promise.promisify(db.channels.getRank);
7 const dbMultiGetChannelRank = Promise.promisify(db.channels.getRanks);
8 const dbGetAliases = Promise.promisify(db.getAliases);
10 const DEFAULT_PROFILE = Object.freeze({ image: '', text: '' });
12 class Account {
13 constructor(ip, user, aliases) {
14 this.ip = ip;
15 this.user = user;
16 this.aliases = aliases;
17 this.channelRank = -1;
18 this.guestName = null;
20 this.update();
23 update() {
24 if (this.user !== null) {
25 this.name = this.user.name;
26 this.globalRank = this.user.global_rank;
27 } else if (this.guestName !== null) {
28 this.name = this.guestName;
29 this.globalRank = 0;
30 } else {
31 this.name = '';
32 this.globalRank = -1;
34 this.lowername = this.name.toLowerCase();
35 this.effectiveRank = Math.max(this.channelRank, this.globalRank);
36 this.profile = (this.user === null) ? DEFAULT_PROFILE : this.user.profile;
40 module.exports.Account = Account;
42 module.exports.rankForName = async function rankForNameAsync(name, channel) {
43 const [globalRank, channelRank] = await Promise.all([
44 dbGetGlobalRank(name),
45 dbGetChannelRank(channel, name)
46 ]);
48 return Math.max(globalRank, channelRank);
51 module.exports.rankForIP = async function rankForIP(ip, channel) {
52 const aliases = await dbGetAliases(ip);
53 const [globalRanks, channelRanks] = await Promise.all([
54 dbMultiGetGlobalRank(aliases),
55 dbMultiGetChannelRank(channel, aliases)
56 ]);
58 return Math.max.apply(Math, globalRanks.concat(channelRanks));