Merge pull request #782 from thelounge/xpaw/correct-link-chan

Use correct channel when pushing link prefetch messages
This commit is contained in:
Jérémie Astori 2016-12-10 19:44:35 -05:00 committed by GitHub
commit b1478c1ae1
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,21 +1,19 @@
"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) {
module.exports = function(client, chan, originalMsg) {
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, "")
.split(" ")
.filter(w => /^https?:\/\//.test(w));
@ -24,23 +22,17 @@ module.exports = function(irc, network) {
return;
}
var chan = network.getChannel(data.target);
if (typeof chan === "undefined") {
return;
}
var msg = new Msg({
self: data.nick === irc.user.nick,
let msg = new Msg({
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);
var link = escapeHeader(links[0]);
const link = escapeHeader(links[0]);
fetch(link, function(res) {
parse(msg, link, res, client);
});
});
};
function parse(msg, url, 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 = {