threema_action.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 {UriService} from '../services/uri';
  18. export default [
  19. '$timeout',
  20. '$state',
  21. 'UriService',
  22. function($timeout, $state: ng.ui.IStateService, uriService: UriService) {
  23. const validateThreemaId = (id: string): boolean => {
  24. return id !== undefined && id !== null && /^[0-9A-Z]{8}/.test(id);
  25. };
  26. const addAction = (params) => {
  27. return function(e: Event) {
  28. if (!validateThreemaId(params.id)) {
  29. return false;
  30. }
  31. $state.go('messenger.home.create', {
  32. type: 'contact',
  33. initParams: {
  34. identity: params.id,
  35. },
  36. });
  37. };
  38. };
  39. const composeAction = (params) => {
  40. return function(e: Event) {
  41. if (!validateThreemaId(params.id)) {
  42. return false;
  43. }
  44. const text = params.text || '';
  45. $state.go('messenger.home.conversation', {
  46. type: 'contact',
  47. id: params.id,
  48. initParams: {
  49. text: text,
  50. },
  51. });
  52. };
  53. };
  54. const getThreemaActionHandler = (name: string) => {
  55. switch (name.toLowerCase()) {
  56. case 'add':
  57. return addAction;
  58. case 'compose':
  59. return composeAction;
  60. default:
  61. return null;
  62. }
  63. };
  64. return {
  65. restrict: 'A',
  66. scope: {},
  67. link(scope, el, attrs) {
  68. $timeout(() => {
  69. // tslint:disable-next-line: prefer-for-of (see #98)
  70. for (let i = 0; i < el[0].childNodes.length; i++) {
  71. const node: HTMLElement = el[0].childNodes[i];
  72. if (node.nodeType === Node.ELEMENT_NODE
  73. && node.tagName.toLowerCase() === 'a') {
  74. const link = (node as HTMLElement).innerText;
  75. if (link !== undefined && link.toLowerCase().startsWith('threema://')) {
  76. const matches = (/\bthreema:\/\/([a-z]+)\?([^\s]+)\b/gi).exec(link);
  77. if (matches !== null) {
  78. const handler = getThreemaActionHandler(matches[1]);
  79. const params = uriService.parseQueryParams(matches[2]);
  80. if (handler !== null && params !== null) {
  81. node.addEventListener('click', handler(params));
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }, 0);
  88. },
  89. };
  90. },
  91. ];