From b48fd4789a4b1cd331429db9a9deb60cdbdb7154 Mon Sep 17 00:00:00 2001 From: Abe Haskins Date: Sat, 14 May 2016 15:57:24 -0700 Subject: [PATCH 01/22] Updates for SDK 3.0.0 --- README.md | 45 +++++++--------------------------- src/js/firechat.js | 56 +++++++++++++++++++++++++++++++++---------- website/docs/index.md | 50 ++++++++++++++++++-------------------- 3 files changed, 76 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 541603d..743872c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Version](https://badge.fury.io/gh/firebase%2Ffirechat.svg)](http://badge.fury.io/gh/firebase%2Ffirechat) -Firechat is a simple, extensible chat widget powered by [Firebase](https://www.firebase.com/?utm_source=firechat). +Firechat is a simple, extensible chat widget powered by [Firebase](https://firebase.google.com/?utm_source=firechat). It is intended to serve as a concise, documented foundation for chat products built on Firebase. It works out of the box, and is easily extended. @@ -14,7 +14,7 @@ Visit [firechat.firebaseapp.com](https://firechat.firebaseapp.com/) to see a liv ## Setup -Firechat uses [Firebase](https://www.firebase.com/?utm_source=firechat) as a backend, so it requires no server-side +Firechat uses [Firebase](https://firebase.google.com/) as a backend, so it requires no server-side code. It can be added to any web app by including a few JavaScript files ```HTML @@ -22,39 +22,13 @@ code. It can be added to any web app by including a few JavaScript files - + - - + + ``` -giving your users a way to authenticate - -```HTML - - -Login with Twitter -``` - and initializing the chat. ```HTML @@ -73,12 +47,11 @@ For detailed integration instructions, see the [Firechat documentation](https:// ## Getting Started with Firebase Firechat requires Firebase in order to store data. You can -[sign up here](https://www.firebase.com/signup/?utm_source=firechat) for a free account. +[sign up here](https://firebase.google.com) for a free account. ## Getting Help -If you have a question about Firechat, search the -[Firebase tag on Stack Overflow](http://stackoverflow.com/questions/tagged/firebase) to see if it has already been -answered. If it hasn't been asked, post a [new question](http://stackoverflow.com/questions/ask?tags=firebase+firechat). +If you have a question about Firechat, search the +[Firebase tag on Stack Overflow](http://stackoverflow.com/questions/tagged/firebase) to see if it has already been +answered. If it hasn't been asked, post a [new question](http://stackoverflow.com/questions/ask?tags=firebase+firechat). We keep a close eye on those tags, and will answer your question soon. - diff --git a/src/js/firechat.js b/src/js/firechat.js index 4f62f6e..811e101 100644 --- a/src/js/firechat.js +++ b/src/js/firechat.js @@ -97,12 +97,12 @@ // Initialize Firebase listeners and callbacks for the supported bindings. _setupDataEvents: function() { // Monitor connection state so we can requeue disconnect operations if need be. - this._firebase.root().child('.info/connected').on('value', function(snapshot) { + this.getRoot(this._firebase).child('.info/connected').on('value', function(snapshot) { if (snapshot.val() === true) { // We're connected (or reconnected)! Set up our presence state. for (var i = 0; i < this._presenceBits; i++) { var op = this._presenceBits[i], - ref = this._firebase.root().child(op.ref); + ref = this.getRoot(this._firebase).child(op.ref); ref.onDisconnect().set(op.offlineValue); ref.set(op.onlineValue); @@ -112,7 +112,7 @@ // Generate a unique session id for the visit. var sessionRef = this._userRef.child('sessions').push(); - this._sessionId = sessionRef.key(); + this._sessionId = this.getKey(sessionRef); this._queuePresenceOperation(sessionRef, true, null); // Register our username in the public user listing. @@ -196,11 +196,11 @@ }, _onNewMessage: function(roomId, snapshot) { var message = snapshot.val(); - message.id = snapshot.key(); + message.id = Firechat.prototype.getKey(snapshot); this._invokeEventCallbacks('message-add', roomId, message); }, _onRemoveMessage: function(roomId, snapshot) { - var messageId = snapshot.key(); + var messageId = Firechat.prototype.getKey(snapshot); this._invokeEventCallbacks('message-remove', roomId, messageId); }, _onLeaveRoom: function(roomId) { @@ -212,7 +212,7 @@ var notification = snapshot.val(); if (!notification.read) { if (notification.notificationType !== 'suspension' || notification.data.suspendedUntil < new Date().getTime()) { - snapshot.ref().child('read').set(true); + snapshot.ref.child('read').set(true); } this._invokeEventCallbacks('notification', notification); } @@ -228,7 +228,7 @@ return; } - invite.id = invite.id || snapshot.key(); + invite.id = invite.id || self.getKey(snapshot); self.getRoom(invite.roomId, function(room) { invite.toRoomName = room.name; self._invokeEventCallbacks('room-invite', invite); @@ -238,7 +238,7 @@ var self = this, invite = snapshot.val(); - invite.id = invite.id || snapshot.key(); + invite.id = invite.id || self.getKey(snapshot); this._invokeEventCallbacks('room-invite-response', invite); } }; @@ -288,7 +288,7 @@ newRoomRef = this._roomRef.push(); var newRoom = { - id: newRoomRef.key(), + id: self.getKey(newRoomRef), name: roomName, type: roomType || 'public', createdByUserId: this._userId, @@ -302,10 +302,10 @@ newRoomRef.set(newRoom, function(error) { if (!error) { - self.enterRoom(newRoomRef.key()); + self.enterRoom(self.getKey(newRoomRef)); } if (callback) { - callback(newRoomRef.key()); + callback(self.getKey(newRoomRef)); } }); }; @@ -475,7 +475,7 @@ sendInvite = function() { var inviteRef = self._firebase.child('users').child(userId).child('invites').push(); inviteRef.set({ - id: inviteRef.key(), + id: self.getKey(inviteRef), fromUserId: self._userId, fromUserName: self._userName, roomId: roomId @@ -639,4 +639,36 @@ } } }; + + Firechat.prototype.getKey = function(snapshot) { + var key; + if (typeof snapshot.key === 'function') { + key = snapshot.key(); + } else if (typeof snapshot.key === 'string') { + key = snapshot.key; + } else { + key = snapshot.name(); + } + return key; + }; + + Firechat.prototype.getRef = (snapshotOrRef) { + var ref; + if (typeof snapshotOrRef.ref === 'function') { + ref = snapshotOrRef.ref(); + } else { + ref = snapshotOrRef.ref; + } + return ref; + }; + + Firechat.prototype.getRoot = (snapshotOrRef) { + var ref; + if (typeof snapshotOrRef.root === 'function') { + ref = snapshotOrRef.root(); + } else { + ref = snapshotOrRef.root; + } + return ref; + }; })(Firebase); diff --git a/website/docs/index.md b/website/docs/index.md index b4f9cd1..e29619a 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -5,7 +5,7 @@ layout: docs ### Overview -Firechat is a simple, extensible chat widget powered by [Firebase](https://firebase.com/?utm_source=docs&utm_medium=site&utm_campaign=firechat). +Firechat is a simple, extensible chat widget powered by [Firebase](https://firebase.google.com). It is intended to serve as a concise, documented foundation for chat products built on Firebase. It works out of the box, and is easily extended. @@ -26,17 +26,17 @@ In order to use Firechat in your project, you need to include the following file - + - - + + {% endhighlight %} Use the URLs above to download both the minified and non-minified versions of the Firechat JavaScript and CSS files from the Firebase CDN. You can also download them from the [releases page of the Firechat GitHub repository](https://github.com/firebase/firechat/releases). -[Firebase](https://www.firebase.com/docs/web/quickstart.html?utm_source=firechat) and +[Firebase Database](https://firebase.google.com/docs/database/web/start) and [jQuery](https://code.jquery.com/) can be downloaded directly from their respective websites. You can also install Firechat via npm or Bower and its dependencies will be downloaded @@ -50,10 +50,10 @@ $ npm install firechat --save $ bower install firechat --save ``` -#### Getting Started with Firebase +#### Getting Started with the Firebase Realtime Database Firechat requires Firebase in order to sync and store data. You can -[sign up here for a free account](https://www.firebase.com/signup/?utm_medium=web&utm_source=firechat). +[sign up here for a free account](https://firebase.google.com). #### Short Example @@ -72,16 +72,16 @@ Let's put it all together, using Twitter authentication in our example: - + - + - - - - - - - + + + + + + + + + - - -
- +
+ + From 38bb16abc90c28c70f0cbbce86cafc9af9f17c48 Mon Sep 17 00:00:00 2001 From: cmosguy Date: Mon, 30 May 2016 00:36:18 -0700 Subject: [PATCH 04/22] trying to get teh app to work in version 3.0.3 --- website/index.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/website/index.html b/website/index.html index bd9e52d..440a044 100644 --- a/website/index.html +++ b/website/index.html @@ -225,8 +225,6 @@

Authenticate to continue

storageBucket: "firechat-2ed14.appspot.com", }; - Firebase = firebase; - // var chatRef = new Firebase('firechat-2ed14.firebaseio.com'), var chatRef = firebase.initializeApp(config), target = document.getElementById("firechat-container"), From 942b238620bc849ead6cfa4aa73c4ba44d3cab19 Mon Sep 17 00:00:00 2001 From: cmosguy Date: Mon, 30 May 2016 01:19:32 -0700 Subject: [PATCH 05/22] still fiddling with v3.0.3 sdk --- src/js/firechat.js | 12 ++++++------ website/firechat/firechat.js | 12 ++++++------ website/index.html | 15 +++++++++------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/js/firechat.js b/src/js/firechat.js index 90b16fb..1884296 100644 --- a/src/js/firechat.js +++ b/src/js/firechat.js @@ -45,12 +45,12 @@ // Commonly-used Firebase references. this._userRef = null; - this._messageRef = this._firebase.child('room-messages'); - this._roomRef = this._firebase.child('room-metadata'); - this._privateRoomRef = this._firebase.child('room-private-metadata'); - this._moderatorsRef = this._firebase.child('moderators'); - this._suspensionsRef = this._firebase.child('suspensions'); - this._usersOnlineRef = this._firebase.child('user-names-online'); + this._messageRef = this._firebase.database().ref().child('room-messages'); + this._roomRef = this._firebase.database().ref().child('room-metadata'); + this._privateRoomRef = this._firebase.database().ref().child('room-private-metadata'); + this._moderatorsRef = this._firebase.database().ref().child('moderators'); + this._suspensionsRef = this._firebase.database().ref().child('suspensions'); + this._usersOnlineRef = this._firebase.database().ref().child('user-names-online'); // Setup and establish default options. this._options = options || {}; diff --git a/website/firechat/firechat.js b/website/firechat/firechat.js index a16cf52..cd49d7c 100644 --- a/website/firechat/firechat.js +++ b/website/firechat/firechat.js @@ -122,12 +122,12 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct // Commonly-used Firebase references. this._userRef = null; - this._messageRef = this._firebase.child('room-messages'); - this._roomRef = this._firebase.child('room-metadata'); - this._privateRoomRef = this._firebase.child('room-private-metadata'); - this._moderatorsRef = this._firebase.child('moderators'); - this._suspensionsRef = this._firebase.child('suspensions'); - this._usersOnlineRef = this._firebase.child('user-names-online'); + this._messageRef = this._firebase.database().ref().child('room-messages'); + this._roomRef = this._firebase.database().ref().child('room-metadata'); + this._privateRoomRef = this._firebase.database().ref().child('room-private-metadata'); + this._moderatorsRef = this._firebase.database().ref().child('moderators'); + this._suspensionsRef = this._firebase.database().ref().child('suspensions'); + this._usersOnlineRef = this._firebase.database().ref().child('user-names-online'); // Setup and establish default options. this._options = options || {}; diff --git a/website/index.html b/website/index.html index 440a044..3789761 100644 --- a/website/index.html +++ b/website/index.html @@ -226,10 +226,10 @@

