Skip to content

Commit b59b491

Browse files
Refactor skipIfCurrentDJ (#744)
Instead of a utility function, make it part of the booth plugin API.
1 parent 2dd6aa2 commit b59b491

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/controllers/users.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
UserNotFoundError,
55
UserNotInWaitlistError,
66
} from '../errors/index.js';
7-
import skipIfCurrentDJ from '../utils/skipIfCurrentDJ.js';
87
import getOffsetPagination from '../utils/getOffsetPagination.js';
98
import toItemResponse from '../utils/toItemResponse.js';
109
import toListResponse from '../utils/toListResponse.js';
@@ -190,7 +189,14 @@ async function changeAvatar() {
190189
* @param {UserID} userID
191190
*/
192191
async function disconnectUser(uw, userID) {
193-
await skipIfCurrentDJ(uw, userID);
192+
try {
193+
await uw.booth.removeUser(userID);
194+
} catch (err) {
195+
// It's expected that the user would not be in the waitlist
196+
if (!(err instanceof UserNotInWaitlistError)) {
197+
throw err;
198+
}
199+
}
194200

195201
try {
196202
await uw.waitlist.removeUser(userID);

src/plugins/booth.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Mutex from 'p-mutex';
22
import { sql } from 'kysely';
3-
import { EmptyPlaylistError, PlaylistItemNotFoundError } from '../errors/index.js';
3+
import { EmptyPlaylistError, PlaylistItemNotFoundError, UserNotInWaitlistError } from '../errors/index.js';
44
import routes from '../routes/booth.js';
55
import { randomUUID } from 'node:crypto';
66
import { fromJson, jsonb, jsonGroupArray } from '../utils/sqlite.js';
@@ -499,6 +499,19 @@ class Booth {
499499
}
500500
return null;
501501
}
502+
503+
/**
504+
* Remove the given user from the booth. Throw an error if the user is not playing.
505+
*
506+
* @param {UserID} userID
507+
*/
508+
async removeUser(userID) {
509+
const currentDJ = await this.#uw.keyv.get(KEY_CURRENT_DJ_ID);
510+
if (userID !== currentDJ) {
511+
throw new UserNotInWaitlistError({ id: userID });
512+
}
513+
await this.advance({ remove: true });
514+
}
502515
}
503516

504517
/**

src/utils/skipIfCurrentDJ.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)