message.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * Abstract class Message
  3. * @constructor
  4. * @param {*} e
  5. * @param {number} ts
  6. **/
  7. function Message(e, ts) {
  8. /** @const @type {string} **/
  9. this.userId = e["user"];
  10. /** @type {string} */
  11. this.username = e["username"];
  12. /** @const @type {string} **/
  13. this.id = e["id"] || e["ts"];
  14. /** @const @type {number} **/
  15. this.ts = parseFloat(e["ts"]);
  16. /** @type {string} */
  17. this.text = "";
  18. /** @type {Array.<*>} */
  19. this.attachments = [];
  20. /** @type {boolean} */
  21. this.starred = false;
  22. /** @type {boolean} */
  23. this.pinned = false;
  24. /** @type {number|boolean} */
  25. this.edited = false;
  26. /** @type {boolean} */
  27. this.removed = false;
  28. /**
  29. * for each emoji code, an array of user id
  30. * @type {Object.<string, Array.<string>>}
  31. **/
  32. this.reactions = {};
  33. /** @type {number} */
  34. this.version = ts;
  35. this.update(e, ts);
  36. };
  37. /**
  38. * @constructor
  39. * @extends {Message}
  40. **/
  41. function MeMessage(e, ts) {
  42. Message.call(this, e, ts);
  43. };
  44. /**
  45. * @constructor
  46. * @extends {Message}
  47. **/
  48. function NoticeMessage(e, ts) {
  49. Message.call(this, e, ts);
  50. };
  51. /**
  52. * @param {*} e
  53. * @param {number} ts
  54. **/
  55. Message.prototype.update = function(e, ts) {
  56. if (e) {
  57. this.text = e["text"] || "";
  58. if (e["attachments"]) this.attachments = e["attachments"];
  59. this.starred = !!e["is_starred"];
  60. this.pinned = false; // TODO
  61. this.edited = e["edited"] === undefined ? false : e["edited"];
  62. this.removed = !!e["removed"];
  63. if (e["reactions"]) {
  64. var reactions = {};
  65. e["reactions"].forEach(function(r) {
  66. reactions[r["name"]] = [];
  67. r["users"].forEach(function(userId) {
  68. reactions[r["name"]].push(userId);
  69. });
  70. });
  71. this.reactions = reactions;
  72. }
  73. } else {
  74. this.removed = true;
  75. }
  76. this.version = ts;
  77. };
  78. /** @return {*} */
  79. Message.prototype.toStatic = function() {
  80. var reactions = [];
  81. for (var reaction in this.reactions) {
  82. reactions.push({
  83. "name": reaction
  84. ,"users": this.reactions[reaction]
  85. });
  86. }
  87. return {
  88. "id": this.id
  89. ,"user": this.userId
  90. ,"username": this.username
  91. ,"ts": this.ts
  92. ,"text": this.text
  93. ,"attachments": this.attachments.length ? this.attachments : undefined
  94. ,"is_starred": this.is_starred || undefined
  95. ,"edited": this.edited === false ? undefined : this.edited
  96. ,"removed": this.removed || undefined
  97. ,"reactions": reactions
  98. ,"isMeMessage": this instanceof MeMessage || undefined
  99. ,"isNotice": this instanceof NoticeMessage || undefined
  100. };
  101. };
  102. /**
  103. * @param {string} reaction
  104. * @param {string} userId
  105. * @param {number} ts
  106. **/
  107. Message.prototype.addReaction = function(reaction, userId, ts) {
  108. if (!this.reactions[reaction])
  109. this.reactions[reaction] = [];
  110. if (this.reactions[reaction].indexOf(userId) === -1) {
  111. this.reactions[reaction].push(userId);
  112. this.version = ts;
  113. return true;
  114. }
  115. return false;
  116. };
  117. /**
  118. * @param {string} reaction
  119. * @param {string} userId
  120. * @param {number} ts
  121. **/
  122. Message.prototype.removeReaction = function(reaction, userId, ts) {
  123. var updated = false;
  124. if (this.reactions[reaction]) {
  125. if (this.reactions[reaction].length === 1 && this.reactions[reaction][0] === userId) {
  126. delete this.reactions[reaction];
  127. updated = true;
  128. } else {
  129. this.reactions[reaction] = this.reactions[reaction].filter(function(i) {
  130. if (i !== userId)
  131. return true;
  132. updated = true;
  133. return false;
  134. });
  135. }
  136. }
  137. if (updated)
  138. this.version = ts;
  139. return updated;
  140. };
  141. Message.prototype.hasReactionForUser = function(reaction, userId) {
  142. if (this.reactions[reaction])
  143. return this.reactions[reaction].indexOf(userId) !== -1;
  144. return false;
  145. };
  146. /**
  147. * @constructor
  148. * @param {Room|string} room or roomId
  149. * @param {number} keepMessages number of messages to keep in memory
  150. * @param {Array|undefined} evts
  151. * @param {number|undefined} now
  152. **/
  153. function RoomHistory(room, keepMessages, evts, now) {
  154. /** @type {string} */
  155. this.id = typeof room === "string" ? room : room.id;
  156. /** @type {Array.<Message>} */
  157. this.messages = [];
  158. /** @type number */
  159. this.v = 0;
  160. /** @const @type {number} */
  161. this.keepMessages = keepMessages;
  162. if (evts) this.pushAll(evts, now);
  163. };
  164. /**
  165. * @param {Array} evts
  166. * @return {number} biggest ts
  167. **/
  168. RoomHistory.prototype.pushAll = function(evts, t) {
  169. var result = 0;
  170. evts.forEach(function(e) {
  171. result = Math.max(this.push(e, t), result);
  172. }.bind(this));
  173. this.resort();
  174. return result;
  175. };
  176. /**
  177. * @param {*} ev
  178. * @param {number} ts
  179. * @return {Message}
  180. **/
  181. RoomHistory.prototype.messageFactory = function(ev, ts) {
  182. if (ev["isMeMessage"] === true)
  183. return new MeMessage(ev, ts);
  184. if (ev["isNotice"] === true)
  185. return new NoticeMessage(ev, ts);
  186. return new Message(ev, ts);
  187. };
  188. /**
  189. * @param {*} e
  190. * @return {number} t
  191. **/
  192. RoomHistory.prototype.push = function(e, t) {
  193. var exists = false
  194. ,ts;
  195. for (var i =0, nbMsg = this.messages.length; i < nbMsg; i++) {
  196. var msgObj = this.messages[i];
  197. if (msgObj.id === e["id"]) {
  198. ts = msgObj.update(e, t);
  199. exists = true;
  200. break;
  201. }
  202. }
  203. if (!exists) {
  204. var msgObj = this.messageFactory(e, t);
  205. this.messages.push(msgObj);
  206. ts = msgObj.ts;
  207. }
  208. while (this.messages.length > this.keepMessages)
  209. this.messages.shift();
  210. return ts || 0;
  211. };
  212. RoomHistory.prototype.lastMessage = function() {
  213. return this.messages[this.messages.length -1];
  214. };
  215. /**
  216. * @param {string} reaction
  217. * @param {string} userId
  218. * @param {string} msgId
  219. * @param {number} ts
  220. * @return {Message|null}
  221. **/
  222. RoomHistory.prototype.addReaction = function(reaction, userId, msgId, ts) {
  223. var msg = this.getMessageById(msgId);
  224. if (msg)
  225. msg.addReaction(reaction, userId, ts);
  226. return msg;
  227. };
  228. /**
  229. * @param {string} reaction
  230. * @param {string} userId
  231. * @param {string} msgId
  232. * @param {number} ts
  233. * @return {Message|null}
  234. **/
  235. RoomHistory.prototype.removeReaction = function(reaction, userId, msgId, ts) {
  236. var msg = this.getMessageById(msgId);
  237. if (msg)
  238. msg.removeReaction(reaction, userId, ts);
  239. return msg;
  240. };
  241. /**
  242. * @param {number} ts
  243. * @return {Message|null}
  244. **/
  245. RoomHistory.prototype.getMessage = function(ts) {
  246. for (var i =0, nbMessages = this.messages.length; i < nbMessages && ts >= this.messages[i].ts; i++) {
  247. if (this.messages[i].ts === ts)
  248. return this.messages[i];
  249. }
  250. return null;
  251. };
  252. /**
  253. * @param {string} id
  254. * @return {Message|null}
  255. **/
  256. RoomHistory.prototype.getMessageById = function(id) {
  257. for (var i =0, nbMessages = this.messages.length; i < nbMessages; i++) {
  258. if (this.messages[i].id == id)
  259. return this.messages[i];
  260. }
  261. return null;
  262. };
  263. /**
  264. * @return {Array.<*>}
  265. **/
  266. RoomHistory.prototype.toStatic = function(knownVersion) {
  267. var result = [];
  268. for (var i = this.messages.length -1; i >= 0; i--)
  269. if (this.messages[i].version > knownVersion)
  270. result.push(this.messages[i].toStatic());
  271. return result;
  272. };
  273. RoomHistory.prototype.resort = function() {
  274. this.messages.sort(function(a, b) {
  275. return a.ts -b.ts;
  276. });
  277. };
  278. MeMessage.prototype = Object.create(Message.prototype);
  279. MeMessage.prototype.constructor = MeMessage;
  280. NoticeMessage.prototype = Object.create(Message.prototype);
  281. NoticeMessage.prototype.constructor = NoticeMessage;
  282. /** @suppress {undefinedVars,checkTypes} */
  283. (function() {
  284. if (typeof module !== "undefined") module.exports = {
  285. Message: Message
  286. ,MeMessage: MeMessage
  287. ,NoticeMessage: NoticeMessage
  288. ,RoomHistory: RoomHistory
  289. }
  290. })();