mirror of
https://github.com/thelounge/thelounge
synced 2024-11-30 07:50:34 +00:00
Merge pull request #4014 from thelounge/xpaw/no-schema-previews
Disable link prefetching for urls with no schema specified
This commit is contained in:
commit
2a11c07ba9
5 changed files with 52 additions and 14 deletions
|
@ -6,11 +6,13 @@ LinkifyIt.prototype.normalize = function normalize(match) {
|
||||||
if (!match.schema) {
|
if (!match.schema) {
|
||||||
match.schema = "http:";
|
match.schema = "http:";
|
||||||
match.url = "http://" + match.url;
|
match.url = "http://" + match.url;
|
||||||
|
match.noschema = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (match.schema === "//") {
|
if (match.schema === "//") {
|
||||||
match.schema = "http:";
|
match.schema = "http:";
|
||||||
match.url = "http:" + match.url;
|
match.url = "http:" + match.url;
|
||||||
|
match.noschema = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) {
|
if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) {
|
||||||
|
@ -47,11 +49,28 @@ function findLinks(text) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches.map((url) => ({
|
return matches.map(returnUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
function findLinksWithSchema(text) {
|
||||||
|
const matches = linkify.match(text);
|
||||||
|
|
||||||
|
if (!matches) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches.filter((url) => !url.noschema).map(returnUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
function returnUrl(url) {
|
||||||
|
return {
|
||||||
start: url.index,
|
start: url.index,
|
||||||
end: url.lastIndex,
|
end: url.lastIndex,
|
||||||
link: url.url,
|
link: url.url,
|
||||||
}));
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = findLinks;
|
module.exports = {
|
||||||
|
findLinks,
|
||||||
|
findLinksWithSchema,
|
||||||
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import parseStyle from "./ircmessageparser/parseStyle";
|
import parseStyle from "./ircmessageparser/parseStyle";
|
||||||
import findChannels from "./ircmessageparser/findChannels";
|
import findChannels from "./ircmessageparser/findChannels";
|
||||||
import findLinks from "./ircmessageparser/findLinks";
|
import {findLinks} from "./ircmessageparser/findLinks";
|
||||||
import findEmoji from "./ircmessageparser/findEmoji";
|
import findEmoji from "./ircmessageparser/findEmoji";
|
||||||
import findNames from "./ircmessageparser/findNames";
|
import findNames from "./ircmessageparser/findNames";
|
||||||
import merge from "./ircmessageparser/merge";
|
import merge from "./ircmessageparser/merge";
|
||||||
|
|
|
@ -6,7 +6,7 @@ const URL = require("url").URL;
|
||||||
const mime = require("mime-types");
|
const mime = require("mime-types");
|
||||||
const Helper = require("../../helper");
|
const Helper = require("../../helper");
|
||||||
const cleanIrcMessage = require("../../../client/js/helpers/ircmessageparser/cleanIrcMessage");
|
const cleanIrcMessage = require("../../../client/js/helpers/ircmessageparser/cleanIrcMessage");
|
||||||
const findLinks = require("../../../client/js/helpers/ircmessageparser/findLinks");
|
const {findLinksWithSchema} = require("../../../client/js/helpers/ircmessageparser/findLinks");
|
||||||
const storage = require("../storage");
|
const storage = require("../storage");
|
||||||
const currentFetchPromises = new Map();
|
const currentFetchPromises = new Map();
|
||||||
const imageTypeRegex = /^image\/.+/;
|
const imageTypeRegex = /^image\/.+/;
|
||||||
|
@ -20,7 +20,7 @@ module.exports = function (client, chan, msg) {
|
||||||
// Remove all IRC formatting characters before searching for links
|
// Remove all IRC formatting characters before searching for links
|
||||||
const cleanText = cleanIrcMessage(msg.text);
|
const cleanText = cleanIrcMessage(msg.text);
|
||||||
|
|
||||||
msg.previews = findLinks(cleanText).reduce((cleanLinks, link) => {
|
msg.previews = findLinksWithSchema(cleanText).reduce((cleanLinks, link) => {
|
||||||
const url = normalizeURL(link.link);
|
const url = normalizeURL(link.link);
|
||||||
|
|
||||||
// If the URL is invalid and cannot be normalized, don't fetch it
|
// If the URL is invalid and cannot be normalized, don't fetch it
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const expect = require("chai").expect;
|
const expect = require("chai").expect;
|
||||||
const findLinks = require("../../../../../client/js/helpers/ircmessageparser/findLinks");
|
const {
|
||||||
|
findLinks,
|
||||||
|
findLinksWithSchema,
|
||||||
|
} = require("../../../../../client/js/helpers/ircmessageparser/findLinks");
|
||||||
|
|
||||||
describe("findLinks", () => {
|
describe("findLinks", () => {
|
||||||
it("should find url", () => {
|
it("should find url", () => {
|
||||||
|
@ -354,4 +357,24 @@ describe("findLinks", () => {
|
||||||
|
|
||||||
expect(actual).to.deep.equal(expected);
|
expect(actual).to.deep.equal(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should not return urls with no schema if flag is specified", () => {
|
||||||
|
const input = "https://example.global //example.com http://example.group example.py";
|
||||||
|
const expected = [
|
||||||
|
{
|
||||||
|
link: "https://example.global",
|
||||||
|
start: 0,
|
||||||
|
end: 22,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
end: 57,
|
||||||
|
link: "http://example.group",
|
||||||
|
start: 37,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const actual = findLinksWithSchema(input);
|
||||||
|
|
||||||
|
expect(actual).to.deep.equal(expected);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -623,19 +623,15 @@ Vivamus bibendum vulputate tincidunt. Sed vitae ligula felis.`;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fetch protocol-aware links", function (done) {
|
it("should not fetch links without a schema", function () {
|
||||||
const port = this.port;
|
const port = this.port;
|
||||||
const message = this.irc.createMessage({
|
const message = this.irc.createMessage({
|
||||||
text: "//localhost:" + port + "",
|
text: `//localhost:${port} localhost:${port} //localhost:${port}/test localhost:${port}/test`,
|
||||||
});
|
});
|
||||||
|
|
||||||
link(this.irc, this.network.channels[0], message);
|
link(this.irc, this.network.channels[0], message);
|
||||||
|
|
||||||
this.irc.once("msg:preview", function (data) {
|
expect(message.previews).to.be.empty;
|
||||||
expect(data.preview.link).to.equal("http://localhost:" + port + "");
|
|
||||||
expect(data.preview.type).to.equal("error");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should de-duplicate links", function (done) {
|
it("should de-duplicate links", function (done) {
|
||||||
|
|
Loading…
Reference in a new issue