owncast/web/utils/apis.ts

232 lines
6.7 KiB
TypeScript
Raw Normal View History

2020-10-08 09:09:42 +03:00
/* eslint-disable prefer-destructuring */
2020-10-08 10:17:40 +03:00
const ADMIN_USERNAME = process.env.NEXT_PUBLIC_ADMIN_USERNAME;
const ADMIN_STREAMKEY = process.env.NEXT_PUBLIC_ADMIN_STREAMKEY;
2021-01-19 21:34:06 +03:00
export const NEXT_PUBLIC_API_HOST = process.env.NEXT_PUBLIC_API_HOST;
2020-10-04 06:59:25 +03:00
2020-10-08 09:09:42 +03:00
const API_LOCATION = `${NEXT_PUBLIC_API_HOST}api/admin/`;
2020-10-04 06:59:25 +03:00
2020-10-08 10:17:40 +03:00
export const FETCH_INTERVAL = 15000;
2020-10-04 06:59:25 +03:00
// Current inbound broadcaster info
export const STATUS = `${API_LOCATION}status`;
2020-10-04 06:59:25 +03: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`;
// Base url to update config settings
export const SERVER_CONFIG_UPDATE_URL = `${API_LOCATION}config`;
2020-10-04 06:59:25 +03:00
// Get viewer count over time
export const VIEWERS_OVER_TIME = `${API_LOCATION}viewersOverTime`;
// Get active viewer details
export const ACTIVE_VIEWER_DETAILS = `${API_LOCATION}viewers`;
// Get currently connected chat clients
export const CONNECTED_CLIENTS = `${API_LOCATION}chat/clients`;
2020-10-08 10:26:24 +03:00
2021-07-20 08:02:02 +03: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`;
// Get banned IP addresses
export const BANNED_IPS = `${API_LOCATION}chat/users/ipbans`;
// Remove IP ban
export const BANNED_IP_REMOVE = `${API_LOCATION}chat/users/ipbans/remove`;
// Disable/enable a single user
export const USER_SET_MODERATOR = `${API_LOCATION}chat/users/setmoderator`;
// Get list of moderators
export const MODERATORS = `${API_LOCATION}chat/users/moderators`;
2020-10-04 06:59:25 +03:00
// Get hardware stats
export const HARDWARE_STATS = `${API_LOCATION}hardwarestats`;
2020-10-30 04:01:38 +03:00
// Get all logs
export const LOGS_ALL = `${API_LOCATION}logs`;
// Get warnings + errors
export const LOGS_WARN = `${API_LOCATION}logs/warnings`;
// Get chat history
export const CHAT_HISTORY = `${API_LOCATION}chat/messages`;
// Get chat history
export const UPDATE_CHAT_MESSGAE_VIZ = `/api/admin/chat/messagevisibility`;
// Get all access tokens
export const ACCESS_TOKENS = `${API_LOCATION}accesstokens`;
// Delete a single access token
2021-01-14 22:41:07 +03:00
export const DELETE_ACCESS_TOKEN = `${API_LOCATION}accesstokens/delete`;
// Create a new access token
2021-01-14 22:41:07 +03:00
export const CREATE_ACCESS_TOKEN = `${API_LOCATION}accesstokens/create`;
2021-01-06 03:35:37 +03: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-01-19 21:34:06 +03:00
// hard coded social icons list
export const SOCIAL_PLATFORMS_LIST = `${NEXT_PUBLIC_API_HOST}api/socialplatforms`;
// set external action links
2021-07-22 03:28:56 +03:00
export const EXTERNAL_ACTIONS = `${API_LOCATION}api/externalactions`;
Admin social features (#408) * ActivityPub admin pages for configuration * Fix dev build * Add support for requiring follow approval. Closes https://github.com/owncast/owncast/issues/1208 * Point at admin version of followers endpoint * Add setting for toggling displaying fediverse engagement in admin. https://github.com/owncast/owncast/issues/1404 * Add instance URL textfield to federation config and disable federation if it is empty * If instance URL is not https disable federation * Tweak federation toggle text. Make go live message optional * Add federation info modal. Closes https://github.com/owncast/owncast/issues/1544 * Add support for blocked federated domains. For https://github.com/owncast/owncast/issues/1209 * Simplify fediverse post input * Add placeholder Fediverse icon * Tweak federation logo in admin menu. Closes https://github.com/owncast/owncast/issues/1603 * Add global button for composing a fediverse post. Closes https://github.com/owncast/owncast/issues/1610 * Federation -> Social * Add page for listing federated actions. Closes https://github.com/owncast/owncast/issues/1573 * Auto-close social post modal after success * Make user modal action buttons look nicer * Center and reduce width and center count column. Closes https://github.com/owncast/owncast/issues/1580 * Update the followers table to be clearer * Fix exception thrown when passing undefined * Disable federation settings if feature is disabled * Update enable social modal. For https://github.com/owncast/owncast/issues/1594 * Fix type props * Quiet, linter * Move compose button to the left * Add tooltip for compose button * Add NSFW toggle to federation config. Closes https://github.com/owncast/owncast/issues/1628 * Add support for blocking/removing followers. For https://github.com/owncast/owncast/issues/1630 * Allow editing the server url field even when federation is disabled * Continue to update the copy around the social features * Use relative path to action images. Fixes https://github.com/owncast/owncast/issues/1646 * Link IRIs and make action verbse present tense * Update caniuse
2022-01-13 00:52:37 +03:00
// send a message to the fediverse
export const FEDERATION_MESSAGE_SEND = `${API_LOCATION}federation/send`;
// Get followers
export const FOLLOWERS = `${API_LOCATION}followers`;
// Get followers pending approval
export const FOLLOWERS_PENDING = `${API_LOCATION}followers/pending`;
// Get followers who were blocked or rejected
export const FOLLOWERS_BLOCKED = `${API_LOCATION}followers/blocked`;
// Approve, reject a follow request
export const SET_FOLLOWER_APPROVAL = `${API_LOCATION}followers/approve`;
// List of inbound federated actions that took place.
export const FEDERATION_ACTIONS = `${API_LOCATION}federation/actions`;
export const API_STREAM_HEALTH_METRICS = `${API_LOCATION}metrics/video`;
export const API_YP_RESET = `${API_LOCATION}yp/reset`;
2021-01-25 00:09:59 +03:00
export const TEMP_UPDATER_API = LOGS_ALL;
const GITHUB_RELEASE_URL = 'https://api.github.com/repos/owncast/owncast/releases/latest';
2020-11-04 05:15:38 +03:00
2020-12-30 02:35:54 +03:00
interface FetchOptions {
data?: any;
method?: string;
auth?: boolean;
}
2020-12-30 02:35:54 +03:00
export async function fetchData(url: string, options?: FetchOptions) {
const { data, method = 'GET', auth = true } = options || {};
2021-07-22 03:28:56 +03:00
// eslint-disable-next-line no-undef
const requestOptions: RequestInit = {
method,
};
if (data) {
requestOptions.body = JSON.stringify(data);
}
2020-10-04 06:59:25 +03:00
if (auth && ADMIN_USERNAME && ADMIN_STREAMKEY) {
const encoded = btoa(`${ADMIN_USERNAME}:${ADMIN_STREAMKEY}`);
requestOptions.headers = {
Authorization: `Basic ${encoded}`,
};
requestOptions.mode = 'cors';
requestOptions.credentials = 'include';
}
try {
const response = await fetch(url, requestOptions);
const json = await response.json();
if (!response.ok) {
const message = json.message || `An error has occurred: ${response.status}`;
2020-10-04 09:07:37 +03:00
throw new Error(message);
}
return json;
} catch (error) {
console.error(error);
return error;
// console.log(error)
// throw new Error(error)
2020-10-04 09:07:37 +03:00
}
2020-10-04 06:59:25 +03:00
}
2020-11-04 05:15:38 +03:00
export async function getUnauthedData(url: string, options?: FetchOptions) {
const opts = {
method: 'GET',
auth: false,
...options,
};
return fetchData(url, opts);
}
export async function fetchExternalData(url: string) {
2020-11-04 05:15:38 +03:00
try {
const response = await fetch(url, {
referrerPolicy: 'no-referrer', // Send no referrer header for privacy reasons.
referrer: '',
});
2020-11-04 05:15:38 +03: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 {};
}
export async function getGithubRelease() {
return fetchExternalData(GITHUB_RELEASE_URL);
}
2020-11-04 05:15:38 +03:00
// Stolen from https://gist.github.com/prenagha/98bbb03e27163bc2f5e4
const VPAT = /^\d+(\.\d+){0,2}$/;
function upToDate(local, remote) {
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;
}
// 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') {
recentReleaseVersion = recentReleaseVersion.substr(1);
}
if (!upToDate(currentVersion, recentReleaseVersion)) {
return recentReleaseVersion;
}
return null;
}