Merge pull request #1195 from thelounge/xpaw/consistent-user-object

Do not store unnecessary information in user objects
This commit is contained in:
Jérémie Astori 2017-06-08 02:46:25 -04:00 committed by GitHub
commit 5a5bf823a0
11 changed files with 28 additions and 17 deletions

View file

@ -417,7 +417,7 @@ $(function() {
nicks = [];
for (i in data.users) {
nicks.push(data.users[i].name);
nicks.push(data.users[i].nick);
}
nicks = nicks.sort(function(a, b) {

View file

@ -6,6 +6,6 @@
{{/unless}}
<div class="user-mode {{modes mode}}">
{{/diff}}
<span role="button" class="user {{colorClass name}}" data-name="{{name}}">{{mode}}{{name}}</span>
<span role="button" class="user {{colorClass nick}}" data-name="{{nick}}">{{mode}}{{nick}}</span>
{{/each}}
</div>

View file

@ -77,7 +77,7 @@ Chan.prototype.sortUsers = function(irc) {
this.users = this.users.sort(function(a, b) {
if (a.mode === b.mode) {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
return a.nick.toLowerCase() < b.nick.toLowerCase() ? -1 : 1;
}
return userModeSortPriority[a.mode] - userModeSortPriority[b.mode];
@ -85,7 +85,7 @@ Chan.prototype.sortUsers = function(irc) {
};
Chan.prototype.getMode = function(name) {
var user = _.find(this.users, {name: name});
var user = _.find(this.users, {nick: name});
if (user) {
return user.mode;
}

View file

@ -7,13 +7,21 @@ module.exports = User;
function User(attr, prefixLookup) {
_.defaults(this, attr, {
modes: [],
mode: "",
nick: ""
});
// irc-framework sets character mode, but lounge works with symbols
this.modes = this.modes.map(mode => prefixLookup[mode]);
// TODO: Remove this
this.name = this.nick;
this.mode = (this.modes && this.modes[0]) || "";
if (this.modes[0]) {
this.mode = this.modes[0];
}
}
User.prototype.toJSON = function() {
return {
nick: this.nick,
mode: this.mode,
};
};

View file

@ -14,7 +14,7 @@ module.exports = function(irc, network) {
if (data.kicked === irc.user.nick) {
chan.users = [];
} else {
chan.users = _.without(chan.users, _.find(chan.users, {name: data.kicked}));
chan.users = _.without(chan.users, _.find(chan.users, {nick: data.kicked}));
}
client.emit("users", {

View file

@ -81,7 +81,7 @@ module.exports = function(irc, network) {
return;
}
const user = _.find(targetChan.users, {name: mode.param});
const user = _.find(targetChan.users, {nick: mode.param});
if (!user) {
return;
}

View file

@ -10,7 +10,12 @@ module.exports = function(irc, network) {
return;
}
chan.users = data.users.map(u => new User(u, network.prefixLookup));
chan.users = data.users.map(user => {
return new User({
nick: user.nick,
modes: user.modes,
}, network.prefixLookup);
});
chan.sortUsers(irc);

View file

@ -25,11 +25,11 @@ module.exports = function(irc, network) {
}
network.channels.forEach(chan => {
var user = _.find(chan.users, {name: data.nick});
var user = _.find(chan.users, {nick: data.nick});
if (typeof user === "undefined") {
return;
}
user.name = data.new_nick;
user.nick = data.new_nick;
chan.sortUsers(irc);
client.emit("users", {
chan: chan.id

View file

@ -18,7 +18,7 @@ module.exports = function(irc, network) {
chan: chan.id
});
} else {
var user = _.find(chan.users, {name: from});
var user = _.find(chan.users, {nick: from});
chan.users = _.without(chan.users, user);
client.emit("users", {
chan: chan.id

View file

@ -8,7 +8,7 @@ module.exports = function(irc, network) {
irc.on("quit", function(data) {
network.channels.forEach(chan => {
var from = data.nick;
var user = _.find(chan.users, {name: from});
var user = _.find(chan.users, {nick: from});
if (typeof user === "undefined") {
return;
}

View file

@ -32,9 +32,7 @@ describe("Chan", function() {
};
var getUserNames = function(chan) {
return chan.users.map(function(u) {
return u.name;
});
return chan.users.map(u => u.nick);
};
it("should sort a simple user list", function() {