/* Copyright 2014 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ 'use strict'; /* This service wraps up Matrix API calls. This serves to isolate the caller from changes to the underlying url paths, as well as attach common params (e.g. access_token) to requests. */ angular.module('matrixService', []) .factory('matrixService', ['$http', '$q', '$rootScope', function($http, $q, $rootScope) { /* * Permanent storage of user information * The config contains: * - homeserver url * - Identity server url * - user_id * - access_token * - version: the version of this cache */ var config; var roomIdToAlias = {}; var aliasToRoomId = {}; // Current version of permanent storage var configVersion = 0; var prefixPath = "/_matrix/client/api/v1"; var MAPPING_PREFIX = "alias_for_"; var doRequest = function(method, path, params, data, $httpParams) { if (!config) { console.warn("No config exists. Cannot perform request to "+path); return; } // Inject the access token if (!params) { params = {}; } params.access_token = config.access_token; if (path.indexOf(prefixPath) !== 0) { path = prefixPath + path; } return doBaseRequest(config.homeserver, method, path, params, data, undefined, $httpParams); }; var doBaseRequest = function(baseUrl, method, path, params, data, headers, $httpParams) { var request = { method: method, url: baseUrl + path, params: params, data: data, headers: headers }; // Add additional $http parameters if ($httpParams) { angular.extend(request, $httpParams); } return $http(request); }; var doRegisterLogin = function(path, loginType, sessionId, userName, password, threepidCreds) { var data = {}; if (loginType === "m.login.recaptcha") { var challengeToken = Recaptcha.get_challenge(); var captchaEntry = Recaptcha.get_response(); data = { type: "m.login.recaptcha", challenge: challengeToken, response: captchaEntry }; } else if (loginType === "m.login.email.identity") { data = { threepidCreds: threepidCreds }; } else if (loginType === "m.login.password") { data = { user: userName, password: password }; } if (sessionId) { data.session = sessionId; } data.type = loginType; console.log("doRegisterLogin >>> " + loginType); return doRequest("POST", path, undefined, data); }; return { /****** Home server API ******/ prefix: prefixPath, // Register an user register: function(user_name, password, threepidCreds, useCaptcha) { // registration is composed of multiple requests, to check you can // register, then to actually register. This deferred will fire when // all the requests are done, along with the final response. var deferred = $q.defer(); var path = "/register"; // check we can actually register with this HS. doRequest("GET", path, undefined, undefined).then( function(response) { console.log("/register [1] : "+JSON.stringify(response)); var flows = response.data.flows; var knownTypes = [ "m.login.password", "m.login.recaptcha", "m.login.email.identity" ]; // if they entered 3pid creds, we want to use a flow which uses it. var useThreePidFlow = threepidCreds != undefined; var flowIndex = 0; var firstRegType = undefined; for (var i=0; i