diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index df146b596..dd99506b1 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -352,14 +352,26 @@ export function apiStatusMastodon(router: Router): void { router.post<{ Params: { id: string } }>( "/v1/statuses/:id/reblog", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.reblogStatus( - convertId(ctx.params.id, IdType.IceshrimpId), - ); - ctx.body = convertStatus(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const id = convertId(ctx.params.id, IdType.IceshrimpId); + const note = await getNote(id, user).catch(_ => null); + + if (note === null) { + ctx.status = 404; + return; + } + + ctx.body = await NoteHelpers.reblogNote(note, user) + .then(p => NoteConverter.encode(p, user)) + .then(p => convertStatus(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -371,14 +383,26 @@ export function apiStatusMastodon(router: Router): void { router.post<{ Params: { id: string } }>( "/v1/statuses/:id/unreblog", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.unreblogStatus( - convertId(ctx.params.id, IdType.IceshrimpId), - ); - ctx.body = convertStatus(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const id = convertId(ctx.params.id, IdType.IceshrimpId); + const note = await getNote(id, user).catch(_ => null); + + if (note === null) { + ctx.status = 404; + return; + } + + ctx.body = await NoteHelpers.unreblogNote(note, user) + .then(p => NoteConverter.encode(p, user)) + .then(p => convertStatus(p)); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/helpers/note.ts b/packages/backend/src/server/api/mastodon/helpers/note.ts index 4d8e70b1c..4e383ee2c 100644 --- a/packages/backend/src/server/api/mastodon/helpers/note.ts +++ b/packages/backend/src/server/api/mastodon/helpers/note.ts @@ -1,5 +1,5 @@ import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; -import { Metas, Notes } from "@/models/index.js"; +import { Metas, Notes, Users } from "@/models/index.js"; import { generateVisibilityQuery } from "@/server/api/common/generate-visibility-query.js"; import { generateMutedUserQuery } from "@/server/api/common/generate-muted-user-query.js"; import { generateBlockedUserQuery } from "@/server/api/common/generate-block-query.js"; @@ -8,6 +8,8 @@ import { ILocalUser } from "@/models/entities/user.js"; import { getNote } from "@/server/api/common/getters.js"; import createReaction from "@/services/note/reaction/create.js"; import deleteReaction from "@/services/note/reaction/delete.js"; +import createNote from "@/services/note/create.js"; +import deleteNote from "@/services/note/delete.js"; export class NoteHelpers { public static async getDefaultReaction(): Promise { @@ -27,6 +29,25 @@ export class NoteHelpers { return getNote(note.id, user); } + public static async reblogNote(note: Note, user: ILocalUser): Promise { + const data = { + createdAt: new Date(), + files: [], + renote: note + }; + return await createNote(user, data); + } + + public static async unreblogNote(note: Note, user: ILocalUser): Promise { + return Notes.findBy({ + userId: user.id, + renoteId: note.id, + }) + .then(p => p.map(n => deleteNote(user, n))) + .then(p => Promise.all(p)) + .then(_ => getNote(note.id, user)); + } + public static async getNoteDescendants(note: Note | string, user: ILocalUser | null, limit: number = 10, depth: number = 2): Promise { const noteId = typeof note === "string" ? note : note.id; const query = makePaginationQuery(Notes.createQueryBuilder("note"))