feat(api-server): add endpoint to get volume state (#2813)

* add volume getter

* format

* add volume route
This commit is contained in:
Johannes7k75
2025-01-10 04:25:32 +01:00
committed by GitHub
parent fdc798ad87
commit e61757a7fc
3 changed files with 31 additions and 0 deletions

View File

@ -27,6 +27,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
ctx.ipc.on('ytmd:player-api-loaded', () => { ctx.ipc.on('ytmd:player-api-loaded', () => {
ctx.ipc.send('ytmd:setup-time-changed-listener'); ctx.ipc.send('ytmd:setup-time-changed-listener');
ctx.ipc.send('ytmd:setup-repeat-changed-listener'); ctx.ipc.send('ytmd:setup-repeat-changed-listener');
ctx.ipc.send('ytmd:setup-volume-changed-listener');
}); });
ctx.ipc.on( ctx.ipc.on(
@ -34,6 +35,11 @@ export const backend = createBackend<BackendType, APIServerConfig>({
(mode: RepeatMode) => (this.currentRepeatMode = mode), (mode: RepeatMode) => (this.currentRepeatMode = mode),
); );
ctx.ipc.on(
'ytmd:volume-changed',
(newVolume: number) => (this.volume = newVolume),
);
this.run(config.hostname, config.port); this.run(config.hostname, config.port);
}, },
stop() { stop() {
@ -95,6 +101,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
ctx, ctx,
() => this.songInfo, () => this.songInfo,
() => this.currentRepeatMode, () => this.currentRepeatMode,
() => this.volume,
); );
registerAuth(this.app, ctx); registerAuth(this.app, ctx);

View File

@ -245,6 +245,24 @@ const routes = {
}, },
}, },
}), }),
getVolumeState: createRoute({
method: 'get',
path: `/api/${API_VERSION}/volume`,
summary: 'get volume state',
description: 'Get the current volume state of the player',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: z.object({
state: z.number(),
}),
},
},
},
},
}),
setFullscreen: createRoute({ setFullscreen: createRoute({
method: 'post', method: 'post',
path: `/api/${API_VERSION}/fullscreen`, path: `/api/${API_VERSION}/fullscreen`,
@ -496,6 +514,7 @@ export const register = (
{ window }: BackendContext<APIServerConfig>, { window }: BackendContext<APIServerConfig>,
songInfoGetter: () => SongInfo | undefined, songInfoGetter: () => SongInfo | undefined,
repeatModeGetter: () => RepeatMode | undefined, repeatModeGetter: () => RepeatMode | undefined,
volumeGetter: () => number | undefined,
) => { ) => {
const controller = getSongControls(window); const controller = getSongControls(window);
@ -587,6 +606,10 @@ export const register = (
ctx.status(204); ctx.status(204);
return ctx.body(null); return ctx.body(null);
}); });
app.openapi(routes.getVolumeState, (ctx) => {
ctx.status(200);
return ctx.json({ state: volumeGetter() ?? 0 });
});
app.openapi(routes.setFullscreen, (ctx) => { app.openapi(routes.setFullscreen, (ctx) => {
const { state } = ctx.req.valid('json'); const { state } = ctx.req.valid('json');
controller.setFullscreen(state); controller.setFullscreen(state);

View File

@ -13,6 +13,7 @@ export type BackendType = {
oldConfig?: APIServerConfig; oldConfig?: APIServerConfig;
songInfo?: SongInfo; songInfo?: SongInfo;
currentRepeatMode?: RepeatMode; currentRepeatMode?: RepeatMode;
volume?: number;
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>; init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
run: (hostname: string, port: number) => void; run: (hostname: string, port: number) => void;