2020-11-24 15:44:33 +00:00
|
|
|
/*
|
|
|
|
* Collect api fetch commands for actions here
|
|
|
|
* (chunk and tiles requests in ui/ChunkLoader*.js)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-03-17 01:24:30 +00:00
|
|
|
import { t } from 'ttag';
|
2020-11-24 15:44:33 +00:00
|
|
|
|
2021-04-30 17:59:38 +00:00
|
|
|
import { dateToString } from '../core/utils';
|
|
|
|
|
2020-11-24 15:44:33 +00:00
|
|
|
/*
|
|
|
|
* Adds customizeable timeout to fetch
|
|
|
|
* defaults to 8s
|
|
|
|
*/
|
2021-04-30 17:59:38 +00:00
|
|
|
async function fetchWithTimeout(resource, options = {}) {
|
2020-11-24 15:44:33 +00:00
|
|
|
const { timeout = 8000 } = options;
|
|
|
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
const id = setTimeout(() => controller.abort(), timeout);
|
|
|
|
|
|
|
|
const response = await fetch(resource, {
|
|
|
|
...options,
|
|
|
|
signal: controller.signal,
|
|
|
|
});
|
|
|
|
clearTimeout(id);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-03-17 01:24:30 +00:00
|
|
|
* Parse response from API
|
|
|
|
* @param response
|
|
|
|
* @return Object of response
|
2020-11-24 15:44:33 +00:00
|
|
|
*/
|
2021-03-17 01:24:30 +00:00
|
|
|
async function parseAPIresponse(response) {
|
2021-03-17 14:11:24 +00:00
|
|
|
const { status: code } = response;
|
|
|
|
|
|
|
|
if (code === 429) {
|
|
|
|
let error = t`You made too many requests`;
|
|
|
|
const retryAfter = response.headers.get('Retry-After');
|
|
|
|
if (!Number.isNaN(Number(retryAfter))) {
|
|
|
|
const ti = Math.floor(retryAfter / 60);
|
|
|
|
error += `, ${t`try again after ${ti}min`}`;
|
2021-03-17 10:00:58 +00:00
|
|
|
}
|
2021-03-17 01:24:30 +00:00
|
|
|
return {
|
2021-03-17 14:11:24 +00:00
|
|
|
errors: [error],
|
2021-03-17 01:24:30 +00:00
|
|
|
};
|
|
|
|
}
|
2020-11-24 15:44:33 +00:00
|
|
|
|
|
|
|
try {
|
2021-03-17 01:24:30 +00:00
|
|
|
return await response.json();
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
2021-03-17 14:11:24 +00:00
|
|
|
errors: [t`Connection error ${code} :(`],
|
2021-03-17 01:24:30 +00:00
|
|
|
};
|
2020-11-24 15:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-03-17 01:24:30 +00:00
|
|
|
* Make API POST Request
|
|
|
|
* @param url URL of post api endpoint
|
|
|
|
* @param body Body of request
|
|
|
|
* @return Object with response or error Array
|
2020-11-24 15:44:33 +00:00
|
|
|
*/
|
2021-03-17 01:24:30 +00:00
|
|
|
async function makeAPIPOSTRequest(url, body) {
|
|
|
|
try {
|
|
|
|
const response = await fetchWithTimeout(url, {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'include',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
2020-11-24 15:44:33 +00:00
|
|
|
|
2021-03-17 01:24:30 +00:00
|
|
|
return parseAPIresponse(response);
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
errors: [t`Could not connect to server, please try again later :(`],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make API GET Request
|
|
|
|
* @param url URL of get api endpoint
|
|
|
|
* @return Object with response or error Array
|
|
|
|
*/
|
|
|
|
async function makeAPIGETRequest(url) {
|
2020-11-24 15:44:33 +00:00
|
|
|
try {
|
2021-03-17 01:24:30 +00:00
|
|
|
const response = await fetchWithTimeout(url, {
|
|
|
|
credentials: 'include',
|
|
|
|
});
|
|
|
|
|
|
|
|
return parseAPIresponse(response);
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
errors: [t`Could not connect to server, please try again later :(`],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* block / unblock user
|
|
|
|
* @param userId id of user to block
|
|
|
|
* @param block true if block, false if unblock
|
|
|
|
* @return error string or null if successful
|
|
|
|
*/
|
2022-01-03 13:33:26 +00:00
|
|
|
export async function requestBlock(userId, block) {
|
2021-03-17 01:24:30 +00:00
|
|
|
const res = await makeAPIPOSTRequest(
|
|
|
|
'api/block',
|
|
|
|
{ userId, block },
|
|
|
|
);
|
|
|
|
if (res.errors) {
|
|
|
|
return res.errors[0];
|
|
|
|
}
|
|
|
|
if (res.status === 'ok') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return t`Unknown Error`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start new DM channel with user
|
2022-01-03 13:33:26 +00:00
|
|
|
* @param query Object with either userId or userName: string
|
2021-03-17 01:24:30 +00:00
|
|
|
* @return channel Array on success, error string if not
|
|
|
|
*/
|
|
|
|
export async function requestStartDm(query) {
|
|
|
|
const res = await makeAPIPOSTRequest(
|
|
|
|
'api/startdm',
|
|
|
|
query,
|
|
|
|
);
|
|
|
|
if (res.errors) {
|
|
|
|
return res.errors[0];
|
2020-11-24 15:44:33 +00:00
|
|
|
}
|
2021-03-17 01:24:30 +00:00
|
|
|
if (res.channel) {
|
|
|
|
return res.channel;
|
|
|
|
}
|
|
|
|
return t`Unknown Error`;
|
2020-11-24 15:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set receiving of all DMs on/off
|
2021-03-17 01:24:30 +00:00
|
|
|
* @param block true if blocking all dms, false if unblocking
|
|
|
|
* @return error string or null if successful
|
2020-11-24 15:44:33 +00:00
|
|
|
*/
|
2022-01-03 13:33:26 +00:00
|
|
|
export async function requestBlockDm(block) {
|
2021-03-17 01:24:30 +00:00
|
|
|
const res = await makeAPIPOSTRequest(
|
|
|
|
'api/blockdm',
|
|
|
|
{ block },
|
|
|
|
);
|
|
|
|
if (res.errors) {
|
|
|
|
return res.errors[0];
|
2020-11-24 15:44:33 +00:00
|
|
|
}
|
2021-03-17 01:24:30 +00:00
|
|
|
if (res.status === 'ok') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return t`Unknown Error`;
|
2020-11-24 15:44:33 +00:00
|
|
|
}
|
2020-11-25 23:17:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* leaving Chat Channel (i.e. DM channel)
|
2021-03-17 01:24:30 +00:00
|
|
|
* @param channelId 8nteger id of channel
|
|
|
|
* @return error string or null if successful
|
2020-11-25 23:17:23 +00:00
|
|
|
*/
|
2022-01-03 13:33:26 +00:00
|
|
|
export async function requestLeaveChan(channelId) {
|
2021-03-17 01:24:30 +00:00
|
|
|
const res = await makeAPIPOSTRequest(
|
|
|
|
'api/leavechan',
|
|
|
|
{ channelId },
|
|
|
|
);
|
|
|
|
if (res.errors) {
|
|
|
|
return res.errors[0];
|
2020-11-25 23:17:23 +00:00
|
|
|
}
|
2021-03-17 01:24:30 +00:00
|
|
|
if (res.status === 'ok') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return t`Unknown Error`;
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:00:58 +00:00
|
|
|
export async function requestSolveCaptcha(text) {
|
|
|
|
const res = await makeAPIPOSTRequest(
|
2021-03-17 01:24:30 +00:00
|
|
|
'api/captcha',
|
|
|
|
{ text },
|
|
|
|
);
|
2021-03-17 10:00:58 +00:00
|
|
|
if (!res.errors && !res.success) {
|
|
|
|
return {
|
|
|
|
errors: [t`Server answered with gibberish :(`],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return res;
|
2021-03-17 01:24:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 17:59:38 +00:00
|
|
|
export async function requestHistoricalTimes(day, canvasId) {
|
|
|
|
try {
|
|
|
|
const date = dateToString(day);
|
|
|
|
const url = `api/history?day=${date}&id=${canvasId}`;
|
|
|
|
const response = await fetchWithTimeout(url);
|
|
|
|
if (response.status !== 200) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const times = await response.json();
|
|
|
|
const parsedTimes = times
|
|
|
|
.map((a) => `${a.substr(0, 2)}:${a.substr(-2, 2)}`);
|
|
|
|
return [
|
|
|
|
'00:00',
|
|
|
|
...parsedTimes,
|
|
|
|
];
|
|
|
|
} catch {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 01:24:30 +00:00
|
|
|
export function requestPasswordChange(newPassword, password) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/change_passwd',
|
|
|
|
{ password, newPassword },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function requestResendVerify() {
|
|
|
|
return makeAPIGETRequest(
|
2021-04-30 22:51:08 +00:00
|
|
|
'api/auth/resend_verify',
|
2021-03-17 01:24:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:51:08 +00:00
|
|
|
export async function requestLogOut() {
|
|
|
|
const ret = makeAPIGETRequest(
|
|
|
|
'api/auth/logout',
|
|
|
|
);
|
|
|
|
return !ret.errors;
|
|
|
|
}
|
|
|
|
|
2021-03-17 01:24:30 +00:00
|
|
|
export function requestNameChange(name) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/change_name',
|
|
|
|
{ name },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestMailChange(email, password) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/change_mail',
|
|
|
|
{ email, password },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestLogin(nameoremail, password) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/local',
|
|
|
|
{ nameoremail, password },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestRegistration(name, email, password) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/register',
|
|
|
|
{ name, email, password },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestNewPassword(email) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/restore_password',
|
|
|
|
{ email },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestDeleteAccount(password) {
|
|
|
|
return makeAPIPOSTRequest(
|
|
|
|
'api/auth/delete_account',
|
|
|
|
{ password },
|
|
|
|
);
|
2020-11-25 23:17:23 +00:00
|
|
|
}
|