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.

96 lines
3.1 KiB

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