From aab4433a55d6f9f93dc25fe2ae01d3e0cb8a951c Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Sun, 15 Oct 2023 19:39:36 +0200 Subject: [PATCH] Add oauth token to websocket connection --- .env.EXAMPLE | 1 + README.md | 7 +++++++ src/lib/server/timeline.ts | 9 ++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.env.EXAMPLE b/.env.EXAMPLE index 650ec3e..09b67e4 100644 --- a/.env.EXAMPLE +++ b/.env.EXAMPLE @@ -2,6 +2,7 @@ HASHTAG_FILTER = ichlausche,music,musik,nowplaying,tunetuesday,nowlistening YOUTUBE_API_KEY = CHANGE_ME ODESLI_API_KEY = CHANGE_ME MASTODON_INSTANCE = 'metalhead.club' +MASTODON_ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN_HERE' BASE_URL = 'https://moshingmammut.phlaym.net' VERBOSE = false IGNORE_USERS = @moshhead@metalhead.club diff --git a/README.md b/README.md index dede4f9..a29a207 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,13 @@ because the API is the only way to check if a YouTube link leads to music or som If `ODESLI_API_KEY` is unset, your rate limit to the song.link API will be lower. +Add `MASTODON_ACCESS_TOKEN` as well, see [Creating our application + +](https://docs.joinmastodon.org/client/token/#app) in the Mastodon documentation. +`read:statuses` is the only required scope. An access token will be displayed in your settings. Use that! + +There are currently no plans to implement an actual authentication flow. + Run `npm run build` and copy the output folder, usually `build` to `$APP_DIR` on your server. #### On your server again diff --git a/src/lib/server/timeline.ts b/src/lib/server/timeline.ts index 784332b..54f040d 100644 --- a/src/lib/server/timeline.ts +++ b/src/lib/server/timeline.ts @@ -1,5 +1,6 @@ import { HASHTAG_FILTER, + MASTODON_ACCESS_TOKEN, MASTODON_INSTANCE, ODESLI_API_KEY, YOUTUBE_API_KEY @@ -352,10 +353,11 @@ export class TimelineReader { } private startWebsocket() { - const socket = new WebSocket(`wss://${MASTODON_INSTANCE}/api/v1/streaming`); + const socket = new WebSocket( + `wss://${MASTODON_INSTANCE}/api/v1/streaming?type=subscribe&stream=public:local&access_token=${MASTODON_ACCESS_TOKEN}` + ); socket.onopen = () => { log.log('Connected to WS'); - socket.send('{ "type": "subscribe", "stream": "public:local"}'); }; socket.onmessage = async (event) => { try { @@ -392,7 +394,8 @@ export class TimelineReader { }; socket.onclose = (event) => { log.warn( - `Websocket connection to ${MASTODON_INSTANCE} closed. Code: ${event.code}, reason: '${event.reason}'` + `Websocket connection to ${MASTODON_INSTANCE} closed. Code: ${event.code}, reason: '${event.reason}'`, + event ); setTimeout(() => { log.info(`Attempting to reconenct to WS`);