From f2cd0b13fe9d16b759deadd9063e3c9fbcf7b220 Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Mon, 27 Oct 2014 20:29:24 +0000 Subject: [PATCH] Bug 1074672 Part 3 - Update the Loop room list to use the new mozLoop.rooms API. r=Standard8 --- browser/components/loop/content/js/panel.js | 6 +- browser/components/loop/content/js/panel.jsx | 6 +- .../loop/content/shared/js/actions.js | 16 +++ .../loop/content/shared/js/roomListStore.js | 103 ++++++++---------- .../loop/test/desktop-local/panel_test.js | 5 + .../loop/test/shared/roomListStore_test.js | 10 +- browser/components/loop/ui/fake-mozLoop.js | 46 ++++++++ browser/components/loop/ui/ui-showcase.js | 2 +- browser/components/loop/ui/ui-showcase.jsx | 2 +- 9 files changed, 121 insertions(+), 75 deletions(-) diff --git a/browser/components/loop/content/js/panel.js b/browser/components/loop/content/js/panel.js index 75cf30099ad4..77e7c9484414 100644 --- a/browser/components/loop/content/js/panel.js +++ b/browser/components/loop/content/js/panel.js @@ -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() { diff --git a/browser/components/loop/content/js/panel.jsx b/browser/components/loop/content/js/panel.jsx index 04771f774415..a6b5a0a8fee4 100644 --- a/browser/components/loop/content/js/panel.jsx +++ b/browser/components/loop/content/js/panel.jsx @@ -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() { diff --git a/browser/components/loop/content/shared/js/actions.js b/browser/components/loop/content/shared/js/actions.js index 8ac8c765cd09..416dfb65db94 100644 --- a/browser/components/loop/content/shared/js/actions.js +++ b/browser/components/loop/content/shared/js/actions.js @@ -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. diff --git a/browser/components/loop/content/shared/js/roomListStore.js b/browser/components/loop/content/shared/js/roomListStore.js index 2871f630d3cf..97b1638cb66c 100644 --- a/browser/components/loop/content/shared/js/roomListStore.js +++ b/browser/components/loop/content/shared/js/roomListStore.js @@ -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; diff --git a/browser/components/loop/test/desktop-local/panel_test.js b/browser/components/loop/test/desktop-local/panel_test.js index 49d5c9c17866..c70504733f74 100644 --- a/browser/components/loop/test/desktop-local/panel_test.js +++ b/browser/components/loop/test/desktop-local/panel_test.js @@ -49,6 +49,11 @@ describe("loop.panel", function() { callback(null, []); }, on: sandbox.stub() + }, + rooms: { + getAll: function(callback) { + callback(null, []); + } } }; diff --git a/browser/components/loop/test/shared/roomListStore_test.js b/browser/components/loop/test/shared/roomListStore_test.js index d2e60de83b7c..b52175d18b29 100644 --- a/browser/components/loop/test/shared/roomListStore_test.js +++ b/browser/components/loop/test/shared/roomListStore_test.js @@ -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); diff --git a/browser/components/loop/ui/fake-mozLoop.js b/browser/components/loop/ui/fake-mozLoop.js index 5a5e7717bf1f..b212a00e84e7 100644 --- a/browser/components/loop/ui/fake-mozLoop.js +++ b/browser/components/loop/ui/fake-mozLoop.js @@ -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 }; diff --git a/browser/components/loop/ui/ui-showcase.js b/browser/components/loop/ui/ui-showcase.js index 91e1fef79e81..d3094b981f8e 100644 --- a/browser/components/loop/ui/ui-showcase.js +++ b/browser/components/loop/ui/ui-showcase.js @@ -59,7 +59,7 @@ var dispatcher = new loop.Dispatcher(); var roomListStore = new loop.store.RoomListStore({ dispatcher: dispatcher, - mozLoop: {} + mozLoop: navigator.mozLoop }); // Local mocks diff --git a/browser/components/loop/ui/ui-showcase.jsx b/browser/components/loop/ui/ui-showcase.jsx index 455abe826ac0..6fda09c81c14 100644 --- a/browser/components/loop/ui/ui-showcase.jsx +++ b/browser/components/loop/ui/ui-showcase.jsx @@ -59,7 +59,7 @@ var dispatcher = new loop.Dispatcher(); var roomListStore = new loop.store.RoomListStore({ dispatcher: dispatcher, - mozLoop: {} + mozLoop: navigator.mozLoop }); // Local mocks