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",
"motd",
"message",
"link",
"names",
"nick",
"part",

View File

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

View File

@ -1,7 +1,8 @@
"use strict";
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
const Chan = require("../../models/chan");
const Msg = require("../../models/msg");
const LinkPrefetch = require("./link");
module.exports = function(irc, network) {
var client = this;
@ -89,5 +90,7 @@ module.exports = function(irc, network) {
highlight: highlight
});
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) {
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) {
res.send("<title>test</title>");
});
this.irc.createMessage({
message: "http://localhost:9002/basic"
});
this.irc.once("toggle", function(data) {
assert.equal(data.head, "test");
done();

View File

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