diff --git a/src/controllers/users.js b/src/controllers/users.js index ab0edaef..4f97a5ea 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -4,7 +4,6 @@ import { UserNotFoundError, UserNotInWaitlistError, } from '../errors/index.js'; -import skipIfCurrentDJ from '../utils/skipIfCurrentDJ.js'; import getOffsetPagination from '../utils/getOffsetPagination.js'; import toItemResponse from '../utils/toItemResponse.js'; import toListResponse from '../utils/toListResponse.js'; @@ -190,7 +189,14 @@ async function changeAvatar() { * @param {UserID} userID */ async function disconnectUser(uw, userID) { - await skipIfCurrentDJ(uw, userID); + try { + await uw.booth.removeUser(userID); + } catch (err) { + // It's expected that the user would not be in the waitlist + if (!(err instanceof UserNotInWaitlistError)) { + throw err; + } + } try { await uw.waitlist.removeUser(userID); diff --git a/src/plugins/booth.js b/src/plugins/booth.js index 0fe67280..56a1d19f 100644 --- a/src/plugins/booth.js +++ b/src/plugins/booth.js @@ -1,6 +1,6 @@ import Mutex from 'p-mutex'; import { sql } from 'kysely'; -import { EmptyPlaylistError, PlaylistItemNotFoundError } from '../errors/index.js'; +import { EmptyPlaylistError, PlaylistItemNotFoundError, UserNotInWaitlistError } from '../errors/index.js'; import routes from '../routes/booth.js'; import { randomUUID } from 'node:crypto'; import { fromJson, jsonb, jsonGroupArray } from '../utils/sqlite.js'; @@ -499,6 +499,19 @@ class Booth { } return null; } + + /** + * Remove the given user from the booth. Throw an error if the user is not playing. + * + * @param {UserID} userID + */ + async removeUser(userID) { + const currentDJ = await this.#uw.keyv.get(KEY_CURRENT_DJ_ID); + if (userID !== currentDJ) { + throw new UserNotInWaitlistError({ id: userID }); + } + await this.advance({ remove: true }); + } } /** diff --git a/src/utils/skipIfCurrentDJ.js b/src/utils/skipIfCurrentDJ.js deleted file mode 100644 index bd72c0c2..00000000 --- a/src/utils/skipIfCurrentDJ.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {import('../Uwave.js').default} uw - */ -function getCurrentDJ(uw) { - return uw.keyv.get('booth:currentDJ'); -} - -/** - * @param {import('../Uwave.js').default} uw - * @param {import('../schema.js').UserID} userID - */ -async function skipIfCurrentDJ(uw, userID) { - const currentDJ = await getCurrentDJ(uw); - if (userID.toString() === currentDJ) { - await uw.booth.advance({ remove: true }); - } -} - -export default skipIfCurrentDJ;