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.

116 lines
3.5 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. updateLastActive(username: String, lastActive: Date): void {
  57. this._activeUsersList
  58. .filter(u => u.userName == username)
  59. .forEach(u => u.lastActive = lastActive)
  60. }
  61. /**
  62. * getActiveUsers
  63. */
  64. public getActiveUsers(): void {
  65. if (JsonAPI.authToken != null) {
  66. this._getActiveUsersAjax(JsonAPI.authToken).then((data) => {
  67. // // activeUsers = data;
  68. // sessionStorage.setItem('activeUsers', JSON.stringify(data));
  69. // log.trace(sessionStorage.getItem('activeUsers'));
  70. log.info(`Subject: received ajax active users`);
  71. data.map((d: any) => {
  72. if (d.lastActive == null) return null;
  73. d.lastActive = new Date(d.lastActive);
  74. return d;
  75. });
  76. this._activeUsersList = data;
  77. this.notify();
  78. });
  79. } else {
  80. log.error("Auth token is null");
  81. }
  82. }
  83. private async _getActiveUsersAjax(authToken: string): Promise<any> {
  84. let headers = new Headers();
  85. headers.append("X-AUTH-TOKEN", authToken);
  86. let response = await fetch(JsonAPI.ACTIVE_USERS_GET, {
  87. method: "GET",
  88. headers: headers,
  89. });
  90. log.debug(response.clone());
  91. if (fetchErrorHandler(response.clone(), this._notificationService)) {
  92. return null;
  93. }
  94. let data = await response.json();
  95. // return data;
  96. return new Promise((resolve, reject) => {
  97. if (data != null) {
  98. resolve(data);
  99. } else reject("Response data null");
  100. });
  101. }
  102. private helperMethod() { }
  103. }