message.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 {MessageService} from '../services/message';
  18. import {ReceiverService} from '../services/receiver';
  19. import {WebClientService} from '../services/webclient';
  20. export default [
  21. 'WebClientService',
  22. 'MessageService',
  23. 'ReceiverService',
  24. '$mdDialog',
  25. '$translate',
  26. '$rootScope',
  27. '$log',
  28. function(webClientService: WebClientService, messageService: MessageService,
  29. receiverService: ReceiverService,
  30. $mdDialog: ng.material.IDialogService, $translate: ng.translate.ITranslateService,
  31. $rootScope: ng.IRootScopeService, $log: ng.ILogService) {
  32. return {
  33. restrict: 'E',
  34. scope: {},
  35. bindToController: {
  36. type: '=eeeType',
  37. receiver: '=eeeReceiver',
  38. message: '=eeeMessage',
  39. resolution: '=?eeeResolution',
  40. },
  41. controllerAs: 'ctrl',
  42. controller: [function() {
  43. // Return the contact
  44. const getIdentity = function(message: threema.Message) {
  45. if (message.isOutbox) {
  46. return webClientService.me.id;
  47. }
  48. if (message.partnerId != null) {
  49. return message.partnerId;
  50. }
  51. return null;
  52. };
  53. // Defaults and variables
  54. if (this.resolution == null) {
  55. this.resolution = 'low';
  56. }
  57. this.contact = webClientService.contacts.get(getIdentity(this.message));
  58. // Show...
  59. this.isStatusMessage = this.message.isStatus;
  60. this.isContactMessage = !this.message.isStatus
  61. && webClientService.contacts.has(getIdentity(this.message));
  62. this.isGroup = this.type as threema.ReceiverType === 'group';
  63. this.isContact = this.type as threema.ReceiverType === 'contact';
  64. this.isBusinessReceiver = receiverService.isBusinessContact(this.receiver);
  65. this.showName = !this.message.isOutbox && this.isGroup;
  66. // show avatar only if a name is shown
  67. this.showAvatar = this.showName;
  68. this.showText = this.message.type === 'text' || this.message.caption;
  69. this.showMedia = this.message.type !== 'text';
  70. this.showState = messageService.showStatusIcon(this.message as threema.Message, this.receiver);
  71. this.showQuote = this.message.quote !== undefined;
  72. this.showVoipInfo = this.message.type === 'voipStatus';
  73. this.access = messageService.getAccess(this.message, this.receiver);
  74. this.ack = (ack: boolean) => {
  75. webClientService.ackMessage(this.receiver, this.message, ack);
  76. };
  77. this.quote = () => {
  78. // set message as quoted
  79. webClientService.setQuote(this.receiver, this.message);
  80. };
  81. this.delete = (ev) => {
  82. const confirm = $mdDialog.confirm()
  83. .title($translate.instant('messenger.CONFIRM_DELETE_TITLE'))
  84. .textContent($translate.instant('common.ARE_YOU_SURE'))
  85. .targetEvent(ev)
  86. .ok($translate.instant('common.YES'))
  87. .cancel($translate.instant('common.CANCEL'));
  88. $mdDialog.show(confirm).then((result) => {
  89. webClientService.deleteMessage(this.receiver, this.message);
  90. }, () => { /* do nothing */});
  91. };
  92. this.copy = (ev) => {
  93. $log.debug('TODO implement copy');
  94. };
  95. this.download = (ev) => {
  96. this.downloading = true;
  97. webClientService.requestBlob(this.message.id, this.receiver)
  98. .then((buffer: ArrayBuffer) => {
  99. $rootScope.$apply(() => {
  100. this.downloading = false;
  101. // this.downloaded = true;
  102. switch (this.message.type) {
  103. case 'image':
  104. case 'video':
  105. case 'file':
  106. case 'audio':
  107. saveAs(new Blob([buffer]), messageService.getFileName(this.message));
  108. break;
  109. default:
  110. $log.warn('Ignored download request for message type', this.message.type);
  111. }
  112. });
  113. })
  114. .catch((error) => {
  115. $log.error('error downloading blob ', error);
  116. this.downloading = false;
  117. // this.downloaded = true;
  118. });
  119. };
  120. this.isDownloading = () => {
  121. return this.downloading;
  122. };
  123. }],
  124. link: function(scope: any, element: ng.IAugmentedJQuery, attrs) {
  125. // Prevent status messages from being draggable
  126. const domElement: HTMLElement = element[0];
  127. if (scope.ctrl.isStatusMessage) {
  128. domElement.ondragstart = () => false;
  129. }
  130. },
  131. templateUrl: 'directives/message.html',
  132. };
  133. },
  134. ];