A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.3 KiB

  1. import * as log from "loglevel";
  2. import { Subject } from "../observe/Observable";
  3. import { Observer } from "../observe/Observer";
  4. import { JsonAPI } from "../singleton/JsonAPI";
  5. import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel";
  6. import { fetchErrorHandler } from "./FetchErrorHandler";
  7. import { NotificationService } from "../service/NotificationService";
  8. export class UserModel implements Subject<ActiveUserViewModel> {
  9. /**
  10. * @type {Observer[]} List of subscribers. In real life, the list of
  11. * subscribers can be stored more comprehensively (categorized by event
  12. * type, etc.).
  13. */
  14. private readonly observers: Observer<ActiveUserViewModel>[] = [];
  15. private _activeUsersList: ActiveUserViewModel[] = Array();
  16. private readonly _notificationService: NotificationService;
  17. constructor(notificationService: NotificationService) {
  18. this._activeUsersList = [];
  19. this._notificationService = notificationService;
  20. }
  21. /**
  22. * Getter activeUsersList
  23. * @return {ActiveUserViewModel[] }
  24. */
  25. public get activeUsersList(): ActiveUserViewModel[] {
  26. return this._activeUsersList;
  27. }
  28. /**
  29. * The subscription management methods.
  30. */
  31. public attach(observer: Observer<ActiveUserViewModel>): void {
  32. log.info("Subject: Attached an observer.");
  33. this.observers.push(observer);
  34. }
  35. public detach(observer: Observer<ActiveUserViewModel>): void {
  36. const observerIndex = this.observers.indexOf(observer);
  37. this.observers.splice(observerIndex, 1);
  38. log.info("Subject: Detached an observer.");
  39. }
  40. /**
  41. * Trigger an update in each subscriber.
  42. */
  43. public notify(): void {
  44. log.info("Subject: Notifying observers...");
  45. for (const observer of this.observers) {
  46. observer.update({ data: this._activeUsersList!, op: "" });
  47. }
  48. }
  49. public someBusinessMethod(activeuserList: ActiveUserViewModel[]): void {
  50. this._activeUsersList = activeuserList;
  51. this.helperMethod();
  52. log.info(`Subject: My state has just changed`);
  53. log.trace(activeuserList);
  54. this.notify();
  55. }
  56. /**
  57. * getActiveUsers
  58. */
  59. public getActiveUsers(): void {
  60. if (JsonAPI.authToken != null) {
  61. this._getActiveUsersAjax(JsonAPI.authToken).then((data) => {
  62. // // activeUsers = data;
  63. // sessionStorage.setItem('activeUsers', JSON.stringify(data));
  64. // log.trace(sessionStorage.getItem('activeUsers'));
  65. log.info(`Subject: received ajax active users`);
  66. data.map((d: any) => {
  67. if (d.lastActive == null) return null;
  68. d.lastActive = new Date(d.lastActive);
  69. return d;
  70. });
  71. this._activeUsersList = data;
  72. this.notify();
  73. });
  74. } else {
  75. log.error("Auth token is null");
  76. }
  77. }
  78. private async _getActiveUsersAjax(authToken: string): Promise<any> {
  79. let headers = new Headers();
  80. headers.append("X-AUTH-TOKEN", authToken);
  81. let response = await fetch(JsonAPI.ACTIVE_USERS_GET, {
  82. method: "GET",
  83. headers: headers,
  84. });
  85. log.debug(response.clone());
  86. if (fetchErrorHandler(response.clone(), this._notificationService)) {
  87. return null;
  88. }
  89. let data = await response.json();
  90. // return data;
  91. return new Promise((resolve, reject) => {
  92. if (data != null) {
  93. resolve(data);
  94. } else reject("Response data null");
  95. });
  96. }
  97. private helperMethod() {}
  98. }