group.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 {AvatarControllerModel} from './avatar';
  19. // Type aliases
  20. import ControllerModelMode = threema.ControllerModelMode;
  21. export class GroupControllerModel implements threema.ControllerModel {
  22. private $log: ng.ILogService;
  23. private $translate: ng.translate.ITranslateService;
  24. private $mdDialog: ng.material.IDialogService;
  25. public members: string[];
  26. public name: string;
  27. public subject: string;
  28. public isLoading = false; // TODO: Show loading indicator
  29. private addContactPlaceholder: string;
  30. private group: threema.GroupReceiver;
  31. private webClientService: WebClientService;
  32. private avatarController: AvatarControllerModel;
  33. private mode: ControllerModelMode;
  34. private onRemovedCallback: threema.OnRemovedCallback;
  35. constructor($log: ng.ILogService, $translate: ng.translate.ITranslateService, $mdDialog: ng.material.IDialogService,
  36. webClientService: WebClientService,
  37. mode: ControllerModelMode,
  38. group?: threema.GroupReceiver) {
  39. this.$log = $log;
  40. this.$translate = $translate;
  41. this.$mdDialog = $mdDialog;
  42. this.group = group;
  43. this.mode = mode;
  44. this.webClientService = webClientService;
  45. this.addContactPlaceholder = $translate.instant('messenger.GROUP_SELECT_CONTACTS');
  46. switch (this.getMode()) {
  47. case ControllerModelMode.EDIT:
  48. this.subject = $translate.instant('messenger.EDIT_RECEIVER', {
  49. receiverName: '@NAME@',
  50. }).replace('@NAME@', this.group.displayName);
  51. this.name = this.group.displayName;
  52. this.members = this.group.members;
  53. this.avatarController = new AvatarControllerModel(
  54. this.$log, this.webClientService, this.group,
  55. );
  56. break;
  57. case ControllerModelMode.VIEW:
  58. case ControllerModelMode.CHAT:
  59. this.subject = this.group.displayName;
  60. this.members = this.group.members;
  61. break;
  62. case ControllerModelMode.NEW:
  63. this.subject = $translate.instant('messenger.CREATE_GROUP');
  64. this.members = [];
  65. this.avatarController = new AvatarControllerModel(
  66. this.$log, this.webClientService, null,
  67. );
  68. break;
  69. default:
  70. $log.error('Invalid controller model mode: ', this.getMode());
  71. }
  72. }
  73. public getMaxMemberSize(): number {
  74. return this.webClientService.getMaxGroupMemberSize();
  75. }
  76. public setOnRemoved(callback: threema.OnRemovedCallback): void {
  77. this.onRemovedCallback = callback;
  78. }
  79. public getMode(): ControllerModelMode {
  80. return this.mode;
  81. }
  82. public isValid(): boolean {
  83. return this.members.filter((identity: string) => {
  84. return identity !== this.webClientService.getProfile().identity;
  85. }).length > 0;
  86. }
  87. public canChat(): boolean {
  88. return true;
  89. }
  90. public canEdit(): boolean {
  91. return this.group.access !== undefined && (
  92. this.group.access.canChangeAvatar === true
  93. || this.group.access.canChangeName === true
  94. || this.group.access.canChangeMembers === true
  95. );
  96. }
  97. public canClean(): boolean {
  98. return this.canChat();
  99. }
  100. public clean(ev: any): any {
  101. const confirm = this.$mdDialog.confirm()
  102. .title(this.$translate.instant('messenger.DELETE_THREAD'))
  103. .textContent(this.$translate.instant('messenger.DELETE_THREAD_MESSAGE', {count: 1}))
  104. .targetEvent(ev)
  105. .ok(this.$translate.instant('common.YES'))
  106. .cancel(this.$translate.instant('common.CANCEL'));
  107. this.$mdDialog.show(confirm).then(() => {
  108. this.reallyClean();
  109. }, () => {
  110. this.$log.debug('clean canceled');
  111. });
  112. }
  113. private reallyClean(): any {
  114. if (!this.canClean()) {
  115. this.$log.error('not allowed to clean this contact');
  116. return;
  117. }
  118. this.isLoading = true;
  119. this.webClientService.cleanReceiverConversation(this.group)
  120. .then(() => {
  121. this.isLoading = false;
  122. })
  123. .catch(() => {
  124. this.isLoading = false;
  125. });
  126. }
  127. public canShowQr(): boolean {
  128. return false;
  129. }
  130. public leave(ev): void {
  131. const confirm = this.$mdDialog.confirm()
  132. .title(this.$translate.instant('messenger.GROUP_LEAVE'))
  133. .textContent(this.$translate.instant(
  134. this.group.administrator === this.webClientService.getProfile().identity
  135. ? 'messenger.GROUP_REALLY_LEAVE_ADMIN'
  136. : 'messenger.GROUP_REALLY_LEAVE'))
  137. .targetEvent(ev)
  138. .ok(this.$translate.instant('common.OK'))
  139. .cancel(this.$translate.instant('common.CANCEL'));
  140. this.$mdDialog.show(confirm).then(() => {
  141. this.reallyLeave();
  142. }, () => {
  143. this.$log.debug('leave canceled');
  144. });
  145. }
  146. private reallyLeave(): void {
  147. if (!this.group.access.canLeave) {
  148. this.$log.error('cannot leave group');
  149. return;
  150. }
  151. this.isLoading = true;
  152. this.webClientService.leaveGroup(this.group)
  153. .then(() => {
  154. this.isLoading = false;
  155. })
  156. .catch(() => {
  157. this.isLoading = false;
  158. });
  159. }
  160. public delete(ev): void {
  161. const confirm = this.$mdDialog.confirm()
  162. .title(this.$translate.instant('messenger.GROUP_DELETE'))
  163. .textContent(this.$translate.instant('messenger.GROUP_DELETE_REALLY'))
  164. .targetEvent(ev)
  165. .ok(this.$translate.instant('common.OK'))
  166. .cancel(this.$translate.instant('common.CANCEL'));
  167. this.$mdDialog.show(confirm).then(() => {
  168. this.reallyDelete();
  169. }, () => {
  170. this.$log.debug('delete canceled');
  171. });
  172. }
  173. private reallyDelete(): void {
  174. if (!this.group.access.canDelete) {
  175. this.$log.error('can not delete group');
  176. return;
  177. }
  178. this.isLoading = true;
  179. this.webClientService.deleteGroup(this.group)
  180. .then(() => {
  181. this.isLoading = false;
  182. if (this.onRemovedCallback) {
  183. this.onRemovedCallback(this.group.id);
  184. }
  185. })
  186. .catch(() => {
  187. this.isLoading = false;
  188. });
  189. }
  190. public sync(ev): void {
  191. if (!this.group.access.canSync) {
  192. this.$log.error('cannot sync group');
  193. return;
  194. }
  195. this.isLoading = true;
  196. this.webClientService.syncGroup(this.group)
  197. .then(() => {
  198. this.isLoading = false;
  199. })
  200. .catch((errorCode) => {
  201. this.isLoading = false;
  202. this.showError(errorCode);
  203. });
  204. }
  205. public save(): Promise<threema.GroupReceiver> {
  206. switch (this.getMode()) {
  207. case ControllerModelMode.EDIT:
  208. return this.webClientService.modifyGroup(
  209. this.group.id,
  210. this.members,
  211. this.name,
  212. this.avatarController.avatarChanged ? this.avatarController.getAvatar() : undefined,
  213. );
  214. case ControllerModelMode.NEW:
  215. return this.webClientService.createGroup(
  216. this.members,
  217. this.name,
  218. this.avatarController.getAvatar());
  219. default:
  220. this.$log.error('not allowed to save group');
  221. }
  222. }
  223. public onChangeMembers(identities: string[]): void {
  224. this.members = identities;
  225. }
  226. public getMembers(): string[] {
  227. return this.members;
  228. }
  229. /**
  230. * Show an error message in a dialog.
  231. */
  232. private showError(errorCode: string): void {
  233. if (errorCode === undefined) {
  234. errorCode = 'unknown';
  235. }
  236. this.$mdDialog.show(
  237. this.$mdDialog.alert()
  238. .clickOutsideToClose(true)
  239. .title(this.group.displayName)
  240. .textContent(this.$translate.instant('validationError.modifyReceiver.' + errorCode))
  241. .ok(this.$translate.instant('common.OK')),
  242. );
  243. }
  244. }