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.

106 lines
4.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 { 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 _messagePageMap: Map<string, number>;
  22. private readonly _messagesMap: Map<string, ChatMessageViewModel[]>;
  23. constructor() {
  24. this.state = null;
  25. this._messagePageMap = new Map();
  26. this._messagesMap = new Map();
  27. }
  28. /**
  29. * The subscription management methods.
  30. */
  31. public attach(observer: Observer): void {
  32. console.log('Subject: Attached an observer.');
  33. this._observers.push(observer);
  34. }
  35. public detach(observer: Observer): void {
  36. const observerIndex = this._observers.indexOf(observer);
  37. this._observers.splice(observerIndex, 1);
  38. console.log('Subject: Detached an observer.');
  39. }
  40. private storeUserMessages(username: string, messages: ChatMessageViewModel[]) {
  41. this._messagesMap.set(username, messages);
  42. }
  43. private getStoredUserMessages(username: string): ChatMessageViewModel[] {
  44. return this._messagesMap.get(username)!;
  45. }
  46. /**
  47. * Trigger an update in each subscriber.
  48. */
  49. public notify(userName: string): void {
  50. console.log('Subject: Notifying observers...');
  51. for (const observer of this._observers) {
  52. observer.update(this._messagesMap.get(userName));
  53. }
  54. }
  55. public someBusinessMethod(chatMessageList: ChatMessageViewModel[]): void {
  56. this.state = chatMessageList;
  57. console.log(`Subject: My state has just changed`);
  58. console.log(chatMessageList);
  59. this.notify("some user");
  60. }
  61. public async getMessages(contactName: string, passphrase: string, lastMessageTime: string | null): Promise<ChatMessageViewModel[]> {
  62. if(this._messagePageMap.get(contactName) == null)
  63. this._messagePageMap.set(contactName, 0);
  64. else {
  65. log.debug('page number before = ' + this._messagePageMap.get(contactName)!)
  66. this._messagePageMap.set(contactName, this._messagePageMap.get(contactName)! + 1);
  67. log.debug('page number after = ' + this._messagePageMap.get(contactName)!)
  68. }
  69. const pageNumber = this._messagePageMap.get(contactName)
  70. const cVMs = await ChatModelHelper.getMessages(contactName, passphrase, pageNumber!, lastMessageTime, this);
  71. if (cVMs != null) {
  72. log.info('Subject: My state has just changed')
  73. // this._messagesMap.set(userName, cVMs);
  74. const existingMessages = this.getStoredUserMessages(contactName);
  75. log.debug(existingMessages);
  76. log.debug(cVMs);
  77. if (existingMessages != null) {
  78. // existingMessages.forEach(function (elem) {
  79. // cVMs.push(elem);
  80. // })
  81. const newArr = cVMs.concat(existingMessages)
  82. // log.debug(newArr);
  83. this.storeUserMessages(contactName, cVMs);
  84. // this.storeUserMessages(contactName, cVMs);
  85. }
  86. else {
  87. this.storeUserMessages(contactName, cVMs);
  88. }
  89. JsonAPI.contactName = contactName;
  90. this.notify(contactName);
  91. }
  92. else {
  93. log.error('Messages were null');
  94. }
  95. return cVMs;
  96. }
  97. }