Authenticate to continue

}; // var chatRef = new Firebase('firechat-2ed14.firebaseio.com'), - var chatRef = firebase.initializeApp(config), - target = document.getElementById("firechat-container"), - authModal = $('#auth-modal').modal({show: false}), - chat = new FirechatUI(chatRef, target); + var chatRef = firebase.initializeApp(config); + var target = document.getElementById("firechat-container"); + var authModal = $('#auth-modal').modal({show: false}); + var chat = new FirechatUI(chatRef, target); chat.on('auth-required', function () { authModal.modal('show'); @@ -263,8 +263,11 @@

Authenticate to continue

} function logout() { - chatRef.unauth(); - location.reload(); + chatRef.auth().signOut().then(function(){ + location.reload(); + }, function(error){ + console.log('could not logout' + error); + }); } From b8f4a2bdf8a0d791c337f2bf82f217f42713adf1 Mon Sep 17 00:00:00 2001 From: cmosguy Date: Mon, 30 May 2016 02:09:40 -0700 Subject: [PATCH 06/22] my final check in woo hoo! --- src/js/firechat.js | 26 +++++++++++++------------- website/firechat/firechat.js | 26 +++++++++++++------------- website/index.html | 35 +++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/js/firechat.js b/src/js/firechat.js index 1884296..e7de84a 100644 --- a/src/js/firechat.js +++ b/src/js/firechat.js @@ -97,12 +97,12 @@ // Initialize Firebase listeners and callbacks for the supported bindings. _setupDataEvents: function() { // Monitor connection state so we can requeue disconnect operations if need be. - this.getRoot(this._firebase).child('.info/connected').on('value', function(snapshot) { + this.getRoot(this._firebase.database().ref()).child('.info/connected').on('value', function(snapshot) { if (snapshot.val() === true) { // We're connected (or reconnected)! Set up our presence state. for (var i = 0; i < this._presenceBits; i++) { var op = this._presenceBits[i], - ref = this.getRoot(this._firebase).child(op.ref); + ref = this.getRoot(this._firebase.database().ref()).child(op.ref); ref.onDisconnect().set(op.offlineValue); ref.set(op.onlineValue); @@ -250,11 +250,11 @@ Firechat.prototype.setUser = function(userId, userName, callback) { var self = this; - self._firebase.onAuth(function(authData) { + self._firebase.auth().onAuthStateChanged(function(authData) { if (authData) { self._userId = userId.toString(); self._userName = userName.toString(); - self._userRef = self._firebase.child('users').child(self._userId); + self._userRef = self._firebase.database().ref().child('users').child(self._userId); self._loadUserMetadata(function() { root.setTimeout(function() { callback(self._user); @@ -301,7 +301,7 @@ name: roomName, type: roomType || 'public', createdByUserId: this._userId, - createdAt: Firebase.ServerValue.TIMESTAMP + createdAt: firebase.database.ServerValue.TIMESTAMP }; if (roomType === 'private') { @@ -343,7 +343,7 @@ }); // Set presence bit for the room and queue it for removal on disconnect. - var presenceRef = self._firebase.child('room-users').child(roomId).child(self._userId).child(self._sessionId); + var presenceRef = self._firebase.database().ref().child('room-users').child(roomId).child(self._userId).child(self._sessionId); self._queuePresenceOperation(presenceRef, { id: self._userId, name: self._userName @@ -372,7 +372,7 @@ // Leave a chat room. Firechat.prototype.leaveRoom = function(roomId) { var self = this, - userRoomRef = self._firebase.child('room-users').child(roomId); + userRoomRef = self._firebase.database().ref().child('room-users').child(roomId); // Remove listener for new messages to this room. self._messageRef.child(roomId).off(); @@ -398,7 +398,7 @@ message = { userId: self._userId, name: self._userName, - timestamp: Firebase.ServerValue.TIMESTAMP, + timestamp: firebase.database.ServerValue.TIMESTAMP, message: messageContent, type: messageType || 'default' }, @@ -413,7 +413,7 @@ } newMessageRef = self._messageRef.child(roomId).push(); - newMessageRef.setWithPriority(message, Firebase.ServerValue.TIMESTAMP, cb); + newMessageRef.setWithPriority(message, firebase.database.ServerValue.TIMESTAMP, cb); }; Firechat.prototype.deleteMessage = function(roomId, messageId, cb) { @@ -444,11 +444,11 @@ // Send a moderator notification to a specific user. Firechat.prototype.sendSuperuserNotification = function(userId, notificationType, data, cb) { var self = this, - userNotificationsRef = self._firebase.child('users').child(userId).child('notifications'); + userNotificationsRef = self._firebase.database().ref().child('users').child(userId).child('notifications'); userNotificationsRef.push({ fromUserId: self._userId, - timestamp: Firebase.ServerValue.TIMESTAMP, + timestamp: firebase.database.ServerValue.TIMESTAMP, notificationType: notificationType, data: data || {} }, cb); @@ -482,7 +482,7 @@ Firechat.prototype.inviteUser = function(userId, roomId) { var self = this, sendInvite = function() { - var inviteRef = self._firebase.child('users').child(userId).child('invites').push(); + var inviteRef = self._firebase.database().ref().child('users').child(userId).child('invites').push(); inviteRef.set({ id: self.getKey(inviteRef), fromUserId: self._userId, @@ -551,7 +551,7 @@ Firechat.prototype.getUsersByRoom = function() { var self = this, roomId = arguments[0], - query = self._firebase.child('room-users').child(roomId), + query = self._firebase.database().ref().child('room-users').child(roomId), cb = arguments[arguments.length - 1], limit = null; diff --git a/website/firechat/firechat.js b/website/firechat/firechat.js index cd49d7c..c48800d 100644 --- a/website/firechat/firechat.js +++ b/website/firechat/firechat.js @@ -174,12 +174,12 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct // Initialize Firebase listeners and callbacks for the supported bindings. _setupDataEvents: function() { // Monitor connection state so we can requeue disconnect operations if need be. - this.getRoot(this._firebase).child('.info/connected').on('value', function(snapshot) { + this.getRoot(this._firebase.database().ref()).child('.info/connected').on('value', function(snapshot) { if (snapshot.val() === true) { // We're connected (or reconnected)! Set up our presence state. for (var i = 0; i < this._presenceBits; i++) { var op = this._presenceBits[i], - ref = this.getRoot(this._firebase).child(op.ref); + ref = this.getRoot(this._firebase.database().ref()).child(op.ref); ref.onDisconnect().set(op.offlineValue); ref.set(op.onlineValue); @@ -327,11 +327,11 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct Firechat.prototype.setUser = function(userId, userName, callback) { var self = this; - self._firebase.onAuth(function(authData) { + self._firebase.auth().onAuthStateChanged(function(authData) { if (authData) { self._userId = userId.toString(); self._userName = userName.toString(); - self._userRef = self._firebase.child('users').child(self._userId); + self._userRef = self._firebase.database().ref().child('users').child(self._userId); self._loadUserMetadata(function() { root.setTimeout(function() { callback(self._user); @@ -378,7 +378,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct name: roomName, type: roomType || 'public', createdByUserId: this._userId, - createdAt: Firebase.ServerValue.TIMESTAMP + createdAt: firebase.database.ServerValue.TIMESTAMP }; if (roomType === 'private') { @@ -420,7 +420,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct }); // Set presence bit for the room and queue it for removal on disconnect. - var presenceRef = self._firebase.child('room-users').child(roomId).child(self._userId).child(self._sessionId); + var presenceRef = self._firebase.database().ref().child('room-users').child(roomId).child(self._userId).child(self._sessionId); self._queuePresenceOperation(presenceRef, { id: self._userId, name: self._userName @@ -449,7 +449,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct // Leave a chat room. Firechat.prototype.leaveRoom = function(roomId) { var self = this, - userRoomRef = self._firebase.child('room-users').child(roomId); + userRoomRef = self._firebase.database().ref().child('room-users').child(roomId); // Remove listener for new messages to this room. self._messageRef.child(roomId).off(); @@ -475,7 +475,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct message = { userId: self._userId, name: self._userName, - timestamp: Firebase.ServerValue.TIMESTAMP, + timestamp: firebase.database.ServerValue.TIMESTAMP, message: messageContent, type: messageType || 'default' }, @@ -490,7 +490,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct } newMessageRef = self._messageRef.child(roomId).push(); - newMessageRef.setWithPriority(message, Firebase.ServerValue.TIMESTAMP, cb); + newMessageRef.setWithPriority(message, firebase.database.ServerValue.TIMESTAMP, cb); }; Firechat.prototype.deleteMessage = function(roomId, messageId, cb) { @@ -521,11 +521,11 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct // Send a moderator notification to a specific user. Firechat.prototype.sendSuperuserNotification = function(userId, notificationType, data, cb) { var self = this, - userNotificationsRef = self._firebase.child('users').child(userId).child('notifications'); + userNotificationsRef = self._firebase.database().ref().child('users').child(userId).child('notifications'); userNotificationsRef.push({ fromUserId: self._userId, - timestamp: Firebase.ServerValue.TIMESTAMP, + timestamp: firebase.database.ServerValue.TIMESTAMP, notificationType: notificationType, data: data || {} }, cb); @@ -559,7 +559,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct Firechat.prototype.inviteUser = function(userId, roomId) { var self = this, sendInvite = function() { - var inviteRef = self._firebase.child('users').child(userId).child('invites').push(); + var inviteRef = self._firebase.database().ref().child('users').child(userId).child('invites').push(); inviteRef.set({ id: self.getKey(inviteRef), fromUserId: self._userId, @@ -628,7 +628,7 @@ this["FirechatDefaultTemplates"]["templates/user-search-list-item.html"] = funct Firechat.prototype.getUsersByRoom = function() { var self = this, roomId = arguments[0], - query = self._firebase.child('room-users').child(roomId), + query = self._firebase.database().ref().child('room-users').child(roomId), cb = arguments[arguments.length - 1], limit = null; diff --git a/website/index.html b/website/index.html index 3789761..e806303 100644 --- a/website/index.html +++ b/website/index.html @@ -195,7 +195,7 @@

Authenticate to continue