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.

85 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. import { NotificationService } from "./NotificationService";
  2. // @ts-ignore
  3. import * as alertify from "alertifyjs";
  4. import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel";
  5. import log = require("loglevel");
  6. import bootbox = require("bootbox");
  7. import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
  8. export class AlertifyNotificationService implements NotificationService {
  9. private readonly _alertify = alertify;
  10. constructor() {
  11. this._alertify.set("notifier", "position", "top-center");
  12. }
  13. success(message: string): void {
  14. this._alertify.success(message);
  15. }
  16. error(message: string): void {
  17. this._alertify.error(message);
  18. }
  19. errorWithDelay(message: string, delay: number): void {
  20. const _delay = this._alertify.get("notifier", "delay");
  21. this._alertify.set("notifier", "delay", delay);
  22. this._alertify.error(message);
  23. this._alertify.set("notifier", "delay", _delay);
  24. }
  25. warning(message: string): void {
  26. this._alertify.warning(message);
  27. }
  28. message(message: string): void {
  29. this._alertify.message(message);
  30. }
  31. passphrasePrompt(
  32. vm: ActiveUserViewModel,
  33. vms: ActiveUserViewModel[],
  34. cb1: (v: ActiveUserViewModel, op: string) => ChatMessageViewModel[],
  35. cb2: (...x: any) => any,
  36. cb3: (...x: any) => any
  37. ): void {
  38. if (!vm.passphrase) {
  39. bootbox.prompt({
  40. title: "Please enter the passphrase",
  41. inputType: "password",
  42. callback: async function (result) {
  43. if (result) {
  44. const valid = await cb3(result, vm.userName);
  45. if (!valid) {
  46. bootbox.alert("Some error occured. Please check your password");
  47. log.error("invalid password");
  48. return;
  49. }
  50. vm.unlocked = true;
  51. vm.passphrase = result;
  52. const chatMessages: ChatMessageViewModel[] = await cb1(vm, "new");
  53. vms
  54. .filter((v) => v.userName == vm.userName)
  55. .map((v) => {
  56. v.passphrase = result;
  57. v.unlocked = true;
  58. if (chatMessages.length > 0) {
  59. v.lastMessageTime = new Date(
  60. chatMessages[chatMessages.length - 1].messageTime
  61. );
  62. const lastMessageText = (v.lastMessageText =
  63. chatMessages[chatMessages.length - 1].message);
  64. if (lastMessageText.length > 15) {
  65. v.lastMessageText =
  66. chatMessages[chatMessages.length - 1].message.slice(
  67. 0,
  68. 15
  69. ) + "...";
  70. }
  71. }
  72. });
  73. // vm.lastMessageTime = new Date(chatMessages[chatMessages.length - 1].messageTime);
  74. cb2(vm, vms);
  75. // log.debug(vm);
  76. // log.debug(vms);
  77. }
  78. },
  79. });
  80. } else {
  81. cb1(vm, "new");
  82. }
  83. }
  84. }