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.

82 lines
2.5 KiB

  1. import { Sprintf } from "../global/Sprintf";
  2. import { Routes } from "../routes/Routes";
  3. import { ReencryptionDTO } from "../dto/ReencryptionDTO";
  4. import { ChatMessageDTO } from "../dto/ChatMessageDTO";
  5. import { JsonAPI } from "../../chat/singleton/JsonAPI";
  6. import * as log from "loglevel";
  7. export async function getAllMessages(user: string, authToken: string) {
  8. let headers = new Headers();
  9. // headers.append('Accept','application/json')
  10. // headers.append('Content-Type', 'application/json');
  11. headers.append("X-AUTH-TOKEN", authToken);
  12. let response = await fetch(`${Routes.Admin.getAllMessagesURL}${user}`, {
  13. method: "GET",
  14. headers: headers,
  15. });
  16. return response.json() as Promise<ReencryptionDTO[]>;
  17. }
  18. async function getAllRegularUsers(authToken: string) {
  19. let headers = new Headers();
  20. // headers.append('Accept','application/json')
  21. // headers.append('Content-Type', 'application/json');
  22. headers.append("X-AUTH-TOKEN", authToken);
  23. let response = await fetch(`${Routes.Admin.getAllRegularUsersURL}`, {
  24. method: "GET",
  25. headers: headers,
  26. });
  27. let data = (await response.json()) as string[];
  28. return data;
  29. }
  30. export async function sendReencryptedMessages(
  31. rencryptionDTOs: ReencryptionDTO[],
  32. authToken: string
  33. ) {
  34. let headers = new Headers();
  35. // console.log("Token = " + btoa("hmm" + ":" + "hmm"))
  36. // headers.append('Accept','application/json')
  37. headers.append("Content-Type", "application/json");
  38. headers.append("X-AUTH-TOKEN", authToken);
  39. fetch(Routes.Admin.reencryptURL, {
  40. method: "POST",
  41. headers: headers,
  42. body: JSON.stringify(rencryptionDTOs),
  43. }).then((response) => console.log(response));
  44. }
  45. export async function getOneMessage(
  46. toUser: string,
  47. page: number
  48. ): Promise<ChatMessageDTO[]> {
  49. const headers = new Headers();
  50. if (JsonAPI.authToken == null) {
  51. log.error("authToken null");
  52. return [];
  53. }
  54. headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
  55. const url = Sprintf(JsonAPI.CHAT_MESSAGE_PAGE_GET, toUser, page, 1);
  56. log.debug(url);
  57. const response = await fetch(url, {
  58. method: "GET",
  59. headers: headers,
  60. });
  61. log.debug(response.clone());
  62. // if (fetchErrorHandler(response.clone(), this._notificationService)) {
  63. // return null;
  64. // }
  65. const data: Promise<any> = await response.json();
  66. function func(data: any) {
  67. const d1 = data.map((d: any) => {
  68. if (d.messageTime == null) return null;
  69. d.messageTime = new Date(d.messageTime);
  70. return d;
  71. });
  72. return d1;
  73. }
  74. const data2 = func(data);
  75. return data2;
  76. }