mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2025-01-25 06:41:36 -07:00
[mastodon-client] POST /statuses/:id/favourite, /statuses/:id/favourite
This commit is contained in:
parent
f29fee74a0
commit
93a4db4418
2 changed files with 60 additions and 22 deletions
|
@ -11,6 +11,8 @@ import { getNote } from "@/server/api/common/getters.js";
|
||||||
import authenticate from "@/server/api/authenticate.js";
|
import authenticate from "@/server/api/authenticate.js";
|
||||||
import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js";
|
import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js";
|
||||||
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
||||||
|
import createReaction from "@/services/note/reaction/create.js";
|
||||||
|
import deleteReaction from "@/services/note/reaction/delete.js";
|
||||||
|
|
||||||
function normalizeQuery(data: any) {
|
function normalizeQuery(data: any) {
|
||||||
const str = querystring.stringify(data);
|
const str = querystring.stringify(data);
|
||||||
|
@ -281,21 +283,37 @@ export function apiStatusMastodon(router: Router): void {
|
||||||
router.post<{ Params: { id: string } }>(
|
router.post<{ Params: { id: string } }>(
|
||||||
"/v1/statuses/:id/favourite",
|
"/v1/statuses/:id/favourite",
|
||||||
async (ctx) => {
|
async (ctx) => {
|
||||||
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
||||||
const accessTokens = ctx.headers.authorization;
|
|
||||||
const client = getClient(BASE_URL, accessTokens);
|
|
||||||
const react = await NoteHelpers.getDefaultReaction();
|
|
||||||
try {
|
try {
|
||||||
const a = (await client.createEmojiReaction(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
const user = auth[0] ?? null;
|
||||||
react,
|
|
||||||
)) as any;
|
if (!user) {
|
||||||
//const data = await client.favouriteStatus(ctx.params.id) as any;
|
ctx.status = 401;
|
||||||
ctx.body = convertStatus(a.data);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const id = convertId(ctx.params.id, IdType.IceshrimpId);
|
||||||
|
const note = await getNote(id, user).catch(_ => null);
|
||||||
|
|
||||||
|
if (note === null) {
|
||||||
|
ctx.status = 404;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reaction = await NoteHelpers.getDefaultReaction().catch(_ => null);
|
||||||
|
|
||||||
|
if (reaction === null) {
|
||||||
|
ctx.status = 500;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = await NoteHelpers.reactToNote(note, user, reaction)
|
||||||
|
.then(p => NoteConverter.encode(p, user))
|
||||||
|
.then(p => convertStatus(p));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
ctx.status = 401;
|
ctx.status = 400;
|
||||||
ctx.body = e.response.data;
|
ctx.body = e.response.data;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -303,16 +321,26 @@ export function apiStatusMastodon(router: Router): void {
|
||||||
router.post<{ Params: { id: string } }>(
|
router.post<{ Params: { id: string } }>(
|
||||||
"/v1/statuses/:id/unfavourite",
|
"/v1/statuses/:id/unfavourite",
|
||||||
async (ctx) => {
|
async (ctx) => {
|
||||||
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
||||||
const accessTokens = ctx.headers.authorization;
|
|
||||||
const client = getClient(BASE_URL, accessTokens);
|
|
||||||
const react = await NoteHelpers.getDefaultReaction();
|
|
||||||
try {
|
try {
|
||||||
const data = await client.deleteEmojiReaction(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
const user = auth[0] ?? null;
|
||||||
react,
|
|
||||||
);
|
if (!user) {
|
||||||
ctx.body = convertStatus(data.data);
|
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.removeReactFromNote(note, user)
|
||||||
|
.then(p => NoteConverter.encode(p, user))
|
||||||
|
.then(p => convertStatus(p));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
ctx.status = 401;
|
ctx.status = 401;
|
||||||
|
|
|
@ -5,9 +5,9 @@ import { generateMutedUserQuery } from "@/server/api/common/generate-muted-user-
|
||||||
import { generateBlockedUserQuery } from "@/server/api/common/generate-block-query.js";
|
import { generateBlockedUserQuery } from "@/server/api/common/generate-block-query.js";
|
||||||
import { Note } from "@/models/entities/note.js";
|
import { Note } from "@/models/entities/note.js";
|
||||||
import { ILocalUser } from "@/models/entities/user.js";
|
import { ILocalUser } from "@/models/entities/user.js";
|
||||||
import querystring from "node:querystring";
|
|
||||||
import { getNote } from "@/server/api/common/getters.js";
|
import { getNote } from "@/server/api/common/getters.js";
|
||||||
import { ObjectLiteral, SelectQueryBuilder } from "typeorm";
|
import createReaction from "@/services/note/reaction/create.js";
|
||||||
|
import deleteReaction from "@/services/note/reaction/delete.js";
|
||||||
|
|
||||||
export class NoteHelpers {
|
export class NoteHelpers {
|
||||||
public static async getDefaultReaction(): Promise<string> {
|
public static async getDefaultReaction(): Promise<string> {
|
||||||
|
@ -17,6 +17,16 @@ export class NoteHelpers {
|
||||||
.then(p => p[0].defaultReaction);
|
.then(p => p[0].defaultReaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async reactToNote(note: Note, user: ILocalUser, reaction: string): Promise<Note> {
|
||||||
|
await createReaction(user, note, reaction);
|
||||||
|
return getNote(note.id, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async removeReactFromNote(note: Note, user: ILocalUser): Promise<Note> {
|
||||||
|
await deleteReaction(user, note);
|
||||||
|
return getNote(note.id, user);
|
||||||
|
}
|
||||||
|
|
||||||
public static async getNoteDescendants(note: Note | string, user: ILocalUser | null, limit: number = 10, depth: number = 2): Promise<Note[]> {
|
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 noteId = typeof note === "string" ? note : note.id;
|
||||||
const query = makePaginationQuery(Notes.createQueryBuilder("note"))
|
const query = makePaginationQuery(Notes.createQueryBuilder("note"))
|
||||||
|
|
Loading…
Reference in a new issue