avatar.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. export class AvatarControllerModel {
  19. private $log: ng.ILogService;
  20. private avatar: ArrayBuffer = null;
  21. private loadAvatar: Promise<string>;
  22. private onChangeAvatar: (image: ArrayBuffer) => void;
  23. private _avatarChanged: boolean = false;
  24. constructor($log: ng.ILogService,
  25. webClientService: WebClientService,
  26. receiver: threema.Receiver) {
  27. this.$log = $log;
  28. this.loadAvatar = new Promise((resolve, reject) => {
  29. if (receiver === null) {
  30. resolve(null);
  31. return;
  32. }
  33. if (receiver.avatar.high === undefined) {
  34. webClientService.requestAvatar(receiver, true)
  35. .then((image: string) => resolve(image))
  36. .catch(() => reject());
  37. } else {
  38. resolve(receiver.avatar.high);
  39. }
  40. });
  41. // bind to the editor
  42. this.onChangeAvatar = (image: ArrayBuffer) => {
  43. this.avatar = image;
  44. this._avatarChanged = true;
  45. };
  46. }
  47. /**
  48. * Return the avatar bytes (or null if no avatar is defined).
  49. */
  50. public getAvatar(): ArrayBuffer | null {
  51. return this.avatar;
  52. }
  53. /**
  54. * Return whether this avatar was changed.
  55. *
  56. * This will return true if an avatar was added or removed. It does not
  57. * actually look at the content to determine whether the bytes of the
  58. * avatar really changed.
  59. */
  60. public get avatarChanged(): boolean {
  61. return this._avatarChanged;
  62. }
  63. }