[mastodon-client] POST /statuses/:id/reblog, /statuses/:id/unreblog

This commit is contained in:
Laura Hausmann 2023-09-29 13:54:59 +02:00
parent 3dffaf5594
commit 5b7e0b8ede
Signed by: zotan
GPG key ID: D044E84C5BE01605
2 changed files with 60 additions and 15 deletions

View file

@ -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;

View file

@ -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<string> {
@ -27,6 +29,25 @@ export class NoteHelpers {
return getNote(note.id, user);
}
public static async reblogNote(note: Note, user: ILocalUser): Promise<Note> {
const data = {
createdAt: new Date(),
files: [],
renote: note
};
return await createNote(user, data);
}
public static async unreblogNote(note: Note, user: ILocalUser): Promise<Note> {
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<Note[]> {
const noteId = typeof note === "string" ? note : note.id;
const query = makePaginationQuery(Notes.createQueryBuilder("note"))