message.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 {ReceiverService} from './receiver';
  18. export class MessageAccess {
  19. public quote = false;
  20. public ack = false;
  21. public dec = false;
  22. public delete = false;
  23. public download = false;
  24. public copy = false;
  25. }
  26. export class MessageService {
  27. private $log: ng.ILogService;
  28. private receiverService: ReceiverService;
  29. public static $inject = ['$log', 'ReceiverService'];
  30. constructor($log: ng.ILogService, receiverService: ReceiverService) {
  31. this.$log = $log;
  32. this.receiverService = receiverService;
  33. }
  34. public getAccess(message: threema.Message, receiver: threema.Receiver): MessageAccess {
  35. let access = new MessageAccess();
  36. if (message !== undefined && receiver !== undefined && message.temporaryId === undefined) {
  37. access.quote = (message.type === 'text')
  38. || (message.type === 'location')
  39. || (message.caption !== undefined);
  40. // disable copy for current version
  41. // access.copy = access.quote;
  42. if (message !== undefined
  43. && message.isOutbox === false
  44. && this.receiverService.isContact(receiver)) {
  45. access.ack = message.state !== 'user-ack';
  46. access.dec = message.state !== 'user-dec';
  47. }
  48. switch (message.type) {
  49. case 'image':
  50. case 'video':
  51. case 'audio':
  52. case 'file':
  53. access.download = true;
  54. break;
  55. default:
  56. access.download = false;
  57. }
  58. access.delete = true;
  59. }
  60. return access;
  61. }
  62. public showStatusIcon(message: threema.Message, receiver: threema.Receiver): boolean {
  63. if (message !== null && receiver !== null) {
  64. let messageState = message.state;
  65. // MessageState messageState = messageModel.getState();
  66. // group message/distribution list message icons only on pending or failing states
  67. switch (receiver.type) {
  68. case 'group':
  69. if (message.isOutbox && (message.temporaryId === undefined || message.temporaryId === null)) {
  70. return messageState === 'send-failed'
  71. || messageState === 'sending'
  72. || (messageState === 'pending' && message.type !== 'ballot');
  73. }
  74. return false;
  75. case 'distributionList':
  76. return false;
  77. case 'contact':
  78. if (!message.isOutbox) {
  79. return messageState === 'user-ack'
  80. || messageState === 'user-dec';
  81. } else if (this.receiverService.isBusinessContact(receiver)) {
  82. // move this into a service
  83. return messageState === 'send-failed'
  84. || messageState === 'sending'
  85. || messageState === 'pending';
  86. }
  87. return true;
  88. default:
  89. return false;
  90. }
  91. }
  92. return false;
  93. }
  94. /**
  95. * return the filename of a message (image, audio, file)
  96. * used for downloads
  97. * @param message
  98. * @returns filename string or null
  99. */
  100. public getFileName(message: threema.Message): string {
  101. if (message === undefined
  102. || message === null) {
  103. return null;
  104. }
  105. let getFileName = (prefix: string, postfix?: string): string => {
  106. if (message.id === undefined) {
  107. this.$log.warn('missing id on message model');
  108. return null;
  109. }
  110. return prefix
  111. + '-' + message.id
  112. + (postfix !== undefined ? '.' + postfix : '');
  113. };
  114. switch (message.type) {
  115. case 'image':
  116. return getFileName('image', 'jpg');
  117. case 'video':
  118. return getFileName('video', 'mpg');
  119. case 'file':
  120. if (message.file !== undefined) {
  121. return message.file.name;
  122. }
  123. // should not happen
  124. this.$log.warn('file message without file object', message.id);
  125. return getFileName('file');
  126. case 'audio':
  127. return getFileName('audio', 'mp4');
  128. default:
  129. // ignore file types without a read file
  130. return null;
  131. }
  132. }
  133. /**
  134. * Create a message object with a temporaryId
  135. */
  136. public createTemporary(receiver: threema.Receiver, msgType: string,
  137. messageData: threema.MessageData): threema.Message {
  138. let now = new Date();
  139. let message = {
  140. temporaryId: receiver.type + receiver.id + Math.random(),
  141. type: msgType,
  142. isOutbox: true,
  143. state: 'pending',
  144. id: undefined,
  145. body: undefined,
  146. date: ('0' + now.getHours()).slice(-2) + ':' + ('0' + now.getMinutes()).slice(-2),
  147. partnerId: receiver.id,
  148. isStatus: false,
  149. quote: undefined,
  150. caption: msgType === 'file' ? (messageData as threema.FileMessageData).caption : null,
  151. } as threema.Message;
  152. if (msgType === 'text') {
  153. message.body = (messageData as threema.TextMessageData).text;
  154. message.quote = (messageData as threema.TextMessageData).quote;
  155. }
  156. return message;
  157. }
  158. }