distributionList.ts 6.4 KB

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