latest_message.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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', 'ReceiverService', 'MessageService', '$filter',
  22. function(webClientService: WebClientService, receiverService: ReceiverService,
  23. messageService: MessageService, $filter: any) {
  24. return {
  25. restrict: 'EA',
  26. scope: {},
  27. bindToController: {
  28. type: '=eeeType',
  29. message: '=eeeMessage',
  30. receiver: '=eeeReceiver',
  31. },
  32. controllerAs: 'ctrl',
  33. controller: [function() {
  34. // Utilities
  35. const getIdentity = function(message) {
  36. // TODO: Get rid of duplication with eeeMessage directive
  37. if (message.isOutbox) {
  38. return webClientService.me.id;
  39. }
  40. if (message.partnerId !== null) {
  41. return message.partnerId;
  42. }
  43. return null;
  44. };
  45. // Conversation properties
  46. this.isGroup = this.type as threema.ReceiverType === 'group';
  47. this.isDistributionList = !this.isGroup
  48. && this.type as threema.ReceiverType === 'distributionList';
  49. this.showVoipInfo = this.message
  50. && (this.message as threema.Message).type === 'voipStatus';
  51. if (this.showVoipInfo) {
  52. this.statusIcon = 'phone_locked';
  53. } else if (this.isGroup) {
  54. this.statusIcon = 'group';
  55. } else if (this.isDistributionList) {
  56. this.statusIcon = 'forum';
  57. } else if (!this.message.isOutbox) {
  58. this.statusIcon = 'reply';
  59. } else if (messageService.showStatusIcon(this.message, this.receiver)) {
  60. // Show status icon of incoming messages every time
  61. this.statusIcon = $filter('messageStateIcon')(this.message);
  62. } else {
  63. // Do not show a status icon
  64. this.statusIcon = null;
  65. }
  66. // Find sender of latest message in group chats
  67. this.contact = null;
  68. if (this.message) {
  69. this.contact = webClientService.contacts.get(getIdentity(this.message));
  70. }
  71. // Typing indicator
  72. this.isTyping = () => false;
  73. if (this.isGroup === false
  74. && this.isDistributionList === false
  75. && this.contact !== null) {
  76. this.isTyping = () => {
  77. return webClientService.isTyping(this.contact);
  78. };
  79. }
  80. this.isHidden = () => {
  81. return this.receiver.locked;
  82. };
  83. // Show...
  84. this.showIcon = this.message
  85. && this.message.type !== 'text'
  86. && this.message.type !== 'status';
  87. this.getDraft = () => {
  88. return webClientService.getDraft(this.receiver);
  89. };
  90. this.showDraft = () => {
  91. if (receiverService.isConversationActive(this.receiver)) {
  92. // Don't show draft if conversation is active
  93. return false;
  94. }
  95. const draft = this.getDraft();
  96. return draft !== undefined && draft !== null;
  97. };
  98. }],
  99. templateUrl: 'directives/latest_message.html',
  100. };
  101. },
  102. ];