Skip to content

Commit 109e9f8

Browse files
sent44sent44JellyBrick
authored
feat(api-server): Add repeat mode and seek time API (#2630)
Co-authored-by: sent44 <[email protected]> Co-authored-by: JellyBrick <[email protected]>
1 parent 9163b6f commit 109e9f8

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/plugins/api-server/backend/main.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { registerAuth, registerControl } from './routes';
1313
import { type APIServerConfig, AuthStrategy } from '../config';
1414

1515
import type { BackendType } from './types';
16+
import type { RepeatMode } from '@/types/datahost-get-state';
1617

1718
export const backend = createBackend<BackendType, APIServerConfig>({
1819
async start(ctx) {
@@ -23,7 +24,14 @@ export const backend = createBackend<BackendType, APIServerConfig>({
2324
this.songInfo = songInfo;
2425
});
2526

26-
ctx.ipc.on('ytmd:player-api-loaded', () => ctx.ipc.send('ytmd:setup-time-changed-listener'));
27+
ctx.ipc.on('ytmd:player-api-loaded', () =>
28+
ctx.ipc.send('ytmd:setup-time-changed-listener'),
29+
);
30+
31+
ctx.ipc.on(
32+
'ytmd:repeat-changed',
33+
(mode: RepeatMode) => (this.currentRepeatMode = mode),
34+
);
2735

2836
this.run(config.hostname, config.port);
2937
},
@@ -75,7 +83,12 @@ export const backend = createBackend<BackendType, APIServerConfig>({
7583
});
7684

7785
// routes
78-
registerControl(this.app, ctx, () => this.songInfo);
86+
registerControl(
87+
this.app,
88+
ctx,
89+
() => this.songInfo,
90+
() => this.currentRepeatMode,
91+
);
7992
registerAuth(this.app, ctx);
8093

8194
// swagger

src/plugins/api-server/backend/routes/control.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
SetFullscreenSchema,
1616
} from '../scheme';
1717

18+
import type { RepeatMode } from '@/types/datahost-get-state';
1819
import type { SongInfo } from '@/providers/song-info';
1920
import type { BackendContext } from '@/types/contexts';
2021
import type { APIServerConfig } from '../../config';
@@ -160,6 +161,24 @@ const routes = {
160161
},
161162
},
162163
}),
164+
repeatMode: createRoute({
165+
method: 'get',
166+
path: `/api/${API_VERSION}/repeat-mode`,
167+
summary: 'get current repeat mode',
168+
description: 'Get the current repeat mode (NONE, ALL, ONE)',
169+
responses: {
170+
200: {
171+
description: 'Success',
172+
content: {
173+
'application/json': {
174+
schema: z.object({
175+
mode: z.enum(['ONE', 'NONE', 'ALL']).nullable(),
176+
}),
177+
},
178+
},
179+
},
180+
},
181+
}),
163182
switchRepeat: createRoute({
164183
method: 'post',
165184
path: `/api/${API_VERSION}/switch-repeat`,
@@ -275,6 +294,25 @@ const routes = {
275294
},
276295
},
277296
}),
297+
seekTime: createRoute({
298+
method: 'get',
299+
path: `/api/${API_VERSION}/seek-time`,
300+
summary: 'get current play time and video duration',
301+
description: 'Get current play time and video duration in seconds',
302+
responses: {
303+
200: {
304+
description: 'Success',
305+
content: {
306+
'application/json': {
307+
schema: z.object({
308+
current: z.number().nullable().openapi({ example: 3 }),
309+
duration: z.number().nullable().openapi({ example: 233 }),
310+
}),
311+
},
312+
},
313+
},
314+
},
315+
}),
278316
songInfo: createRoute({
279317
method: 'get',
280318
path: `/api/${API_VERSION}/song-info`,
@@ -300,6 +338,7 @@ export const register = (
300338
app: HonoApp,
301339
{ window }: BackendContext<APIServerConfig>,
302340
songInfoGetter: () => SongInfo | undefined,
341+
repeatModeGetter: () => RepeatMode | undefined,
303342
) => {
304343
const controller = getSongControls(window);
305344

@@ -365,6 +404,11 @@ export const register = (
365404
ctx.status(204);
366405
return ctx.body(null);
367406
});
407+
408+
app.openapi(routes.repeatMode, (ctx) => {
409+
ctx.status(200);
410+
return ctx.json({ mode: repeatModeGetter() ?? null });
411+
});
368412
app.openapi(routes.switchRepeat, (ctx) => {
369413
const { iteration } = ctx.req.valid('json');
370414
controller.switchRepeat(iteration);

src/plugins/api-server/backend/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { serve } from '@hono/node-server';
33

44
import type { BackendContext } from '@/types/contexts';
55
import type { SongInfo } from '@/providers/song-info';
6+
import type { RepeatMode } from '@/types/datahost-get-state';
67
import type { APIServerConfig } from '../config';
78

89
export type HonoApp = Hono;
@@ -11,6 +12,7 @@ export type BackendType = {
1112
server?: ReturnType<typeof serve>;
1213
oldConfig?: APIServerConfig;
1314
songInfo?: SongInfo;
15+
currentRepeatMode?: RepeatMode;
1416

1517
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
1618
run: (hostname: string, port: number) => void;

0 commit comments

Comments
 (0)