room.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @constructor
  3. **/
  4. function Room(id) {
  5. /** @const @type {string} */
  6. this.id = id;
  7. /** @type {string} */
  8. this.name;
  9. /** @type {string} */
  10. this.created;
  11. /** @type {Chatter} */
  12. this.creator;
  13. /** @type {boolean} */
  14. this.archived;
  15. /** @type {boolean} */
  16. this.isMember;
  17. /** @type {boolean} */
  18. this.starred = false;
  19. /** @type {number} */
  20. this.lastRead = 0;
  21. /** @type {number} */
  22. this.lastMsg;
  23. /** @type {Object.<string, Chatter>} */
  24. this.users = {};
  25. /** @type {string|undefined} */
  26. this.topic;
  27. /** @type {number|undefined} */
  28. this.topicTs;
  29. /** @type {Chatter|undefined} */
  30. this.topicCreator;
  31. /** @type {string|undefined} */
  32. this.purpose;
  33. /** @type {number|undefined} */
  34. this.purposeTs;
  35. /** @type {Chatter|undefined} */
  36. this.purposeCreator;
  37. /** @type {number} */
  38. this.version = 0;
  39. /** @type {boolean} */
  40. this.isPrivate;
  41. /** @type {Array<Message>|null} */
  42. this.pins;
  43. };
  44. /**
  45. * @param {number} lastMsg
  46. * @param {number} t
  47. **/
  48. Room.prototype.setLastMsg = function(lastMsg, t) {
  49. if (!this.lastMsg || this.lastMsg < lastMsg) {
  50. this.lastMsg = lastMsg;
  51. this.version = t;
  52. return true;
  53. }
  54. return false;
  55. };
  56. /**
  57. * @param {number} t
  58. **/
  59. Room.prototype.toStatic = function(t) {
  60. if (t >= this.version)
  61. return null;
  62. var res = {
  63. "id": this.id
  64. ,"name": this.name
  65. ,"created": this.created
  66. ,"creator": this.creator ? this.creator.id : undefined
  67. ,"is_archived": this.archived
  68. ,"is_member": this.isMember
  69. ,"last_read": this.lastRead
  70. ,"last_msg": this.lastMsg
  71. ,"is_private": this.isPrivate
  72. ,"is_starred": this.starred || undefined
  73. ,"pins": this.exposePins()
  74. };
  75. if (this.isMember) {
  76. res["members"] = this.users ? Object.keys(this.users) : [];
  77. res["topic"] = {
  78. "value": this.topic
  79. ,"creator": this.topicCreator ? this.topicCreator.id : null
  80. ,"last_set": this.topicTs
  81. }
  82. res["purpose"] = {
  83. "value": this.purpose
  84. ,"creator": this.purposeCreator ? this.purposeCreator.id : null
  85. ,"last_set": this.purposeTs
  86. }
  87. }
  88. return res;
  89. };
  90. /**
  91. * @param {*} chanData
  92. * @param {ChatContext} ctx
  93. * @param {number} t
  94. * @param {string=} idPrefix
  95. **/
  96. Room.prototype.update = function(chanData, ctx, t, idPrefix) {
  97. idPrefix = idPrefix || "";
  98. if (chanData["name"] !== undefined) this.name = chanData["name"];
  99. if (chanData["created"] !== undefined) this.created = chanData["created"];
  100. if (chanData["creator"] !== undefined) this.creator = ctx.users[chanData["creator"]];
  101. if (chanData["is_archived"] !== undefined) this.archived = chanData["is_archived"];
  102. if (chanData["is_member"] !== undefined) this.isMember = chanData["is_member"];
  103. if (chanData["last_read"] !== undefined) this.lastRead = Math.max(parseFloat(chanData["last_read"]), this.lastRead);
  104. if (chanData["last_msg"] !== undefined) this.lastMsg = parseFloat(chanData["last_msg"]);
  105. if (this.lastMsg)
  106. this.lastRead = Math.min(this.lastRead, this.lastMsg);
  107. if (chanData["is_private"] !== undefined) this.isPrivate = chanData["is_private"];
  108. if (chanData["pins"] !== undefined) this.pins = chanData["pins"];
  109. this.starred = !!chanData["is_starred"];
  110. if (chanData["members"]) {
  111. this.users = {};
  112. if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
  113. var member = ctx.users[idPrefix +chanData["members"][i]];
  114. this.users[member.id] = member;
  115. member.channels[this.id] = this;
  116. }
  117. }
  118. if (chanData["topic"]) {
  119. this.topic = chanData["topic"]["value"];
  120. this.topicCreator = ctx.users[idPrefix +chanData["topic"]["creator"]];
  121. this.topicTs = chanData["topic"]["last_set"];
  122. }
  123. if (chanData["purpose"]) {
  124. this.purpose = chanData["purpose"]["value"];
  125. this.purposeCreator = ctx.users[idPrefix +chanData["purpose"]["creator"]];
  126. this.purposeTs = chanData["purpose"]["last_set"];
  127. }
  128. this.version = Math.max(this.version, t);
  129. };
  130. Room.prototype.exposePins = function() {
  131. if (this.pins) {
  132. var msgs = [];
  133. this.pins.forEach(function(msg) {
  134. msgs.push(msg.toStatic());
  135. });
  136. return msgs;
  137. }
  138. };
  139. Room.prototype.matchString = function(str, Utils) {
  140. return {
  141. name: Utils.getClosestDistanceString(str, this.name),
  142. members: Utils.getClosestDistanceString(str, Object.values(/** @type {Object<string, Chatter|null>!} */ (this.users)), function(m) { return m ? m.getName() : null; }),
  143. topic: Utils.getClosestDistanceString(str, this.topic),
  144. purpose: Utils.getClosestDistanceString(str, this.purpose)
  145. };
  146. };
  147. /**
  148. * @constructor
  149. * @extends {Room}
  150. * @param {string} id
  151. * @param {Chatter} user
  152. **/
  153. function PrivateMessageRoom(id, user) {
  154. Room.call(this, id);
  155. /** @const @type {Chatter} */
  156. this.user = user;
  157. this.name = user.getName();
  158. this.isPrivate = true;
  159. user.privateRoom = this;
  160. };
  161. PrivateMessageRoom.prototype = Object.create(Room.prototype);
  162. PrivateMessageRoom.prototype.constructor = PrivateMessageRoom;
  163. /**
  164. * @param {number} t
  165. **/
  166. PrivateMessageRoom.prototype.toStatic = function(t) {
  167. return t >= this.version ? null: {
  168. "id": this.id
  169. ,"created": this.created
  170. ,"user": this.user.id
  171. ,"last_read": this.lastRead
  172. ,"last_msg": this.lastMsg
  173. ,"pv": true
  174. ,"is_starred": this.starred || undefined
  175. ,"pins": this.exposePins()
  176. };
  177. };
  178. /** @suppress {undefinedVars,checkTypes} */
  179. (function() {
  180. if (typeof module !== "undefined") {
  181. module.exports.Room = Room;
  182. module.exports.PrivateMessageRoom = PrivateMessageRoom;
  183. }
  184. })();