From 07e3a00990ccfee1854cc7a0c312088fac130794 Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Tue, 2 Apr 2024 10:41:28 +0200 Subject: [PATCH] refactor: preparatory code for re-introducing custom account for chat responses --- app.go | 24 ++- frontend/src/ui/pages/Dashboard.tsx | 2 +- frontend/src/ui/pages/Onboarding.tsx | 25 +-- frontend/src/ui/pages/TwitchSettings.tsx | 190 ++++++++++++----------- frontend/wailsjs/go/main/App.d.ts | 4 +- frontend/wailsjs/go/main/App.js | 8 +- log/attr.go | 6 +- loyalty/manager.go | 5 +- twitch/api.go | 31 ++-- twitch/chat/data.go | 1 + twitch/chat/module.go | 31 +++- twitch/client/auth.go | 15 +- twitch/client/client.go | 56 ++++--- twitch/scopes.go | 4 +- 14 files changed, 227 insertions(+), 175 deletions(-) diff --git a/app.go b/app.go index 2d57ea8..62988c3 100644 --- a/app.go +++ b/app.go @@ -241,12 +241,26 @@ func (a *App) GetKilovoltBind() string { return a.httpServer.Config.Get().Bind } -func (a *App) GetTwitchAuthURL() string { - return twitch.GetAuthorizationURL(a.twitchManager.Client().API) +func (a *App) GetTwitchAuthURL(state string) string { + return twitch.GetAuthorizationURL(a.twitchManager.Client().API, state) } -func (a *App) GetTwitchLoggedUser() (helix.User, error) { - return a.twitchManager.Client().GetLoggedUser() +func (a *App) GetTwitchLoggedUser(key string) (helix.User, error) { + userClient, err := twitch.GetUserClient(a.db, key, false) + if err != nil { + return helix.User{}, err + } + + users, err := userClient.GetUsers(&helix.UsersParams{}) + if err != nil { + return helix.User{}, err + } + + if len(users.Data.Users) < 1 { + return helix.User{}, errors.New("no users found") + } + + return users.Data.Users[0], nil } func (a *App) GetLastLogs() []log.Entry { @@ -341,7 +355,7 @@ func (a *App) interactiveAuth(client kv.Client, message map[string]any) bool { func (a *App) showFatalError(err error, text string, fields ...any) { if err != nil { - fields = append(fields, log.Error(err), slog.String("Z", string(debug.Stack()))) + fields = append(fields, log.ErrorSkip(err, 2), slog.String("Z", string(debug.Stack()))) slog.Error(text, fields...) runtime.EventsEmit(a.ctx, "fatalError") a.isFatalError.Set(true) diff --git a/frontend/src/ui/pages/Dashboard.tsx b/frontend/src/ui/pages/Dashboard.tsx index 572f611..f04917f 100644 --- a/frontend/src/ui/pages/Dashboard.tsx +++ b/frontend/src/ui/pages/Dashboard.tsx @@ -543,7 +543,7 @@ function ProblemList() { }; void kv.subscribeKey('twitch/auth-keys', onKeyChange); - const url = await GetTwitchAuthURL(); + const url = await GetTwitchAuthURL('stream'); BrowserOpenURL(url); }; diff --git a/frontend/src/ui/pages/Onboarding.tsx b/frontend/src/ui/pages/Onboarding.tsx index 3008151..a3480c0 100644 --- a/frontend/src/ui/pages/Onboarding.tsx +++ b/frontend/src/ui/pages/Onboarding.tsx @@ -450,15 +450,13 @@ function TwitchEventsStep() { const { t } = useTranslation(); const [userStatus, setUserStatus] = useState(null); const [twitchConfig, setTwitchConfig] = useModule(modules.twitchConfig); - const [botConfig, setBotConfig] = useModule(modules.twitchChatConfig); const [uiConfig, setUiConfig] = useModule(modules.uiConfig); - const [authKeys, setAuthKeys] = useState(null); const kv = useSelector((state: RootState) => state.api.client); const dispatch = useAppDispatch(); const getUserInfo = async () => { try { - const res = await GetTwitchLoggedUser(); + const res = await GetTwitchLoggedUser('twitch/auth-keys'); setUserStatus(res); } catch (e) { console.error(e); @@ -467,7 +465,7 @@ function TwitchEventsStep() { }; const startAuthFlow = async () => { - const url = await GetTwitchAuthURL(); + const url = await GetTwitchAuthURL('stream'); BrowserOpenURL(url); }; @@ -477,16 +475,6 @@ function TwitchEventsStep() { await dispatch( setTwitchConfig({ ...twitchConfig, - enable_bot: true, - }), - ); - await dispatch( - setBotConfig({ - ...botConfig, - username: userStatus.login, - oauth: `oauth:${authKeys.access_token}`, - channel: userStatus.login, - chat_history: 5, }), ); } @@ -503,17 +491,10 @@ function TwitchEventsStep() { // Get user info void getUserInfo(); - const onKeyChange = (newValue: string) => { - setAuthKeys(JSON.parse(newValue) as TwitchCredentials); + const onKeyChange = () => { void getUserInfo(); }; - void kv.getKey('twitch/auth-keys').then((auth) => { - if (auth) { - setAuthKeys(JSON.parse(auth) as TwitchCredentials); - } - }); - void kv.subscribeKey('twitch/auth-keys', onKeyChange); return () => { void kv.unsubscribeKey('twitch/auth-keys', onKeyChange); diff --git a/frontend/src/ui/pages/TwitchSettings.tsx b/frontend/src/ui/pages/TwitchSettings.tsx index e4c7176..9449d8a 100644 --- a/frontend/src/ui/pages/TwitchSettings.tsx +++ b/frontend/src/ui/pages/TwitchSettings.tsx @@ -53,54 +53,6 @@ const Step = styled('li', { }, }); -function TwitchChatSettings() { - const [chatConfig, setChatConfig, loadStatus] = useModule( - modules.twitchChatConfig, - ); - const [twitchConfig, setTwitchConfig] = useModule(modules.twitchConfig); - const status = useStatus(loadStatus.save); - const dispatch = useAppDispatch(); - const { t } = useTranslation(); - const disabled = status?.type === 'pending'; - - return ( -
{ - void dispatch(setTwitchConfig(twitchConfig)); - void dispatch(setChatConfig(chatConfig)); - ev.preventDefault(); - }} - > - - {t('pages.twitch-settings.bot-chat-header')} - - - - - dispatch( - apiReducer.actions.twitchChatConfigChanged({ - ...chatConfig, - command_cooldown: parseInt(ev.target.value, 10), - }), - ) - } - /> - - - - ); -} - type TestResult = { open: boolean; error?: Error }; function TwitchAPISettings() { @@ -275,25 +227,63 @@ const TwitchPic = styled('img', { }); const TwitchName = styled('p', { fontWeight: 'bold' }); -function TwitchEventSubSettings() { +async function startAuthFlow(target: string) { + const url = await GetTwitchAuthURL(target); + BrowserOpenURL(url); +} + +function TwitchUserBlock({ authKey }: { authKey: string }) { const { t } = useTranslation(); - const [userStatus, setUserStatus] = useState(null); + const [user, setUser] = useState(null); const kv = useAppSelector((state) => state.api.client); const getUserInfo = async () => { try { - const res = await GetTwitchLoggedUser(); - setUserStatus(res); + const res = await GetTwitchLoggedUser(authKey); + setUser(res); } catch (e) { console.error(e); - setUserStatus({ ok: false, error: (e as Error).message }); + setUser({ ok: false, error: (e as Error).message }); } }; - const startAuthFlow = async () => { - const url = await GetTwitchAuthURL(); - BrowserOpenURL(url); - }; + useEffect(() => { + // Get user info + void getUserInfo(); + + const onKeyChange = () => { + void getUserInfo(); + }; + void kv.subscribeKey(authKey, onKeyChange); + return () => { + void kv.unsubscribeKey(authKey, onKeyChange); + }; + }, []); + + if (user !== null) { + if ('id' in user) { + return ( + + + {t('pages.twitch-settings.events.authenticated-as')} + + + {user.display_name} + + ); + } + return {t('pages.twitch-settings.events.err-no-user')}; + } + + return {t('pages.twitch-settings.events.loading-data')}; +} + +function TwitchEventSubSettings() { + const { t } = useTranslation(); + const kv = useAppSelector((state) => state.api.client); const sendFakeEvent = async (event: keyof typeof eventsubTests) => { const data = eventsubTests[event]; @@ -307,47 +297,13 @@ function TwitchEventSubSettings() { }); }; - useEffect(() => { - // Get user info - void getUserInfo(); - - const onKeyChange = () => { - void getUserInfo(); - }; - void kv.subscribeKey('twitch/auth-keys', onKeyChange); - return () => { - void kv.unsubscribeKey('twitch/auth-keys', onKeyChange); - }; - }, []); - - let userBlock = {t('pages.twitch-settings.events.loading-data')}; - if (userStatus !== null) { - if ('id' in userStatus) { - userBlock = ( - <> - - - {t('pages.twitch-settings.events.authenticated-as')} - - - {userStatus.display_name} - - - ); - } else { - userBlock = {t('pages.twitch-settings.events.err-no-user')}; - } - } return ( <> {t('pages.twitch-settings.events.auth-message')}