distributionList.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 {ControllerModelMode} from '../types/enums';
  18. export class DistributionListControllerModel implements threema.ControllerModel {
  19. private $log: ng.ILogService;
  20. private $translate: ng.translate.ITranslateService;
  21. private $mdDialog: ng.material.IDialogService;
  22. public members: string[];
  23. public name: string;
  24. public subject: string;
  25. public isLoading = false;
  26. private addContactPlaceholder: string;
  27. private distributionList: threema.DistributionListReceiver;
  28. private webClientService: threema.WebClientService;
  29. private mode: ControllerModelMode;
  30. private onRemovedCallback: any;
  31. constructor($log: ng.ILogService, $translate: ng.translate.ITranslateService, $mdDialog: ng.material.IDialogService,
  32. webClientService: threema.WebClientService,
  33. mode: ControllerModelMode,
  34. distributionList: threema.DistributionListReceiver = undefined) {
  35. this.$log = $log;
  36. this.$translate = $translate;
  37. this.$mdDialog = $mdDialog;
  38. this.distributionList = distributionList;
  39. this.mode = mode;
  40. this.webClientService = webClientService;
  41. this.addContactPlaceholder = $translate.instant('messenger.DISTRIBUTION_LIST_SELECT_MEMBERS');
  42. switch (this.getMode()) {
  43. case ControllerModelMode.EDIT:
  44. this.subject = $translate.instant('messenger.EDIT_RECEIVER', {
  45. receiverName: '@NAME@',
  46. }).replace('@NAME@', this.distributionList.displayName);
  47. this.name = this.distributionList.displayName;
  48. this.members = this.distributionList.members;
  49. break;
  50. case ControllerModelMode.VIEW:
  51. this.subject = this.distributionList.displayName;
  52. this.members = this.distributionList.members;
  53. break;
  54. case ControllerModelMode.NEW:
  55. this.subject = $translate.instant('messenger.CREATE_DISTRIBUTION_LIST');
  56. this.members = [];
  57. break;
  58. default:
  59. $log.error('Invalid controller model mode: ', this.getMode());
  60. }
  61. }
  62. public setOnRemoved(callback: any): void {
  63. this.onRemovedCallback = callback;
  64. }
  65. public getMode(): ControllerModelMode {
  66. return this.mode;
  67. }
  68. public isValid(): boolean {
  69. return this.members.filter((identity: string) => {
  70. return identity !== this.webClientService.getMyIdentity().identity;
  71. }).length > 0;
  72. }
  73. public canEdit(): boolean {
  74. // a distribution list can always be edited
  75. return true;
  76. }
  77. public delete(ev): void {
  78. let confirm = this.$mdDialog.confirm()
  79. .title(this.$translate.instant('messenger.DISTRIBUTION_LIST_DELETE'))
  80. .textContent(this.$translate.instant('messenger.DISTRIBUTION_LIST_DELETE_REALLY'))
  81. .targetEvent(ev)
  82. .ok(this.$translate.instant('common.OK'))
  83. .cancel(this.$translate.instant('common.CANCEL'));
  84. this.$mdDialog.show(confirm).then(() => {
  85. this.reallyDelete();
  86. if (this.onRemovedCallback) {
  87. this.onRemovedCallback(this.distributionList.id);
  88. }
  89. }, () => {
  90. this.$log.debug('delete canceled');
  91. });
  92. }
  93. private reallyDelete(): void {
  94. if (!this.distributionList.access.canDelete) {
  95. this.$log.error('cannot delete distribution list');
  96. return;
  97. }
  98. this.isLoading = true;
  99. this.webClientService.deleteDistributionList(this.distributionList).then(() => {
  100. this.isLoading = false;
  101. }).catch(() => {
  102. this.isLoading = false;
  103. });
  104. }
  105. public save(): Promise<threema.DistributionListReceiver> {
  106. switch (this.getMode()) {
  107. case ControllerModelMode.EDIT:
  108. return this.webClientService.modifyDistributionList(
  109. this.distributionList.id,
  110. this.members,
  111. this.name,
  112. );
  113. case ControllerModelMode.NEW:
  114. return this.webClientService.createDistributionList(
  115. this.members,
  116. this.name);
  117. default:
  118. this.$log.error('not allowed to save distribution list');
  119. }
  120. }
  121. public onChangeMembers(identities: string[]): void {
  122. this.members = identities;
  123. }
  124. }