group.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. case ControllerModelMode.CHAT:
  58. this.subject = this.group.displayName;
  59. this.members = this.group.members;
  60. break;
  61. case ControllerModelMode.NEW:
  62. this.subject = $translate.instant('messenger.CREATE_GROUP');
  63. this.members = [];
  64. this.avatarController = new AvatarControllerModel(
  65. this.$log, this.webClientService, null,
  66. );
  67. break;
  68. default:
  69. $log.error('Invalid controller model mode: ', this.getMode());
  70. }
  71. }
  72. public getMaxMemberSize(): number {
  73. return this.webClientService.getMaxGroupMemberSize();
  74. }
  75. public setOnRemoved(callback: any): void {
  76. this.onRemovedCallback = callback;
  77. }
  78. public getMode(): ControllerModelMode {
  79. return this.mode;
  80. }
  81. public isValid(): boolean {
  82. return this.members.filter((identity: string) => {
  83. return identity !== this.webClientService.getMyIdentity().identity;
  84. }).length > 0;
  85. }
  86. public canView(): boolean {
  87. return true;
  88. }
  89. public canEdit(): boolean {
  90. return this.group.access !== undefined && (
  91. this.group.access.canChangeAvatar === true
  92. || this.group.access.canChangeName === true
  93. || this.group.access.canChangeMembers === true
  94. );
  95. }
  96. public leave(ev): void {
  97. let confirm = this.$mdDialog.confirm()
  98. .title(this.$translate.instant('messenger.GROUP_LEAVE'))
  99. .textContent(this.$translate.instant(
  100. this.group.administrator === this.webClientService.getMyIdentity().identity
  101. ? 'messenger.GROUP_REALLY_LEAVE_ADMIN'
  102. : 'messenger.GROUP_REALLY_LEAVE'))
  103. .targetEvent(ev)
  104. .ok(this.$translate.instant('common.OK'))
  105. .cancel(this.$translate.instant('common.CANCEL'));
  106. this.$mdDialog.show(confirm).then(() => {
  107. this.reallyLeave();
  108. }, () => {
  109. this.$log.debug('leave canceled');
  110. });
  111. }
  112. private reallyLeave(): void {
  113. if (!this.group.access.canLeave) {
  114. this.$log.error('cannot leave group');
  115. return;
  116. }
  117. this.isLoading = true;
  118. this.webClientService.leaveGroup(this.group)
  119. .then(() => {
  120. this.isLoading = false;
  121. })
  122. .catch(() => {
  123. this.isLoading = false;
  124. });
  125. }
  126. public delete(ev): void {
  127. let confirm = this.$mdDialog.confirm()
  128. .title(this.$translate.instant('messenger.GROUP_DELETE'))
  129. .textContent(this.$translate.instant('messenger.GROUP_DELETE_REALLY'))
  130. .targetEvent(ev)
  131. .ok(this.$translate.instant('common.OK'))
  132. .cancel(this.$translate.instant('common.CANCEL'));
  133. this.$mdDialog.show(confirm).then(() => {
  134. this.reallyDelete();
  135. }, () => {
  136. this.$log.debug('delete canceled');
  137. });
  138. }
  139. private reallyDelete(): void {
  140. if (!this.group.access.canDelete) {
  141. this.$log.error('can not delete group');
  142. return;
  143. }
  144. this.isLoading = true;
  145. this.webClientService.deleteGroup(this.group)
  146. .then(() => {
  147. this.isLoading = false;
  148. if (this.onRemovedCallback) {
  149. this.onRemovedCallback(this.group.id);
  150. }
  151. })
  152. .catch(() => {
  153. this.isLoading = false;
  154. });
  155. }
  156. public sync(ev): void {
  157. if (!this.group.access.canSync) {
  158. this.$log.error('cannot sync group');
  159. return;
  160. }
  161. this.isLoading = true;
  162. this.webClientService.syncGroup(this.group)
  163. .then(() => {
  164. this.isLoading = false;
  165. })
  166. .catch(() => {
  167. this.isLoading = false;
  168. });
  169. }
  170. public save(): Promise<threema.GroupReceiver> {
  171. switch (this.getMode()) {
  172. case ControllerModelMode.EDIT:
  173. return this.webClientService.modifyGroup(
  174. this.group.id,
  175. this.members,
  176. this.name,
  177. this.avatarController.getAvatar(),
  178. );
  179. case ControllerModelMode.NEW:
  180. return this.webClientService.createGroup(
  181. this.members,
  182. this.name,
  183. this.avatarController.getAvatar());
  184. default:
  185. this.$log.error('not allowed to save group');
  186. }
  187. }
  188. public onChangeMembers(identities: string[]): void {
  189. this.members = identities;
  190. }
  191. }