Clone instances of User in Msg to avoid unintentional mutations

This commit is contained in:
Jérémie Astori 2017-11-29 01:22:03 -05:00
parent 36e0ce46b4
commit 4ec10b922a
No known key found for this signature in database
GPG key ID: B9A4F245CD67BDE8
4 changed files with 33 additions and 2 deletions

View file

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

View file

@ -6,6 +6,18 @@ var id = 0;
class Msg {
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, {
from: "",
id: id++,

View file

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

View file

@ -3,8 +3,27 @@
const expect = require("chai").expect;
const Msg = require("../../src/models/msg");
const User = require("../../src/models/user");
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() {
const msg = new Msg({
previews: [{