converse-message-view.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Converse.js
  2. // https://conversejs.org
  3. //
  4. // Copyright (c) 2013-2018, the Converse.js developers
  5. // Licensed under the Mozilla Public License (MPLv2)
  6. import converse from "@converse/headless/converse-core";
  7. import filesize from "filesize";
  8. import html from "./utils/html";
  9. import tpl_csn from "templates/csn.html";
  10. import tpl_file_progress from "templates/file_progress.html";
  11. import tpl_info from "templates/info.html";
  12. import tpl_message from "templates/message.html";
  13. import tpl_message_versions_modal from "templates/message_versions_modal.html";
  14. import u from "utils/emoji";
  15. import xss from "xss";
  16. const { Backbone, _, moment } = converse.env;
  17. converse.plugins.add('converse-message-view', {
  18. dependencies: ["converse-modal"],
  19. initialize () {
  20. /* The initialize function gets called as soon as the plugin is
  21. * loaded by converse.js's plugin machinery.
  22. */
  23. const { _converse } = this,
  24. { __ } = _converse;
  25. _converse.api.settings.update({
  26. 'show_images_inline': true
  27. });
  28. _converse.MessageVersionsModal = _converse.BootstrapModal.extend({
  29. toHTML () {
  30. return tpl_message_versions_modal(_.extend(
  31. this.model.toJSON(), {
  32. '__': __
  33. }));
  34. }
  35. });
  36. _converse.MessageView = _converse.ViewWithAvatar.extend({
  37. events: {
  38. 'click .chat-msg__edit-modal': 'showMessageVersionsModal'
  39. },
  40. initialize () {
  41. if (this.model.vcard) {
  42. this.model.vcard.on('change', this.render, this);
  43. }
  44. this.model.on('change', this.onChanged, this);
  45. this.model.on('destroy', this.remove, this);
  46. },
  47. async render () {
  48. const is_followup = u.hasClass('chat-msg--followup', this.el);
  49. if (this.model.isOnlyChatStateNotification()) {
  50. this.renderChatStateNotification()
  51. } else if (this.model.get('file') && !this.model.get('oob_url')) {
  52. if (!this.model.file) {
  53. _converse.log("Attempted to render a file upload message with no file data");
  54. return this.el;
  55. }
  56. this.renderFileUploadProgresBar();
  57. } else if (this.model.get('type') === 'error') {
  58. this.renderErrorMessage();
  59. } else {
  60. await this.renderChatMessage();
  61. }
  62. if (is_followup) {
  63. u.addClass('chat-msg--followup', this.el);
  64. }
  65. return this.el;
  66. },
  67. async onChanged (item) {
  68. // Jot down whether it was edited because the `changed`
  69. // attr gets removed when this.render() gets called further
  70. // down.
  71. const edited = item.changed.edited;
  72. if (this.model.changed.progress) {
  73. return this.renderFileUploadProgresBar();
  74. }
  75. if (_.filter(['correcting', 'message', 'type', 'upload'],
  76. prop => Object.prototype.hasOwnProperty.call(this.model.changed, prop)).length) {
  77. await this.render();
  78. }
  79. if (edited) {
  80. this.onMessageEdited();
  81. }
  82. },
  83. onMessageEdited () {
  84. if (this.model.get('is_archived')) {
  85. return;
  86. }
  87. this.el.addEventListener('animationend', () => u.removeClass('onload', this.el));
  88. u.addClass('onload', this.el);
  89. },
  90. replaceElement (msg) {
  91. if (!_.isNil(this.el.parentElement)) {
  92. this.el.parentElement.replaceChild(msg, this.el);
  93. }
  94. this.setElement(msg);
  95. return this.el;
  96. },
  97. async renderChatMessage () {
  98. const is_me_message = this.isMeCommand(),
  99. moment_time = moment(this.model.get('time')),
  100. role = this.model.vcard ? this.model.vcard.get('role') : null,
  101. roles = role ? role.split(',') : [];
  102. const msg = u.stringToElement(tpl_message(
  103. _.extend(
  104. this.model.toJSON(), {
  105. '__': __,
  106. 'is_me_message': is_me_message,
  107. 'roles': roles,
  108. 'pretty_time': moment_time.format(_converse.time_format),
  109. 'time': moment_time.format(),
  110. 'extra_classes': this.getExtraMessageClasses(),
  111. 'label_show': __('Show more'),
  112. 'username': this.model.getDisplayName()
  113. })
  114. ));
  115. const url = this.model.get('oob_url');
  116. if (url) {
  117. msg.querySelector('.chat-msg__media').innerHTML = _.flow(
  118. _.partial(u.renderFileURL, _converse),
  119. _.partial(u.renderMovieURL, _converse),
  120. _.partial(u.renderAudioURL, _converse),
  121. _.partial(u.renderImageURL, _converse))(url);
  122. }
  123. let text = this.getMessageText();
  124. const msg_content = msg.querySelector('.chat-msg__text');
  125. if (text && text !== url) {
  126. if (is_me_message) {
  127. text = text.substring(4);
  128. }
  129. text = xss.filterXSS(text, {'whiteList': {}});
  130. msg_content.innerHTML = _.flow(
  131. _.partial(u.geoUriToHttp, _, _converse.geouri_replacement),
  132. _.partial(u.addMentionsMarkup, _, this.model.get('references'), this.model.collection.chatbox),
  133. u.addHyperlinks,
  134. u.renderNewLines,
  135. _.partial(u.addEmoji, _converse, _)
  136. )(text);
  137. }
  138. const promises = [];
  139. promises.push(u.renderImageURLs(_converse, msg_content));
  140. if (this.model.get('type') !== 'headline') {
  141. promises.push(this.renderAvatar(msg));
  142. }
  143. await Promise.all(promises);
  144. this.replaceElement(msg);
  145. this.model.collection.trigger('rendered', this);
  146. },
  147. renderErrorMessage () {
  148. const moment_time = moment(this.model.get('time')),
  149. msg = u.stringToElement(
  150. tpl_info(_.extend(this.model.toJSON(), {
  151. 'extra_classes': 'chat-error',
  152. 'isodate': moment_time.format()
  153. })));
  154. return this.replaceElement(msg);
  155. },
  156. renderChatStateNotification () {
  157. let text;
  158. const from = this.model.get('from'),
  159. name = this.model.getDisplayName();
  160. if (this.model.get('chat_state') === _converse.COMPOSING) {
  161. if (this.model.get('sender') === 'me') {
  162. text = __('Typing from another device');
  163. } else {
  164. text = __('%1$s is typing', name);
  165. }
  166. } else if (this.model.get('chat_state') === _converse.PAUSED) {
  167. if (this.model.get('sender') === 'me') {
  168. text = __('Stopped typing on the other device');
  169. } else {
  170. text = __('%1$s has stopped typing', name);
  171. }
  172. } else if (this.model.get('chat_state') === _converse.GONE) {
  173. text = __('%1$s has gone away', name);
  174. } else {
  175. return;
  176. }
  177. const isodate = moment().format();
  178. this.replaceElement(
  179. u.stringToElement(
  180. tpl_csn({
  181. 'message': text,
  182. 'from': from,
  183. 'isodate': isodate
  184. })));
  185. },
  186. renderFileUploadProgresBar () {
  187. const msg = u.stringToElement(tpl_file_progress(
  188. _.extend(this.model.toJSON(), {
  189. '__': __,
  190. 'filename': this.model.file.name,
  191. 'filesize': filesize(this.model.file.size)
  192. })));
  193. this.replaceElement(msg);
  194. this.renderAvatar();
  195. },
  196. showMessageVersionsModal (ev) {
  197. ev.preventDefault();
  198. if (_.isUndefined(this.model.message_versions_modal)) {
  199. this.model.message_versions_modal = new _converse.MessageVersionsModal({'model': this.model});
  200. }
  201. this.model.message_versions_modal.show(ev);
  202. },
  203. getMessageText () {
  204. if (this.model.get('is_encrypted')) {
  205. return this.model.get('plaintext') ||
  206. (_converse.debug ? __('Unencryptable OMEMO message') : null);
  207. }
  208. return this.model.get('message');
  209. },
  210. isMeCommand () {
  211. const text = this.getMessageText();
  212. if (!text) {
  213. return false;
  214. }
  215. return text.startsWith('/me ');
  216. },
  217. processMessageText () {
  218. var text = this.get('message');
  219. text = u.geoUriToHttp(text, _converse.geouri_replacement);
  220. },
  221. getExtraMessageClasses () {
  222. let extra_classes = this.model.get('is_delayed') && 'delayed' || '';
  223. if (this.model.get('type') === 'groupchat' && this.model.get('sender') === 'them') {
  224. if (this.model.collection.chatbox.isUserMentioned(this.model)) {
  225. // Add special class to mark groupchat messages
  226. // in which we are mentioned.
  227. extra_classes += ' mentioned';
  228. }
  229. }
  230. if (this.model.get('correcting')) {
  231. extra_classes += ' correcting';
  232. }
  233. return extra_classes;
  234. }
  235. });
  236. }
  237. });