Re-use .previews to order incoming previews instead of extra links

This commit is contained in:
Jérémie Astori 2017-07-21 01:28:51 -04:00
parent 1c8ea0b75c
commit 900d41bf47
No known key found for this signature in database
GPG key ID: B9A4F245CD67BDE8
7 changed files with 45 additions and 30 deletions

View file

@ -8,6 +8,10 @@ const templates = require("../views");
module.exports = renderPreview;
function renderPreview(preview, msg) {
if (preview.type === "loading") {
return;
}
preview.shown = options.shouldOpenMessagePreview(preview.type);
const container = msg.closest(".chat");

View file

@ -1,6 +1,6 @@
{{> ../user_name nick=from}}
<span class="text">{{{parse text}}}</span>
{{#each links}}
<div class="preview" data-url="{{this}}"></div>
{{#each previews}}
<div class="preview" data-url="{{link}}"></div>
{{/each}}

View file

@ -10,8 +10,8 @@
<span class="content">
<span class="text">{{{parse text}}}</span>
{{#each links}}
<div class="preview" data-url="{{this}}"></div>
{{#each previews}}
<div class="preview" data-url="{{link}}"></div>
{{/each}}
</span>
</div>

View file

@ -31,7 +31,6 @@ function Msg(attr) {
_.defaults(this, attr, {
from: "",
id: id++,
links: [],
previews: [],
text: "",
type: Msg.Type.MESSAGE,

View file

@ -24,30 +24,28 @@ module.exports = function(client, chan, msg) {
return;
}
msg.links = Array.from(new Set( // Remove duplicate links
msg.previews = Array.from(new Set( // Remove duplicate links
links.map((link) => escapeHeader(link.link))
)).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse
)).map((link) => ({
type: "loading",
head: "",
body: "",
thumb: "",
link: link,
})).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse
msg.links.forEach((link) => {
fetch(link, function(res) {
msg.previews.forEach((preview) => {
fetch(preview.link, function(res) {
if (res === null) {
return;
}
parse(msg, link, res, client);
parse(msg, preview, res, client);
});
});
};
function parse(msg, url, res, client) {
const preview = {
type: "",
head: "",
body: "",
thumb: "",
link: url,
};
function parse(msg, preview, res, client) {
switch (res.type) {
case "text/html":
var $ = cheerio.load(res.data);
@ -130,8 +128,6 @@ function emitPreview(client, msg, preview) {
}
}
msg.previews.push(preview);
client.emit("msg:preview", {
id: msg.id,
preview: preview

View file

@ -34,6 +34,14 @@ describe("Link plugin", function() {
link(this.irc, this.network.channels[0], message);
expect(message.previews).to.deep.equal([{
body: "",
head: "",
link: url,
thumb: "",
type: "loading"
}]);
this.app.get("/basic", function(req, res) {
res.send("<title>test title</title><meta name='description' content='simple description'>");
});
@ -44,7 +52,6 @@ describe("Link plugin", function() {
expect(data.preview.body).to.equal("simple description");
expect(data.preview.link).to.equal(url);
expect(message.links).to.deep.equal([url]);
expect(message.previews).to.deep.equal([data.preview]);
done();
});
@ -181,10 +188,19 @@ describe("Link plugin", function() {
link(this.irc, this.network.channels[0], message);
expect(message.links).to.deep.equal([
"http://localhost:9002/one",
"http://localhost:9002/two"
]);
expect(message.previews).to.eql([{
body: "",
head: "",
link: "http://localhost:9002/one",
thumb: "",
type: "loading"
}, {
body: "",
head: "",
link: "http://localhost:9002/two",
thumb: "",
type: "loading"
}]);
this.app.get("/one", function(req, res) {
res.send("<title>first title</title>");
@ -199,13 +215,14 @@ describe("Link plugin", function() {
this.irc.on("msg:preview", function(data) {
if (data.preview.link === "http://localhost:9002/one") {
expect(data.preview.head).to.equal("first title");
previews[0] = data.preview;
} else if (data.preview.link === "http://localhost:9002/two") {
expect(data.preview.head).to.equal("second title");
previews[1] = data.preview;
}
previews.push(data.preview);
if (previews.length === 2) {
expect(message.previews).to.deep.equal(previews);
if (previews[0] && previews[1]) {
expect(message.previews).to.eql(previews);
done();
}
});

View file

@ -21,7 +21,6 @@ MockClient.prototype.createMessage = function(opts) {
text: "dummy message",
nick: "test-user",
target: "#test-channel",
links: [],
previews: [],
}, opts);