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.

88 lines
2.9 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 { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
  7. import { JsonAPI } from "../singleton/JsonAPI";
  8. import log = require('loglevel');
  9. import { EncryptionService } from "../service/EncryptionService";
  10. import { SJCLEncryptionService } from "../service/SJCLEncryptionService";
  11. import { ChatMessageDTO } from "../dto/ChatMessageDTO";
  12. import { ChatModelHelper } from "./ChatModelHelper";
  13. export class ChatModel implements Subject {
  14. /**
  15. * @type {Observer[]} List of subscribers. In real life, the list of
  16. * subscribers can be stored more comprehensively (categorized by event
  17. * type, etc.).
  18. */
  19. private readonly _observers: Observer[] = [];
  20. private state: ChatMessageViewModel[] | null;
  21. private readonly _messagesMap: Map<string, ChatMessageViewModel[]>;
  22. constructor() {
  23. this.state = null;
  24. this._messagesMap = new Map();
  25. }
  26. /**
  27. * The subscription management methods.
  28. */
  29. public attach(observer: Observer): void {
  30. console.log('Subject: Attached an observer.');
  31. this._observers.push(observer);
  32. }
  33. public detach(observer: Observer): void {
  34. const observerIndex = this._observers.indexOf(observer);
  35. this._observers.splice(observerIndex, 1);
  36. console.log('Subject: Detached an observer.');
  37. }
  38. public setUserMessages(username: string, messages: ChatMessageViewModel[]) {
  39. this._messagesMap.set(username, messages);
  40. }
  41. /**
  42. * Trigger an update in each subscriber.
  43. */
  44. public notify(userName: string): void {
  45. console.log('Subject: Notifying observers...');
  46. for (const observer of this._observers) {
  47. observer.update(this._messagesMap.get(userName));
  48. }
  49. }
  50. public someBusinessMethod(chatMessageList: ChatMessageViewModel[]): void {
  51. this.state = chatMessageList;
  52. this.helperMethod();
  53. console.log(`Subject: My state has just changed`);
  54. console.log(chatMessageList);
  55. this.notify("some user");
  56. }
  57. public async getmessages(userName: string, passphrase: string, lastMessageTime: string | null): Promise<ChatMessageViewModel[]> {
  58. const cVMs = await ChatModelHelper.getMessages(userName, passphrase, lastMessageTime, this);
  59. if (cVMs != null) {
  60. log.info('Subject: My state has just changed')
  61. log.debug(cVMs);
  62. this._messagesMap.set(userName, cVMs);
  63. this.notify(userName);
  64. }
  65. else {
  66. log.error('Messages were null');
  67. }
  68. return cVMs;
  69. }
  70. private helperMethod() { }
  71. public populateMessages(): void {
  72. }
  73. }