contact.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ContactControllerModel implements threema.ControllerModel<threema.ContactReceiver> {
  22. private logTag = '[ContactControllerModel]';
  23. // Angular services
  24. private $log: ng.ILogService;
  25. private $translate: ng.translate.ITranslateService;
  26. private $mdDialog: ng.material.IDialogService;
  27. private onRemovedCallback: threema.OnRemovedCallback;
  28. public firstName?: string;
  29. public lastName?: string;
  30. public identity: string;
  31. public subject: string;
  32. public access: threema.ContactReceiverAccess;
  33. public isLoading = false;
  34. private contact: threema.ContactReceiver | null;
  35. private webClientService: WebClientService;
  36. private firstNameLabel: string;
  37. private avatarController: AvatarControllerModel;
  38. private mode = ControllerModelMode.NEW;
  39. constructor($log: ng.ILogService, $translate: ng.translate.ITranslateService, $mdDialog: ng.material.IDialogService,
  40. webClientService: WebClientService,
  41. mode: ControllerModelMode,
  42. contact?: threema.ContactReceiver) {
  43. this.$log = $log;
  44. this.$translate = $translate;
  45. this.$mdDialog = $mdDialog;
  46. if (contact === undefined) {
  47. if (mode !== ControllerModelMode.NEW) {
  48. throw new Error('ContactControllerModel: Contact may not be undefined for mode ' + mode);
  49. }
  50. } else {
  51. this.contact = contact;
  52. }
  53. this.webClientService = webClientService;
  54. this.mode = mode;
  55. switch (this.getMode()) {
  56. case ControllerModelMode.EDIT:
  57. this.subject = $translate.instant('messenger.EDIT_RECEIVER');
  58. this.firstName = this.contact!.firstName;
  59. this.lastName = this.contact!.lastName;
  60. this.avatarController = new AvatarControllerModel(
  61. this.$log, this.webClientService, this.contact,
  62. );
  63. this.access = this.contact!.access;
  64. this.firstNameLabel = this.access.canChangeLastName ?
  65. $translate.instant('messenger.FIRST_NAME') :
  66. $translate.instant('messenger.NAME');
  67. break;
  68. case ControllerModelMode.VIEW:
  69. case ControllerModelMode.CHAT:
  70. this.subject = this.contact!.displayName;
  71. this.access = this.contact!.access;
  72. break;
  73. case ControllerModelMode.NEW:
  74. this.subject = $translate.instant('messenger.ADD_CONTACT');
  75. break;
  76. default:
  77. $log.error(this.logTag, 'Invalid controller model mode: ', this.getMode());
  78. }
  79. }
  80. public setOnRemoved(callback: threema.OnRemovedCallback): void {
  81. this.onRemovedCallback = callback;
  82. }
  83. public getMode(): ControllerModelMode {
  84. return this.mode;
  85. }
  86. public isValid(): boolean {
  87. // edit and new is always valid
  88. if (this.getMode() === ControllerModelMode.EDIT) {
  89. return true;
  90. }
  91. return this.identity !== undefined && this.identity.length === 8;
  92. }
  93. public canChat(): boolean {
  94. return this.contact !== null && this.contact.id !== this.webClientService.me.id;
  95. }
  96. public canEdit(): boolean {
  97. return this.access !== undefined && (
  98. this.access.canChangeAvatar === true
  99. || this.access.canChangeFirstName === true
  100. || this.access.canChangeLastName === true
  101. );
  102. }
  103. public canClean(): boolean {
  104. return this.canChat();
  105. }
  106. public clean(ev: any): any {
  107. const confirm = this.$mdDialog.confirm()
  108. .title(this.$translate.instant('messenger.DELETE_THREAD'))
  109. .textContent(this.$translate.instant('messenger.DELETE_THREAD_MESSAGE', {count: 1}))
  110. .targetEvent(ev)
  111. .ok(this.$translate.instant('common.YES'))
  112. .cancel(this.$translate.instant('common.NO'));
  113. this.$mdDialog.show(confirm).then(() => {
  114. this.reallyClean();
  115. }, () => {
  116. this.$log.debug(this.logTag, 'Clean canceled');
  117. });
  118. }
  119. private reallyClean(): any {
  120. if (!this.contact) {
  121. this.$log.error(this.logTag, 'reallyClean: Contact is null');
  122. return;
  123. }
  124. if (!this.canClean()) {
  125. this.$log.error(this.logTag, 'Not allowed to clean this contact');
  126. return;
  127. }
  128. this.isLoading = true;
  129. this.webClientService.cleanReceiverConversation(this.contact)
  130. .then(() => {
  131. this.isLoading = false;
  132. })
  133. .catch((error) => {
  134. // TODO: Handle this properly / show an error message
  135. this.$log.error(this.logTag, `Cleaning receiver conversation failed: ${error}`);
  136. this.isLoading = false;
  137. });
  138. }
  139. public canShowQr(): boolean {
  140. return false;
  141. }
  142. public save(): Promise<threema.ContactReceiver> {
  143. switch (this.getMode()) {
  144. case ControllerModelMode.EDIT:
  145. return this.webClientService.modifyContact(
  146. this.contact!.id,
  147. this.firstName,
  148. this.lastName,
  149. this.avatarController.avatarChanged ? this.avatarController.getAvatar() : undefined,
  150. );
  151. case ControllerModelMode.NEW:
  152. return this.webClientService.addContact(this.identity);
  153. default:
  154. this.$log.error(this.logTag, 'Cannot save contact, invalid mode');
  155. return Promise.reject('Cannot save contact, invalid mode');
  156. }
  157. }
  158. public onChangeMembers(identities: string[]): void {
  159. // Do nothing
  160. }
  161. public getMembers(): string[] {
  162. return [this.identity];
  163. }
  164. }