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.
81 lines
2.2 KiB
81 lines
2.2 KiB
import { Sprintf } from "../global/Sprintf";
|
|
import { Routes } from "../routes/Routes";
|
|
import { ReencryptionDTO } from "../dto/ReencryptionDTO";
|
|
import { ChatMessageDTO } from "../dto/ChatMessageDTO";
|
|
import { JsonAPI } from "../../chat/singleton/JsonAPI";
|
|
import * as log from "loglevel";
|
|
import { StatsDTO } from "../dto/StatsDTO";
|
|
import { createApiHeaders } from "./util";
|
|
|
|
export async function getAllMessages(user: string, authToken: string) {
|
|
let response = await fetch(`${Routes.Admin.getAllMessages}${user}`, {
|
|
method: "GET",
|
|
headers: createApiHeaders(authToken),
|
|
});
|
|
return response.json() as Promise<ReencryptionDTO[]>;
|
|
}
|
|
|
|
async function getAllRegularUsers(authToken: string) {
|
|
let response = await fetch(`${Routes.Admin.getOtherUsers}`, {
|
|
method: "GET",
|
|
headers: createApiHeaders(authToken),
|
|
});
|
|
let data = (await response.json()) as string[];
|
|
return data;
|
|
}
|
|
|
|
export async function sendReencryptedMessages(
|
|
rencryptionDTOs: ReencryptionDTO[],
|
|
authToken: string
|
|
) {
|
|
return fetch(Routes.Admin.reencrypt, {
|
|
method: "POST",
|
|
headers: createApiHeaders(authToken),
|
|
body: JSON.stringify(rencryptionDTOs),
|
|
});
|
|
}
|
|
|
|
export async function getOneMessage(
|
|
toUser: string,
|
|
page: number,
|
|
authToken: string
|
|
): Promise<ChatMessageDTO[]> {
|
|
const url = Sprintf(JsonAPI.CHAT_MESSAGE_PAGE_GET, toUser, page, 1);
|
|
log.debug(url);
|
|
const response = await fetch(url, {
|
|
method: "GET",
|
|
headers: createApiHeaders(authToken),
|
|
});
|
|
const data: Promise<any> = await response.json();
|
|
function func(data: any) {
|
|
const d1 = data.map((d: any) => {
|
|
if (d.messageTime == null) return null;
|
|
|
|
d.messageTime = new Date(d.messageTime);
|
|
return d;
|
|
});
|
|
return d1;
|
|
}
|
|
const data2 = func(data);
|
|
return data2;
|
|
}
|
|
|
|
export async function getStats(authToken: string) {
|
|
const response = await fetch("/api/stats/", {
|
|
headers: createApiHeaders(authToken),
|
|
method: "GET",
|
|
});
|
|
return (await response.json()) as StatsDTO;
|
|
}
|
|
|
|
export async function sendMessageAJAX(
|
|
chatMessageDTO: ChatMessageDTO,
|
|
authToken: string
|
|
) {
|
|
const response = fetch(JsonAPI.MESSAGE_POST, {
|
|
method: "POST",
|
|
headers: createApiHeaders(authToken),
|
|
body: JSON.stringify(chatMessageDTO),
|
|
});
|
|
return response;
|
|
}
|