diff --git a/src/dashboard/scripts/verify-login.js b/src/dashboard/scripts/verify-login.js index bd5ba9b..3087934 100644 --- a/src/dashboard/scripts/verify-login.js +++ b/src/dashboard/scripts/verify-login.js @@ -1,11 +1,10 @@ /* global nodecg, NodeCG */ -(async () => { +;(async () => { const loginURL = '/login/twitch' const verifyURL = '/login/twitch/verify' const { timeBetweenUpdates = 60000, requireLogin = true } = nodecg.bundleConfig - const loggedInStatus = NodeCG.Replicant('login.status', 'nodecg-twitchie') await NodeCG.waitForReplicants(loggedInStatus) @@ -18,7 +17,7 @@ // trigger nodecg's "login" express event to be emitted // manually hitting this endpoint should ensure this happens const verifyLogin = async () => { - const response = await fetch(verifyURL, { method: 'GET', credentials: 'include' }) + const response = await fetch(verifyURL) if (!response.ok) { throw new Error('Login endpoint returned error', response.status) @@ -40,7 +39,7 @@ pollForLoginStatus() - loggedInStatus.on('change', isLoggedIn => { + loggedInStatus.on('change', (isLoggedIn) => { if (requireLogin && !isLoggedIn) { redirectToLogin() } diff --git a/src/extension/client/index.ts b/src/extension/client/index.ts index d44fec7..18720c6 100644 --- a/src/extension/client/index.ts +++ b/src/extension/client/index.ts @@ -16,7 +16,7 @@ class TwitchieClientWrapper { public client?: ChatClient - public isConnected = () => !!this.client + public isConnected = () => this.client && (this.client.isConnected || this.client.isConnecting) public disconnect = async () => { if (!this.client) { @@ -26,7 +26,7 @@ class TwitchieClientWrapper { try { await this.client.quit() } catch (e) { - // Oh Well + context.log.error('Could not tear down twitch chat client!', e) } this.client = undefined @@ -42,19 +42,14 @@ class TwitchieClientWrapper { const authProvider = new StaticAuthProvider(context.config.clientID, auth.token) this.api = new ApiClient({ authProvider }) - this.client = new ChatClient({ authProvider, - channels: () => { - const currentChannel = context.replicants.channel.id.value || auth.username - return currentChannel ? [currentChannel] : [] - }, + webSocket: true, }) try { bindChatHandlers(this.client) await this.client.connect() - context.replicants.channel.id.value = auth.username } catch (error) { this.api = undefined diff --git a/src/extension/modules/chat/channel.ts b/src/extension/modules/chat/channel.ts index 62ab622..811d1e4 100644 --- a/src/extension/modules/chat/channel.ts +++ b/src/extension/modules/chat/channel.ts @@ -13,10 +13,12 @@ context.replicants.channel.id.on('change', (_, oldChannel) => { } try { + context.log.debug(`Parting ${oldChannel}...`) context.twitch.client.part(oldChannel) + context.log.debug('Parted successfully.') } catch (e) { // Oh No!! - console.log(e) + context.log.error('Failed to part old channel', e) } }) @@ -24,7 +26,7 @@ context.replicants.channel.id.on('change', (_, oldChannel) => { // channel id actually resolves to a real user // trying to join nonexistent channels on twitch irc can // cause issues -context.replicants.user.info.on('change', async newUserInfo => { +context.replicants.user.info.on('change', async (newUserInfo) => { if (!newUserInfo) { return } @@ -33,6 +35,9 @@ context.replicants.user.info.on('change', async newUserInfo => { return } + context.log.debug(`Joining #${newUserInfo.login}...`) await context.twitch.client.join(newUserInfo.login) + context.log.debug('Joined successfully.') + context.replicants.chat.channel.value = newUserInfo.login }) diff --git a/src/extension/service.ts b/src/extension/service.ts index 37a6c20..2aeb68c 100644 --- a/src/extension/service.ts +++ b/src/extension/service.ts @@ -14,22 +14,6 @@ const isTwitchSession = (session: any) => const hasAccessDetails = (session: any) => session.passport.user.username && session.passport.user.accessToken -const performConnect = (session: any) => { - const { user } = session.passport - - log.debug('Performing connect...') - - return twitch.connect({ - username: user.username, - token: user.accessToken, - }) -} - -const performDisconnect = () => { - log.debug('Performing disconnect...') - return twitch.disconnect() -} - const handleConnect = (session: any) => { log.debug('Handling connect...') @@ -37,12 +21,17 @@ const handleConnect = (session: any) => { throw new Error('Invalid session data receieved') } - return performConnect(session) + const { user } = session.passport + + return twitch.connect({ + username: user.username, + token: user.accessToken, + }) } const handleDisconnect = () => { log.debug('Handling disconnect...') - return performDisconnect() + return twitch.disconnect() } // listen for login and logout events emitted from nodecg's login module