contact.ts 6.0 KB

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