Use correct channel when pushing link prefetch messages

Fixes #781
This commit is contained in:
Pavel Djundik 2016-12-09 22:46:53 +02:00
parent 085ede43df
commit 62d4cd8fe8
5 changed files with 37 additions and 44 deletions

View File

@ -24,7 +24,6 @@ var events = [
"mode", "mode",
"motd", "motd",
"message", "message",
"link",
"names", "names",
"nick", "nick",
"part", "part",

View File

@ -1,21 +1,19 @@
"use strict"; "use strict";
var cheerio = require("cheerio"); const cheerio = require("cheerio");
var Msg = require("../../models/msg"); const Msg = require("../../models/msg");
var request = require("request"); const request = require("request");
var Helper = require("../../helper"); const Helper = require("../../helper");
var es = require("event-stream"); const es = require("event-stream");
process.setMaxListeners(0); process.setMaxListeners(0);
module.exports = function(irc, network) { module.exports = function(client, chan, originalMsg) {
var client = this;
irc.on("privmsg", function(data) {
if (!Helper.config.prefetch) { if (!Helper.config.prefetch) {
return; return;
} }
const links = data.message const links = originalMsg.text
.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "") .replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "")
.split(" ") .split(" ")
.filter(w => /^https?:\/\//.test(w)); .filter(w => /^https?:\/\//.test(w));
@ -24,23 +22,17 @@ module.exports = function(irc, network) {
return; return;
} }
var chan = network.getChannel(data.target); let msg = new Msg({
if (typeof chan === "undefined") {
return;
}
var msg = new Msg({
self: data.nick === irc.user.nick,
type: Msg.Type.TOGGLE, type: Msg.Type.TOGGLE,
time: data.time, // msg handles it if it isn't defined time: originalMsg.time,
self: originalMsg.self,
}); });
chan.pushMessage(client, msg); chan.pushMessage(client, msg);
var link = escapeHeader(links[0]); const link = escapeHeader(links[0]);
fetch(link, function(res) { fetch(link, function(res) {
parse(msg, link, res, client); parse(msg, link, res, client);
}); });
});
}; };
function parse(msg, url, res, client) { function parse(msg, url, res, client) {
@ -51,7 +43,6 @@ function parse(msg, url, res, client) {
body: "", body: "",
thumb: "", thumb: "",
link: url, link: url,
time: msg.time,
}; };
switch (res.type) { switch (res.type) {

View File

@ -1,7 +1,8 @@
"use strict"; "use strict";
var Chan = require("../../models/chan"); const Chan = require("../../models/chan");
var Msg = require("../../models/msg"); const Msg = require("../../models/msg");
const LinkPrefetch = require("./link");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
@ -89,5 +90,7 @@ module.exports = function(irc, network) {
highlight: highlight highlight: highlight
}); });
chan.pushMessage(client, msg, !self); chan.pushMessage(client, msg, !self);
LinkPrefetch(client, chan, msg);
} }
}; };

View File

@ -21,16 +21,16 @@ describe("Link plugin", function() {
}); });
it("should be able to fetch basic information about URLs", function(done) { it("should be able to fetch basic information about URLs", function(done) {
link.call(this.irc, this.irc, this.network); let message = this.irc.createMessage({
text: "http://localhost:9002/basic"
});
link(this.irc, this.network.channels[0], message);
this.app.get("/basic", function(req, res) { this.app.get("/basic", function(req, res) {
res.send("<title>test</title>"); res.send("<title>test</title>");
}); });
this.irc.createMessage({
message: "http://localhost:9002/basic"
});
this.irc.once("toggle", function(data) { this.irc.once("toggle", function(data) {
assert.equal(data.head, "test"); assert.equal(data.head, "test");
done(); done();

View File

@ -18,12 +18,12 @@ util.inherits(MockClient, EventEmitter);
MockClient.prototype.createMessage = function(opts) { MockClient.prototype.createMessage = function(opts) {
var message = _.extend({ var message = _.extend({
message: "dummy message", text: "dummy message",
nick: "test-user", nick: "test-user",
target: "#test-channel" target: "#test-channel"
}, opts); }, opts);
this.emit("privmsg", message); return message;
}; };
module.exports = { module.exports = {