From 3b06d306dccae609b9c14c31557fa7e21ec0c192 Mon Sep 17 00:00:00 2001 From: Jason Murray Date: Tue, 3 Sep 2024 22:08:55 +0100 Subject: [PATCH] Add gofundme --- src/components/ChatFeed.vue | 2 +- src/components/NotificationFeed.vue | 573 +++++++++++++++------------- 2 files changed, 306 insertions(+), 269 deletions(-) diff --git a/src/components/ChatFeed.vue b/src/components/ChatFeed.vue index 365c19e..3a4820d 100644 --- a/src/components/ChatFeed.vue +++ b/src/components/ChatFeed.vue @@ -6,7 +6,7 @@ const iframeSrc = ref(""); const chatScrollContainer = ref(null); const url = new URL(window.location.href); -const channel = url.searchParams.get("channel"); +const channel = url.searchParams.get("channel") ?? 'pablogz205'; const size = url.searchParams.get("size") ?? 1; const font = url.searchParams.get("font") ?? 1; iframeSrc.value = `https://www.giambaj.it/twitch/jchat/v2/?channel=${channel}&size=${size}&font=${font}`; diff --git a/src/components/NotificationFeed.vue b/src/components/NotificationFeed.vue index 71a6e89..1e96712 100644 --- a/src/components/NotificationFeed.vue +++ b/src/components/NotificationFeed.vue @@ -9,6 +9,8 @@ const sound = ref(false); const url = new URL(window.location.href); const bearer = url.searchParams.get("bearer"); +const debug = url.searchParams.get("debug"); +const gfm = url.searchParams.get("gfm") ?? 'from-home-simulator-to-real-racing'; onMounted(() => { topBox.value = document.querySelector('.top'); @@ -48,14 +50,52 @@ function toggleAudio(el) { } +// gofundme notification +let lastDonation = 0; +let gfmUrl = 'https://corsproxy.io/?' + encodeURIComponent(`https://gateway.gofundme.com/web-gateway/v1/feed/${gfm}/donations?limit=10`); + +setInterval(() => { + fetch(gfmUrl) + .then(response => response.json()) + .then(data => { + + // flip array so we get the oldest donations first + if (lastDonation != 0) { + data.references.donations.reverse(); + } + + // loop through array of donations in data.references.donations + for(i = 0; i < data.references.donations.length; i++) { + let donation = data.references.donations[i]; + + if (lastDonation == 0) { + lastDonation = donation.id; + break; + } + + if (donation.id <= lastDonation) { + break; + } + + let notification = { + id: donation.donation_id, + user: donation.name, + color: '-yellow-600', + nice_name: 'Donation £' + donation.amount, + text: `Donated £${donation.amount} to Rally!`, + tts: `${donation.name} donated £${donation.amount} to Rally!` + }; + + addNotification(notification); + lastDonation = donation.id; + } + }); +}, 30000); // connect to twitch via websockets wss://eventsub.wss.twitch.tv/ws let ws = []; let i = 0; -//ws[i] = new WebSocket('wss://eventsub.wss.twitch.tv/ws'); -ws[i] = new WebSocket('wss://example.com'); - -//const ws = new WebSocket('wss://example.com/ws'); +ws[i] = debug ? new WebSocket('wss://example.com') : new WebSocket('wss://eventsub.wss.twitch.tv/ws'); function subscribe_to_events(session_id) { fetch('https://api.twitch.tv/helix/eventsub/subscriptions', { @@ -171,291 +211,289 @@ const processMessages = function (event) { ws[i].onmessage = processMessages; // DEBUG -let messages = [{ - "metadata": { - "message_id": "GD60uGl4b-rsculGOvnG3CbfWub8sU0-kLka5QQTklE=", - "message_type": "notification", - "message_timestamp": "2024-09-02T16:45:07.795455639Z", - "subscription_type": "channel.chat.notification", - "subscription_version": "1" - }, - "payload": { - "subscription": { - "id": "8bfaa271-dacf-45ad-9252-82961b8e4558", - "status": "enabled", - "type": "channel.chat.notification", - "version": "1", - "condition": { - "broadcaster_user_id": "171093413", - "user_id": "54096715" - }, - "transport": { - "method": "websocket", - "session_id": "AgoQZGprB1c1S96FME9t0ZuU4hIGY2VsbC1j" - }, - "created_at": "2024-09-02T16:42:14.02277498Z", - "cost": 0 +if(debug) { + let messages = [{ + "metadata": { + "message_id": "GD60uGl4b-rsculGOvnG3CbfWub8sU0-kLka5QQTklE=", + "message_type": "notification", + "message_timestamp": "2024-09-02T16:45:07.795455639Z", + "subscription_type": "channel.chat.notification", + "subscription_version": "1" }, - "event": { - "broadcaster_user_id": "171093413", - "broadcaster_user_login": "pablogz205", - "broadcaster_user_name": "PabloGz205", - "source_broadcaster_user_id": null, - "source_broadcaster_user_login": null, - "source_broadcaster_user_name": null, - "chatter_user_id": "54492353", - "chatter_user_login": "keepcalmcallsoul", - "chatter_user_name": "keepcalmcallsoul", - "chatter_is_anonymous": false, - "color": "#8A2BE2", - "badges": [ - { - "set_id": "subscriber", - "id": "9", - "info": "9" - } - ], - "source_badges": null, - "system_message": "keepcalmcallsoul subscribed at Tier 1. They've subscribed for 9 months!", - "message_id": "d6deea58-ce6e-4217-905d-67670369b628", - "source_message_id": null, - "message": { - "text": "what do you think about the mclaren?", - "fragments": [ + "payload": { + "subscription": { + "id": "8bfaa271-dacf-45ad-9252-82961b8e4558", + "status": "enabled", + "type": "channel.chat.notification", + "version": "1", + "condition": { + "broadcaster_user_id": "171093413", + "user_id": "54096715" + }, + "transport": { + "method": "websocket", + "session_id": "AgoQZGprB1c1S96FME9t0ZuU4hIGY2VsbC1j" + }, + "created_at": "2024-09-02T16:42:14.02277498Z", + "cost": 0 + }, + "event": { + "broadcaster_user_id": "171093413", + "broadcaster_user_login": "pablogz205", + "broadcaster_user_name": "PabloGz205", + "source_broadcaster_user_id": null, + "source_broadcaster_user_login": null, + "source_broadcaster_user_name": null, + "chatter_user_id": "54492353", + "chatter_user_login": "keepcalmcallsoul", + "chatter_user_name": "keepcalmcallsoul", + "chatter_is_anonymous": false, + "color": "#8A2BE2", + "badges": [ { - "type": "text", - "text": "what do you think about the mclaren?", - "cheermote": null, - "emote": null, - "mention": null + "set_id": "subscriber", + "id": "9", + "info": "9" } - ] - }, - "notice_type": "resub", - "sub": null, - "resub": { - "cumulative_months": 9, - "duration_months": 9, - "streak_months": null, - "sub_tier": "1000", - "is_prime": false, - "is_gift": false, - "gifter_is_anonymous": null, - "gifter_user_id": null, - "gifter_user_name": null, - "gifter_user_login": null - }, - "sub_gift": null, - "community_sub_gift": null, - "gift_paid_upgrade": null, - "prime_paid_upgrade": null, - "pay_it_forward": null, - "raid": null, - "unraid": null, - "announcement": null, - "bits_badge_tier": null, - "charity_donation": null, - "shared_chat_sub": null, - "shared_chat_resub": null, - "shared_chat_sub_gift": null, - "shared_chat_community_sub_gift": null, - "shared_chat_gift_paid_upgrade": null, - "shared_chat_prime_paid_upgrade": null, - "shared_chat_pay_it_forward": null, - "shared_chat_raid": null, - "shared_chat_announcement": null + ], + "source_badges": null, + "system_message": "keepcalmcallsoul subscribed at Tier 1. They've subscribed for 9 months!", + "message_id": "d6deea58-ce6e-4217-905d-67670369b628", + "source_message_id": null, + "message": { + "text": "what do you think about the mclaren?", + "fragments": [ + { + "type": "text", + "text": "what do you think about the mclaren?", + "cheermote": null, + "emote": null, + "mention": null + } + ] + }, + "notice_type": "resub", + "sub": null, + "resub": { + "cumulative_months": 9, + "duration_months": 9, + "streak_months": null, + "sub_tier": "1000", + "is_prime": false, + "is_gift": false, + "gifter_is_anonymous": null, + "gifter_user_id": null, + "gifter_user_name": null, + "gifter_user_login": null + }, + "sub_gift": null, + "community_sub_gift": null, + "gift_paid_upgrade": null, + "prime_paid_upgrade": null, + "pay_it_forward": null, + "raid": null, + "unraid": null, + "announcement": null, + "bits_badge_tier": null, + "charity_donation": null, + "shared_chat_sub": null, + "shared_chat_resub": null, + "shared_chat_sub_gift": null, + "shared_chat_community_sub_gift": null, + "shared_chat_gift_paid_upgrade": null, + "shared_chat_prime_paid_upgrade": null, + "shared_chat_pay_it_forward": null, + "shared_chat_raid": null, + "shared_chat_announcement": null + } } - } -}, -{ - "metadata": { - "message_id": "andpZpHdH68c9LktLSFNbxmK2O0HT0430OD61ytoXXA=", - "message_type": "notification", - "message_timestamp": "2024-09-02T17:13:31.221692812Z", - "subscription_type": "channel.chat.notification", - "subscription_version": "1" }, - "payload": { - "subscription": { - "id": "8bfaa271-dacf-45ad-9252-82961b8e4558", - "status": "enabled", - "type": "channel.chat.notification", - "version": "1", - "condition": { - "broadcaster_user_id": "171093413", - "user_id": "54096715" - }, - "transport": { - "method": "websocket", - "session_id": "AgoQZGprB1c1S96FME9t0ZuU4hIGY2VsbC1j" - }, - "created_at": "2024-09-02T16:42:14.02277498Z", - "cost": 0 + { + "metadata": { + "message_id": "andpZpHdH68c9LktLSFNbxmK2O0HT0430OD61ytoXXA=", + "message_type": "notification", + "message_timestamp": "2024-09-02T17:13:31.221692812Z", + "subscription_type": "channel.chat.notification", + "subscription_version": "1" }, - "event": { - "broadcaster_user_id": "171093413", - "broadcaster_user_login": "pablogz205", - "broadcaster_user_name": "PabloGz205", - "source_broadcaster_user_id": null, - "source_broadcaster_user_login": null, - "source_broadcaster_user_name": null, - "chatter_user_id": "54096715", - "chatter_user_login": "deleterr", - "chatter_user_name": "Deleterr", - "chatter_is_anonymous": false, - "color": "#28D74B", - "badges": [ - { - "set_id": "moderator", - "id": "1", - "info": "" + "payload": { + "subscription": { + "id": "8bfaa271-dacf-45ad-9252-82961b8e4558", + "status": "enabled", + "type": "channel.chat.notification", + "version": "1", + "condition": { + "broadcaster_user_id": "171093413", + "user_id": "54096715" }, - { - "set_id": "subscriber", - "id": "9", - "info": "9" + "transport": { + "method": "websocket", + "session_id": "AgoQZGprB1c1S96FME9t0ZuU4hIGY2VsbC1j" }, - { - "set_id": "sub-gifter", - "id": "10", - "info": "" - } - ], - "source_badges": null, - "system_message": "Deleterr gifted a Tier 1 sub to JmsTV! They have given 11 Gift Subs in the channel!", - "message_id": "0001ee46-e128-4435-848c-ad24401abe86", - "source_message_id": null, - "message": { - "text": "", - "fragments": [] - }, - "notice_type": "sub_gift", - "sub": null, - "resub": null, - "sub_gift": { - "duration_months": 1, - "cumulative_total": 11, - "recipient_user_id": "108600558", - "recipient_user_name": "JmsTV", - "recipient_user_login": "jmstv", - "sub_tier": "1000", - "community_gift_id": null + "created_at": "2024-09-02T16:42:14.02277498Z", + "cost": 0 }, - "community_sub_gift": null, - "gift_paid_upgrade": null, - "prime_paid_upgrade": null, - "pay_it_forward": null, - "raid": null, - "unraid": null, - "announcement": null, - "bits_badge_tier": null, - "charity_donation": null, - "shared_chat_sub": null, - "shared_chat_resub": null, - "shared_chat_sub_gift": null, - "shared_chat_community_sub_gift": null, - "shared_chat_gift_paid_upgrade": null, - "shared_chat_prime_paid_upgrade": null, - "shared_chat_pay_it_forward": null, - "shared_chat_raid": null, - "shared_chat_announcement": null + "event": { + "broadcaster_user_id": "171093413", + "broadcaster_user_login": "pablogz205", + "broadcaster_user_name": "PabloGz205", + "source_broadcaster_user_id": null, + "source_broadcaster_user_login": null, + "source_broadcaster_user_name": null, + "chatter_user_id": "54096715", + "chatter_user_login": "deleterr", + "chatter_user_name": "Deleterr", + "chatter_is_anonymous": false, + "color": "#28D74B", + "badges": [ + { + "set_id": "moderator", + "id": "1", + "info": "" + }, + { + "set_id": "subscriber", + "id": "9", + "info": "9" + }, + { + "set_id": "sub-gifter", + "id": "10", + "info": "" + } + ], + "source_badges": null, + "system_message": "Deleterr gifted a Tier 1 sub to JmsTV! They have given 11 Gift Subs in the channel!", + "message_id": "0001ee46-e128-4435-848c-ad24401abe86", + "source_message_id": null, + "message": { + "text": "", + "fragments": [] + }, + "notice_type": "sub_gift", + "sub": null, + "resub": null, + "sub_gift": { + "duration_months": 1, + "cumulative_total": 11, + "recipient_user_id": "108600558", + "recipient_user_name": "JmsTV", + "recipient_user_login": "jmstv", + "sub_tier": "1000", + "community_gift_id": null + }, + "community_sub_gift": null, + "gift_paid_upgrade": null, + "prime_paid_upgrade": null, + "pay_it_forward": null, + "raid": null, + "unraid": null, + "announcement": null, + "bits_badge_tier": null, + "charity_donation": null, + "shared_chat_sub": null, + "shared_chat_resub": null, + "shared_chat_sub_gift": null, + "shared_chat_community_sub_gift": null, + "shared_chat_gift_paid_upgrade": null, + "shared_chat_prime_paid_upgrade": null, + "shared_chat_pay_it_forward": null, + "shared_chat_raid": null, + "shared_chat_announcement": null + } } - } -}]; - -setInterval(() => { - - - for(let message of messages) { + }]; - // random chance to process message - if (Math.random() > 0.25) { - continue; - } + setInterval(() => { + + for(let message of messages) { - if (message.metadata.message_type != 'session_keepalive') { - console.log(message); - } + // random chance to process message + if (Math.random() > 0.25) { + continue; + } - switch (message.metadata.message_type) { - case 'session_welcome': - if (i == 0) { - subscribe_to_events(message.payload.session.id); - } else { - ws[i - 1].close(); + if (message.metadata.message_type != 'session_keepalive') { + console.log(message); } - - break; - case 'session_reconnect': - i++; - ws[i] = new WebSocket(message.payload.session.reconnect_url); - break; - case 'notification': - let eventType = message.payload.event.notice_type; + switch (message.metadata.message_type) { + case 'session_welcome': + if (i == 0) { + subscribe_to_events(message.payload.session.id); + } else { + ws[i - 1].close(); + } + + break; + case 'session_reconnect': + i++; + ws[i] = new WebSocket(message.payload.session.reconnect_url); + break; + case 'notification': + + let eventType = message.payload.event.notice_type; + + let notification = { + id: message.metadata.message_id, + user: message.payload.event.chatter_user_name, + text: message.payload.event.message.text ?? '' + }; + + // Shared properties + if (['sub', 'resub', 'sub_gift', 'community_sub_gift'].includes(eventType)) { + notification.tier = message.payload.event[eventType].sub_tier / 1000; + } - let notification = { - id: message.metadata.message_id, - user: message.payload.event.chatter_user_name, - text: message.payload.event.message.text ?? '', - type: message.payload.event.notice_type - }; - - // Shared properties - if (['sub', 'resub', 'sub_gift', 'community_sub_gift'].includes(eventType)) { - notification.tier = message.payload.event[eventType].sub_tier / 1000; - } + if (['sub', 'resub'].includes(eventType)) { + notification.is_prime = message.payload.event[eventType].is_prime; + notification.color = '-green-600'; + } - if (['sub', 'resub'].includes(eventType)) { - notification.is_prime = message.payload.event[eventType].is_prime; - notification.color = '-green-600'; - } + if (['sub_gift', 'community_sub_gift'].includes(eventType)) { + notification.user_total = message.payload.event[eventType].cumulative_total ?? 0; + notification.color = '-orange-600'; + } - if (['sub_gift', 'community_sub_gift'].includes(eventType)) { - notification.user_total = message.payload.event[eventType].cumulative_total ?? 0; - notification.color = '-orange-600'; - } + // Specific properties + switch (message.payload.event.notice_type) { + case 'sub': + notification.nice_name = 'Sub'; + notification.tts = `${notification.user} subbed at tier ${notification.tier}: ${notification.text}`; + break; + case 'resub': + notification.nice_name = 'ReSub'; + notification.months = message.payload.event[eventType].cumulative_months; + notification.tts = `${notification.user} resubbed at tier ${notification.tier}, total ${notification.months} months: ${notification.text}`; + break; + case 'sub_gift': + notification.nice_name = 'Gift x1'; + notification.recipient = message.payload.event[eventType].recipient_user_name; + notification.exclude = message.payload.event[eventType].community_gift_id ? true : false; + notification.tts = `${notification.user} gifted a tier ${notification.tier} sub to ${notification.recipient}`; + break; + case 'community_sub_gift': + notification.nice_name = 'Gift x' + message.payload.event[eventType].total; + notification.text = 'Gifted ' + message.payload.event[eventType].total + ' subs'; + notification.tts = `${notification.user} gifted ${message.payload.event[eventType].total} subs`; + break; + case 'raid': + notification.color = '-purple-600'; + notification.nice_name = 'Raid x' + message.payload.event[eventType].viewer_count; + notification.raider = message.payload.event[eventType].user_name; + notification.tts = `${notification.raider} raided with ${message.payload.event[eventType].viewer_count} viewers`; + break; + } - // Specific properties - switch (message.payload.event.notice_type) { - case 'sub': - notification.nice_name = 'Sub'; - notification.tts = `${notification.user} subbed at tier ${notification.tier}: ${notification.text}`; - break; - case 'resub': - notification.nice_name = 'ReSub'; - notification.months = message.payload.event[eventType].cumulative_months; - notification.tts = `${notification.user} resubbed at tier ${notification.tier}, total ${notification.months} months: ${notification.text}`; - break; - case 'sub_gift': - notification.nice_name = 'Gift x1'; - notification.recipient = message.payload.event[eventType].recipient_user_name; - notification.exclude = message.payload.event[eventType].community_gift_id ? true : false; - notification.tts = `${notification.user} gifted a tier ${notification.tier} sub to ${notification.recipient}`; - break; - case 'community_sub_gift': - notification.type = 'sub_gift'; - notification.nice_name = 'Gift x' + message.payload.event[eventType].total; - notification.text = 'Gifted ' + message.payload.event[eventType].total + ' subs'; - notification.tts = `${notification.user} gifted ${message.payload.event[eventType].total} subs`; - break; - case 'raid': - notification.color = '-purple-600'; - notification.nice_name = 'Raid x' + message.payload.event[eventType].viewer_count; - notification.raider = message.payload.event[eventType].user_name; - notification.tts = `${notification.raider} raided with ${message.payload.event[eventType].viewer_count} viewers`; - break; - } + if (!notification.exclude) { + addNotification(notification); + } - if (!notification.exclude) { - addNotification(notification); + break; } - - break; } - } -}, 3000); - + }, 3000); +}