click_action.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. import {StateService as UiStateService} from '@uirouter/angularjs';
  18. import {UriService} from '../services/uri';
  19. import {WebClientService} from '../services/webclient';
  20. export default [
  21. '$timeout',
  22. '$state',
  23. 'UriService',
  24. 'WebClientService',
  25. function(
  26. $timeout: ng.ITimeoutService,
  27. $state: UiStateService,
  28. uriService: UriService,
  29. webClientService: WebClientService,
  30. ) {
  31. const validateThreemaId = (id: string): boolean => {
  32. return id !== undefined && id !== null && /^([a-zA-Z0-9\*][a-zA-Z0-9]{7})$/.test(id);
  33. };
  34. const viewReceiver = (receiver: threema.Receiver) => {
  35. return function(e: Event) {
  36. if (!receiver) {
  37. return false;
  38. }
  39. $state.go('messenger.home.detail', receiver);
  40. };
  41. };
  42. const addAction = (params) => {
  43. return function(e: Event) {
  44. if (!validateThreemaId(params.id)) {
  45. return false;
  46. }
  47. e.preventDefault();
  48. // Verify the receiver already exists
  49. const contactReceiver = webClientService.contacts.get(params.id);
  50. if (contactReceiver) {
  51. return viewReceiver(contactReceiver)(e);
  52. }
  53. $state.go('messenger.home.create', {
  54. type: 'contact',
  55. initParams: {
  56. identity: params.id,
  57. },
  58. });
  59. };
  60. };
  61. const composeAction = (params) => {
  62. return function(e: Event) {
  63. if (!validateThreemaId(params.id)) {
  64. return false;
  65. }
  66. const text = params.text || '';
  67. $state.go('messenger.home.conversation', {
  68. type: 'contact',
  69. id: params.id,
  70. initParams: {
  71. text: text,
  72. },
  73. });
  74. };
  75. };
  76. const getThreemaActionHandler = (name: string) => {
  77. switch (name.toLowerCase()) {
  78. case 'add':
  79. return addAction;
  80. case 'compose':
  81. return composeAction;
  82. default:
  83. return null;
  84. }
  85. };
  86. return {
  87. restrict: 'A',
  88. scope: {},
  89. link(scope, el, attrs) {
  90. $timeout(() => {
  91. // tslint:disable-next-line: prefer-for-of (see #98)
  92. for (let i = 0; i < el[0].childNodes.length; i++) {
  93. const node: HTMLElement = el[0].childNodes[i];
  94. if (node.nodeType === Node.ELEMENT_NODE) {
  95. switch ( node.tagName.toLowerCase()) {
  96. case 'a':
  97. const link = (node as HTMLElement).innerText;
  98. if (link !== undefined) {
  99. if (link.toLowerCase().startsWith('threema://')) {
  100. const matches = (/\bthreema:\/\/([a-z]+)\?([^\s]+)\b/gi).exec(link);
  101. if (matches !== null) {
  102. const handler = getThreemaActionHandler(matches[1]);
  103. const params = uriService.parseQueryParams(matches[2]);
  104. if (handler !== null && params !== null) {
  105. node.addEventListener('click', handler(params));
  106. }
  107. }
  108. } else if (link.toLowerCase().startsWith('https://threema.id/')) {
  109. // tslint:disable-next-line:max-line-length
  110. const matches = (/\bhttps:\/\/threema\.id\/([a-zA-Z0-9\*][a-zA-Z0-9]{7})\b/gi).exec(link);
  111. if (matches !== null) {
  112. node.addEventListener('click', addAction({id: matches[1]}));
  113. }
  114. }
  115. }
  116. break;
  117. case 'span':
  118. // Support only id mentions (not all or me)
  119. const mentionCssClass = 'mention id';
  120. const cls = node.getAttribute('class');
  121. // Solved with the css classes, because angularJS removes
  122. // all other attributes from the DOMElement
  123. if (cls.substr(0, mentionCssClass.length) === mentionCssClass) {
  124. // Hack to extract the identity from class name
  125. const identity = cls.substring(mentionCssClass.length).trim();
  126. if (validateThreemaId(identity)) {
  127. const contactReceiver = webClientService.contacts.get(identity);
  128. node.addEventListener('click', viewReceiver(contactReceiver));
  129. node.setAttribute('class', cls + ' link');
  130. node.setAttribute('title', contactReceiver.displayName);
  131. }
  132. }
  133. default:
  134. // ignore
  135. }
  136. }
  137. }
  138. }, 0);
  139. },
  140. };
  141. },
  142. ];