Skip to content

Commit

Permalink
/notes/:note.jsonに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
tar-bin committed Dec 21, 2024
1 parent ba6f8e2 commit fe1e003
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
7 changes: 3 additions & 4 deletions packages/backend/src/server/api/ApiCallService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class ApiCallService implements OnApplicationShutdown {
return;
}
this.authenticateService.authenticate(token).then(([user, app]) => {
this.call(endpoint, user, app, body, null, request, reply).then((res) => {
this.call(endpoint, user, app, body, null, request).then((res) => {
if (request.method === 'GET' && endpoint.meta.cacheSec && !token && !user) {
reply.header('Cache-Control', `public, max-age=${endpoint.meta.cacheSec}`);
}
Expand Down Expand Up @@ -168,7 +168,7 @@ export class ApiCallService implements OnApplicationShutdown {
this.call(endpoint, user, app, fields, {
name: multipartData.filename,
path: path,
}, request, reply).then((res) => {
}, request).then((res) => {
this.send(reply, res);
}).catch((err: ApiError) => {
this.#sendApiError(reply, err);
Expand Down Expand Up @@ -239,7 +239,6 @@ export class ApiCallService implements OnApplicationShutdown {
path: string;
} | null,
request: FastifyRequest<{ Body: Record<string, unknown> | undefined, Querystring: Record<string, unknown> }>,
reply: FastifyReply,
) {
const isSecure = user != null && token == null;

Expand Down Expand Up @@ -373,7 +372,7 @@ export class ApiCallService implements OnApplicationShutdown {
}

// API invoking
return await ep.exec(data, user, token, reply, file, request.ip, request.headers).catch((err: Error) => {
return await ep.exec(data, user, token, file, request.ip, request.headers).catch((err: Error) => {
if (err instanceof ApiError || err instanceof AuthenticationError) {
throw err;
} else if (err instanceof IdentifiableError) {
Expand Down
9 changes: 4 additions & 5 deletions packages/backend/src/server/api/endpoint-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { MiLocalUser } from '@/models/User.js';
import type { MiAccessToken } from '@/models/AccessToken.js';
import { ApiError } from './error.js';
import type { IEndpointMeta } from './endpoints.js';
import type { FastifyReply } from 'fastify';

const Ajv = _Ajv.default;

Expand Down Expand Up @@ -40,16 +39,16 @@ type File = {

// TODO: paramsの型をT['params']のスキーマ定義から推論する
type Executor<T extends IEndpointMeta, Ps extends Schema> =
(params: SchemaType<Ps>, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, reply: FastifyReply, file?: File, cleanup?: () => any, ip?: string | null, headers?: Record<string, string> | null) =>
(params: SchemaType<Ps>, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, cleanup?: () => any, ip?: string | null, headers?: Record<string, string> | null) =>
Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>;

export abstract class Endpoint<T extends IEndpointMeta, Ps extends Schema> {
public exec: (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, reply: FastifyReply, file?: File, ip?: string | null, headers?: Record<string, string> | null) => Promise<any>;
public exec: (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, ip?: string | null, headers?: Record<string, string> | null) => Promise<any>;

constructor(meta: T, paramDef: Ps, cb: Executor<T, Ps>) {
const validate = ajv.compile(paramDef);

this.exec = (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, reply: FastifyReply, file?: File, ip?: string | null, headers?: Record<string, string> | null) => {
this.exec = (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, ip?: string | null, headers?: Record<string, string> | null) => {
let cleanup: undefined | (() => void) = undefined;

if (meta.requireFile) {
Expand Down Expand Up @@ -80,7 +79,7 @@ export abstract class Endpoint<T extends IEndpointMeta, Ps extends Schema> {
return Promise.reject(err);
}

return cb(params as SchemaType<Ps>, user, token, reply, file, cleanup, ip, headers);
return cb(params as SchemaType<Ps>, user, token, file, cleanup, ip, headers);
};
}
}
9 changes: 1 addition & 8 deletions packages/backend/src/server/api/endpoints/notes/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const meta = {
tags: ['notes'],

requireCredential: false,
allowGet: true,

res: {
type: 'object',
Expand Down Expand Up @@ -44,18 +43,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private noteEntityService: NoteEntityService,
private getterService: GetterService,
) {
super(meta, paramDef, async (ps, me, token, reply) => {
super(meta, paramDef, async (ps, me) => {
const note = await this.getterService.getNote(ps.noteId).catch(err => {
if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
throw err;
});

if (["followers", "specified"].includes(note.visibility)) {
reply.header('Cache-Control', 'private, max-age=600');
} else {
reply.header('Cache-Control', 'public');
}

return await this.noteEntityService.pack(note, me, {
detail: true,
});
Expand Down
19 changes: 19 additions & 0 deletions packages/backend/src/server/web/ClientServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,25 @@ export class ClientServerService {
}
});

fastify.get<{ Params: { note: string; } }>('/notes/:note.json', async (request, reply) => {
const note = await this.notesRepository.findOneBy({
id: request.params.note,
visibility: In(['public', 'home']),
});
if (note) {
try {
const _note = await this.noteEntityService.pack(note, null);
reply.header('Content-Type', 'application/json; charset=utf-8');
reply.header('Cache-Control', 'public');
return reply.send(_note);
} catch (err) {
return reply.status(500).send({ error: 'Internal Server Error' });
}
} else {
return reply.status(404).send({ error: 'Data not found' });
}
});

// Page
fastify.get<{ Params: { user: string; page: string; } }>('/@:user/pages/:page', async (request, reply) => {
const { username, host } = Acct.parse(request.params.user);
Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/components/MkTimeline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ let tlNotesCount = 0;
async function prepend(data) {
let note = data;
if (data.idOnly) {
note = await misskeyApiGet("notes/show", { noteId: data.id });
const res = await fetch(`/notes/${data.id}.json`);
if (!res.ok) return;
note = await res.json();
}

if (tlComponent.value == null) return;
Expand Down

0 comments on commit fe1e003

Please sign in to comment.