group.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. import {AvatarControllerModel} from './avatar';
  20. export class GroupControllerModel implements threema.ControllerModel {
  21. private $log: ng.ILogService;
  22. private $translate: ng.translate.ITranslateService;
  23. private $mdDialog: ng.material.IDialogService;
  24. public members: string[];
  25. public name: string;
  26. public subject: string;
  27. public isLoading = false;
  28. private addContactPlaceholder: string;
  29. private group: threema.GroupReceiver;
  30. private webClientService: WebClientService;
  31. private avatarController: AvatarControllerModel;
  32. private mode: ControllerModelMode;
  33. private onRemovedCallback: any;
  34. constructor($log: ng.ILogService, $translate: ng.translate.ITranslateService, $mdDialog: ng.material.IDialogService,
  35. webClientService: WebClientService,
  36. mode: ControllerModelMode,
  37. group: threema.GroupReceiver = undefined) {
  38. this.$log = $log;
  39. this.$translate = $translate;
  40. this.$mdDialog = $mdDialog;
  41. this.group = group;
  42. this.mode = mode;
  43. this.webClientService = webClientService;
  44. this.addContactPlaceholder = $translate.instant('messenger.GROUP_SELECT_CONTACTS');
  45. switch (this.getMode()) {
  46. case ControllerModelMode.EDIT:
  47. this.subject = $translate.instant('messenger.EDIT_RECEIVER', {
  48. receiverName: '@NAME@',
  49. }).replace('@NAME@', this.group.displayName);
  50. this.name = this.group.displayName;
  51. this.members = this.group.members;
  52. this.avatarController = new AvatarControllerModel(
  53. this.$log, this.webClientService, this.group,
  54. );
  55. break;
  56. case ControllerModelMode.VIEW:
  57. this.subject = this.group.displayName;
  58. this.members = this.group.members;
  59. break;
  60. case ControllerModelMode.NEW:
  61. this.subject = $translate.instant('messenger.CREATE_GROUP');
  62. this.members = [];
  63. this.avatarController = new AvatarControllerModel(
  64. this.$log, this.webClientService, null,
  65. );
  66. break;
  67. default:
  68. $log.error('Invalid controller model mode: ', this.getMode());
  69. }
  70. }
  71. public setOnRemoved(callback: any): void {
  72. this.onRemovedCallback = callback;
  73. }
  74. public getMode(): ControllerModelMode {
  75. return this.mode;
  76. }
  77. public isValid(): boolean {
  78. return this.members.filter((identity: string) => {
  79. return identity !== this.webClientService.getMyIdentity().identity;
  80. }).length > 0;
  81. }
  82. public canView(): boolean {
  83. return true;
  84. }
  85. public canEdit(): boolean {
  86. return this.group.access !== undefined && (
  87. this.group.access.canChangeAvatar === true
  88. || this.group.access.canChangeName === true
  89. || this.group.access.canChangeMembers === true
  90. );
  91. }
  92. public leave(ev): void {
  93. let confirm = this.$mdDialog.confirm()
  94. .title(this.$translate.instant('messenger.GROUP_LEAVE'))
  95. .textContent(this.$translate.instant(
  96. this.group.administrator === this.webClientService.getMyIdentity().identity
  97. ? 'messenger.GROUP_REALLY_LEAVE_ADMIN'
  98. : 'messenger.GROUP_REALLY_LEAVE'))
  99. .targetEvent(ev)
  100. .ok(this.$translate.instant('common.OK'))
  101. .cancel(this.$translate.instant('common.CANCEL'));
  102. this.$mdDialog.show(confirm).then(() => {
  103. this.reallyLeave();
  104. }, () => {
  105. this.$log.debug('leave canceled');
  106. });
  107. }
  108. private reallyLeave(): void {
  109. if (!this.group.access.canLeave) {
  110. this.$log.error('cannot leave group');
  111. return;
  112. }
  113. this.isLoading = true;
  114. this.webClientService.leaveGroup(this.group)
  115. .then(() => {
  116. this.isLoading = false;
  117. })
  118. .catch(() => {
  119. this.isLoading = false;
  120. });
  121. }
  122. public delete(ev): void {
  123. let confirm = this.$mdDialog.confirm()
  124. .title(this.$translate.instant('messenger.GROUP_DELETE'))
  125. .textContent(this.$translate.instant('messenger.GROUP_DELETE_REALLY'))
  126. .targetEvent(ev)
  127. .ok(this.$translate.instant('common.OK'))
  128. .cancel(this.$translate.instant('common.CANCEL'));
  129. this.$mdDialog.show(confirm).then(() => {
  130. this.reallyDelete();
  131. }, () => {
  132. this.$log.debug('delete canceled');
  133. });
  134. }
  135. private reallyDelete(): void {
  136. if (!this.group.access.canDelete) {
  137. this.$log.error('can not delete group');
  138. return;
  139. }
  140. this.isLoading = true;
  141. this.webClientService.deleteGroup(this.group)
  142. .then(() => {
  143. this.isLoading = false;
  144. if (this.onRemovedCallback) {
  145. this.onRemovedCallback(this.group.id);
  146. }
  147. })
  148. .catch(() => {
  149. this.isLoading = false;
  150. });
  151. }
  152. public sync(ev): void {
  153. if (!this.group.access.canSync) {
  154. this.$log.error('cannot sync group');
  155. return;
  156. }
  157. this.isLoading = true;
  158. this.webClientService.syncGroup(this.group)
  159. .then(() => {
  160. this.isLoading = false;
  161. })
  162. .catch(() => {
  163. this.isLoading = false;
  164. });
  165. }
  166. public save(): Promise<threema.GroupReceiver> {
  167. switch (this.getMode()) {
  168. case ControllerModelMode.EDIT:
  169. return this.webClientService.modifyGroup(
  170. this.group.id,
  171. this.members,
  172. this.name,
  173. this.avatarController.getAvatar(),
  174. );
  175. case ControllerModelMode.NEW:
  176. return this.webClientService.createGroup(
  177. this.members,
  178. this.name,
  179. this.avatarController.getAvatar());
  180. default:
  181. this.$log.error('not allowed to save group');
  182. }
  183. }
  184. public onChangeMembers(identities: string[]): void {
  185. this.members = identities;
  186. }
  187. }