room.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 (chanData["is_private"] !== undefined) this.isPrivate = chanData["is_private"];
  106. if (chanData["pins"] !== undefined) this.pins = chanData["pins"];
  107. this.starred = !!chanData["is_starred"];
  108. if (chanData["members"]) {
  109. this.users = {};
  110. if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
  111. var member = ctx.users[idPrefix +chanData["members"][i]];
  112. this.users[member.id] = member;
  113. member.channels[this.id] = this;
  114. }
  115. }
  116. if (chanData["topic"]) {
  117. this.topic = chanData["topic"]["value"];
  118. this.topicCreator = ctx.users[idPrefix +chanData["topic"]["creator"]];
  119. this.topicTs = chanData["topic"]["last_set"];
  120. }
  121. if (chanData["purpose"]) {
  122. this.purpose = chanData["purpose"]["value"];
  123. this.purposeCreator = ctx.users[idPrefix +chanData["purpose"]["creator"]];
  124. this.purposeTs = chanData["purpose"]["last_set"];
  125. }
  126. this.version = Math.max(this.version, t);
  127. };
  128. Room.prototype.exposePins = function() {
  129. if (this.pins) {
  130. var msgs = [];
  131. this.pins.forEach(function(msg) {
  132. msgs.push(msg.toStatic());
  133. });
  134. return msgs;
  135. }
  136. };
  137. Room.prototype.matchString = function(str, Utils) {
  138. return {
  139. name: Utils.getClosestDistanceString(str, this.name),
  140. members: Utils.getClosestDistanceString(str, Object.values(/** @type {Object<string, Chatter|null>!} */ (this.users)), function(m) { return m ? m.getName() : null; }),
  141. topic: Utils.getClosestDistanceString(str, this.topic),
  142. purpose: Utils.getClosestDistanceString(str, this.purpose)
  143. };
  144. };
  145. /**
  146. * @constructor
  147. * @extends {Room}
  148. * @param {string} id
  149. * @param {Chatter} user
  150. **/
  151. function PrivateMessageRoom(id, user) {
  152. Room.call(this, id);
  153. /** @const @type {Chatter} */
  154. this.user = user;
  155. this.name = user.getName();
  156. this.isPrivate = true;
  157. user.privateRoom = this;
  158. };
  159. PrivateMessageRoom.prototype = Object.create(Room.prototype);
  160. PrivateMessageRoom.prototype.constructor = PrivateMessageRoom;
  161. /**
  162. * @param {number} t
  163. **/
  164. PrivateMessageRoom.prototype.toStatic = function(t) {
  165. return t >= this.version ? null: {
  166. "id": this.id
  167. ,"created": this.created
  168. ,"user": this.user.id
  169. ,"last_read": this.lastRead
  170. ,"last_msg": this.lastMsg
  171. ,"pv": true
  172. ,"is_starred": this.starred || undefined
  173. ,"pins": this.exposePins()
  174. };
  175. };
  176. /** @suppress {undefinedVars,checkTypes} */
  177. (function() {
  178. if (typeof module !== "undefined") {
  179. module.exports.Room = Room;
  180. module.exports.PrivateMessageRoom = PrivateMessageRoom;
  181. }
  182. })();