multichatManager.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /** @interface */
  2. function ChatSystem() {
  3. };
  4. /** */
  5. ChatSystem.prototype.onRequest = function() {};
  6. /**
  7. * @return {string|null}
  8. **/
  9. ChatSystem.prototype.getId = function() {};
  10. /**
  11. * @return {ChatContext}
  12. **/
  13. ChatSystem.prototype.getChatContext = function() {};
  14. /**
  15. * Async fetch history
  16. * @param {Room} chan
  17. * @param {string} text
  18. **/
  19. ChatSystem.prototype.sendMeMsg = function(chan, text) {};
  20. /**
  21. * Async fetch history
  22. * @param {Room} chan
  23. * @param {string} text
  24. * @param {Object=} attachments
  25. **/
  26. ChatSystem.prototype.sendMsg = function(chan, text, attachments) {};
  27. /**
  28. * Async fetch history
  29. * @param {Room} chan
  30. * @param {number} ts
  31. **/
  32. ChatSystem.prototype.removeMsg = function(chan, ts) {};
  33. /**
  34. * Edit msg
  35. * @param {Room} chan
  36. * @param {number} ts
  37. * @param {string} newText
  38. **/
  39. ChatSystem.prototype.editMsg = function(chan, ts, newText) {};
  40. /**
  41. * Add reaction
  42. * @param {Room} chan
  43. * @param {number} ts
  44. * @param {string} reaction
  45. **/
  46. ChatSystem.prototype.addReaction = function(chan, ts, reaction) {};
  47. /**
  48. * Add reaction
  49. * @param {Room} chan
  50. * @param {number} ts
  51. * @param {string} reaction
  52. **/
  53. ChatSystem.prototype.removeReaction = function(chan, ts, reaction) {};
  54. /**
  55. * Add reaction
  56. * @param {Room} chan
  57. * @param {string} contentType
  58. * @param {function((string|null))} callback
  59. **/
  60. ChatSystem.prototype.openUploadFileStream = function(chan, contentType, callback) {};
  61. /**
  62. * Async fetch history
  63. * @param {Room} chan
  64. **/
  65. ChatSystem.prototype.fetchHistory = function(chan) {};
  66. /**
  67. * @param {Room} chan
  68. **/
  69. ChatSystem.prototype.sendTyping = function(chan) {};
  70. /**
  71. * @param {Room} chan
  72. * @param {number} ts
  73. **/
  74. ChatSystem.prototype.markRead = function(chan, ts) {};
  75. /**
  76. * @param {Room} chan
  77. * @param {string} cmd
  78. * @param {string} args
  79. **/
  80. ChatSystem.prototype.sendCommand = function(chan, cmd, args) {};
  81. /**
  82. * @param {number} knownVersion
  83. * @return {Object|null}
  84. **/
  85. ChatSystem.prototype.poll = function(knownVersion) {};
  86. /**
  87. * @constructor
  88. **/
  89. function MultiChatManager() {
  90. /** @type {Array<ChatSystem>} */
  91. this.contexts = [];
  92. }
  93. /**
  94. * @param {ChatSystem} chatSystem
  95. **/
  96. MultiChatManager.prototype.push = function(chatSystem) {
  97. this.contexts.push(chatSystem);
  98. };
  99. /**
  100. * @param {string} id
  101. * @return {ChatSystem|null}
  102. **/
  103. MultiChatManager.prototype.getById = function(id) {
  104. for (var i = 0, nbContexts = this.contexts.length; i < nbContexts; i++)
  105. if (id === this.contexts[i].getId())
  106. return this.contexts[i];
  107. return null;
  108. };
  109. /**
  110. * You may want to return true to break the loop
  111. **/
  112. MultiChatManager.prototype.foreachChannels = function(cb) {
  113. for (var i =0, nbCtx =this.contexts.length; i < nbCtx; i++) {
  114. var ctx = this.contexts[i].getChatContext();
  115. for (var chanId in ctx.channels)
  116. if (cb(ctx.channels[chanId], chanId) === true)
  117. return;
  118. }
  119. };
  120. /**
  121. * You may want to return true to break the loop
  122. **/
  123. MultiChatManager.prototype.foreachContext = function(cb) {
  124. for (var i =0, nbCtx =this.contexts.length; i < nbCtx; i++)
  125. if (cb(this.contexts[i].getChatContext()) === true)
  126. return;
  127. };
  128. /**
  129. * @param {string} chanId
  130. * @return {ChatSystem|null}
  131. **/
  132. MultiChatManager.prototype.getChannelContext = function(chanId) {
  133. for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++) {
  134. var ctx = this.contexts[i].getChatContext();
  135. if (ctx.channels[chanId])
  136. return this.contexts[i];
  137. }
  138. return null;
  139. };
  140. /**
  141. * @param {string} chanId
  142. * @return {Room|null}
  143. **/
  144. MultiChatManager.prototype.getChannel = function(chanId) {
  145. for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++) {
  146. var chan = this.contexts[i].getChatContext().channels[chanId];
  147. if (chan)
  148. return chan;
  149. }
  150. return null;
  151. };
  152. /**
  153. * @param {function(Room, ChatSystem, string):boolean=} filter (room, roomId)
  154. * @return {Array<string>}
  155. **/
  156. MultiChatManager.prototype.getChannelIds = function(filter) {
  157. var ids = [];
  158. for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++) {
  159. var channels = this.contexts[i].getChatContext().channels;
  160. for (var chanId in channels) {
  161. if (!filter || filter(channels[chanId], this.contexts[i], chanId)) {
  162. ids.push(chanId);
  163. }
  164. }
  165. }
  166. return ids;
  167. };
  168. /**
  169. * @param {string} userId
  170. * @return {Chatter|null}
  171. **/
  172. MultiChatManager.prototype.getUser = function(userId) {
  173. for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++) {
  174. var user = this.contexts[i].getChatContext().users[userId];
  175. if (user)
  176. return user;
  177. }
  178. return null;
  179. };
  180. /**
  181. * @param {string} userId
  182. * @return {boolean}
  183. **/
  184. MultiChatManager.prototype.isMe = function(userId) {
  185. for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++)
  186. if (this.contexts[i].getChatContext().self.id === userId)
  187. return true;
  188. return false;
  189. };
  190. MultiChatManager.prototype.poll = function(knownVersion, callback) {
  191. this.contexts.forEach(function(ctx) {
  192. ctx.onRequest();
  193. });
  194. setTimeout((function() {
  195. var liveFeed = {}
  196. ,hasLive = false
  197. ,staticFeed = {}
  198. ,v = 0
  199. ,updated = false;
  200. this.contexts.forEach(function(ctx) {
  201. var id = ctx.getId();
  202. if (id) {
  203. var res = ctx.poll(knownVersion);
  204. if (res) {
  205. if (res["static"])
  206. staticFeed[id] = res["static"];
  207. if (res["live"])
  208. for (var i in res["live"]) {
  209. liveFeed[i] = res["live"][i];
  210. hasLive = true;
  211. }
  212. v = Math.max(v, (res["v"] || 0));
  213. updated = true;
  214. }
  215. }
  216. });
  217. if (updated)
  218. callback({
  219. "live": hasLive ? liveFeed : undefined,
  220. "static": staticFeed,
  221. "v": Math.max(v, knownVersion)
  222. });
  223. else
  224. callback({
  225. "v": knownVersion
  226. });
  227. }).bind(this), 2000);
  228. };
  229. /** @suppress {undefinedVars,checkTypes} */
  230. (function() {
  231. if (typeof module !== "undefined") {
  232. module.exports.MultiChatManager = MultiChatManager;
  233. }
  234. })();