2022-09-14 08:52:31 +00:00
|
|
|
import { PrivateUser, User } from 'common/user'
|
|
|
|
import { generateNewApiKey } from '../api/api-key'
|
2022-09-18 08:13:10 +00:00
|
|
|
import { ENV_CONFIG } from 'common/envs/constants'
|
2022-09-14 08:52:31 +00:00
|
|
|
|
2022-09-16 07:22:13 +00:00
|
|
|
async function postToBot(url: string, body: unknown) {
|
|
|
|
const result = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
|
|
|
const json = await result.json()
|
|
|
|
if (!result.ok) {
|
|
|
|
throw new Error(json.message)
|
|
|
|
} else {
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 08:52:31 +00:00
|
|
|
export async function initLinkTwitchAccount(
|
|
|
|
manifoldUserID: string,
|
|
|
|
manifoldUserAPIKey: string
|
|
|
|
): Promise<[string, Promise<{ twitchName: string; controlToken: string }>]> {
|
2022-09-18 08:13:10 +00:00
|
|
|
const response = await postToBot(
|
|
|
|
`${ENV_CONFIG.twitchBotEndpoint}/api/linkInit`,
|
|
|
|
{
|
|
|
|
manifoldID: manifoldUserID,
|
|
|
|
apiKey: manifoldUserAPIKey,
|
|
|
|
redirectURL: window.location.href,
|
|
|
|
}
|
|
|
|
)
|
2022-09-14 08:52:31 +00:00
|
|
|
const responseFetch = fetch(
|
2022-09-18 08:13:10 +00:00
|
|
|
`${ENV_CONFIG.twitchBotEndpoint}/api/linkResult?userID=${manifoldUserID}`
|
2022-09-14 08:52:31 +00:00
|
|
|
)
|
2022-09-16 07:22:13 +00:00
|
|
|
return [response.twitchAuthURL, responseFetch.then((r) => r.json())]
|
2022-09-14 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function linkTwitchAccountRedirect(
|
|
|
|
user: User,
|
|
|
|
privateUser: PrivateUser
|
|
|
|
) {
|
|
|
|
const apiKey = privateUser.apiKey ?? (await generateNewApiKey(user.id))
|
|
|
|
if (!apiKey) throw new Error("Couldn't retrieve or create Manifold api key")
|
|
|
|
|
|
|
|
const [twitchAuthURL] = await initLinkTwitchAccount(user.id, apiKey)
|
|
|
|
|
|
|
|
window.location.href = twitchAuthURL
|
2022-09-16 15:43:49 +00:00
|
|
|
await new Promise((r) => setTimeout(r, 1e10)) // Wait "forever" for the page to change location
|
2022-09-14 08:52:31 +00:00
|
|
|
}
|
2022-09-16 07:22:13 +00:00
|
|
|
|
|
|
|
export async function updateBotEnabledForUser(
|
|
|
|
privateUser: PrivateUser,
|
|
|
|
botEnabled: boolean
|
|
|
|
) {
|
|
|
|
if (botEnabled) {
|
2022-09-18 08:13:10 +00:00
|
|
|
return postToBot(`${ENV_CONFIG.twitchBotEndpoint}/registerchanneltwitch`, {
|
2022-09-16 07:22:13 +00:00
|
|
|
apiKey: privateUser.apiKey,
|
|
|
|
}).then((r) => {
|
|
|
|
if (!r.success) throw new Error(r.message)
|
|
|
|
})
|
|
|
|
} else {
|
2022-09-18 08:13:10 +00:00
|
|
|
return postToBot(
|
|
|
|
`${ENV_CONFIG.twitchBotEndpoint}/unregisterchanneltwitch`,
|
|
|
|
{
|
|
|
|
apiKey: privateUser.apiKey,
|
|
|
|
}
|
|
|
|
).then((r) => {
|
2022-09-16 07:22:13 +00:00
|
|
|
if (!r.success) throw new Error(r.message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 15:43:49 +00:00
|
|
|
|
|
|
|
export function getOverlayURLForUser(privateUser: PrivateUser) {
|
|
|
|
const controlToken = privateUser?.twitchInfo?.controlToken
|
2022-09-18 08:13:10 +00:00
|
|
|
return `${ENV_CONFIG.twitchBotEndpoint}/overlay?t=${controlToken}`
|
2022-09-16 15:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getDockURLForUser(privateUser: PrivateUser) {
|
|
|
|
const controlToken = privateUser?.twitchInfo?.controlToken
|
2022-09-18 08:13:10 +00:00
|
|
|
return `${ENV_CONFIG.twitchBotEndpoint}/dock?t=${controlToken}`
|
2022-09-16 15:43:49 +00:00
|
|
|
}
|