feat(api-server): Add repeat mode and seek time API (#2630)

Co-authored-by: sent44 <sent44@root533.premium-rootserver.net>
Co-authored-by: JellyBrick <shlee1503@naver.com>
This commit is contained in:
Wasin Chintapanyakul
2024-12-25 06:41:44 +07:00
committed by GitHub
parent 9163b6f04b
commit 109e9f8166
3 changed files with 61 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import { registerAuth, registerControl } from './routes';
import { type APIServerConfig, AuthStrategy } from '../config';
import type { BackendType } from './types';
import type { RepeatMode } from '@/types/datahost-get-state';
export const backend = createBackend<BackendType, APIServerConfig>({
async start(ctx) {
@ -23,7 +24,14 @@ export const backend = createBackend<BackendType, APIServerConfig>({
this.songInfo = songInfo;
});
ctx.ipc.on('ytmd:player-api-loaded', () => ctx.ipc.send('ytmd:setup-time-changed-listener'));
ctx.ipc.on('ytmd:player-api-loaded', () =>
ctx.ipc.send('ytmd:setup-time-changed-listener'),
);
ctx.ipc.on(
'ytmd:repeat-changed',
(mode: RepeatMode) => (this.currentRepeatMode = mode),
);
this.run(config.hostname, config.port);
},
@ -75,7 +83,12 @@ export const backend = createBackend<BackendType, APIServerConfig>({
});
// routes
registerControl(this.app, ctx, () => this.songInfo);
registerControl(
this.app,
ctx,
() => this.songInfo,
() => this.currentRepeatMode,
);
registerAuth(this.app, ctx);
// swagger

View File

@ -15,6 +15,7 @@ import {
SetFullscreenSchema,
} from '../scheme';
import type { RepeatMode } from '@/types/datahost-get-state';
import type { SongInfo } from '@/providers/song-info';
import type { BackendContext } from '@/types/contexts';
import type { APIServerConfig } from '../../config';
@ -160,6 +161,24 @@ const routes = {
},
},
}),
repeatMode: createRoute({
method: 'get',
path: `/api/${API_VERSION}/repeat-mode`,
summary: 'get current repeat mode',
description: 'Get the current repeat mode (NONE, ALL, ONE)',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: z.object({
mode: z.enum(['ONE', 'NONE', 'ALL']).nullable(),
}),
},
},
},
},
}),
switchRepeat: createRoute({
method: 'post',
path: `/api/${API_VERSION}/switch-repeat`,
@ -275,6 +294,25 @@ const routes = {
},
},
}),
seekTime: createRoute({
method: 'get',
path: `/api/${API_VERSION}/seek-time`,
summary: 'get current play time and video duration',
description: 'Get current play time and video duration in seconds',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: z.object({
current: z.number().nullable().openapi({ example: 3 }),
duration: z.number().nullable().openapi({ example: 233 }),
}),
},
},
},
},
}),
songInfo: createRoute({
method: 'get',
path: `/api/${API_VERSION}/song-info`,
@ -300,6 +338,7 @@ export const register = (
app: HonoApp,
{ window }: BackendContext<APIServerConfig>,
songInfoGetter: () => SongInfo | undefined,
repeatModeGetter: () => RepeatMode | undefined,
) => {
const controller = getSongControls(window);
@ -365,6 +404,11 @@ export const register = (
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.repeatMode, (ctx) => {
ctx.status(200);
return ctx.json({ mode: repeatModeGetter() ?? null });
});
app.openapi(routes.switchRepeat, (ctx) => {
const { iteration } = ctx.req.valid('json');
controller.switchRepeat(iteration);

View File

@ -3,6 +3,7 @@ import { serve } from '@hono/node-server';
import type { BackendContext } from '@/types/contexts';
import type { SongInfo } from '@/providers/song-info';
import type { RepeatMode } from '@/types/datahost-get-state';
import type { APIServerConfig } from '../config';
export type HonoApp = Hono;
@ -11,6 +12,7 @@ export type BackendType = {
server?: ReturnType<typeof serve>;
oldConfig?: APIServerConfig;
songInfo?: SongInfo;
currentRepeatMode?: RepeatMode;
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
run: (hostname: string, port: number) => void;