message_text.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // tslint:disable:max-line-length
  18. import {hasValue} from '../helpers';
  19. import {WebClientService} from '../services/webclient';
  20. // Get text depending on type
  21. function getText(message: threema.Message): string {
  22. switch (message.type) {
  23. case 'text':
  24. return message.body;
  25. case 'location':
  26. return message.location.description;
  27. case 'file':
  28. // Prefer caption for file messages, if available
  29. if (message.caption && message.caption.length > 0) {
  30. return message.caption;
  31. } else if (message.file.type === 'image/gif') {
  32. return 'GIF';
  33. }
  34. return message.file.name;
  35. }
  36. return message.caption;
  37. }
  38. export default [
  39. function() {
  40. return {
  41. restrict: 'EA',
  42. scope: {},
  43. bindToController: {
  44. message: '=',
  45. multiLine: '@?multiLine',
  46. linkify: '@?linkify',
  47. },
  48. link: function(scope, elem, attrs) {
  49. scope.$watch(
  50. () => scope.ctrl.message.id,
  51. (newId, oldId) => {
  52. // Register for message ID changes. When it changes, update the text.
  53. // This prevents processing the text more than once.
  54. if (hasValue(newId) && newId !== oldId) {
  55. scope.ctrl.updateText();
  56. }
  57. },
  58. );
  59. scope.$watch(
  60. () => scope.ctrl.message.caption,
  61. (newCaption, oldCaption) => {
  62. // Register for message caption changes. When it changes, update the text.
  63. //
  64. // Background: The caption may change because image messages may be sent from the
  65. // app before the image has been downloaded and parsed. That information
  66. // (thumbnail + caption) is sent later on as an update.
  67. if (hasValue(newCaption) && newCaption !== oldCaption) {
  68. scope.ctrl.updateText();
  69. }
  70. },
  71. );
  72. },
  73. controllerAs: 'ctrl',
  74. controller: ['WebClientService', '$filter', function(webClientService: WebClientService, $filter: ng.IFilterService) {
  75. // TODO: Extract filters into helper functions
  76. const escapeHtml = $filter('escapeHtml') as any;
  77. const markify = $filter('markify') as any;
  78. const emojify = $filter('emojify') as any;
  79. const enlargeSingleEmoji = $filter('enlargeSingleEmoji') as any;
  80. const mentionify = $filter('mentionify') as any;
  81. const linkify = $filter('linkify') as any;
  82. const nlToBr = $filter('nlToBr') as any;
  83. /**
  84. * Apply filters to text.
  85. */
  86. function processText(text: string, largeSingleEmoji: boolean, multiLine: boolean, linkifyText: boolean): string {
  87. const nonLinkified = mentionify(enlargeSingleEmoji(emojify(markify(escapeHtml(text))), largeSingleEmoji));
  88. const maybeLinkified = linkifyText ? linkify(nonLinkified) : nonLinkified;
  89. return nlToBr(maybeLinkified, multiLine);
  90. }
  91. /**
  92. * Text update function.
  93. */
  94. this.updateText = () => {
  95. // Because this.multiLine and this.linkify are bound using an `@` binding,
  96. // they are either undefined or a string. Convert to boolean.
  97. const multiLine = (this.multiLine === undefined || this.multiLine !== 'false');
  98. const linkifyText = (this.linkify === undefined || this.linkify !== 'false');
  99. // Process text once, apply all filter functions
  100. const text = getText(this.message);
  101. this.text = processText(
  102. text,
  103. this.largeSingleEmoji,
  104. multiLine,
  105. linkifyText,
  106. );
  107. };
  108. this.largeSingleEmoji = webClientService.appConfig.largeSingleEmoji;
  109. this.$onInit = function() {
  110. // Process initial text
  111. this.updateText();
  112. };
  113. }],
  114. template: `
  115. <span click-action ng-bind-html="ctrl.text"></span>
  116. `,
  117. };
  118. },
  119. ];