Clone instances of User
in Msg
to avoid unintentional mutations
This commit is contained in:
parent
36e0ce46b4
commit
4ec10b922a
@ -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}}}
|
||||||
|
@ -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++,
|
||||||
|
@ -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,
|
||||||
|
@ -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: [{
|
||||||
|
Loading…
Reference in New Issue
Block a user