room.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. };
  42. /**
  43. * @param {number} lastMsg
  44. * @param {number} t
  45. **/
  46. Room.prototype.setLastMsg = function(lastMsg, t) {
  47. if (this.lastMsg < lastMsg) {
  48. this.lastMsg = lastMsg;
  49. this.version = t;
  50. return true;
  51. }
  52. return false;
  53. };
  54. /**
  55. * @param {number} t
  56. **/
  57. Room.prototype.toStatic = function(t) {
  58. if (t >= this.version)
  59. return null;
  60. var res = {
  61. "id": this.id
  62. ,"name": this.name
  63. ,"created": this.created
  64. ,"creator": this.creator ? this.creator.id : undefined
  65. ,"is_archived": this.archived
  66. ,"is_member": this.isMember
  67. ,"last_read": this.lastRead
  68. ,"last_msg": this.lastMsg
  69. ,"is_private": this.isPrivate
  70. ,"is_starred": this.starred || undefined
  71. };
  72. if (this.isMember) {
  73. res["members"] = this.users ? Object.keys(this.users) : [];
  74. res["topic"] = {
  75. "value": this.topic
  76. ,"creator": this.topicCreator ? this.topicCreator.id : null
  77. ,"last_set": this.topicTs
  78. }
  79. res["purpose"] = {
  80. "value": this.purpose
  81. ,"creator": this.purposeCreator ? this.purposeCreator.id : null
  82. ,"last_set": this.purposeTs
  83. }
  84. }
  85. return res;
  86. };
  87. /**
  88. * @param {*} chanData
  89. * @param {ChatContext} ctx
  90. * @param {number} t
  91. * @param {string=} idPrefix
  92. **/
  93. Room.prototype.update = function(chanData, ctx, t, idPrefix) {
  94. idPrefix = idPrefix || "";
  95. if (chanData["name"] !== undefined) this.name = chanData["name"];
  96. if (chanData["created"] !== undefined) this.created = chanData["created"];
  97. if (chanData["creator"] !== undefined) this.creator = ctx.users[chanData["creator"]];
  98. if (chanData["is_archived"] !== undefined) this.archived = chanData["is_archived"];
  99. if (chanData["is_member"] !== undefined) this.isMember = chanData["is_member"];
  100. if (chanData["last_read"] !== undefined) this.lastRead = Math.max(parseFloat(chanData["last_read"]), this.lastRead);
  101. if (chanData["last_msg"] !== undefined) this.lastMsg = parseFloat(chanData["last_msg"]);
  102. if (chanData["is_private"] !== undefined) this.isPrivate = chanData["is_private"];
  103. this.starred = !!chanData["is_starred"];
  104. if (chanData["members"]) {
  105. this.users = {};
  106. if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
  107. var member = ctx.users[idPrefix +chanData["members"][i]];
  108. this.users[member.id] = member;
  109. member.channels[this.id] = this;
  110. }
  111. }
  112. if (chanData["topic"]) {
  113. this.topic = chanData["topic"]["value"];
  114. this.topicCreator = ctx.users[idPrefix +chanData["topic"]["creator"]];
  115. this.topicTs = chanData["topic"]["last_set"];
  116. }
  117. if (chanData["purpose"]) {
  118. this.purpose = chanData["purpose"]["value"];
  119. this.purposeCreator = ctx.users[idPrefix +chanData["purpose"]["creator"]];
  120. this.purposeTs = chanData["purpose"]["last_set"];
  121. }
  122. this.version = Math.max(this.version, t);
  123. };
  124. /**
  125. * @constructor
  126. * @extends {Room}
  127. * @param {string} id
  128. * @param {Chatter} user
  129. **/
  130. function PrivateMessageRoom(id, user) {
  131. Room.call(this, id);
  132. /** @const @type {Chatter} */
  133. this.user = user;
  134. this.name = this.user.name;
  135. this.isPrivate = true;
  136. user.privateRoom = this;
  137. };
  138. PrivateMessageRoom.prototype = Object.create(Room.prototype);
  139. PrivateMessageRoom.prototype.constructor = PrivateMessageRoom;
  140. /**
  141. * @param {number} t
  142. **/
  143. PrivateMessageRoom.prototype.toStatic = function(t) {
  144. return t >= this.version ? null: {
  145. "id": this.id
  146. ,"created": this.created
  147. ,"user": this.user.id
  148. ,"last_read": this.lastRead
  149. ,"last_msg": this.lastMsg
  150. ,"pv": true
  151. ,"is_starred": this.starred || undefined
  152. };
  153. };
  154. /** @suppress {undefinedVars,checkTypes} */
  155. (function() {
  156. if (typeof module !== "undefined") {
  157. module.exports.Room = Room;
  158. module.exports.PrivateMessageRoom = PrivateMessageRoom;
  159. }
  160. })();