Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL previewing support #122

Merged
merged 5 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function MatrixClient(opts) {
this._ongoingScrollbacks = {};
this._txnCtr = 0;
this.timelineSupport = Boolean(opts.timelineSupport);
this.urlPreviewCache = {};
}
utils.inherits(MatrixClient, EventEmitter);

Expand Down Expand Up @@ -1448,6 +1449,37 @@ MatrixClient.prototype.getCurrentUploads = function() {
return this._http.getCurrentUploads();
};

/**
* 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit vague. Not everyone should be expected to read the react-sdk source to figure out what it means. Can you clarify how it is meant to be used?

* @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, ts, callback) {
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,
ts: ts,
}, undefined, httpApi.PREFIX_MEDIA_R0
).then(function(response) {
// TODO: expire cache occasionally
self.urlPreviewCache[key] = response;
return response;
});
};

/**
* @param {string} roomId
* @param {boolean} isTyping
Expand Down
5 changes: 5 additions & 0 deletions lib/http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down