fix: 🐛 when favorouting/unfavouriting with Mastodon API, send instance default reaction

This commit is contained in:
ThatOneCalculator 2023-09-23 22:05:25 -07:00
parent 081d723cff
commit 284795fdf5
No known key found for this signature in database
GPG Key ID: 8703CACD01000000

View File

@ -12,6 +12,7 @@ import {
convertPoll, convertPoll,
convertStatus, convertStatus,
} from "../converters.js"; } from "../converters.js";
import { fetchMeta } from "@/misc/fetch-meta.js";
function normalizeQuery(data: any) { function normalizeQuery(data: any) {
const str = querystring.stringify(data); const str = querystring.stringify(data);
@ -216,10 +217,11 @@ 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 meta = await fetchMeta();
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization; const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
const react = await getFirstReaction(BASE_URL, accessTokens); const react = meta.defaultReaction;
try { try {
const a = (await client.createEmojiReaction( const a = (await client.createEmojiReaction(
convertId(ctx.params.id, IdType.FirefishId), convertId(ctx.params.id, IdType.FirefishId),
@ -238,10 +240,11 @@ 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 meta = await fetchMeta();
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization; const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
const react = await getFirstReaction(BASE_URL, accessTokens); const react = meta.defaultReaction;
try { try {
const data = await client.deleteEmojiReaction( const data = await client.deleteEmojiReaction(
convertId(ctx.params.id, IdType.FirefishId), convertId(ctx.params.id, IdType.FirefishId),
@ -476,25 +479,3 @@ export function apiStatusMastodon(router: Router): void {
}, },
); );
} }
async function getFirstReaction(
BASE_URL: string,
accessTokens: string | undefined,
) {
const accessTokenArr = accessTokens?.split(" ") ?? [null];
const accessToken = accessTokenArr[accessTokenArr.length - 1];
let react = "⭐";
try {
const api = await axios.post(`${BASE_URL}/api/i/registry/get-unsecure`, {
scope: ["client", "base"],
key: "reactions",
i: accessToken,
});
const reactRaw = api.data;
react = Array.isArray(reactRaw) ? api.data[0] : "⭐";
console.log(api.data);
return react;
} catch (e) {
return react;
}
}