Merge pull request #1359 from thelounge/xpaw/fix-link-previews

Fix remaining issues with link previews
This commit is contained in:
Jérémie Astori 2017-07-22 01:24:13 -04:00 committed by GitHub
commit 30cf980506
2 changed files with 15 additions and 6 deletions

View File

@ -12,6 +12,18 @@ function renderPreview(preview, msg) {
return; return;
} }
const escapedLink = preview.link.replace(/["\\]/g, "\\$&");
const previewContainer = msg.find(`.preview[data-url="${escapedLink}"]`);
// This is to fix a very rare case of rendering a preview twice
// This happens when a very large amount of messages is being sent to the client
// and they get queued, so the `preview` object on the server has time to load before
// it actually gets sent to the server, which makes the loaded preview sent twice,
// once in `msg` and another in `msg:preview`
if (!previewContainer.is(":empty")) {
return;
}
preview.shown = options.shouldOpenMessagePreview(preview.type); preview.shown = options.shouldOpenMessagePreview(preview.type);
const container = msg.closest(".chat"); const container = msg.closest(".chat");
@ -20,12 +32,11 @@ function renderPreview(preview, msg) {
bottom = container.isScrollBottom(); bottom = container.isScrollBottom();
} }
msg.find(`.text a[href="${preview.link}"]`) msg.find(`.text a[href="${escapedLink}"]`)
.first() .first()
.after(templates.msg_preview_toggle({preview: preview})); .after(templates.msg_preview_toggle({preview: preview}).trim());
msg.find(`.preview[data-url="${preview.link}"]`) previewContainer
.first()
.append(templates.msg_preview({preview: preview})); .append(templates.msg_preview({preview: preview}));
if (preview.shown && bottom) { if (preview.shown && bottom) {

View File

@ -1,13 +1,11 @@
"use strict"; "use strict";
const $ = require("jquery"); const $ = require("jquery");
const Handlebars = require("handlebars/runtime");
const renderPreview = require("../renderPreview"); const renderPreview = require("../renderPreview");
const socket = require("../socket"); const socket = require("../socket");
socket.on("msg:preview", function(data) { socket.on("msg:preview", function(data) {
const msg = $("#msg-" + data.id); const msg = $("#msg-" + data.id);
data.link = Handlebars.Utils.escapeExpression(data.link);
renderPreview(data.preview, msg); renderPreview(data.preview, msg);
}); });