/** * @constructor **/ function Room(id) { /** @const @type {string} */ this.id = id; /** @type {string} */ this.name; /** @type {string} */ this.created; /** @type {Chatter} */ this.creator; /** @type {boolean} */ this.archived; /** @type {boolean} */ this.isMember; /** @type {boolean} */ this.starred = false; /** @type {number} */ this.lastRead = 0; /** @type {number} */ this.lastMsg; /** @type {Object.} */ this.users = {}; /** @type {string|undefined} */ this.topic; /** @type {number|undefined} */ this.topicTs; /** @type {Chatter|undefined} */ this.topicCreator; /** @type {string|undefined} */ this.purpose; /** @type {number|undefined} */ this.purposeTs; /** @type {Chatter|undefined} */ this.purposeCreator; /** @type {number} */ this.version = 0; /** @type {boolean} */ this.isPrivate; /** @type {Array|null} */ this.pins; }; /** * @param {number} lastMsg * @param {number} t **/ Room.prototype.setLastMsg = function(lastMsg, t) { if (!this.lastMsg || this.lastMsg < lastMsg) { this.lastMsg = lastMsg; this.version = t; return true; } return false; }; /** * @param {number} t **/ Room.prototype.toStatic = function(t) { if (t >= this.version) return null; var res = { "id": this.id ,"name": this.name ,"created": this.created ,"creator": this.creator ? this.creator.id : undefined ,"is_archived": this.archived ,"is_member": this.isMember ,"last_read": this.lastRead ,"last_msg": this.lastMsg ,"is_private": this.isPrivate ,"is_starred": this.starred || undefined ,"pins": this.exposePins() }; if (this.isMember) { res["members"] = this.users ? Object.keys(this.users) : []; res["topic"] = { "value": this.topic ,"creator": this.topicCreator ? this.topicCreator.id : null ,"last_set": this.topicTs } res["purpose"] = { "value": this.purpose ,"creator": this.purposeCreator ? this.purposeCreator.id : null ,"last_set": this.purposeTs } } return res; }; /** * @param {*} chanData * @param {ChatContext} ctx * @param {number} t * @param {string=} idPrefix **/ Room.prototype.update = function(chanData, ctx, t, idPrefix) { idPrefix = idPrefix || ""; if (chanData["name"] !== undefined) this.name = chanData["name"]; if (chanData["created"] !== undefined) this.created = chanData["created"]; if (chanData["creator"] !== undefined) this.creator = ctx.users[chanData["creator"]]; if (chanData["is_archived"] !== undefined) this.archived = chanData["is_archived"]; if (chanData["is_member"] !== undefined) this.isMember = chanData["is_member"]; if (chanData["last_read"] !== undefined) this.lastRead = Math.max(parseFloat(chanData["last_read"]), this.lastRead); if (chanData["last_msg"] !== undefined) this.lastMsg = parseFloat(chanData["last_msg"]); if (chanData["is_private"] !== undefined) this.isPrivate = chanData["is_private"]; if (chanData["pins"] !== undefined) this.pins = chanData["pins"]; this.starred = !!chanData["is_starred"]; if (chanData["members"]) { this.users = {}; if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) { var member = ctx.users[idPrefix +chanData["members"][i]]; this.users[member.id] = member; member.channels[this.id] = this; } } if (chanData["topic"]) { this.topic = chanData["topic"]["value"]; this.topicCreator = ctx.users[idPrefix +chanData["topic"]["creator"]]; this.topicTs = chanData["topic"]["last_set"]; } if (chanData["purpose"]) { this.purpose = chanData["purpose"]["value"]; this.purposeCreator = ctx.users[idPrefix +chanData["purpose"]["creator"]]; this.purposeTs = chanData["purpose"]["last_set"]; } this.version = Math.max(this.version, t); }; Room.prototype.exposePins = function() { if (this.pins) { var msgs = []; this.pins.forEach(function(msg) { msgs.push(msg.toStatic()); }); return msgs; } }; Room.prototype.matchString = function(str, Utils) { return { name: Utils.getClosestDistanceString(str, this.name), members: Utils.getClosestDistanceString(str, Object.values(/** @type {Object!} */ (this.users)), function(m) { return m ? m.getName() : null; }), topic: Utils.getClosestDistanceString(str, this.topic), purpose: Utils.getClosestDistanceString(str, this.purpose) }; }; /** * @constructor * @extends {Room} * @param {string} id * @param {Chatter} user **/ function PrivateMessageRoom(id, user) { Room.call(this, id); /** @const @type {Chatter} */ this.user = user; this.name = user.getName(); this.isPrivate = true; user.privateRoom = this; }; PrivateMessageRoom.prototype = Object.create(Room.prototype); PrivateMessageRoom.prototype.constructor = PrivateMessageRoom; /** * @param {number} t **/ PrivateMessageRoom.prototype.toStatic = function(t) { return t >= this.version ? null: { "id": this.id ,"created": this.created ,"user": this.user.id ,"last_read": this.lastRead ,"last_msg": this.lastMsg ,"pv": true ,"is_starred": this.starred || undefined ,"pins": this.exposePins() }; }; /** @suppress {undefinedVars,checkTypes} */ (function() { if (typeof module !== "undefined") { module.exports.Room = Room; module.exports.PrivateMessageRoom = PrivateMessageRoom; } })();