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,45 +1,37 @@
"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; if (!Helper.config.prefetch) {
irc.on("privmsg", function(data) { return;
if (!Helper.config.prefetch) { }
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));
if (links.length === 0) { if (links.length === 0) {
return; return;
} }
var chan = network.getChannel(data.target); let msg = new Msg({
if (typeof chan === "undefined") { type: Msg.Type.TOGGLE,
return; time: originalMsg.time,
} self: originalMsg.self,
});
chan.pushMessage(client, msg);
var msg = new Msg({ const link = escapeHeader(links[0]);
self: data.nick === irc.user.nick, fetch(link, function(res) {
type: Msg.Type.TOGGLE, parse(msg, link, res, client);
time: data.time, // msg handles it if it isn't defined
});
chan.pushMessage(client, msg);
var link = escapeHeader(links[0]);
fetch(link, function(res) {
parse(msg, link, 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 = {