"use strict"; /* TODO: - CS: complete register function (doing stages) - Internal: rate limiting - Identity server: linkEmail, authEmail, bindEmail, lookup3pid - uploadContent (?) */ // wrap in a closure for browsers var init = function(exports){ // expose the underlying request object so different environments can use // different request libs (e.g. request or browser-request) var request; exports.request = function(r) { request = r; }; // entry point function MatrixClient(credentials) { if (typeof credentials === "string") { credentials = { "baseUrl": credentials }; } var requiredKeys = [ "baseUrl" ]; for (var i=0; i 0) { // these are thumbnailing params so they probably want the // thumbnailing API... prefix = "/_matrix/media/v1/thumbnail/"; } var fragmentOffset = serverAndMediaId.indexOf("#"), fragment = ""; if (fragmentOffset >= 0) { fragment = serverAndMediaId.substr(fragmentOffset); serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset); } return this.credentials.baseUrl + prefix + serverAndMediaId + (Object.keys(params).length === 0 ? "" : ("?" + encodeParams(params))) + fragment; }, getIdenticonUri: function(identiconString, width, height) { if (!identiconString) { return; } if (!width) { width = 96; } if (!height) { height = 96; } var params = { width: width, height: height }; var path = encodeUri("/_matrix/media/v1/identicon/$ident", { $ident: identiconString }); return this.credentials.baseUrl + path + (Object.keys(params).length === 0 ? "" : ("?" + encodeParams(params))); }, /** * Get the content repository url with query parameters. * @returns An object with a 'base', 'path' and 'params' for base URL, * path and query parameters respectively. */ getContentUri: function() { var params = { access_token: this.credentials.accessToken }; return { base: this.credentials.baseUrl, path: "/_matrix/media/v1/upload", params: params }; }, // Internals // ========= _doAuthedRequest: function(callback, method, path, params, data) { if (!params) { params = {}; } params.access_token = this.credentials.accessToken; return this._doRequest(callback, method, path, params, data); }, _doRequest: function(callback, method, path, params, data) { var fullUri = this.credentials.baseUrl + CLIENT_PREFIX + path; if (!params) { params = {}; } return this._request(callback, method, fullUri, params, data); }, _request: function(callback, method, uri, params, data) { console.log(" => %s", uri); console.log(" %s", JSON.stringify(data)); if (callback !== undefined && !isFunction(callback)) { throw Error("Expected callback to be a function"); } return request( { uri: uri, method: method, withCredentials: false, qs: params, body: data, json: true, headers: HEADERS }, requestCallback(callback) ); } }; var encodeUri = function(pathTemplate, variables) { for (var key in variables) { if (!variables.hasOwnProperty(key)) { continue; } pathTemplate = pathTemplate.replace( key, encodeURIComponent(variables[key]) ); } return pathTemplate; }; // avoiding deps on jquery and co var encodeParams = function(params) { var qs = ""; for (var key in params) { if (!params.hasOwnProperty(key)) { continue; } qs += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(params[key]); } return qs.substring(1); }; var requestCallback = function(userDefinedCallback) { if (!userDefinedCallback) { return undefined; } return function(err, response, body) { if (err) { return userDefinedCallback(err); } if (response.statusCode >= 400) { return userDefinedCallback(body); } else { userDefinedCallback(null, body); } }; }; var isFunction = function(value) { return Object.prototype.toString.call(value) == "[object Function]"; }; }; if (typeof exports === 'undefined') { init(this['matrixcs']={}); // this assigns to "window" on browsers } else { init(exports); }