From 415251dd70e33c909c987f569210aa7035964203 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 31 Mar 2016 18:38:34 +0100 Subject: [PATCH 1/4] WIP url previewing --- lib/client.js | 17 +++++++++++++++++ lib/http-api.js | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/lib/client.js b/lib/client.js index c984b5295d4..b5b1ea66b25 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1448,6 +1448,23 @@ MatrixClient.prototype.getCurrentUploads = function() { return this._http.getCurrentUploads(); }; +/** + * Get a preview of the given URL as OpenGraph metadata + * @param {module:client.callback} callback Optional. + * @return {module:client.Promise} Resolves: Object of OG metadata. + * @return {module:http-api.MatrixError} Rejects: with an error response. + * May return synthesized attributes if the URL lacked OG meta. + */ +MatrixClient.prototype.getUrlPreview = function(url, callback) { + // FIXME: maintain a clientside cache to stop hammering synapse + // especially as synapse doesn't cache yet + return this._http.authedRequestWithPrefix( + callback, "GET", "/preview_url", { + url: url + }, undefined, httpApi.PREFIX_MEDIA_R0 + ); +}; + /** * @param {string} roomId * @param {boolean} isTyping diff --git a/lib/http-api.js b/lib/http-api.js index 9d7edbc7004..92fdd085a71 100644 --- a/lib/http-api.js +++ b/lib/http-api.js @@ -47,6 +47,11 @@ module.exports.PREFIX_UNSTABLE = "/_matrix/client/unstable"; */ module.exports.PREFIX_IDENTITY_V1 = "/_matrix/identity/api/v1"; +/** + * URI path for the media repo API + */ +module.exports.PREFIX_MEDIA_R0 = "/_matrix/media/r0"; + /** * Construct a MatrixHttpApi. * @constructor From c7575f3f16bf3986200c0ab93d4b53e18b46294a Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 3 Apr 2016 01:18:01 +0100 Subject: [PATCH 2/4] cache url preview results --- lib/client.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/client.js b/lib/client.js index b5b1ea66b25..d154d2f4b73 100644 --- a/lib/client.js +++ b/lib/client.js @@ -185,6 +185,7 @@ function MatrixClient(opts) { this._ongoingScrollbacks = {}; this._txnCtr = 0; this.timelineSupport = Boolean(opts.timelineSupport); + this.urlPreviewCache = {}; } utils.inherits(MatrixClient, EventEmitter); @@ -1449,20 +1450,34 @@ MatrixClient.prototype.getCurrentUploads = function() { }; /** - * Get a preview of the given URL as OpenGraph metadata + * Get a preview of the given URL as OpenGraph metadata. + * Caches results to prevent hammering the server. + * @param {string} url The URL to get preview data for + * @param {Number} ts The timestamp (ms since epoch) for querying + * historical preview data. * @param {module:client.callback} callback Optional. * @return {module:client.Promise} Resolves: Object of OG metadata. * @return {module:http-api.MatrixError} Rejects: with an error response. * May return synthesized attributes if the URL lacked OG meta. */ -MatrixClient.prototype.getUrlPreview = function(url, callback) { - // FIXME: maintain a clientside cache to stop hammering synapse - // especially as synapse doesn't cache yet +MatrixClient.prototype.getUrlPreview = function(url, callback, ts) { + var key = ts + "_" + url; + var og = this.urlPreviewCache[key]; + if (og) { + return q(og); + } + + var self = this; return this._http.authedRequestWithPrefix( callback, "GET", "/preview_url", { - url: url + url: url, + ts: ts, }, undefined, httpApi.PREFIX_MEDIA_R0 - ); + ).then(function(response) { + // TODO: expire cache occasionally + self.urlPreviewCache[key] = response; + return response; + }); }; /** From c469ff4c8dbec219da804d0cc2e568497817b58d Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 3 Apr 2016 01:19:34 +0100 Subject: [PATCH 3/4] oops, fix sig --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index d154d2f4b73..e7a2adfc6ff 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1460,7 +1460,7 @@ MatrixClient.prototype.getCurrentUploads = function() { * @return {module:http-api.MatrixError} Rejects: with an error response. * May return synthesized attributes if the URL lacked OG meta. */ -MatrixClient.prototype.getUrlPreview = function(url, callback, ts) { +MatrixClient.prototype.getUrlPreview = function(url, ts, callback) { var key = ts + "_" + url; var og = this.urlPreviewCache[key]; if (og) { From 11e476093554ddc8512fe3bfed2d0da3b3300c32 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Fri, 8 Apr 2016 22:00:07 +0100 Subject: [PATCH 4/4] improve commentary --- lib/client.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index e7a2adfc6ff..e924e5d2f91 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1450,11 +1450,15 @@ MatrixClient.prototype.getCurrentUploads = function() { }; /** - * Get a preview of the given URL as OpenGraph metadata. + * Get a preview of the given URL as of (roughly) the given point in time, + * described as an object with OpenGraph keys and associated values. + * Attributes may be synthesized where actual OG metadata is lacking. * Caches results to prevent hammering the server. * @param {string} url The URL to get preview data for - * @param {Number} ts The timestamp (ms since epoch) for querying - * historical preview data. + * @param {Number} ts The preferred point in time that the preview should + * describe (ms since epoch). The preview returned will either be the most + * recent one preceding this timestamp if available, or failing that the next + * most recent available preview. * @param {module:client.callback} callback Optional. * @return {module:client.Promise} Resolves: Object of OG metadata. * @return {module:http-api.MatrixError} Rejects: with an error response.