Merge pull request #1771 from thelounge/astorije/fix-user-in-msg

Clone instances of `User` in `Msg` to avoid unintentional mutations
This commit is contained in:
Pavel Djundik 2017-11-30 11:29:23 +02:00 committed by GitHub
commit 104e270882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View File

@ -3,7 +3,7 @@ invited
{{#if invitedYou}} {{#if invitedYou}}
you you
{{else}} {{else}}
{{> ../user_name invited}} {{> ../user_name target}}
{{/if}} {{/if}}
to to
{{{parse channel}}} {{{parse channel}}}

View File

@ -6,6 +6,18 @@ var id = 0;
class Msg { class Msg {
constructor(attr) { constructor(attr) {
// Some properties need to be copied in the Msg object instead of referenced
if (attr) {
["from", "target"].forEach((prop) => {
if (attr[prop]) {
this[prop] = {
mode: attr[prop].mode,
nick: attr[prop].nick,
};
}
});
}
_.defaults(this, attr, { _.defaults(this, attr, {
from: {}, from: {},
id: id++, id: id++,

View File

@ -16,7 +16,7 @@ module.exports = function(irc, network) {
type: Msg.Type.INVITE, type: Msg.Type.INVITE,
time: data.time, time: data.time,
from: chan.getUser(data.nick), from: chan.getUser(data.nick),
invited: chan.getUser(data.invited), target: chan.getUser(data.invited),
channel: data.channel, channel: data.channel,
highlight: true, highlight: true,
invitedYou: data.invited === irc.user.nick, invitedYou: data.invited === irc.user.nick,

View File

@ -3,8 +3,27 @@
const expect = require("chai").expect; const expect = require("chai").expect;
const Msg = require("../../src/models/msg"); const Msg = require("../../src/models/msg");
const User = require("../../src/models/user");
describe("Msg", function() { describe("Msg", function() {
["from", "target"].forEach((prop) => {
it(`should keep a copy of the original user in the \`${prop}\` property`, function() {
const prefixLookup = {a: "&", o: "@"};
const user = new User({
modes: ["o"],
nick: "foo",
}, prefixLookup);
const msg = new Msg({[prop]: user});
// Mutating the user
user.setModes(["a"], prefixLookup);
user.nick = "bar";
// Message's `.from`/etc. should still refer to the original user
expect(msg[prop]).to.deep.equal({mode: "@", nick: "foo"});
});
});
describe("#findPreview(link)", function() { describe("#findPreview(link)", function() {
const msg = new Msg({ const msg = new Msg({
previews: [{ previews: [{