message.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 {isContactReceiver} from '../typeguards';
  18. import {ReceiverService} from './receiver';
  19. export class MessageAccess {
  20. public quote = false;
  21. public ack = false;
  22. public dec = false;
  23. public delete = false;
  24. public download = false;
  25. public copy = false;
  26. }
  27. export class MessageService {
  28. // Angular services
  29. private $log: ng.ILogService;
  30. private $timeout: ng.ITimeoutService;
  31. // Own services
  32. private receiverService: ReceiverService;
  33. // Other
  34. private timeoutDelaySeconds = 30;
  35. public static $inject = ['$log', '$timeout', 'ReceiverService'];
  36. constructor($log: ng.ILogService, $timeout: ng.ITimeoutService, receiverService: ReceiverService) {
  37. this.$log = $log;
  38. this.$timeout = $timeout;
  39. this.receiverService = receiverService;
  40. }
  41. public getAccess(message: threema.Message, receiver: threema.Receiver): MessageAccess {
  42. const access = new MessageAccess();
  43. if (message !== undefined) {
  44. access.quote = (message.type === 'text')
  45. || (message.type === 'location')
  46. || (message.caption !== undefined && message.caption !== null && message.caption.length > 0);
  47. access.copy = access.quote;
  48. if (receiver !== undefined && message.temporaryId === undefined) {
  49. if (message.isOutbox === false
  50. && isContactReceiver(receiver)
  51. && message.type !== 'voipStatus') {
  52. access.ack = message.state !== 'user-ack';
  53. access.dec = message.state !== 'user-dec';
  54. }
  55. switch (message.type) {
  56. case 'image':
  57. case 'video':
  58. case 'audio':
  59. case 'file':
  60. access.download = true;
  61. break;
  62. default:
  63. access.download = false;
  64. }
  65. access.delete = true;
  66. }
  67. }
  68. return access;
  69. }
  70. /**
  71. * Return the quotable text in this message, or null.
  72. */
  73. public getQuoteText(message: threema.Message): string | null {
  74. let quoteText = null;
  75. if (message !== null && message !== undefined) {
  76. switch (message.type) {
  77. case 'text':
  78. quoteText = message.body;
  79. break;
  80. case 'location':
  81. quoteText = message.location.description;
  82. break;
  83. case 'file':
  84. case 'image':
  85. quoteText = message.caption;
  86. break;
  87. default:
  88. // Ignore (handled below)
  89. }
  90. }
  91. return quoteText;
  92. }
  93. public showStatusIcon(message: threema.Message, receiver: threema.Receiver): boolean {
  94. if (message !== null && receiver !== null) {
  95. const messageState = message.state;
  96. // group message/distribution list message icons only on pending or failing states
  97. switch (receiver.type) {
  98. case 'group':
  99. if (message.isOutbox && (message.temporaryId === undefined || message.temporaryId === null)) {
  100. return messageState === 'send-failed'
  101. || messageState === 'sending'
  102. || (messageState === 'pending' && message.type !== 'ballot');
  103. }
  104. return false;
  105. case 'distributionList':
  106. return false;
  107. case 'contact':
  108. if (!message.isOutbox) {
  109. return messageState === 'user-ack'
  110. || messageState === 'user-dec';
  111. } else if (this.receiverService.isBusinessContact(receiver)) {
  112. // move this into a service
  113. return messageState === 'send-failed'
  114. || messageState === 'sending'
  115. || messageState === 'pending';
  116. }
  117. return true;
  118. default:
  119. return false;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * Create a message object with a temporaryId
  126. */
  127. public createTemporary(receiver: threema.Receiver, msgType: string,
  128. messageData: threema.MessageData): threema.Message {
  129. const now = new Date();
  130. const message = {
  131. temporaryId: receiver.type + receiver.id + Math.random(),
  132. type: msgType,
  133. isOutbox: true,
  134. state: 'pending',
  135. id: undefined,
  136. body: undefined,
  137. date: Math.floor(Date.now() / 1000),
  138. partnerId: receiver.id,
  139. isStatus: false,
  140. quote: undefined,
  141. caption: msgType === 'file' ? (messageData as threema.FileMessageData).caption : null,
  142. } as threema.Message;
  143. if (msgType === 'text') {
  144. message.body = (messageData as threema.TextMessageData).text;
  145. message.quote = (messageData as threema.TextMessageData).quote;
  146. }
  147. // Add delay for timeout checking
  148. this.$timeout(() => {
  149. // Set the state to timeout if it is still pending.
  150. // Note: If sending the message worked, by now the message object
  151. // will have been replaced by a new one and the state change would
  152. // have no effect anyways...
  153. if (message.state === 'pending') {
  154. message.state = 'timeout';
  155. }
  156. }, this.timeoutDelaySeconds * 1000);
  157. return message;
  158. }
  159. }