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.

113 lines
4.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import { Builder } from "builder-pattern";
  2. import * as Handlebars from "handlebars";
  3. import * as log from "loglevel";
  4. import { ChatController } from "./controller/ChatController";
  5. import { UserController } from "./controller/UserController";
  6. import { ChatModel } from "./model/ChatModel";
  7. import { ChatModelHelper } from "./model/ChatModelHelper";
  8. import { UserModel } from "./model/UserModel";
  9. import { AlertifyNotificationService } from "./service/AlertifyNotificationService";
  10. import { EncryptionServiceFactory } from "./service/EncryptionServiceFactory";
  11. import { FuseSearchService } from "./service/FuseSearchService";
  12. import { MarkDownItMarkDownService } from "./service/MarkDownItMarkDownService";
  13. import { NotificationService } from "./service/NotificationService";
  14. import { SearchService } from "./service/SearchService";
  15. import { TemplateFactory } from "./template/TemplateFactory";
  16. import { ChatView } from "./view/ChatView";
  17. import { ChatViewDeps } from "./view/ChatViewDeps";
  18. import { UserView } from "./view/UserView";
  19. import { UserViewDeps } from "./view/UserViewDeps";
  20. import { ActiveUserViewModel } from "./viewmodel/ActiveUserViewModel";
  21. import moment = require("moment");
  22. // log.setLevel("TRACE");
  23. const usersListElement = document.getElementById("contacts-box");
  24. const userSearchButton = document.getElementById("user-search");
  25. const userSearchInputElement = document.getElementById(
  26. "user-search-term"
  27. ) as HTMLInputElement;
  28. const userSearchCancelButton = document.getElementById("user-search-cancel");
  29. const chatArea = document.getElementById("chat-area-new");
  30. const activeUserSearchService: SearchService<ActiveUserViewModel> = new FuseSearchService(
  31. ["userName"]
  32. );
  33. const ns: NotificationService = new AlertifyNotificationService();
  34. const encryptionService = EncryptionServiceFactory.getEncryptionService();
  35. const chatModelHelper = new ChatModelHelper(encryptionService, ns);
  36. const chatModel = new ChatModel(chatModelHelper);
  37. const userModel = new UserModel(ns);
  38. const cvDeps: ChatViewDeps = {
  39. chatModel: chatModel,
  40. // @ts-ignore: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'. Type 'null' is not assignable to type 'HTMLElement'.
  41. messageContainer: chatArea,
  42. messageSendTemplate: TemplateFactory.getTemplate(
  43. "msg_container_send_template"
  44. ),
  45. messageReceiveTemplate: TemplateFactory.getTemplate("msg_container_template"),
  46. markdownService: new MarkDownItMarkDownService(),
  47. encryptionService: encryptionService,
  48. notificationService: ns,
  49. userModel: userModel,
  50. };
  51. const chatView = new ChatView(cvDeps);
  52. chatModel.attach(chatView);
  53. const chatController = new ChatController(chatModel, chatView);
  54. const uvDeps: UserViewDeps = {
  55. model: userModel,
  56. chatModel: chatModel,
  57. // @ts-ignore: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'. Type 'null' is not assignable to type 'HTMLElement'.
  58. usersListElement: usersListElement,
  59. userSearchInputElement: userSearchInputElement,
  60. // @ts-ignore: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'. Type 'null' is not assignable to type 'HTMLElement'.
  61. userSearchButton: userSearchButton,
  62. // @ts-ignore: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'. Type 'null' is not assignable to type 'HTMLElement'.
  63. userSearchCancelButton: userSearchCancelButton,
  64. searchService: activeUserSearchService,
  65. userContactOnlineTemplate: TemplateFactory.getTemplate(
  66. "user-contact-online-template"
  67. ),
  68. userContactOfflineTemplate: TemplateFactory.getTemplate(
  69. "user-contact-offline-template"
  70. ),
  71. notificationService: ns,
  72. };
  73. const userView = new UserView(uvDeps);
  74. userModel.attach(userView);
  75. const userController = new UserController(userModel, userView);
  76. userController.getActiveUsers();
  77. // @ts-ignore
  78. Handlebars.registerHelper("moment", require("helper-moment"));
  79. Handlebars.registerHelper("avatar", function () {
  80. return '<div class="img_cont_msg"> <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img_msg"> </div>';
  81. });
  82. Handlebars.registerHelper("fromNow", function (date: string) {
  83. if (date == null) return ": Never";
  84. return moment(date).fromNow();
  85. });
  86. Handlebars.registerHelper("msgDateFormat", function (date: string) {
  87. return moment(date).calendar(moment.now(), {
  88. lastWeek: "DD/MM/YY hh:mm A",
  89. sameElse: "DD/MM/YY hh:mm A",
  90. });
  91. });
  92. Handlebars.registerHelper("lockIcon", function (unlocked: boolean) {
  93. switch (unlocked) {
  94. case true: {
  95. return '<i class="fas fa-lock-open user-passphrase"></i>';
  96. }
  97. default: {
  98. return '<i class="fas fa-lock user-passphrase"></i>';
  99. }
  100. }
  101. });
  102. ns.success("Welcome");
  103. // ns.errorWithDelay("Hmm very long error notif", 10);
  104. const test = Builder<UserViewDeps>().build();