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
: '' });
13 constructor(ip
, user
, aliases
) {
16 this.aliases
= aliases
;
17 this.channelRank
= -1;
18 this.guestName
= null;
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
;
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
)
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
)
58 return Math
.max
.apply(Math
, globalRanks
.concat(channelRanks
));