2014-08-12 22:32:18 -04:00
|
|
|
/*
|
|
|
|
Copyright 2014 matrix.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-15 09:06:48 -04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2014-08-12 10:10:52 -04:00
|
|
|
angular.module('matrixService', [])
|
2014-08-14 08:59:33 -04:00
|
|
|
.factory('matrixService', ['$http', '$q', '$rootScope', function($http, $q, $rootScope) {
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
// 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) {
|
2014-08-15 09:06:48 -04:00
|
|
|
if (!config) {
|
|
|
|
console.warn("No config exists. Cannot perform request to "+path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
// Inject the access token
|
|
|
|
if (!params) {
|
|
|
|
params = {};
|
|
|
|
}
|
2014-08-15 09:06:48 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
params.access_token = config.access_token;
|
|
|
|
|
2014-08-18 10:50:55 -04:00
|
|
|
if (path.indexOf(prefixPath) !== 0) {
|
|
|
|
path = prefixPath + path;
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
return doBaseRequest(config.homeserver, method, path, params, data, undefined);
|
|
|
|
};
|
|
|
|
|
2014-08-21 07:36:14 -04:00
|
|
|
var doBaseRequest = function(baseUrl, method, path, params, data, headers, $httpParams) {
|
|
|
|
|
|
|
|
var request = {
|
2014-08-12 10:10:52 -04:00
|
|
|
method: method,
|
|
|
|
url: baseUrl + path,
|
|
|
|
params: params,
|
|
|
|
data: data,
|
|
|
|
headers: headers
|
2014-08-21 07:36:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add additional $http parameters
|
|
|
|
if ($httpParams) {
|
|
|
|
angular.extend(request, $httpParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $http(request);
|
2014-08-12 10:10:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
/****** Home server API ******/
|
|
|
|
prefix: prefixPath,
|
|
|
|
|
|
|
|
// Register an user
|
|
|
|
register: function(user_name, password) {
|
|
|
|
// The REST path spec
|
|
|
|
var path = "/register";
|
|
|
|
|
|
|
|
return doRequest("POST", path, undefined, {
|
|
|
|
user_id: user_name,
|
|
|
|
password: password
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create a room
|
|
|
|
create: function(room_id, visibility) {
|
|
|
|
// The REST path spec
|
2014-08-27 06:33:56 -04:00
|
|
|
var path = "/createRoom";
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
return doRequest("POST", path, undefined, {
|
|
|
|
visibility: visibility,
|
|
|
|
room_alias_name: room_id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// List all rooms joined or been invited to
|
2014-08-27 04:24:01 -04:00
|
|
|
rooms: function(limit, feedback) {
|
2014-08-12 10:10:52 -04:00
|
|
|
// The REST path spec
|
2014-08-27 04:24:01 -04:00
|
|
|
|
2014-08-26 11:19:17 -04:00
|
|
|
var path = "/initialSync";
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-27 04:24:01 -04:00
|
|
|
var params = {};
|
|
|
|
if (limit) {
|
|
|
|
params.limit = limit;
|
|
|
|
}
|
|
|
|
if (feedback) {
|
|
|
|
params.feedback = feedback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return doRequest("GET", path, params);
|
2014-08-12 10:10:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Joins a room
|
|
|
|
join: function(room_id) {
|
2014-08-26 09:59:31 -04:00
|
|
|
return this.membershipChange(room_id, undefined, "join");
|
2014-08-12 10:10:52 -04:00
|
|
|
},
|
|
|
|
|
2014-08-12 12:17:10 -04:00
|
|
|
joinAlias: function(room_alias) {
|
|
|
|
var path = "/join/$room_alias";
|
|
|
|
room_alias = encodeURIComponent(room_alias);
|
|
|
|
|
|
|
|
path = path.replace("$room_alias", room_alias);
|
|
|
|
|
2014-08-27 08:59:14 -04:00
|
|
|
// TODO: PUT with txn ID
|
|
|
|
return doRequest("POST", path, undefined, {});
|
2014-08-12 12:17:10 -04:00
|
|
|
},
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
// Invite a user to a room
|
|
|
|
invite: function(room_id, user_id) {
|
2014-08-26 05:24:47 -04:00
|
|
|
return this.membershipChange(room_id, user_id, "invite");
|
2014-08-12 10:10:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Leaves a room
|
|
|
|
leave: function(room_id) {
|
2014-08-26 09:59:31 -04:00
|
|
|
return this.membershipChange(room_id, undefined, "leave");
|
2014-08-26 05:24:47 -04:00
|
|
|
},
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-26 05:24:47 -04:00
|
|
|
membershipChange: function(room_id, user_id, membershipValue) {
|
|
|
|
// The REST path spec
|
2014-08-26 09:59:31 -04:00
|
|
|
var path = "/rooms/$room_id/$membership";
|
2014-08-26 05:24:47 -04:00
|
|
|
path = path.replace("$room_id", encodeURIComponent(room_id));
|
2014-08-26 09:59:31 -04:00
|
|
|
path = path.replace("$membership", encodeURIComponent(membershipValue));
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-26 09:59:31 -04:00
|
|
|
var data = {};
|
|
|
|
if (user_id !== undefined) {
|
|
|
|
data = { user_id: user_id };
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use PUT with transaction IDs
|
|
|
|
return doRequest("POST", path, undefined, data);
|
2014-08-12 10:10:52 -04:00
|
|
|
},
|
|
|
|
|
2014-08-18 11:11:08 -04:00
|
|
|
// Retrieves the room ID corresponding to a room alias
|
|
|
|
resolveRoomAlias:function(room_alias) {
|
2014-08-27 09:54:29 -04:00
|
|
|
var path = "/matrix/client/api/v1/directory/room/$room_alias";
|
2014-08-18 11:11:08 -04:00
|
|
|
room_alias = encodeURIComponent(room_alias);
|
|
|
|
|
|
|
|
path = path.replace("$room_alias", room_alias);
|
|
|
|
|
|
|
|
return doRequest("GET", path, undefined, {});
|
|
|
|
},
|
|
|
|
|
2014-08-26 12:21:48 -04:00
|
|
|
sendMessage: function(room_id, txn_id, content) {
|
2014-08-12 10:10:52 -04:00
|
|
|
// The REST path spec
|
2014-08-26 12:21:48 -04:00
|
|
|
var path = "/rooms/$room_id/send/m.room.message/$txn_id";
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-26 12:21:48 -04:00
|
|
|
if (!txn_id) {
|
|
|
|
txn_id = "m" + new Date().getTime();
|
2014-08-12 10:10:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like the cmd client, escape room ids
|
|
|
|
room_id = encodeURIComponent(room_id);
|
|
|
|
|
|
|
|
// Customize it
|
|
|
|
path = path.replace("$room_id", room_id);
|
2014-08-26 12:21:48 -04:00
|
|
|
path = path.replace("$txn_id", txn_id);
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
return doRequest("PUT", path, undefined, content);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Send a text message
|
|
|
|
sendTextMessage: function(room_id, body, msg_id) {
|
|
|
|
var content = {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: body
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.sendMessage(room_id, msg_id, content);
|
|
|
|
},
|
|
|
|
|
2014-08-13 05:42:28 -04:00
|
|
|
// Send an image message
|
2014-08-20 10:18:50 -04:00
|
|
|
sendImageMessage: function(room_id, image_url, image_body, msg_id) {
|
2014-08-13 05:42:28 -04:00
|
|
|
var content = {
|
|
|
|
msgtype: "m.image",
|
|
|
|
url: image_url,
|
2014-08-20 10:18:50 -04:00
|
|
|
body: image_body
|
2014-08-13 05:42:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return this.sendMessage(room_id, msg_id, content);
|
|
|
|
},
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
// Send an emote message
|
|
|
|
sendEmoteMessage: function(room_id, body, msg_id) {
|
|
|
|
var content = {
|
|
|
|
msgtype: "m.emote",
|
|
|
|
body: body
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.sendMessage(room_id, msg_id, content);
|
|
|
|
},
|
|
|
|
|
|
|
|
// get a snapshot of the members in a room.
|
|
|
|
getMemberList: function(room_id) {
|
|
|
|
// Like the cmd client, escape room ids
|
|
|
|
room_id = encodeURIComponent(room_id);
|
|
|
|
|
2014-08-26 11:19:17 -04:00
|
|
|
var path = "/rooms/$room_id/members";
|
2014-08-12 10:10:52 -04:00
|
|
|
path = path.replace("$room_id", room_id);
|
|
|
|
return doRequest("GET", path);
|
|
|
|
},
|
2014-08-14 12:23:47 -04:00
|
|
|
|
|
|
|
paginateBackMessages: function(room_id, from_token, limit) {
|
2014-08-26 11:19:17 -04:00
|
|
|
var path = "/rooms/$room_id/messages";
|
2014-08-14 12:23:47 -04:00
|
|
|
path = path.replace("$room_id", room_id);
|
|
|
|
var params = {
|
|
|
|
from: from_token,
|
2014-08-19 09:19:48 -04:00
|
|
|
limit: limit,
|
|
|
|
dir: 'b'
|
2014-08-14 12:23:47 -04:00
|
|
|
};
|
|
|
|
return doRequest("GET", path, params);
|
|
|
|
},
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
// get a list of public rooms on your home server
|
|
|
|
publicRooms: function() {
|
2014-08-27 09:51:59 -04:00
|
|
|
var path = "/publicRooms"
|
2014-08-12 10:10:52 -04:00
|
|
|
return doRequest("GET", path);
|
|
|
|
},
|
|
|
|
|
|
|
|
// get a display name for this user ID
|
|
|
|
getDisplayName: function(userId) {
|
|
|
|
return this.getProfileInfo(userId, "displayname");
|
|
|
|
},
|
|
|
|
|
|
|
|
// get the profile picture url for this user ID
|
|
|
|
getProfilePictureUrl: function(userId) {
|
|
|
|
return this.getProfileInfo(userId, "avatar_url");
|
|
|
|
},
|
|
|
|
|
|
|
|
// update your display name
|
|
|
|
setDisplayName: function(newName) {
|
|
|
|
var content = {
|
|
|
|
displayname: newName
|
|
|
|
};
|
|
|
|
return this.setProfileInfo(content, "displayname");
|
|
|
|
},
|
|
|
|
|
|
|
|
// update your profile picture url
|
|
|
|
setProfilePictureUrl: function(newUrl) {
|
|
|
|
var content = {
|
|
|
|
avatar_url: newUrl
|
|
|
|
};
|
|
|
|
return this.setProfileInfo(content, "avatar_url");
|
|
|
|
},
|
|
|
|
|
|
|
|
setProfileInfo: function(data, info_segment) {
|
|
|
|
var path = "/profile/$user/" + info_segment;
|
|
|
|
path = path.replace("$user", config.user_id);
|
|
|
|
return doRequest("PUT", path, undefined, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
getProfileInfo: function(userId, info_segment) {
|
|
|
|
var path = "/profile/$user_id/" + info_segment;
|
|
|
|
path = path.replace("$user_id", userId);
|
|
|
|
return doRequest("GET", path);
|
|
|
|
},
|
|
|
|
|
|
|
|
login: function(userId, password) {
|
|
|
|
// TODO We should be checking to make sure the client can support
|
|
|
|
// logging in to this HS, else use the fallback.
|
|
|
|
var path = "/login";
|
|
|
|
var data = {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": userId,
|
|
|
|
"password": password
|
|
|
|
};
|
|
|
|
return doRequest("POST", path, undefined, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
// hit the Identity Server for a 3PID request.
|
2014-08-22 05:34:27 -04:00
|
|
|
linkEmail: function(email, clientSecret, sendAttempt) {
|
2014-08-12 10:10:52 -04:00
|
|
|
var path = "/matrix/identity/api/v1/validate/email/requestToken"
|
2014-08-22 05:34:27 -04:00
|
|
|
var data = "clientSecret="+clientSecret+"&email=" + encodeURIComponent(email)+"&sendAttempt="+sendAttempt;
|
2014-08-12 10:10:52 -04:00
|
|
|
var headers = {};
|
|
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
|
|
return doBaseRequest(config.identityServer, "POST", path, {}, data, headers);
|
|
|
|
},
|
|
|
|
|
2014-08-22 05:34:27 -04:00
|
|
|
authEmail: function(clientSecret, tokenId, code) {
|
2014-08-12 10:10:52 -04:00
|
|
|
var path = "/matrix/identity/api/v1/validate/email/submitToken";
|
2014-08-22 05:34:27 -04:00
|
|
|
var data = "token="+code+"&sid="+tokenId+"&clientSecret="+clientSecret;
|
|
|
|
var headers = {};
|
|
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
|
|
return doBaseRequest(config.identityServer, "POST", path, {}, data, headers);
|
|
|
|
},
|
|
|
|
|
|
|
|
bindEmail: function(userId, tokenId, clientSecret) {
|
|
|
|
var path = "/matrix/identity/api/v1/3pid/bind";
|
|
|
|
var data = "mxid="+encodeURIComponent(userId)+"&sid="+tokenId+"&clientSecret="+clientSecret;
|
2014-08-12 10:10:52 -04:00
|
|
|
var headers = {};
|
|
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
|
|
return doBaseRequest(config.identityServer, "POST", path, {}, data, headers);
|
|
|
|
},
|
|
|
|
|
2014-08-18 10:50:55 -04:00
|
|
|
uploadContent: function(file) {
|
|
|
|
var path = "/matrix/content";
|
|
|
|
var headers = {
|
|
|
|
"Content-Type": undefined // undefined means angular will figure it out
|
|
|
|
};
|
|
|
|
var params = {
|
|
|
|
access_token: config.access_token
|
|
|
|
};
|
2014-08-21 07:36:14 -04:00
|
|
|
|
|
|
|
// If the file is actually a Blob object, prevent $http from JSON-stringified it before sending
|
|
|
|
// (Equivalent to jQuery ajax processData = false)
|
|
|
|
var $httpParams;
|
|
|
|
if (file instanceof Blob) {
|
|
|
|
$httpParams = {
|
|
|
|
transformRequest: angular.identity
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return doBaseRequest(config.homeserver, "POST", path, params, file, headers, $httpParams);
|
2014-08-18 10:50:55 -04:00
|
|
|
},
|
|
|
|
|
2014-08-15 05:20:14 -04:00
|
|
|
// start listening on /events
|
|
|
|
getEventStream: function(from, timeout) {
|
|
|
|
var path = "/events";
|
|
|
|
var params = {
|
|
|
|
from: from,
|
|
|
|
timeout: timeout
|
|
|
|
};
|
|
|
|
return doRequest("GET", path, params);
|
|
|
|
},
|
2014-08-18 04:44:29 -04:00
|
|
|
|
|
|
|
// Indicates if user authentications details are stored in cache
|
|
|
|
isUserLoggedIn: function() {
|
|
|
|
var config = this.config();
|
|
|
|
|
|
|
|
// User is considered logged in if his cache is not empty and contains
|
|
|
|
// an access token
|
|
|
|
if (config && config.access_token) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-14 08:59:33 -04:00
|
|
|
},
|
2014-08-26 10:25:27 -04:00
|
|
|
|
|
|
|
// Enum of presence state
|
|
|
|
presence: {
|
|
|
|
offline: "offline",
|
|
|
|
unavailable: "unavailable",
|
|
|
|
online: "online",
|
|
|
|
free_for_chat: "free_for_chat"
|
|
|
|
},
|
|
|
|
|
|
|
|
// Set the logged in user presence state
|
|
|
|
setUserPresence: function(presence) {
|
|
|
|
var path = "/presence/$user_id/status";
|
|
|
|
path = path.replace("$user_id", config.user_id);
|
|
|
|
return doRequest("PUT", path, undefined, {
|
|
|
|
state: presence
|
|
|
|
});
|
|
|
|
},
|
2014-08-18 04:44:29 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
/****** Permanent storage of user information ******/
|
|
|
|
|
|
|
|
// Returns the current config
|
|
|
|
config: function() {
|
|
|
|
if (!config) {
|
|
|
|
config = localStorage.getItem("config");
|
|
|
|
if (config) {
|
|
|
|
config = JSON.parse(config);
|
|
|
|
|
|
|
|
// Reset the cache if the version loaded is not the expected one
|
|
|
|
if (configVersion !== config.version) {
|
|
|
|
config = undefined;
|
|
|
|
this.saveConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Set a new config (Use saveConfig to actually store it permanently)
|
|
|
|
setConfig: function(newConfig) {
|
|
|
|
config = newConfig;
|
2014-08-22 05:34:27 -04:00
|
|
|
console.log("new IS: "+config.identityServer);
|
2014-08-12 10:10:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Commits config into permanent storage
|
|
|
|
saveConfig: function() {
|
|
|
|
config.version = configVersion;
|
|
|
|
localStorage.setItem("config", JSON.stringify(config));
|
|
|
|
},
|
2014-08-27 09:07:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
/****** Room aliases management ******/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enhance data returned by rooms() and publicRooms() by adding room_alias
|
|
|
|
* & room_display_name which are computed from data already retrieved from the server.
|
|
|
|
* @param {Array} data the response of rooms() and publicRooms()
|
|
|
|
* @returns {Array} the same array with enriched objects
|
|
|
|
*/
|
|
|
|
assignRoomAliases: function(data) {
|
|
|
|
for (var i=0; i<data.length; i++) {
|
|
|
|
var alias = this.getRoomIdToAliasMapping(data[i].room_id);
|
|
|
|
if (alias) {
|
|
|
|
// use the existing alias from storage
|
|
|
|
data[i].room_alias = alias;
|
|
|
|
data[i].room_display_name = alias;
|
|
|
|
}
|
|
|
|
else if (data[i].aliases && data[i].aliases[0]) {
|
|
|
|
// save the mapping
|
|
|
|
// TODO: select the smarter alias from the array
|
|
|
|
this.createRoomIdToAliasMapping(data[i].room_id, data[i].aliases[0]);
|
|
|
|
data[i].room_display_name = data[i].aliases[0];
|
|
|
|
}
|
|
|
|
else if (data[i].membership == "invite" && "inviter" in data[i]) {
|
|
|
|
data[i].room_display_name = data[i].inviter + "'s room"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// last resort use the room id
|
|
|
|
data[i].room_display_name = data[i].room_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
},
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
createRoomIdToAliasMapping: function(roomId, alias) {
|
|
|
|
localStorage.setItem(MAPPING_PREFIX+roomId, alias);
|
|
|
|
},
|
|
|
|
|
|
|
|
getRoomIdToAliasMapping: function(roomId) {
|
|
|
|
return localStorage.getItem(MAPPING_PREFIX+roomId);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}]);
|