Skip to content

Commit

Permalink
Allow disabling auto-leave
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Dec 29, 2023
1 parent fdc73c5 commit c2cea59
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/controllers/booth.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ async function skipBooth(req) {
return toItemResponse({});
}

/** @typedef {{ userID: string }} LeaveBoothBody */
/** @typedef {{ userID: string, autoLeave: boolean }} LeaveBoothBody */

/**
* @type {import('../types.js').AuthenticatedController<{}, {}, LeaveBoothBody>}
*/
async function leaveBooth(req) {
const { user: self } = req;
const { userID } = req.body;
const { userID, autoLeave } = req.body;
const { acl, booth, users } = req.uwave;

const skippingSelf = userID === self.id;

if (skippingSelf) {
await booth.setRemoveAfterCurrentPlay(self);
return toItemResponse({});
const value = await booth.setRemoveAfterCurrentPlay(self, autoLeave);
return toItemResponse({ autoLeave: value });
}

if (!await acl.isAllowed(self, 'booth.skip.other')) {
Expand All @@ -158,8 +158,8 @@ async function leaveBooth(req) {
throw new UserNotFoundError({ id: userID });
}

await booth.setRemoveAfterCurrentPlay(user);
return toItemResponse({});
const value = await booth.setRemoveAfterCurrentPlay(user, autoLeave);
return toItemResponse({ autoLeave: value });
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/plugins/booth.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ const REMOVE_AFTER_CURRENT_PLAY_SCRIPT = {
local k_dj = KEYS[1]
local k_remove = KEYS[2]
local user_id = ARGV[1]
local value = ARGV[2]
local current_dj_id = redis.call('GET', k_dj)
if current_dj_id == user_id then
return redis.call('SET', k_remove, 'true')
if value == 'true' then
redis.call('SET', k_remove, 'true')
return 1
else
redis.call('DEL', k_remove)
return 0
end
else
return { err = 'You are not currently playing' }
return redis.error_reply('You are not currently playing')
end
`,
};
Expand Down Expand Up @@ -449,9 +456,15 @@ class Booth {

/**
* @param {User} user
* @param {boolean} remove
*/
async setRemoveAfterCurrentPlay(user) {
await this.#uw.redis['uw:removeAfterCurrentPlay'](...REMOVE_AFTER_CURRENT_PLAY_SCRIPT.keys, user._id.toString());
async setRemoveAfterCurrentPlay(user, remove) {
const newValue = await this.#uw.redis['uw:removeAfterCurrentPlay'](
...REMOVE_AFTER_CURRENT_PLAY_SCRIPT.keys,
user._id.toString(),
remove,
);
return newValue === 1;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/routes/booth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function boothRoutes() {
schema(validations.skipBooth),
route(controller.skipBooth),
)
// POST /booth/leave - Auto-remove the current DJ on the next advance.
.post(
// PUT /booth/leave - Auto-remove the current DJ on the next advance.
.put(
'/leave',
protect(),
schema(validations.leaveBooth),
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ declare module 'ioredis' {
/** Run the move-waitlist script, declared in src/plugins/waitlist.js. */
'uw:moveWaitlist'(...args: [...keys: string[], userId: string, position: number]): Promise<string[]>;
/** Run the remove-after-current-play script, declared in src/plugins/booth.js. */
'uw:removeAfterCurrentPlay'(...args: [...keys: string[], userId: string]): Promise<void>;
'uw:removeAfterCurrentPlay'(...args: [...keys: string[], userId: string, remove: boolean]): Promise<0 | 1>;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const leaveBooth = /** @type {const} */ ({
type: 'object',
properties: {
userID: { $ref: 'https://ns.u-wave.net/schemas/definitions.json#/definitions/ObjectID' },
autoLeave: { type: 'boolean', default: true },
},
},
});
Expand Down

0 comments on commit c2cea59

Please sign in to comment.