[mastodon-client] DELETE /statuses/:id

This commit is contained in:
Laura Hausmann 2023-09-29 18:04:35 +02:00
parent 1a03044be8
commit 501991f5e1
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
4 changed files with 48 additions and 28 deletions

View File

@ -40,7 +40,7 @@ export function convertNotification(notification: MastodonEntity.Notification) {
export function convertPoll(poll: Entity.Poll) { export function convertPoll(poll: Entity.Poll) {
return simpleConvert(poll); return simpleConvert(poll);
} }
export function convertReaction(reaction: Entity.Reaction) { export function convertReaction(reaction: MastodonEntity.Reaction) {
if (reaction.accounts) { if (reaction.accounts) {
reaction.accounts = reaction.accounts.map(convertAccount); reaction.accounts = reaction.accounts.map(convertAccount);
} }
@ -50,7 +50,7 @@ export function convertRelationship(relationship: Entity.Relationship) {
return simpleConvert(relationship); return simpleConvert(relationship);
} }
export function convertStatus(status: Entity.Status) { export function convertStatus(status: MastodonEntity.Status) {
status.account = convertAccount(status.account); status.account = convertAccount(status.account);
status.id = convertId(status.id, IdType.MastodonId); status.id = convertId(status.id, IdType.MastodonId);
if (status.in_reply_to_account_id) if (status.in_reply_to_account_id)
@ -75,7 +75,7 @@ export function convertStatus(status: Entity.Status) {
return status; return status;
} }
export function convertConversation(conversation: Entity.Conversation) { export function convertConversation(conversation: MastodonEntity.Conversation) {
conversation.id = convertId(conversation.id, IdType.MastodonId); conversation.id = convertId(conversation.id, IdType.MastodonId);
conversation.accounts = conversation.accounts.map(convertAccount); conversation.accounts = conversation.accounts.map(convertAccount);
if (conversation.last_status) { if (conversation.last_status) {

View File

@ -1,7 +1,6 @@
import Router from "@koa/router"; import Router from "@koa/router";
import { getClient } from "../ApiMastodonCompatibleService.js"; import { getClient } from "../ApiMastodonCompatibleService.js";
import { emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js"; import { emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js";
import axios from "axios";
import querystring from "node:querystring"; import querystring from "node:querystring";
import qs from "qs"; import qs from "qs";
import { convertId, IdType } from "../../index.js"; import { convertId, IdType } from "../../index.js";
@ -11,8 +10,6 @@ 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";
import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/server/api/mastodon/endpoints/timeline.js"; import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/server/api/mastodon/endpoints/timeline.js";
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js";
@ -172,28 +169,45 @@ export function apiStatusMastodon(router: Router): void {
} }
}); });
router.delete<{ Params: { id: string } }>("/v1/statuses/:id", async (ctx) => { router.delete<{ Params: { id: string } }>("/v1/statuses/:id", async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.deleteStatus( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = data.data; if (!user) {
ctx.status = 401;
return;
}
const noteId = convertId(ctx.params.id, IdType.IceshrimpId);
const note = await getNote(noteId, user ?? null).then(n => n).catch(() => null);
if (!note) {
ctx.status = 404;
ctx.body = {
error: "Note not found"
}
return;
}
if (user.id !== note.userId) {
ctx.status = 403;
ctx.body = {
error: "Cannot delete someone else's note"
}
return;
}
ctx.body = await NoteHelpers.deleteNote(note, user)
.then(p => convertStatus(p));
} catch (e: any) { } catch (e: any) {
console.error(e.response.data, ctx.params.id); console.error(`Error processing ${ctx.method} /api${ctx.path}: ${e.message}`);
ctx.status = 401; ctx.status = 500;
ctx.body = e.response.data; ctx.body = {
error: e.message
}
} }
}); });
interface IReaction {
id: string;
createdAt: string;
user: MisskeyEntity.User;
type: string;
}
router.get<{ Params: { id: string } }>( router.get<{ Params: { id: string } }>(
"/v1/statuses/:id/context", "/v1/statuses/:id/context",
async (ctx) => { async (ctx) => {
@ -250,9 +264,6 @@ export function apiStatusMastodon(router: Router): void {
router.get<{ Params: { id: string } }>( router.get<{ Params: { id: string } }>(
"/v1/statuses/:id/reblogged_by", "/v1/statuses/:id/reblogged_by",
async (ctx) => { async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try { try {
const auth = await authenticate(ctx.headers.authorization, null); const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? null; const user = auth[0] ?? null;

View File

@ -17,8 +17,8 @@ namespace MastodonEntity {
in_reply_to_id: string | null; in_reply_to_id: string | null;
in_reply_to_account_id: string | null; in_reply_to_account_id: string | null;
reblog: Status | null; reblog: Status | null;
content: string; content: string | undefined;
text: string | null; text: string | null | undefined;
created_at: string; created_at: string;
emojis: Emoji[]; emojis: Emoji[];
replies_count: number; replies_count: number;

View File

@ -15,6 +15,7 @@ import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { AccountCache, LinkPaginationObject, UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { AccountCache, LinkPaginationObject, UserHelpers } from "@/server/api/mastodon/helpers/user.js";
import { addPinned, removePinned } from "@/services/i/pin.js"; import { addPinned, removePinned } from "@/services/i/pin.js";
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
export class NoteHelpers { export class NoteHelpers {
public static async getDefaultReaction(): Promise<string> { public static async getDefaultReaction(): Promise<string> {
@ -112,6 +113,14 @@ export class NoteHelpers {
return note; return note;
} }
public static async deleteNote(note: Note, user: ILocalUser): Promise<MastodonEntity.Status> {
if (user.id !== note.userId) throw new Error("Can't delete someone elses note");
const status = await NoteConverter.encode(note, user);
await deleteNote(user, note);
status.content = undefined;
return status;
}
public static async getNoteFavoritedBy(note: Note, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> { public static async getNoteFavoritedBy(note: Note, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
if (limit > 80) limit = 80; if (limit > 80) limit = 80;
const query = PaginationHelpers.makePaginationQuery( const query = PaginationHelpers.makePaginationQuery(