2020-10-08 08:09:42 +02:00
|
|
|
/* eslint-disable prefer-destructuring */
|
2020-10-08 09:17:40 +02:00
|
|
|
const ADMIN_USERNAME = process.env.NEXT_PUBLIC_ADMIN_USERNAME;
|
|
|
|
const ADMIN_STREAMKEY = process.env.NEXT_PUBLIC_ADMIN_STREAMKEY;
|
2021-01-19 19:34:06 +01:00
|
|
|
export const NEXT_PUBLIC_API_HOST = process.env.NEXT_PUBLIC_API_HOST;
|
2020-10-04 05:59:25 +02:00
|
|
|
|
2020-10-08 08:09:42 +02:00
|
|
|
const API_LOCATION = `${NEXT_PUBLIC_API_HOST}api/admin/`;
|
2020-10-04 05:59:25 +02:00
|
|
|
|
2020-10-08 09:17:40 +02:00
|
|
|
export const FETCH_INTERVAL = 15000;
|
|
|
|
|
2020-10-04 05:59:25 +02:00
|
|
|
// Current inbound broadcaster info
|
2020-11-06 03:30:14 +01:00
|
|
|
export const STATUS = `${API_LOCATION}status`;
|
2020-10-04 05:59:25 +02:00
|
|
|
|
|
|
|
// Disconnect inbound stream
|
|
|
|
export const DISCONNECT = `${API_LOCATION}disconnect`;
|
|
|
|
|
|
|
|
// Change the current streaming key in memory
|
|
|
|
export const STREAMKEY_CHANGE = `${API_LOCATION}changekey`;
|
|
|
|
|
|
|
|
// Current server config
|
|
|
|
export const SERVER_CONFIG = `${API_LOCATION}serverconfig`;
|
|
|
|
|
2020-12-28 10:11:26 +01:00
|
|
|
// Base url to update config settings
|
|
|
|
export const SERVER_CONFIG_UPDATE_URL = `${API_LOCATION}config`;
|
|
|
|
|
2020-10-04 05:59:25 +02:00
|
|
|
// Get viewer count over time
|
|
|
|
export const VIEWERS_OVER_TIME = `${API_LOCATION}viewersOverTime`;
|
|
|
|
|
2021-07-22 02:19:31 +02:00
|
|
|
// Get currently connected chat clients
|
|
|
|
export const CONNECTED_CLIENTS = `${API_LOCATION}chat/clients`;
|
2020-10-08 09:26:24 +02:00
|
|
|
|
2021-07-20 07:02:02 +02:00
|
|
|
// Get list of disabled/blocked chat users
|
|
|
|
export const DISABLED_USERS = `${API_LOCATION}chat/users/disabled`;
|
|
|
|
|
|
|
|
// Disable/enable a single user
|
|
|
|
export const USER_ENABLED_TOGGLE = `${API_LOCATION}chat/users/setenabled`;
|
|
|
|
|
2020-10-04 05:59:25 +02:00
|
|
|
// Get hardware stats
|
|
|
|
export const HARDWARE_STATS = `${API_LOCATION}hardwarestats`;
|
|
|
|
|
2020-10-30 02:01:38 +01:00
|
|
|
// Get all logs
|
|
|
|
export const LOGS_ALL = `${API_LOCATION}logs`;
|
|
|
|
|
|
|
|
// Get warnings + errors
|
|
|
|
export const LOGS_WARN = `${API_LOCATION}logs/warnings`;
|
|
|
|
|
2020-12-25 20:03:18 +01:00
|
|
|
// Get chat history
|
2020-12-27 02:36:46 +01:00
|
|
|
export const CHAT_HISTORY = `${API_LOCATION}chat/messages`;
|
2020-12-23 07:05:17 +01:00
|
|
|
|
2020-12-25 20:03:18 +01:00
|
|
|
// Get chat history
|
|
|
|
export const UPDATE_CHAT_MESSGAE_VIZ = `${NEXT_PUBLIC_API_HOST}api/admin/chat/updatemessagevisibility`;
|
|
|
|
|
2021-01-05 02:49:45 +01:00
|
|
|
// Get all access tokens
|
|
|
|
export const ACCESS_TOKENS = `${API_LOCATION}accesstokens`;
|
|
|
|
|
|
|
|
// Delete a single access token
|
2021-01-14 20:41:07 +01:00
|
|
|
export const DELETE_ACCESS_TOKEN = `${API_LOCATION}accesstokens/delete`;
|
2021-01-05 02:49:45 +01:00
|
|
|
|
|
|
|
// Create a new access token
|
2021-01-14 20:41:07 +01:00
|
|
|
export const CREATE_ACCESS_TOKEN = `${API_LOCATION}accesstokens/create`;
|
2020-12-23 07:05:17 +01:00
|
|
|
|
2021-01-06 01:35:37 +01:00
|
|
|
// Get webhooks
|
|
|
|
export const WEBHOOKS = `${API_LOCATION}webhooks`;
|
|
|
|
|
|
|
|
// Delete a single webhook
|
|
|
|
export const DELETE_WEBHOOK = `${API_LOCATION}webhooks/delete`;
|
|
|
|
|
|
|
|
// Create a single webhook
|
|
|
|
export const CREATE_WEBHOOK = `${API_LOCATION}webhooks/create`;
|
2021-03-15 23:27:19 +01:00
|
|
|
|
2021-01-19 19:34:06 +01:00
|
|
|
// hard coded social icons list
|
|
|
|
export const SOCIAL_PLATFORMS_LIST = `${NEXT_PUBLIC_API_HOST}api/socialplatforms`;
|
|
|
|
|
2021-03-15 23:27:19 +01:00
|
|
|
// set external action links
|
|
|
|
export const EXTERNAL_ACTIONS = `${API_LOCATION}api/externalactions`
|
|
|
|
|
2021-02-08 00:51:11 +01:00
|
|
|
export const API_YP_RESET = `${API_LOCATION}yp/reset`;
|
|
|
|
|
2021-01-24 22:09:59 +01:00
|
|
|
export const TEMP_UPDATER_API = LOGS_ALL;
|
|
|
|
|
2021-02-04 23:50:03 +01:00
|
|
|
const GITHUB_RELEASE_URL = 'https://api.github.com/repos/owncast/owncast/releases/latest';
|
2020-11-04 03:15:38 +01:00
|
|
|
|
2020-12-30 00:35:54 +01:00
|
|
|
interface FetchOptions {
|
|
|
|
data?: any;
|
|
|
|
method?: string;
|
|
|
|
auth?: boolean;
|
2021-02-08 00:51:11 +01:00
|
|
|
}
|
2020-12-30 00:35:54 +01:00
|
|
|
|
2021-01-27 10:46:08 +01:00
|
|
|
export async function fetchData(url: string, options?: FetchOptions) {
|
2021-02-08 00:51:11 +01:00
|
|
|
const { data, method = 'GET', auth = true } = options || {};
|
2020-11-08 01:44:11 +01:00
|
|
|
|
2020-12-25 20:03:18 +01:00
|
|
|
const requestOptions: RequestInit = {
|
|
|
|
method,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data) {
|
2021-02-04 23:50:03 +01:00
|
|
|
requestOptions.body = JSON.stringify(data);
|
2020-11-08 01:44:11 +01:00
|
|
|
}
|
2020-10-04 05:59:25 +02:00
|
|
|
|
2020-12-25 20:03:18 +01:00
|
|
|
if (auth && ADMIN_USERNAME && ADMIN_STREAMKEY) {
|
|
|
|
const encoded = btoa(`${ADMIN_USERNAME}:${ADMIN_STREAMKEY}`);
|
|
|
|
requestOptions.headers = {
|
2021-02-04 23:50:03 +01:00
|
|
|
Authorization: `Basic ${encoded}`,
|
|
|
|
};
|
2020-12-25 20:03:18 +01:00
|
|
|
requestOptions.mode = 'cors';
|
|
|
|
requestOptions.credentials = 'include';
|
2020-12-23 07:05:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-12-25 20:03:18 +01:00
|
|
|
const response = await fetch(url, requestOptions);
|
2021-02-05 23:23:18 +01:00
|
|
|
const json = await response.json();
|
|
|
|
|
2020-12-23 07:05:17 +01:00
|
|
|
if (!response.ok) {
|
2021-02-05 23:23:18 +01:00
|
|
|
const message = json.message || `An error has occurred: ${response.status}`;
|
2020-10-04 08:07:37 +02:00
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
} catch (error) {
|
2021-02-04 23:50:03 +01:00
|
|
|
return error;
|
|
|
|
// console.log(error)
|
|
|
|
// throw new Error(error)
|
2020-10-04 08:07:37 +02:00
|
|
|
}
|
2020-10-08 08:09:42 +02:00
|
|
|
return {};
|
2020-10-04 05:59:25 +02:00
|
|
|
}
|
2020-11-04 03:15:38 +01:00
|
|
|
|
2021-04-04 06:25:21 +02:00
|
|
|
export async function fetchExternalData(url: string) {
|
2020-11-04 03:15:38 +01:00
|
|
|
try {
|
2021-04-05 05:18:31 +02:00
|
|
|
const response = await fetch(url, {
|
|
|
|
referrerPolicy: 'no-referrer', // Send no referrer header for privacy reasons.
|
|
|
|
referrer: '',
|
|
|
|
});
|
2020-11-04 03:15:38 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
const message = `An error has occured: ${response.status}`;
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
const json = await response.json();
|
|
|
|
return json;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-04-04 06:25:21 +02:00
|
|
|
export async function getGithubRelease() {
|
|
|
|
return fetchExternalData(GITHUB_RELEASE_URL);
|
|
|
|
}
|
|
|
|
|
2020-11-04 03:15:38 +01:00
|
|
|
// Stolen from https://gist.github.com/prenagha/98bbb03e27163bc2f5e4
|
|
|
|
const VPAT = /^\d+(\.\d+){0,2}$/;
|
|
|
|
function upToDate(local, remote) {
|
2021-02-08 00:51:11 +01:00
|
|
|
if (!local || !remote || local.length === 0 || remote.length === 0) return false;
|
|
|
|
if (local === remote) return true;
|
|
|
|
if (VPAT.test(local) && VPAT.test(remote)) {
|
|
|
|
const lparts = local.split('.');
|
|
|
|
while (lparts.length < 3) lparts.push('0');
|
|
|
|
const rparts = remote.split('.');
|
|
|
|
while (rparts.length < 3) rparts.push('0');
|
|
|
|
// eslint-disable-next-line no-plusplus
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
const l = parseInt(lparts[i], 10);
|
|
|
|
const r = parseInt(rparts[i], 10);
|
|
|
|
if (l === r)
|
|
|
|
// eslint-disable-next-line no-continue
|
|
|
|
continue;
|
|
|
|
return l > r;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return local >= remote;
|
2020-11-13 13:43:27 +01:00
|
|
|
}
|
2021-01-27 10:46:08 +01:00
|
|
|
|
|
|
|
// Make a request to the server status API and the Github releases API
|
|
|
|
// and return a release if it's newer than the server version.
|
|
|
|
export async function upgradeVersionAvailable(currentVersion) {
|
|
|
|
const recentRelease = await getGithubRelease();
|
|
|
|
let recentReleaseVersion = recentRelease.tag_name;
|
|
|
|
|
|
|
|
if (recentReleaseVersion.substr(0, 1) === 'v') {
|
2021-02-08 00:51:11 +01:00
|
|
|
recentReleaseVersion = recentReleaseVersion.substr(1);
|
2021-01-27 10:46:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!upToDate(currentVersion, recentReleaseVersion)) {
|
2021-02-08 00:51:11 +01:00
|
|
|
return recentReleaseVersion;
|
2021-01-27 10:46:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2021-02-08 00:51:11 +01:00
|
|
|
}
|