Skip to content

Commit

Permalink
Merge pull request #1395 from matrix-org/travis/less-previews
Browse files Browse the repository at this point in the history
Batch up URL previews to prevent excessive requests
  • Loading branch information
turt2live authored Jun 2, 2020
2 parents bebeec7 + 76da708 commit 19fe9b8
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function MatrixClient(opts) {
this._isGuest = false;
this._ongoingScrollbacks = {};
this.timelineSupport = Boolean(opts.timelineSupport);
this.urlPreviewCache = {};
this.urlPreviewCache = {}; // key=preview key, value=Promise for preview (may be an error)
this._notifTimelineSet = null;
this.unstableClientRelationAggregation = !!opts.unstableClientRelationAggregation;

Expand Down Expand Up @@ -2991,25 +2991,32 @@ MatrixClient.prototype.setRoomReadMarkers = async function(
* May return synthesized attributes if the URL lacked OG meta.
*/
MatrixClient.prototype.getUrlPreview = function(url, ts, callback) {
// 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;

const key = ts + "_" + url;
const og = this.urlPreviewCache[key];
if (og) {
return Promise.resolve(og);

// If there's already a request in flight (or we've handled it), return that instead.
const cachedPreview = this.urlPreviewCache[key];
if (cachedPreview) {
if (callback) {
cachedPreview.then(callback).catch(callback);
}
return cachedPreview;
}

const self = this;
return this._http.authedRequest(
const resp = this._http.authedRequest(
callback, "GET", "/preview_url", {
url: url,
ts: ts,
}, undefined, {
prefix: PREFIX_MEDIA_R0,
},
).then(function(response) {
// TODO: expire cache occasionally
self.urlPreviewCache[key] = response;
return response;
});
);
// TODO: Expire the URL preview cache sometimes
this.urlPreviewCache[key] = resp;
return resp;
};

/**
Expand Down

0 comments on commit 19fe9b8

Please sign in to comment.