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.

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