From bcb4071993d8d656babf08ffad521f506aac53f7 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 1 Jun 2021 11:17:30 +0100 Subject: [PATCH 1/2] Strip hash from urls being previewed to de-duplicate --- src/client.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/client.js b/src/client.js index cb60705efe2..4c71adf502e 100644 --- a/src/client.js +++ b/src/client.js @@ -3368,6 +3368,10 @@ MatrixClient.prototype.getUrlPreview = function(url, ts, callback) { // Surely 60-second accuracy is enough for anyone. ts = Math.floor(ts / 60000) * 60000; + const parsed = new URL(url); + parsed.hash = ""; // strip the hash as it won't affect the preview + url = parsed.toString(); + const key = ts + "_" + url; // If there's already a request in flight (or we've handled it), return that instead. From 79f74fdff4d1f1461d5420af4d813a01972b67c9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 7 Jul 2021 17:54:46 +0100 Subject: [PATCH 2/2] Improve return type for getUrlPreview --- src/client.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index a94ff04b34f..da2d2454780 100644 --- a/src/client.ts +++ b/src/client.ts @@ -414,6 +414,19 @@ interface IUploadKeySignaturesResponse { }>>; } +export interface IPreviewUrlResponse { + [key: string]: string | number; + "og:title": string; + "og:type": string; + "og:url": string; + "og:image"?: string; + "og:image:type"?: string; + "og:image:height"?: number; + "og:image:width"?: number; + "og:description"?: string; + "matrix:image:size"?: number; +} + /** * Represents a Matrix Client. Only directly construct this if you want to use * custom modules. Normally, {@link createClient} should be used @@ -3695,7 +3708,7 @@ export class MatrixClient extends EventEmitter { * @return {module:http-api.MatrixError} Rejects: with an error response. * May return synthesized attributes if the URL lacked OG meta. */ - public getUrlPreview(url: string, ts: number, callback?: Callback): Promise { + public getUrlPreview(url: string, ts: number, callback?: Callback): Promise { // bucket the timestamp to the nearest minute to prevent excessive spam to the server // Surely 60-second accuracy is enough for anyone. ts = Math.floor(ts / 60000) * 60000;