message_text.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  32. return message.file.name;
  33. }
  34. return message.caption;
  35. }
  36. export default [
  37. function() {
  38. return {
  39. restrict: 'EA',
  40. scope: {},
  41. bindToController: {
  42. message: '=',
  43. multiLine: '@?multiLine',
  44. linkify: '@?linkify',
  45. },
  46. link: function(scope, elem, attrs) {
  47. scope.$watch(
  48. () => scope.ctrl.message.id,
  49. (newId, oldId) => {
  50. // Register for message ID changes. When it changes, update the text.
  51. // This prevents processing the text more than once.
  52. if (hasValue(newId) && newId !== oldId) {
  53. scope.ctrl.updateText();
  54. }
  55. },
  56. );
  57. scope.$watch(
  58. () => scope.ctrl.message.caption,
  59. (newCaption, oldCaption) => {
  60. // Register for message caption changes. When it changes, update the text.
  61. //
  62. // Background: The caption may change because image messages may be sent from the
  63. // app before the image has been downloaded and parsed. That information
  64. // (thumbnail + caption) is sent later on as an update.
  65. if (hasValue(newCaption) && newCaption !== oldCaption) {
  66. scope.ctrl.updateText();
  67. }
  68. },
  69. );
  70. },
  71. controllerAs: 'ctrl',
  72. controller: ['WebClientService', '$filter', function(webClientService: WebClientService, $filter: ng.IFilterService) {
  73. // TODO: Extract filters into helper functions
  74. const escapeHtml = $filter('escapeHtml') as any;
  75. const markify = $filter('markify') as any;
  76. const emojify = $filter('emojify') as any;
  77. const enlargeSingleEmoji = $filter('enlargeSingleEmoji') as any;
  78. const mentionify = $filter('mentionify') as any;
  79. const linkify = $filter('linkify') as any;
  80. const nlToBr = $filter('nlToBr') as any;
  81. /**
  82. * Apply filters to text.
  83. */
  84. function processText(text: string, largeSingleEmoji: boolean, multiLine: boolean, linkifyText: boolean): string {
  85. const nonLinkified = mentionify(enlargeSingleEmoji(emojify(markify(escapeHtml(text))), largeSingleEmoji));
  86. const maybeLinkified = linkifyText ? linkify(nonLinkified) : nonLinkified;
  87. return nlToBr(maybeLinkified, multiLine);
  88. }
  89. /**
  90. * Text update function.
  91. */
  92. this.updateText = () => {
  93. // Because this.multiLine and this.linkify are bound using an `@` binding,
  94. // they are either undefined or a string. Convert to boolean.
  95. const multiLine = (this.multiLine === undefined || this.multiLine !== 'false');
  96. const linkifyText = (this.linkify === undefined || this.linkify !== 'false');
  97. // Process text once, apply all filter functions
  98. const text = getText(this.message);
  99. this.text = processText(
  100. text,
  101. this.largeSingleEmoji,
  102. multiLine,
  103. linkifyText,
  104. );
  105. };
  106. this.largeSingleEmoji = webClientService.appConfig.largeSingleEmoji;
  107. this.$onInit = function() {
  108. // Process initial text
  109. this.updateText();
  110. };
  111. }],
  112. template: `
  113. <span click-action ng-bind-html="ctrl.text"></span>
  114. `,
  115. };
  116. },
  117. ];