Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: added twitch subscriptions and chat #1163

Merged
merged 9 commits into from
Apr 24, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mage-website",
"version": "0.2.11",
"version": "0.2.12",
"license": "GPL-3.0",
"private": true,
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function send({
const res = await fetch(`${base}/${path}`, opts)
if (res.ok || res.status === 422) {
const text = await res.text()
if (path === 'wsinit/wsid' || path.includes('wsinit/channelid')) return text
if (path.includes('wsinit/channelid')) return text
return text ? JSON.parse(text) : {}
}

Expand Down
14 changes: 4 additions & 10 deletions src/lib/components/Channel/Chat/ChatInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,13 @@
}
} else if (!chatMessage.startsWith('/') || chatMessage.startsWith('/ai')) {
if (chatMessage === null || chatMessage.match(/^\s*$/) !== null) return
const completeMessage = {
isAiChatEnabled: channel.isAiChatEnabled,
body: chatMessage,
state: { timestamp: Date.now() },
user: {
userId: $page.data.user?.userId || '',
username: $page.data.user?.user?.username || ''
}
}
emitMessageToChannel({
channelSocket: channel.socket,
channelId: channel._id,
message: JSON.stringify(completeMessage)
message: {
isAiChatEnabled: channel.isAiChatEnabled,
body: chatMessage
}
})
}
chatMessage = ''
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Channel/Chat/Message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
{/if}
<div class="d-flex align-items-center">
{#if sender.platform === 'twitch'}
<IconSocialTwitch />
<div style="display: ruby;"><IconSocialTwitch /></div>
{/if}
{#if sender.platform === 'youtube'}
<div style="display: ruby;"><IconSocialYouTube /></div>
Expand Down
16 changes: 12 additions & 4 deletions src/routes/channel/[channelId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
}

onMount(async () => {
clearInterval(platformPollingInterval)
platformPollingInterval = null
await loadChannel()
await handleWebsocket()
await loadMoreChannels()
Expand Down Expand Up @@ -237,18 +239,24 @@
channel.socket.addEventListener('error', (data: any) => {
console.log('channel socket connection error')
console.log(data)
clearInterval(platformPollingInterval)
platformPollingInterval = null
})
channel.socket.addEventListener('close', (data: any) => {
console.log('channel socket connection close')
console.log(data)

//if manually closed, don't reconnect
if (data.code === 1005) return
if (data.code === 1005) {
clearInterval(platformPollingInterval)
platformPollingInterval = null
return
}
attemptReconnect()
})
}
} catch (error) {
if (error) attemptReconnect()
} catch (err) {
if (err) attemptReconnect()
}
}

Expand Down Expand Up @@ -319,7 +327,7 @@
}

const getPlatformChatYouTube = async () => {
if (channel.isLive) {
if (channel.isLive && channel.platforms?.some((platform: any) => platform.name === 'YouTube')) {
let url = `youtube/messages?userId=${channel.userId}`
if (youtubeChatPageToken) {
url += `&pageToken=${youtubeChatPageToken}`
Expand Down
Loading