message.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 = e["is_starred"] || false;
  22. /** @type {number|boolean} */
  23. this.edited = false;
  24. /** @type {boolean} */
  25. this.removed = false;
  26. /**
  27. * for each emoji code, an array of user id
  28. * @type {Object.<string, Array.<string>>}
  29. **/
  30. this.reactions = {};
  31. /** @type {number} */
  32. this.version = ts;
  33. this.update(e, ts);
  34. };
  35. /**
  36. * @constructor
  37. * @extends {Message}
  38. **/
  39. function MeMessage(e, ts) {
  40. Message.call(this, e, ts);
  41. };
  42. /**
  43. * @constructor
  44. * @extends {Message}
  45. **/
  46. function NoticeMessage(e, ts) {
  47. Message.call(this, e, ts);
  48. };
  49. /**
  50. * @param {*} e
  51. * @param {number} ts
  52. **/
  53. Message.prototype.update = function(e, ts) {
  54. if (e) {
  55. this.text = e["text"] || "";
  56. if (e["attachments"]) this.attachments = e["attachments"];
  57. this.starred = !!e["is_starred"];
  58. this.edited = e["edited"] === undefined ? false : e["edited"];
  59. this.removed = !!e["removed"];
  60. if (e["reactions"]) {
  61. var reactions = {};
  62. e["reactions"].forEach(function(r) {
  63. reactions[r["name"]] = [];
  64. r["users"].forEach(function(userId) {
  65. reactions[r["name"]].push(userId);
  66. });
  67. });
  68. this.reactions = reactions;
  69. }
  70. } else {
  71. this.removed = true;
  72. }
  73. this.version = ts;
  74. };
  75. /** @return {*} */
  76. Message.prototype.toStatic = function() {
  77. var reactions = [];
  78. for (var reaction in this.reactions) {
  79. reactions.push({
  80. "name": reaction
  81. ,"users": this.reactions[reaction]
  82. });
  83. }
  84. return {
  85. "id": this.id
  86. ,"user": this.userId
  87. ,"username": this.username
  88. ,"ts": this.ts
  89. ,"text": this.text
  90. ,"attachments": this.attachments.length ? this.attachments : undefined
  91. ,"is_starred": this.starred || undefined
  92. ,"edited": this.edited === false ? undefined : this.edited
  93. ,"removed": this.removed || undefined
  94. ,"reactions": reactions
  95. ,"isMeMessage": this instanceof MeMessage || undefined
  96. ,"isNotice": this instanceof NoticeMessage || undefined
  97. };
  98. };
  99. /**
  100. * @param {string} reaction
  101. * @param {string} userId
  102. * @param {number} ts
  103. **/
  104. Message.prototype.addReaction = function(reaction, userId, ts) {
  105. if (!this.reactions[reaction])
  106. this.reactions[reaction] = [];
  107. if (this.reactions[reaction].indexOf(userId) === -1) {
  108. this.reactions[reaction].push(userId);
  109. this.version = ts;
  110. return true;
  111. }
  112. return false;
  113. };
  114. /**
  115. * @param {string} reaction
  116. * @param {string} userId
  117. * @param {number} ts
  118. **/
  119. Message.prototype.removeReaction = function(reaction, userId, ts) {
  120. var updated = false;
  121. if (this.reactions[reaction]) {
  122. if (this.reactions[reaction].length === 1 && this.reactions[reaction][0] === userId) {
  123. delete this.reactions[reaction];
  124. updated = true;
  125. } else {
  126. this.reactions[reaction] = this.reactions[reaction].filter(function(i) {
  127. if (i !== userId)
  128. return true;
  129. updated = true;
  130. return false;
  131. });
  132. }
  133. }
  134. if (updated)
  135. this.version = ts;
  136. return updated;
  137. };
  138. Message.prototype.hasReactionForUser = function(reaction, userId) {
  139. if (this.reactions[reaction])
  140. return this.reactions[reaction].indexOf(userId) !== -1;
  141. return false;
  142. };
  143. /**
  144. * @constructor
  145. * @param {Room|string} room or roomId
  146. * @param {number} keepMessages number of messages to keep in memory
  147. * @param {number} maxAge number of messages to keep in memory
  148. * @param {Array|undefined} evts
  149. * @param {number|undefined} now
  150. **/
  151. function RoomHistory(room, keepMessages, maxAge, evts, now) {
  152. /** @type {string} */
  153. this.id = typeof room === "string" ? room : room.id;
  154. /** @type {Array.<Message>} */
  155. this.messages = [];
  156. /** @type {number} */
  157. this.maxAge = maxAge;
  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. this.cleanOld(t);
  209. return ts || 0;
  210. };
  211. RoomHistory.prototype.cleanOld = function(t) {
  212. while (this.messages.length > this.keepMessages)
  213. this.messages.shift();
  214. if (this.maxAge) {
  215. for (var i =0; i < this.messages.length; i++) {
  216. if (this.messages[i].version < t -this.maxAge) {
  217. this.messages.splice(i--, 1);
  218. }
  219. }
  220. }
  221. };
  222. RoomHistory.prototype.lastMessage = function() {
  223. return this.messages[this.messages.length -1];
  224. };
  225. /**
  226. * @param {string} reaction
  227. * @param {string} userId
  228. * @param {string} msgId
  229. * @param {number} ts
  230. * @return {Message|null} WARNING if null, you *should* fetch new message and append it
  231. **/
  232. RoomHistory.prototype.addReaction = function(reaction, userId, msgId, ts) {
  233. var msg = this.getMessageById(msgId);
  234. if (msg)
  235. msg.addReaction(reaction, userId, ts);
  236. return msg;
  237. };
  238. /**
  239. * @param {string} reaction
  240. * @param {string} userId
  241. * @param {string} msgId
  242. * @param {number} ts
  243. * @return {Message|null} WARNING if null, you *should* fetch new message and append it
  244. **/
  245. RoomHistory.prototype.removeReaction = function(reaction, userId, msgId, ts) {
  246. var msg = this.getMessageById(msgId);
  247. if (msg)
  248. msg.removeReaction(reaction, userId, ts);
  249. return msg;
  250. };
  251. /**
  252. * @param {number} ts
  253. * @return {Message|null}
  254. **/
  255. RoomHistory.prototype.getMessage = function(ts) {
  256. for (var i =0, nbMessages = this.messages.length; i < nbMessages && ts >= this.messages[i].ts; i++) {
  257. if (this.messages[i].ts === ts)
  258. return this.messages[i];
  259. }
  260. return null;
  261. };
  262. /**
  263. * @param {string} id
  264. * @return {Message|null}
  265. **/
  266. RoomHistory.prototype.getMessageById = function(id) {
  267. for (var i =0, nbMessages = this.messages.length; i < nbMessages; i++) {
  268. if (this.messages[i].id == id)
  269. return this.messages[i];
  270. }
  271. return null;
  272. };
  273. RoomHistory.prototype.last = function() {
  274. return this.messages[this.messages.length -1];
  275. };
  276. /**
  277. * @return {Array.<*>}
  278. **/
  279. RoomHistory.prototype.toStatic = function(knownVersion) {
  280. var result = [];
  281. for (var i = this.messages.length -1; i >= 0; i--)
  282. if (this.messages[i].version > knownVersion)
  283. result.push(this.messages[i].toStatic());
  284. return result;
  285. };
  286. RoomHistory.prototype.resort = function() {
  287. this.messages.sort(function(a, b) {
  288. return a.ts -b.ts;
  289. });
  290. };
  291. MeMessage.prototype = Object.create(Message.prototype);
  292. MeMessage.prototype.constructor = MeMessage;
  293. NoticeMessage.prototype = Object.create(Message.prototype);
  294. NoticeMessage.prototype.constructor = NoticeMessage;
  295. /** @suppress {undefinedVars,checkTypes} */
  296. (function() {
  297. if (typeof module !== "undefined") module.exports = {
  298. Message: Message
  299. ,MeMessage: MeMessage
  300. ,NoticeMessage: NoticeMessage
  301. ,RoomHistory: RoomHistory
  302. }
  303. })();