Last active
August 17, 2017 13:13
-
-
Save rjoydip-zz/183de0fad0a345400e1472e25c894e41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| /** | |
| * @description Establish a reference to the `window` object of the `Firechat` variable. | |
| */ | |
| var root = this; | |
| /** | |
| * Firechat | |
| * @description Firechat constructor function | |
| * @constructs | |
| * @param {*} firebaseRef firebase config object | |
| * @param {*} options options is optional right it is not being used | |
| * @example | |
| * var chat = new Firechat(firebaseRef, { | |
| * room_length: 20 | |
| * }); | |
| */ | |
| function Firechat(firebaseRef, options) { | |
| this._options = options || {}; | |
| /** | |
| * @description Firebase db references. | |
| */ | |
| this._roomRef = firebaseRef.child('room-metadata'); | |
| } | |
| /** | |
| * Export for global | |
| * @description Export the Firechat object as a global. | |
| */ | |
| root.Firechat = Firechat; | |
| /** | |
| * Init | |
| * @description Firechat initialize | |
| * @param {string} uid - logged in user uid | |
| * @param {number} _roomLen - Length of the room | |
| * @param {function} @callbak | |
| * @return callback | |
| */ | |
| Firechat.prototype.init = function(uid, _roomLen, callback) { | |
| var self = this; | |
| self._userId = uid; | |
| self._roomLen = _roomLen || this.options.room_length; | |
| self.getLastRoomId(function(roomId) { | |
| self.isRoomExists(function(isRoomExistsFlag) { | |
| if (isRoomExistsFlag) { | |
| self.isRoomFree(roomId, function(_isRoomFreeFlag) { | |
| if (_isRoomFreeFlag === false) { | |
| self.createRoom("Global Chat", function(roomId) { | |
| self._roomId = roomId; | |
| if (callback) { | |
| callback(self._roomId); | |
| } | |
| }); | |
| } else { | |
| if (callback) { | |
| callback(self._roomId); | |
| } | |
| } | |
| }); | |
| } else { | |
| self.createRoom("Global Chat", function(roomId) { | |
| self._roomId = roomId; | |
| if (callback) { | |
| callback(self._roomId); | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| }; | |
| /** | |
| * Create Room | |
| * @description Create and automatically enter a new chat room. | |
| * @param {roomName} string - Creates a random room string | |
| * @param {function} @callbak | |
| * @return callback | |
| */ | |
| Firechat.prototype.createRoom = function(roomName, callback) { | |
| var self = this, | |
| newRoomRef = self._roomRef.push(); | |
| newRoomRef.set({ | |
| room_name: roomName, | |
| user_length: 0, | |
| createdAt: firebase.database.ServerValue.TIMESTAMP | |
| }, function(error) { | |
| if (error) { | |
| return false; | |
| } else { | |
| if (callback) { | |
| callback(newRoomRef.key); | |
| } | |
| } | |
| }); | |
| }; | |
| /** | |
| * Enter Room | |
| * @description Enter a chat room. | |
| * @param {string} roomId | |
| */ | |
| Firechat.prototype.enterRoom = function(roomId) { | |
| var self = this; | |
| self.updateRoomUserLength(roomId, 1); // type = 1 : increment | |
| }; | |
| /** | |
| * Leave Room | |
| * @description Leave a chat room. | |
| * @param {string} roomId | |
| */ | |
| Firechat.prototype.leaveRoom = function(roomId) { | |
| var self = this; | |
| self.updateRoomUserLength(roomId, 0); // type = 0 : decrement | |
| }; | |
| /** | |
| * Send Message | |
| * @description Send message or store message in <room-metadata> key in firebase | |
| * @param {string} roomId - This roomId is the nested object of room-metadata | |
| * @param {object} message - This is the original content | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.sendMessage = function(roomId, message, callback) { | |
| var self = this, | |
| msg = 'Message Saved'; | |
| if (roomId) { | |
| self._roomRef.child(roomId).child('message').push(message); | |
| } else { | |
| self.getLastRoomId(function(prevId) { | |
| self._roomRef.child(prevId).child('message').push(message); | |
| }); | |
| } | |
| if (callback) { | |
| callback(msg); | |
| } | |
| }; | |
| /** | |
| * Get Message | |
| * @description get message of some perticular room | |
| * @param {string} roomId | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.getMessage = function(roomId, callback) { | |
| var self = this; | |
| self._roomRef.child(roomId).child('message').once('value', function(snaphot) { | |
| if (callback) { | |
| callback(snaphot.val()); | |
| } | |
| }); | |
| } | |
| /** | |
| * Update Room User Length | |
| * @description update user present in room | |
| * @param {string} roomId | |
| * @param {number} type - 1: increment and 0: decrement | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.updateRoomUserLength = function(roomId, type, callback) { | |
| var self = this; | |
| var childRef = self._roomRef.child(roomId); | |
| childRef.once('value', function(snaphot) { | |
| childRef.update({ | |
| user_length: (type === 1) ? (snaphot.val().user_length + 1) : (snaphot.val().user_length - 1) | |
| }); | |
| }); | |
| }; | |
| /** | |
| * Is Room Free | |
| * @description This will check whether the last room is free or not | |
| * @param {string} roomId | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.isRoomFree = function(roomId, callback) { | |
| var self = this, | |
| isRoomFreeFLag = true; | |
| self._roomRef.child(roomId).once('value', function(snaphot) { | |
| if (snaphot.val().user_length > 0 && (snaphot.val().user_length % self._options.room_length === 0 || snaphot.val().user_length > self._options.room_length)) { | |
| isRoomFreeFLag = false; | |
| if (callback) { | |
| callback(isRoomFreeFLag); | |
| } | |
| } else { | |
| if (callback) { | |
| callback(isRoomFreeFLag); | |
| } | |
| } | |
| }); | |
| }; | |
| /** | |
| * Is All Room Free | |
| * @description This will check whether the last room is free or not | |
| * @param {string} roomId | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.isAllRoomFree = function(callback) { | |
| var self = this, | |
| roomLength = 0, | |
| trueLength = 0; | |
| self._roomRef.once('value', function(snaphot) { | |
| snaphot.forEach(function(snap) { | |
| roomLength++; | |
| if (snap.val().user_length === self._options.room_length) { | |
| trueLength++; | |
| return; | |
| } | |
| }); | |
| if (roomLength === trueLength) { | |
| if (callback) { | |
| callback(false); | |
| } | |
| } else { | |
| if (callback) { | |
| callback(true); | |
| } | |
| } | |
| }); | |
| }; | |
| /** | |
| * Get Last Room Id | |
| * @description get last roomId | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.getLastRoomId = function(callback) { | |
| var self = this, | |
| _roomId; | |
| self._roomRef.once('value', function(snapshot) { | |
| for (val in snapshot.val()) { | |
| _roomId = val; | |
| } | |
| if (callback) { | |
| callback(_roomId); | |
| } | |
| }); | |
| }; | |
| /** | |
| * Get provious last Id | |
| * @description get previous last roomId | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.getPreviousLastId = function(callback) { | |
| var self = this, | |
| index = 0, | |
| _roomIdArr = []; | |
| self._roomRef.once('value', function(snapshot) { | |
| for (val in snapshot.val()) { | |
| _roomIdArr.push(val); | |
| } | |
| if (callback) { | |
| callback(_roomIdArr[_roomIdArr.length - 2]); | |
| } | |
| }); | |
| }; | |
| /** | |
| * Get Last Message | |
| * @description get last message | |
| * @param {string} roomId | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.getLastMessage = function(callback) { | |
| var self = this, | |
| _message = []; | |
| self._roomRef.once('value', function(snapshot) { | |
| snapshot.forEach(function(room) { | |
| if (room.val().message !== undefined) { | |
| Object.keys(room.val().message).forEach(function(key) { | |
| _message.push(room.val().message[key]); | |
| }); | |
| } | |
| }); | |
| if (callback) { | |
| callback(_message[_message.length - 1]); | |
| } | |
| // self.getRoomIds(function(roomIds) { | |
| // var rooms = Object.values(roomIds); | |
| // rooms.forEach(function(room) { | |
| // if (room.message !== undefined) { | |
| // Object.keys(room.message).forEach(function(key) { | |
| // _message.push(room.message[key]); | |
| // }); | |
| // } | |
| // }); | |
| // if (callback) { | |
| // callback(_message[_message.length - 1]); | |
| // } | |
| // }); | |
| }); | |
| }; | |
| /** | |
| * Is Room Exists | |
| * @description Is room exists | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.isRoomExists = function(callback) { | |
| var self = this; | |
| self._roomRef.once('value', function(snapshot) { | |
| if (callback) { | |
| callback((snapshot.val() !== null)); | |
| } | |
| }); | |
| }; | |
| /** | |
| * Get Room Ref | |
| * @description Get firebase room object reference | |
| * @return {string} room objecet reference | |
| */ | |
| Firechat.prototype.getRoomRef = function() { | |
| return this._roomRef; | |
| }; | |
| /** | |
| * Get Room Ids | |
| * @description Get all room ids | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.getRoomIds = function(callback) { | |
| var self = this; | |
| self._roomRef.once('value', function(snapshot) { | |
| if (callback) { | |
| callback(snapshot.val()); | |
| } | |
| }); | |
| }; | |
| /** | |
| * Search Chat | |
| * @description Get search message according to the input(username) | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.searchChat = function(input, callback) { | |
| var self = this, | |
| _message = []; | |
| self._roomRef.once('value', function(snapshot) { | |
| snapshot.forEach(function(room) { | |
| if (room.val().message !== undefined) { | |
| Object.keys(room.val().message).forEach(function(key) { | |
| _message.push(room.val().message[key]); | |
| }); | |
| } | |
| }); | |
| var filteredData = _message.filter(function(a) { | |
| var _a = a.username.toLowerCase(); | |
| if (_a === input) { | |
| return a; | |
| } | |
| }); | |
| if (callback) { | |
| callback(filteredData); | |
| } | |
| // self.getRoomIds(function(roomIds) { | |
| // var rooms = Object.values(roomIds); | |
| // rooms.forEach(function(room) { | |
| // if (room.message !== undefined) { | |
| // Object.keys(room.message).forEach(function(key) { | |
| // _message.push(room.message[key]); | |
| // }); | |
| // } | |
| // }); | |
| // // sort data | |
| // var filteredData = _message.filter(function(a) { | |
| // var _a = a.username.toLowerCase(); | |
| // if (_a === input) { | |
| // return a; | |
| // } | |
| // }); | |
| // if (callback) { | |
| // callback(filteredData); | |
| // } | |
| // }); | |
| }); | |
| }; | |
| /** | |
| * Search Chat By Key | |
| * @description Get search data according to the input and room key | |
| * @param {string} input | |
| * @param {string} roomKey | |
| * @param {function} @callback | |
| * @return callback | |
| */ | |
| Firechat.prototype.searchChatByKey = function(input, roomKey, callback) { | |
| var self = this, | |
| _message = []; | |
| self._roomRef.once('value', function(snapshot) { | |
| self.getMessage(roomKey, function(message) { | |
| if (message !== undefined) { | |
| Object.keys(message).forEach(function(key) { | |
| _message.push(message[key]); | |
| }); | |
| } | |
| // sort data | |
| var filteredData = _message.filter(function(a) { | |
| var _a = a.username.toLowerCase(); | |
| if (_a === input) { | |
| return a; | |
| } | |
| }); | |
| if (callback) { | |
| callback(filteredData); | |
| } | |
| }); | |
| }); | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment