feat(api-server): add absolute seek endpoint (#2748)

Co-authored-by: JellyBrick <shlee1503@naver.com>
This commit is contained in:
Franz DC
2024-12-25 07:46:51 +08:00
committed by GitHub
parent 65f4339fd1
commit 237dde9765
4 changed files with 42 additions and 20 deletions

View File

@ -8,6 +8,7 @@ import {
AuthHeadersSchema,
type ResponseSongInfo,
SongInfoSchema,
SeekSchema,
GoForwardScheme,
GoBackSchema,
SwitchRepeatSchema,
@ -103,7 +104,28 @@ const routes = {
},
},
}),
seekTo: createRoute({
method: 'post',
path: `/api/${API_VERSION}/seek-to`,
summary: 'seek',
description: 'Seek to a specific time in the current song',
request: {
headers: AuthHeadersSchema,
body: {
description: 'seconds to seek to',
content: {
'application/json': {
schema: SeekSchema,
},
},
},
},
responses: {
204: {
description: 'Success',
},
},
}),
goBack: createRoute({
method: 'post',
path: `/api/${API_VERSION}/go-back`,
@ -294,25 +316,6 @@ 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`,
@ -384,6 +387,13 @@ export const register = (
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.seekTo, (ctx) => {
const { seconds } = ctx.req.valid('json');
controller.seekTo(seconds);
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.goBack, (ctx) => {
const { seconds } = ctx.req.valid('json');
controller.goBack(seconds);

View File

@ -1,5 +1,6 @@
export * from './auth';
export * from './song-info';
export * from './seek';
export * from './go-back';
export * from './go-forward';
export * from './switch-repeat';

View File

@ -0,0 +1,5 @@
import { z } from '@hono/zod-openapi';
export const SeekSchema = z.object({
seconds: z.number(),
});

View File

@ -34,6 +34,12 @@ export default (win: BrowserWindow) => {
playPause: () => win.webContents.send('ytmd:toggle-play'),
like: () => win.webContents.send('ytmd:update-like', 'LIKE'),
dislike: () => win.webContents.send('ytmd:update-like', 'DISLIKE'),
seekTo: (seconds: ArgsType<number>) => {
const secondsNumber = parseNumberFromArgsType(seconds);
if (secondsNumber !== null) {
win.webContents.send('ytmd:seek-to', seconds);
}
},
goBack: (seconds: ArgsType<number>) => {
const secondsNumber = parseNumberFromArgsType(seconds);
if (secondsNumber !== null) {