2021-11-12 20:48:26 +01:00
|
|
|
import { ADMIN_API_TOKEN, ADMIN_API_URL, ADMIN_URL, OPID_PROFILE_SCREEN_PROVIDER } from "../Enum/EnvironmentVariable";
|
2022-03-11 17:02:58 +01:00
|
|
|
import Axios, { AxiosResponse } from "axios";
|
2022-03-15 15:50:25 +01:00
|
|
|
import { isMapDetailsData, MapDetailsData } from "../Messages/JsonMessages/MapDetailsData";
|
|
|
|
import { isRoomRedirect, RoomRedirect } from "../Messages/JsonMessages/RoomRedirect";
|
2021-12-16 15:57:37 +01:00
|
|
|
import { AdminApiData, isAdminApiData } from "../Messages/JsonMessages/AdminApiData";
|
2022-03-11 17:02:58 +01:00
|
|
|
import * as tg from "generic-type-guard";
|
|
|
|
import { isNumber } from "generic-type-guard";
|
2022-03-21 10:33:25 +01:00
|
|
|
import { isWokaDetail } from "../Messages/JsonMessages/PlayerTextures";
|
2022-03-11 17:02:58 +01:00
|
|
|
import qs from "qs";
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-01-15 03:19:58 +01:00
|
|
|
export interface AdminBannedData {
|
2021-06-24 10:09:10 +02:00
|
|
|
is_banned: boolean;
|
|
|
|
message: string;
|
2021-01-15 03:19:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-11 17:02:58 +01:00
|
|
|
const isFetchMemberDataByUuidResponse = new tg.IsInterface()
|
|
|
|
.withProperties({
|
|
|
|
email: tg.isString,
|
|
|
|
userUuid: tg.isString,
|
|
|
|
tags: tg.isArray(tg.isString),
|
|
|
|
visitCardUrl: tg.isNullable(tg.isString),
|
|
|
|
textures: tg.isArray(isWokaDetail),
|
|
|
|
messages: tg.isArray(tg.isUnknown),
|
|
|
|
})
|
|
|
|
.withOptionalProperties({
|
|
|
|
anonymous: tg.isBoolean,
|
|
|
|
userRoomToken: tg.isString,
|
|
|
|
})
|
|
|
|
.get();
|
|
|
|
|
|
|
|
export type FetchMemberDataByUuidResponse = tg.GuardedType<typeof isFetchMemberDataByUuidResponse>;
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
class AdminApi {
|
2021-08-15 08:51:35 +02:00
|
|
|
/**
|
|
|
|
* @var playUri: is url of the room
|
|
|
|
* @var userId: can to be undefined or email or uuid
|
|
|
|
* @return MapDetailsData|RoomRedirect
|
|
|
|
*/
|
|
|
|
async fetchMapDetails(playUri: string, userId?: string): Promise<MapDetailsData | RoomRedirect> {
|
2020-11-13 18:00:22 +01:00
|
|
|
if (!ADMIN_API_URL) {
|
2021-06-24 10:09:10 +02:00
|
|
|
return Promise.reject(new Error("No admin backoffice set!"));
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 08:51:35 +02:00
|
|
|
const params: { playUri: string; userId?: string } = {
|
2021-07-13 19:09:07 +02:00
|
|
|
playUri,
|
2021-08-15 08:51:35 +02:00
|
|
|
userId,
|
2020-11-13 18:00:22 +01:00
|
|
|
};
|
|
|
|
|
2022-03-15 15:50:25 +01:00
|
|
|
const res = await Axios.get<unknown, AxiosResponse<unknown>>(ADMIN_API_URL + "/api/map", {
|
2021-06-24 10:09:10 +02:00
|
|
|
headers: { Authorization: `${ADMIN_API_TOKEN}` },
|
|
|
|
params,
|
|
|
|
});
|
2022-03-15 15:50:25 +01:00
|
|
|
if (!isMapDetailsData(res.data) && !isRoomRedirect(res.data)) {
|
|
|
|
throw new Error(
|
|
|
|
"Invalid answer received from the admin for the /api/map endpoint. Received: " +
|
|
|
|
JSON.stringify(res.data)
|
|
|
|
);
|
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
return res.data;
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
async fetchMemberDataByUuid(
|
|
|
|
userIdentifier: string | null,
|
|
|
|
roomId: string,
|
2022-03-11 17:02:58 +01:00
|
|
|
ipAddress: string,
|
|
|
|
characterLayers: string[]
|
2021-07-27 16:37:01 +02:00
|
|
|
): Promise<FetchMemberDataByUuidResponse> {
|
2020-11-13 18:00:22 +01:00
|
|
|
if (!ADMIN_API_URL) {
|
2021-06-24 10:09:10 +02:00
|
|
|
return Promise.reject(new Error("No admin backoffice set!"));
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
2022-03-11 17:02:58 +01:00
|
|
|
const res = await Axios.get<unknown, AxiosResponse<unknown>>(ADMIN_API_URL + "/api/room/access", {
|
|
|
|
params: { userIdentifier, roomId, ipAddress, characterLayers },
|
2021-06-24 10:09:10 +02:00
|
|
|
headers: { Authorization: `${ADMIN_API_TOKEN}` },
|
2022-03-11 17:02:58 +01:00
|
|
|
paramsSerializer: (p) => {
|
|
|
|
return qs.stringify(p, { arrayFormat: "brackets" });
|
|
|
|
},
|
2021-06-24 10:09:10 +02:00
|
|
|
});
|
2022-03-11 17:02:58 +01:00
|
|
|
if (!isFetchMemberDataByUuidResponse(res.data)) {
|
|
|
|
throw new Error(
|
|
|
|
"Invalid answer received from the admin for the /api/room/access endpoint. Received: " +
|
|
|
|
JSON.stringify(res.data)
|
|
|
|
);
|
|
|
|
}
|
2021-03-01 22:32:50 +01:00
|
|
|
return res.data;
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
|
|
|
|
if (!ADMIN_API_URL) {
|
2021-06-24 10:09:10 +02:00
|
|
|
return Promise.reject(new Error("No admin backoffice set!"));
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
|
2021-06-24 10:09:10 +02:00
|
|
|
const res = await Axios.get(ADMIN_API_URL + "/api/login-url/" + organizationMemberToken, {
|
|
|
|
headers: { Authorization: `${ADMIN_API_TOKEN}` },
|
|
|
|
});
|
2021-12-16 15:57:37 +01:00
|
|
|
if (!isAdminApiData(res.data)) {
|
|
|
|
console.error("Message received from /api/login-url is not in the expected format. Message: ", res.data);
|
|
|
|
throw new Error("Message received from /api/login-url is not in the expected format.");
|
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
return res.data;
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
reportPlayer(
|
|
|
|
reportedUserUuid: string,
|
|
|
|
reportedUserComment: string,
|
|
|
|
reporterUserUuid: string,
|
|
|
|
reportWorldSlug: string
|
|
|
|
) {
|
2022-01-27 18:38:33 +01:00
|
|
|
if (!ADMIN_API_URL) {
|
|
|
|
return Promise.reject(new Error("No admin backoffice set!"));
|
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
return Axios.post(
|
|
|
|
`${ADMIN_API_URL}/api/report`,
|
|
|
|
{
|
2020-11-13 18:00:22 +01:00
|
|
|
reportedUserUuid,
|
|
|
|
reportedUserComment,
|
|
|
|
reporterUserUuid,
|
2021-06-24 10:09:10 +02:00
|
|
|
reportWorldSlug,
|
2020-11-13 18:00:22 +01:00
|
|
|
},
|
|
|
|
{
|
2021-06-24 10:09:10 +02:00
|
|
|
headers: { Authorization: `${ADMIN_API_TOKEN}` },
|
|
|
|
}
|
|
|
|
);
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
2021-01-15 03:19:58 +01:00
|
|
|
|
2021-07-13 19:09:07 +02:00
|
|
|
async verifyBanUser(userUuid: string, ipAddress: string, roomUrl: string): Promise<AdminBannedData> {
|
2021-01-15 03:19:58 +01:00
|
|
|
if (!ADMIN_API_URL) {
|
2021-06-24 10:09:10 +02:00
|
|
|
return Promise.reject(new Error("No admin backoffice set!"));
|
2021-01-15 03:19:58 +01:00
|
|
|
}
|
|
|
|
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
|
2021-06-24 10:09:10 +02:00
|
|
|
return Axios.get(
|
|
|
|
ADMIN_API_URL +
|
2021-07-13 19:09:07 +02:00
|
|
|
"/api/ban" +
|
2021-06-24 10:09:10 +02:00
|
|
|
"?ipAddress=" +
|
2021-07-13 19:09:07 +02:00
|
|
|
encodeURIComponent(ipAddress) +
|
2021-06-24 10:09:10 +02:00
|
|
|
"&token=" +
|
2021-07-13 19:09:07 +02:00
|
|
|
encodeURIComponent(userUuid) +
|
|
|
|
"&roomUrl=" +
|
|
|
|
encodeURIComponent(roomUrl),
|
2021-06-24 10:09:10 +02:00
|
|
|
{ headers: { Authorization: `${ADMIN_API_TOKEN}` } }
|
2021-01-15 03:19:58 +01:00
|
|
|
).then((data) => {
|
|
|
|
return data.data;
|
|
|
|
});
|
|
|
|
}
|
2021-07-20 15:16:51 +02:00
|
|
|
|
|
|
|
async getUrlRoomsFromSameWorld(roomUrl: string): Promise<string[]> {
|
|
|
|
if (!ADMIN_API_URL) {
|
|
|
|
return Promise.reject(new Error("No admin backoffice set!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Axios.get(ADMIN_API_URL + "/api/room/sameWorld" + "?roomUrl=" + encodeURIComponent(roomUrl), {
|
|
|
|
headers: { Authorization: `${ADMIN_API_TOKEN}` },
|
|
|
|
}).then((data) => {
|
|
|
|
return data.data;
|
|
|
|
});
|
|
|
|
}
|
2021-09-05 18:17:49 +02:00
|
|
|
|
2021-11-12 20:48:26 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param accessToken
|
|
|
|
*/
|
2021-09-05 18:17:49 +02:00
|
|
|
getProfileUrl(accessToken: string): string {
|
2021-11-12 20:48:26 +01:00
|
|
|
if (!OPID_PROFILE_SCREEN_PROVIDER) {
|
2021-09-05 18:17:49 +02:00
|
|
|
throw new Error("No admin backoffice set!");
|
|
|
|
}
|
2021-11-12 20:48:26 +01:00
|
|
|
return `${OPID_PROFILE_SCREEN_PROVIDER}?accessToken=${accessToken}`;
|
2021-09-05 18:17:49 +02:00
|
|
|
}
|
2021-11-08 19:27:01 +01:00
|
|
|
|
|
|
|
async logoutOauth(token: string) {
|
|
|
|
await Axios.get(ADMIN_API_URL + `/oauth/logout?token=${token}`);
|
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const adminApi = new AdminApi();
|