Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/plugins/api-server/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
},
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/plugins/api-server/backend/routes/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand All @@ -300,6 +338,7 @@ export const register = (
app: HonoApp,
{ window }: BackendContext<APIServerConfig>,
songInfoGetter: () => SongInfo | undefined,
repeatModeGetter: () => RepeatMode | undefined,
) => {
const controller = getSongControls(window);

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/api-server/backend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading