workadventure/back/src/Services/AdminApi.ts

116 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-10-06 15:37:00 +02:00
import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable";
import Axios from "axios";
2020-10-20 16:39:23 +02:00
import {v4} from "uuid";
2020-10-06 15:37:00 +02:00
export interface AdminApiData {
organizationSlug: string
worldSlug: string
roomSlug: string
mapUrlStart: string
tags: string[]
policy_type: number
2020-10-06 15:37:00 +02:00
userUuid: string
2020-10-20 16:39:23 +02:00
messages?: unknown[],
textures: CharacterTexture[]
2020-10-06 15:37:00 +02:00
}
2020-10-20 16:39:23 +02:00
export interface CharacterTexture {
id: number,
level: number,
url: string,
rights: string
}
export interface FetchMemberDataByUuidResponse {
uuid: string;
tags: string[];
2020-10-20 16:39:23 +02:00
textures: CharacterTexture[];
messages: unknown[];
}
2020-10-06 15:37:00 +02:00
class AdminApi {
2020-10-13 15:12:24 +02:00
async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise<AdminApiData> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
const params: { organizationSlug: string, worldSlug: string, roomSlug?: string } = {
2020-10-13 15:12:24 +02:00
organizationSlug,
worldSlug
};
if (roomSlug) {
params.roomSlug = roomSlug;
2020-10-13 15:12:24 +02:00
}
const res = await Axios.get(ADMIN_API_URL + '/api/map',
2020-10-13 15:12:24 +02:00
{
headers: {"Authorization": `${ADMIN_API_TOKEN}`},
2020-10-13 15:12:24 +02:00
params
}
)
return res.data;
}
2020-10-20 16:39:23 +02:00
async fetchMemberDataByUuid(uuid: string): Promise<FetchMemberDataByUuidResponse> {
2020-10-06 15:37:00 +02:00
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
2020-10-20 16:39:23 +02:00
try {
const res = await Axios.get(ADMIN_API_URL+'/api/membership/'+uuid,
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
)
return res.data;
} catch (e) {
if (e?.response?.status == 404) {
// If we get an HTTP 404, the token is invalid. Let's perform an anonymous login!
console.warn('Cannot find user with uuid "'+uuid+'". Performing an anonymous login instead.');
return {
uuid: v4(),
tags: [],
textures: [],
messages: [],
}
} else {
throw e;
}
}
2020-10-06 15:37:00 +02:00
}
2020-10-20 16:39:23 +02:00
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
2020-10-06 15:37:00 +02:00
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
const res = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken,
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
)
return res.data;
2020-10-06 15:37:00 +02:00
}
async fetchCheckUserByToken(organizationMemberToken: string): Promise<AdminApiData> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
const res = await Axios.get(ADMIN_API_URL+'/api/check-user/'+organizationMemberToken,
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
)
return res.data;
}
2020-10-20 16:39:23 +02:00
reportPlayer(reportedUserUuid: string, reportedUserComment: string, reporterUserUuid: string) {
return Axios.post(`${ADMIN_API_URL}/api/report`, {
reportedUserUuid,
reportedUserComment,
reporterUserUuid,
},
{
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
});
}
2020-10-06 15:37:00 +02:00
}
2020-10-13 15:12:24 +02:00
export const adminApi = new AdminApi();