group.ts 11 KB

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