1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Fix up stylistic warnings.

This commit is contained in:
Kegan Dougal
2015-06-03 10:37:41 +01:00
parent 67931f05ce
commit bdaad5b940
2 changed files with 112 additions and 76 deletions

View File

@@ -34,5 +34,9 @@ matrixcs.request(function(opts, callback) {
}); });
return defer.promise; return defer.promise;
}); });
/**
* Export a modified matrix library with Promise support.
*/
module.exports = matrixcs; module.exports = matrixcs;

View File

@@ -9,7 +9,7 @@ TODO:
*/ */
// wrap in a closure for browsers // wrap in a closure for browsers
var init = function(exports){ var init = function(exports) {
// expose the underlying request object so different environments can use // expose the underlying request object so different environments can use
// different request libs (e.g. request or browser-request) // different request libs (e.g. request or browser-request)
var request; var request;
@@ -35,7 +35,7 @@ var init = function(exports){
var requiredKeys = [ var requiredKeys = [
"baseUrl" "baseUrl"
]; ];
for (var i=0; i<requiredKeys.length; i++) { for (var i = 0; i < requiredKeys.length; i++) {
if (!credentials.hasOwnProperty(requiredKeys[i])) { if (!credentials.hasOwnProperty(requiredKeys[i])) {
throw new Error("Missing required key: " + requiredKeys[i]); throw new Error("Missing required key: " + requiredKeys[i]);
} }
@@ -151,7 +151,8 @@ var init = function(exports){
* @param {String} type the type of the state events to be returned (optional) * @param {String} type the type of the state events to be returned (optional)
* @param {String} stateKey the stateKey of the state events to be returned * @param {String} stateKey the stateKey of the state events to be returned
* (optional, requires type to be specified) * (optional, requires type to be specified)
* @return {MatrixEvent[]} an array of MatrixEvents from the store, filtered by roomid, type and state key. * @return {MatrixEvent[]} an array of MatrixEvents from the store,
* filtered by roomid, type and state key.
*/ */
getStateEvents: function(roomId, type, stateKey) { getStateEvents: function(roomId, type, stateKey) {
var stateEvents = []; var stateEvents = [];
@@ -160,7 +161,9 @@ var init = function(exports){
if (this.rooms[roomId].state.hasOwnProperty(type)) { if (this.rooms[roomId].state.hasOwnProperty(type)) {
for (stateKey in this.rooms[roomId].state[type]) { for (stateKey in this.rooms[roomId].state[type]) {
if (this.rooms[roomId].state[type].hasOwnProperty(stateKey)) { if (this.rooms[roomId].state[type].hasOwnProperty(stateKey)) {
stateEvents.push(this.rooms[roomId].state[type][stateKey]); stateEvents.push(
this.rooms[roomId].state[type][stateKey]
);
} }
} }
} }
@@ -186,7 +189,8 @@ var init = function(exports){
* @param {String} roomId the Room ID whose state is to be returned * @param {String} roomId the Room ID whose state is to be returned
* @param {String} type the type of the state events to be returned * @param {String} type the type of the state events to be returned
* @param {String} stateKey the stateKey of the state events to be returned * @param {String} stateKey the stateKey of the state events to be returned
* @return {MatrixEvent} a single MatrixEvent from the store, filtered by roomid, type and state key. * @return {MatrixEvent} a single MatrixEvent from the store, filtered
* by roomid, type and state key.
*/ */
getStateEvent: function(roomId, type, stateKey) { getStateEvent: function(roomId, type, stateKey) {
return this.rooms[roomId].state[type][stateKey]; return this.rooms[roomId].state[type][stateKey];
@@ -268,12 +272,14 @@ var init = function(exports){
// tracking current display name / avatar per-message // tracking current display name / avatar per-message
// pagination // pagination
// re-sending (including persisting pending messages to be sent) // re-sending (including persisting pending messages to be sent)
// - Need a nice way to callback the app for arbitrary events like displayname changes // - Need a nice way to callback the app for arbitrary events like
// displayname changes
// due to ambiguity (or should this be on a chat-specific layer)? // due to ambiguity (or should this be on a chat-specific layer)?
// reconnect after connectivity outages // reconnect after connectivity outages
/* /*
* Helper method for retrieving the name of a room suitable for display in the UI * Helper method for retrieving the name of a room suitable for display
* in the UI
* TODO: in future, this should be being generated serverside. * TODO: in future, this should be being generated serverside.
* @param {String} roomId ID of room whose name is to be resolved * @param {String} roomId ID of room whose name is to be resolved
* @return {String} human-readable label for room. * @return {String} human-readable label for room.
@@ -284,7 +290,8 @@ var init = function(exports){
return roomId; return roomId;
} }
// check for an alias, if any. for now, assume first alias is the official one. // check for an alias, if any. for now, assume first alias is the
// official one.
var alias; var alias;
var mRoomAliases = this.store.getStateEvents(roomId, 'm.room.aliases')[0]; var mRoomAliases = this.store.getStateEvents(roomId, 'm.room.aliases')[0];
if (mRoomAliases) { if (mRoomAliases) {
@@ -293,7 +300,7 @@ var init = function(exports){
var mRoomName = this.store.getStateEvent(roomId, 'm.room.name', ''); var mRoomName = this.store.getStateEvent(roomId, 'm.room.name', '');
if (mRoomName) { if (mRoomName) {
return mRoomName.event.content.name + (alias ? " (" + alias + ")": ""); return mRoomName.event.content.name + (alias ? " (" + alias + ")" : "");
} }
else if (alias) { else if (alias) {
return alias; return alias;
@@ -309,26 +316,41 @@ var init = function(exports){
return "Unknown"; return "Unknown";
} }
else if (members.length == 1) { else if (members.length == 1) {
return members[0].event.content.displayname || members[0].event.user_id; return (
members[0].event.content.displayname ||
members[0].event.user_id
);
} }
else if (members.length == 2) { else if (members.length == 2) {
return (members[0].event.content.displayname || members[0].event.user_id) + " and " + return (
(members[1].event.content.displayname || members[1].event.user_id); (members[0].event.content.displayname ||
members[0].event.user_id) +
" and " +
(members[1].event.content.displayname ||
members[1].event.user_id)
);
} }
else { else {
return (members[0].event.content.displayname || members[0].event.user_id) + " and " + return (
(members.length - 1) + " others"; (members[0].event.content.displayname ||
members[0].event.user_id) +
" and " +
(members.length - 1) + " others"
);
} }
} }
}, },
/* /*
* Helper method for retrieving the name of a user suitable for display in the UI * Helper method for retrieving the name of a user suitable for display
* in the context of a room - i.e. disambiguating from any other users in the room. * in the UI in the context of a room - i.e. disambiguating from any
* other users in the room.
* XXX: This could perhaps also be generated serverside, perhaps by just passing * XXX: This could perhaps also be generated serverside, perhaps by just passing
* a 'disambiguate' flag down on membership entries which have ambiguous displaynames? * a 'disambiguate' flag down on membership entries which have ambiguous
* displaynames?
* @param {String} userId ID of the user whose name is to be resolved * @param {String} userId ID of the user whose name is to be resolved
* @param {String} roomId ID of room to be used as the context for resolving the name * @param {String} roomId ID of room to be used as the context for
* resolving the name.
* @return {String} human-readable name of the user. * @return {String} human-readable name of the user.
*/ */
getFriendlyDisplayName: function(userId, roomId) { getFriendlyDisplayName: function(userId, roomId) {
@@ -361,7 +383,8 @@ var init = function(exports){
* High level helper method to call initialSync, emit the resulting events, * High level helper method to call initialSync, emit the resulting events,
* and then start polling the eventStream for new events. * and then start polling the eventStream for new events.
* @param {function} callback Callback invoked whenever new event are available * @param {function} callback Callback invoked whenever new event are available
* @param {Number} historyLen amount of historical timeline events to emit during from the initial sync * @param {Number} historyLen amount of historical timeline events to
* emit during from the initial sync.
*/ */
startClient: function(callback, historyLen) { startClient: function(callback, historyLen) {
historyLen = historyLen || 12; historyLen = historyLen || 12;
@@ -371,7 +394,10 @@ var init = function(exports){
this.initialSync(historyLen, function(err, data) { this.initialSync(historyLen, function(err, data) {
if (err) { if (err) {
if (this.config && this.config.debug) { if (this.config && this.config.debug) {
console.error("startClient error on initialSync: %s", JSON.stringify(err)); console.error(
"startClient error on initialSync: %s",
JSON.stringify(err)
);
} }
callback(err); callback(err);
} else { } else {
@@ -385,7 +411,9 @@ var init = function(exports){
events.push(new MatrixEvent(data.rooms[i].state[j])); events.push(new MatrixEvent(data.rooms[i].state[j]));
} }
for (j = 0; j < data.rooms[i].messages.chunk.length; j++) { for (j = 0; j < data.rooms[i].messages.chunk.length; j++) {
events.push(new MatrixEvent(data.rooms[i].messages.chunk[j])); events.push(
new MatrixEvent(data.rooms[i].messages.chunk[j])
);
} }
} }
callback(undefined, events, false); callback(undefined, events, false);
@@ -407,7 +435,10 @@ var init = function(exports){
this.eventStream(this.fromToken, 30000, function(err, data) { this.eventStream(this.fromToken, 30000, function(err, data) {
if (err) { if (err) {
if (this.config && this.config.debug) { if (this.config && this.config.debug) {
console.error("error polling for events via eventStream: %s", JSON.stringify(err)); console.error(
"error polling for events via eventStream: %s",
JSON.stringify(err)
);
} }
callback(err); callback(err);
// retry every few seconds // retry every few seconds
@@ -427,7 +458,8 @@ var init = function(exports){
}, },
/* /*
* High level helper method to stop the client from polling and allow a clean shutdown * High level helper method to stop the client from polling and allow a
* clean shutdown.
*/ */
stopClient: function() { stopClient: function() {
this.clientRunning = false; this.clientRunning = false;
@@ -629,7 +661,7 @@ var init = function(exports){
); );
return this._doAuthedRequest(callback, "PUT", path, undefined, { return this._doAuthedRequest(callback, "PUT", path, undefined, {
membership : membershipValue, membership: membershipValue,
reason: reason reason: reason
}); });
}, },
@@ -658,9 +690,9 @@ var init = function(exports){
var path = info ? var path = info ?
encodeUri("/profile/$userId/$info", encodeUri("/profile/$userId/$info",
{ $userId: userId, $info: info } ) : { $userId: userId, $info: info }) :
encodeUri("/profile/$userId", encodeUri("/profile/$userId",
{ $userId: userId } ); { $userId: userId });
return this._doAuthedRequest(callback, "GET", path); return this._doAuthedRequest(callback, "GET", path);
}, },
@@ -710,7 +742,7 @@ var init = function(exports){
}); });
var validStates = ["offline", "online", "unavailable"]; var validStates = ["offline", "online", "unavailable"];
if (validStates.indexOf(presence) == -1) { if (validStates.indexOf(presence) == -1) {
throw new Error("Bad presence value: "+presence); throw new Error("Bad presence value: " + presence);
} }
var content = { var content = {
presence: presence presence: presence
@@ -758,7 +790,7 @@ var init = function(exports){
self.store.setPresenceEvents( self.store.setPresenceEvents(
map(data.presence, eventMapper) map(data.presence, eventMapper)
); );
for (var i = 0 ; i < data.rooms.length; i++) { for (var i = 0; i < data.rooms.length; i++) {
self.store.setStateEvents( self.store.setStateEvents(
map(data.rooms[i].state, eventMapper) map(data.rooms[i].state, eventMapper)
); );
@@ -868,7 +900,7 @@ var init = function(exports){
addPushRule: function(scope, kind, ruleId, body, callback) { addPushRule: function(scope, kind, ruleId, body, callback) {
// NB. Scope not uri encoded because devices need the '/' // NB. Scope not uri encoded because devices need the '/'
var path = encodeUri("/pushrules/"+scope+"/$kind/$ruleId", { var path = encodeUri("/pushrules/" + scope + "/$kind/$ruleId", {
$kind: kind, $kind: kind,
$ruleId: ruleId $ruleId: ruleId
}); });
@@ -879,7 +911,7 @@ var init = function(exports){
deletePushRule: function(scope, kind, ruleId, callback) { deletePushRule: function(scope, kind, ruleId, callback) {
// NB. Scope not uri encoded because devices need the '/' // NB. Scope not uri encoded because devices need the '/'
var path = encodeUri("/pushrules/"+scope+"/$kind/$ruleId", { var path = encodeUri("/pushrules/" + scope + "/$kind/$ruleId", {
$kind: kind, $kind: kind,
$ruleId: ruleId $ruleId: ruleId
}); });
@@ -954,8 +986,8 @@ var init = function(exports){
/** /**
* Get the content repository url with query parameters. * Get the content repository url with query parameters.
* @returns An object with a 'base', 'path' and 'params' for base URL, * @return {Object} An object with a 'base', 'path' and 'params' for
* path and query parameters respectively. * base URL, path and query parameters respectively.
*/ */
getContentUri: function() { getContentUri: function() {
var params = { var params = {
@@ -1069,7 +1101,7 @@ var init = function(exports){
if (typeof exports === 'undefined') { if (typeof exports === 'undefined') {
// this assigns to "window" on browsers // this assigns to "window" on browsers
init(this.matrixcs={}); // jshint ignore:line init(this.matrixcs = {}); // jshint ignore:line
} }
else { else {
init(exports); init(exports);