Bug 1074672 Part 3 - Update the Loop room list to use the new mozLoop.rooms API. r=Standard8

This commit is contained in:
Nicolas Perriault 2014-10-27 20:29:24 +00:00
parent 2b9758ae20
commit f2cd0b13fe
9 changed files with 121 additions and 75 deletions

View File

@ -539,11 +539,7 @@ loop.panel = (function(_, mozL10n) {
},
_onRoomListChanged: function() {
var storeState = this.props.store.getStoreState();
this.setState({
error: storeState.error,
rooms: storeState.rooms
});
this.setState(this.props.store.getStoreState());
},
_getListHeading: function() {

View File

@ -539,11 +539,7 @@ loop.panel = (function(_, mozL10n) {
},
_onRoomListChanged: function() {
var storeState = this.props.store.getStoreState();
this.setState({
error: storeState.error,
rooms: storeState.rooms
});
this.setState(this.props.store.getStoreState());
},
_getListHeading: function() {

View File

@ -134,6 +134,22 @@ loop.shared.actions = (function() {
GetAllRooms: Action.define("getAllRooms", {
}),
/**
* An error occured while trying to fetch the room list.
* XXX: should move to some roomActions module - refs bug 1079284
*/
GetAllRoomsError: Action.define("getAllRoomsError", {
error: String
}),
/**
* Updates room list.
* XXX: should move to some roomActions module - refs bug 1079284
*/
UpdateRoomList: Action.define("updateRoomList", {
roomList: Array
}),
/**
* Primes localRoomStore with roomLocalId, which triggers the EmptyRoomView
* to do any necessary setup.

View File

@ -10,49 +10,25 @@ loop.store = loop.store || {};
(function() {
"use strict";
/**
* Shared actions.
* @type {Object}
*/
var sharedActions = loop.shared.actions;
/**
* Room validation schema. See validate.js.
* @type {Object}
*/
var roomSchema = {
roomToken: String,
roomUrl: String,
roomName: String,
maxSize: Number,
currSize: Number,
ctime: Number
roomToken: String,
roomUrl: String,
roomName: String,
maxSize: Number,
participants: Array,
ctime: Number
};
/**
* Temporary sample raw room list data.
* XXX Should be removed when we plug the real mozLoop API for rooms.
* See bug 1074664.
* @type {Array}
*/
var temporaryRawRoomList = [{
roomToken: "_nxD4V4FflQ",
roomUrl: "http://sample/_nxD4V4FflQ",
roomName: "First Room Name",
maxSize: 2,
currSize: 0,
ctime: 1405517546
}, {
roomToken: "QzBbvGmIZWU",
roomUrl: "http://sample/QzBbvGmIZWU",
roomName: "Second Room Name",
maxSize: 2,
currSize: 0,
ctime: 1405517418
}, {
roomToken: "3jKS_Els9IU",
roomUrl: "http://sample/3jKS_Els9IU",
roomName: "Third Room Name",
maxSize: 3,
clientMaxSize: 2,
currSize: 1,
ctime: 1405518241
}];
/**
* Room type. Basically acts as a typed object constructor.
*
@ -95,7 +71,9 @@ loop.store = loop.store || {};
this.dispatcher.register(this, [
"getAllRooms",
"openRoom"
"getAllRoomsError",
"openRoom",
"updateRoomList"
]);
}
@ -119,21 +97,6 @@ loop.store = loop.store || {};
this.trigger("change");
},
/**
* Proxy to navigator.mozLoop.rooms.getAll.
* XXX Could probably be removed when bug 1074664 lands.
*
* @param {Function} cb Callback(error, roomList)
*/
_fetchRoomList: function(cb) {
// Faking this.mozLoop.rooms until it's available; bug 1074664.
if (!this.mozLoop.hasOwnProperty("rooms")) {
cb(null, temporaryRawRoomList);
return;
}
this.mozLoop.rooms.getAll(cb);
},
/**
* Maps and sorts the raw room list received from the mozLoop API.
*
@ -158,13 +121,37 @@ loop.store = loop.store || {};
* Gather the list of all available rooms from the MozLoop API.
*/
getAllRooms: function() {
this._fetchRoomList(function(err, rawRoomList) {
this.setStoreState({
error: err,
rooms: this._processRawRoomList(rawRoomList)
});
this.mozLoop.rooms.getAll(function(err, rawRoomList) {
var action;
if (err) {
action = new sharedActions.GetAllRoomsError({error: err});
} else {
action = new sharedActions.UpdateRoomList({roomList: rawRoomList});
}
this.dispatcher.dispatch(action);
}.bind(this));
}
},
/**
* Updates current error state in case getAllRooms failed.
*
* @param {sharedActions.UpdateRoomListError} actionData The action data.
*/
getAllRoomsError: function(actionData) {
this.setStoreState({error: actionData.error});
},
/**
* Updates current room list.
*
* @param {sharedActions.UpdateRoomList} actionData The action data.
*/
updateRoomList: function(actionData) {
this.setStoreState({
error: undefined,
rooms: this._processRawRoomList(actionData.roomList)
});
},
}, Backbone.Events);
loop.store.RoomListStore = RoomListStore;

View File

@ -49,6 +49,11 @@ describe("loop.panel", function() {
callback(null, []);
},
on: sandbox.stub()
},
rooms: {
getAll: function(callback) {
callback(null, []);
}
}
};

View File

@ -50,14 +50,14 @@ describe("loop.store.RoomListStore", function () {
roomUrl: "http://sample/_nxD4V4FflQ",
roomName: "First Room Name",
maxSize: 2,
currSize: 0,
participants: [],
ctime: 1405517546
}, {
roomToken: "QzBbvGmIZWU",
roomUrl: "http://sample/QzBbvGmIZWU",
roomName: "Second Room Name",
maxSize: 2,
currSize: 0,
participants: [],
ctime: 1405517418
}, {
roomToken: "3jKS_Els9IU",
@ -65,7 +65,7 @@ describe("loop.store.RoomListStore", function () {
roomName: "Third Room Name",
maxSize: 3,
clientMaxSize: 2,
currSize: 1,
participants: [],
ctime: 1405518241
}];
@ -93,7 +93,7 @@ describe("loop.store.RoomListStore", function () {
it("should fetch the room list from the mozLoop API", function(done) {
store.once("change", function() {
expect(store.getStoreState().error).to.be.a.null;
expect(store.getStoreState().error).to.be.a.undefined;
expect(store.getStoreState().rooms).to.have.length.of(3);
done();
});
@ -104,7 +104,7 @@ describe("loop.store.RoomListStore", function () {
it("should order the room list using ctime desc", function(done) {
store.once("change", function() {
var storeState = store.getStoreState();
expect(storeState.error).to.be.a.null;
expect(storeState.error).to.be.a.undefined;
expect(storeState.rooms[0].ctime).eql(1405518241);
expect(storeState.rooms[1].ctime).eql(1405517546);
expect(storeState.rooms[2].ctime).eql(1405517418);

View File

@ -2,6 +2,47 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Sample from https://wiki.mozilla.org/Loop/Architecture/Rooms#GET_.2Frooms
var fakeRooms = [
{
"roomToken": "_nxD4V4FflQ",
"roomName": "First Room Name",
"roomUrl": "http://localhost:3000/rooms/_nxD4V4FflQ",
"roomOwner": "Alexis",
"maxSize": 2,
"creationTime": 1405517546,
"ctime": 1405517546,
"expiresAt": 1405534180,
"participants": []
},
{
"roomToken": "QzBbvGmIZWU",
"roomName": "Second Room Name",
"roomUrl": "http://localhost:3000/rooms/QzBbvGmIZWU",
"roomOwner": "Alexis",
"maxSize": 2,
"creationTime": 1405517546,
"ctime": 1405517546,
"expiresAt": 1405534180,
"participants": []
},
{
"roomToken": "3jKS_Els9IU",
"roomName": "UX Discussion",
"roomUrl": "http://localhost:3000/rooms/3jKS_Els9IU",
"roomOwner": "Alexis",
"maxSize": 2,
"clientMaxSize": 2,
"creationTime": 1405517546,
"ctime": 1405517818,
"expiresAt": 1405534180,
"participants": [
{ "displayName": "Alexis", "account": "alexis@example.com", "roomConnectionId": "2a1787a6-4a73-43b5-ae3e-906ec1e763cb" },
{ "displayName": "Adam", "roomConnectionId": "781f012b-f1ea-4ce1-9105-7cfc36fb4ec7" }
]
}
];
/**
* Faking the mozLoop object which doesn't exist in regular web pages.
* @type {Object}
@ -22,5 +63,10 @@ navigator.mozLoop = {
},
on: function() {}
},
rooms: {
getAll: function(callback) {
callback(null, fakeRooms);
}
},
fxAEnabled: true
};

View File

@ -59,7 +59,7 @@
var dispatcher = new loop.Dispatcher();
var roomListStore = new loop.store.RoomListStore({
dispatcher: dispatcher,
mozLoop: {}
mozLoop: navigator.mozLoop
});
// Local mocks

View File

@ -59,7 +59,7 @@
var dispatcher = new loop.Dispatcher();
var roomListStore = new loop.store.RoomListStore({
dispatcher: dispatcher,
mozLoop: {}
mozLoop: navigator.mozLoop
});
// Local mocks