room.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = 0;
  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. **/
  92. Room.prototype.update = function(chanData, ctx, t) {
  93. if (chanData["name"] !== undefined) this.name = chanData["name"];
  94. if (chanData["created"] !== undefined) this.created = chanData["created"];
  95. if (chanData["creator"] !== undefined) this.creator = ctx.users[chanData["creator"]];
  96. if (chanData["is_archived"] !== undefined) this.archived = chanData["is_archived"];
  97. if (chanData["is_member"] !== undefined) this.isMember = chanData["is_member"];
  98. if (chanData["last_read"] !== undefined) this.lastRead = Math.max(parseFloat(chanData["last_read"]), this.lastRead);
  99. if (chanData["last_msg"] !== undefined) this.lastMsg = parseFloat(chanData["last_msg"]);
  100. if (chanData["is_private"] !== undefined) this.isPrivate = chanData["is_private"];
  101. if (chanData["latest"]) this.lastMsg = parseFloat(chanData["latest"]["ts"]);
  102. if (chanData["is_starred"] !== undefined) this.starred = chanData["is_starred"];
  103. if (chanData["members"]) {
  104. this.users = {};
  105. if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
  106. var member = ctx.users[chanData["members"][i]];
  107. this.users[member.id] = member;
  108. member.channels[this.id] = this;
  109. }
  110. }
  111. if (chanData["topic"]) {
  112. this.topic = chanData["topic"]["value"];
  113. this.topicCreator = ctx.users[chanData["topic"]["creator"]];
  114. this.topicTs = chanData["topic"]["last_set"];
  115. }
  116. if (chanData["purpose"]) {
  117. this.purpose = chanData["purpose"]["value"];
  118. this.purposeCreator = ctx.users[chanData["purpose"]["creator"]];
  119. this.purposeTs = chanData["purpose"]["last_set"];
  120. }
  121. this.version = Math.max(this.version, t);
  122. };
  123. /**
  124. * @constructor
  125. * @extends {Room}
  126. * @param {string} id
  127. * @param {Chatter} user
  128. **/
  129. function PrivateMessageRoom(id, user) {
  130. Room.call(this, id);
  131. /** @const @type {Chatter} */
  132. this.user = user;
  133. this.name = this.user.name;
  134. this.isPrivate = true;
  135. user.privateRoom = this;
  136. };
  137. PrivateMessageRoom.prototype = Object.create(Room.prototype);
  138. PrivateMessageRoom.prototype.constructor = PrivateMessageRoom;
  139. /**
  140. * @param {number} t
  141. **/
  142. PrivateMessageRoom.prototype.toStatic = function(t) {
  143. return t >= this.version ? null: {
  144. "id": this.id
  145. ,"created": this.created
  146. ,"user": this.user.id
  147. ,"last_read": this.lastRead
  148. ,"last_msg": this.lastMsg
  149. ,"pv": true
  150. ,"is_starred": this.starred || undefined
  151. };
  152. };
  153. /** @suppress {undefinedVars,checkTypes} */
  154. (function() {
  155. if (typeof module !== "undefined") {
  156. module.exports.Room = Room;
  157. module.exports.PrivateMessageRoom = PrivateMessageRoom;
  158. }
  159. })();