diff --git a/src/plugins/api-server/backend/main.ts b/src/plugins/api-server/backend/main.ts index b1c011d4..40cd145e 100644 --- a/src/plugins/api-server/backend/main.ts +++ b/src/plugins/api-server/backend/main.ts @@ -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({ async start(ctx) { @@ -23,7 +24,14 @@ export const backend = createBackend({ 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({ }); // routes - registerControl(this.app, ctx, () => this.songInfo); + registerControl( + this.app, + ctx, + () => this.songInfo, + () => this.currentRepeatMode, + ); registerAuth(this.app, ctx); // swagger diff --git a/src/plugins/api-server/backend/routes/control.ts b/src/plugins/api-server/backend/routes/control.ts index ad395adc..42510e5d 100644 --- a/src/plugins/api-server/backend/routes/control.ts +++ b/src/plugins/api-server/backend/routes/control.ts @@ -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, 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); diff --git a/src/plugins/api-server/backend/types.ts b/src/plugins/api-server/backend/types.ts index efa847bc..77d66cd2 100644 --- a/src/plugins/api-server/backend/types.ts +++ b/src/plugins/api-server/backend/types.ts @@ -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; oldConfig?: APIServerConfig; songInfo?: SongInfo; + currentRepeatMode?: RepeatMode; init: (ctx: BackendContext) => Promise; run: (hostname: string, port: number) => void;