mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-22 01:47:39 -07:00
[mastodon-client] POST /v1/statuses
This commit is contained in:
parent
b98294e5be
commit
fe15584834
4 changed files with 121 additions and 70 deletions
|
@ -21,76 +21,19 @@ function normalizeQuery(data: any) {
|
|||
|
||||
export function setupEndpointsStatus(router: Router): void {
|
||||
router.post("/v1/statuses", async (ctx) => {
|
||||
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
||||
const accessTokens = ctx.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
let body: any = ctx.request.body;
|
||||
if (body.in_reply_to_id)
|
||||
body.in_reply_to_id = convertId(body.in_reply_to_id, IdType.IceshrimpId);
|
||||
if (body.quote_id)
|
||||
body.quote_id = convertId(body.quote_id, IdType.IceshrimpId);
|
||||
if (
|
||||
(!body.poll && body["poll[options][]"]) ||
|
||||
(!body.media_ids && body["media_ids[]"])
|
||||
) {
|
||||
body = normalizeQuery(body);
|
||||
}
|
||||
const text = body.status;
|
||||
const removed = text.replace(/@\S+/g, "").replace(/\s|/g, "");
|
||||
const isDefaultEmoji = emojiRegexAtStartToEnd.test(removed);
|
||||
const isCustomEmoji = /^:[a-zA-Z0-9@_]+:$/.test(removed);
|
||||
if ((body.in_reply_to_id && isDefaultEmoji) || isCustomEmoji) {
|
||||
const a = await client.createEmojiReaction(
|
||||
body.in_reply_to_id,
|
||||
removed,
|
||||
);
|
||||
ctx.body = a.data;
|
||||
}
|
||||
if (body.in_reply_to_id && removed === "/unreact") {
|
||||
try {
|
||||
const id = body.in_reply_to_id;
|
||||
const post = await client.getStatus(id);
|
||||
const react = post.data.reactions.filter((e) => e.me)[0].name;
|
||||
const data = await client.deleteEmojiReaction(id, react);
|
||||
ctx.body = data.data;
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
ctx.status = 401;
|
||||
ctx.body = e.response.data;
|
||||
}
|
||||
}
|
||||
if (!body.media_ids) body.media_ids = undefined;
|
||||
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
|
||||
if (body.media_ids) {
|
||||
body.media_ids = (body.media_ids as string[]).map((p) =>
|
||||
convertId(p, IdType.IceshrimpId),
|
||||
);
|
||||
}
|
||||
const {sensitive} = body;
|
||||
body.sensitive =
|
||||
typeof sensitive === "string" ? sensitive === "true" : sensitive;
|
||||
const auth = await authenticate(ctx.headers.authorization, null);
|
||||
const user = auth[0] ?? null;
|
||||
|
||||
if (body.poll) {
|
||||
if (
|
||||
body.poll.expires_in != null &&
|
||||
typeof body.poll.expires_in === "string"
|
||||
)
|
||||
body.poll.expires_in = parseInt(body.poll.expires_in);
|
||||
if (
|
||||
body.poll.multiple != null &&
|
||||
typeof body.poll.multiple === "string"
|
||||
)
|
||||
body.poll.multiple = body.poll.multiple == "true";
|
||||
if (
|
||||
body.poll.hide_totals != null &&
|
||||
typeof body.poll.hide_totals === "string"
|
||||
)
|
||||
body.poll.hide_totals = body.poll.hide_totals == "true";
|
||||
if (!user) {
|
||||
ctx.status = 401;
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await client.postStatus(text, body);
|
||||
ctx.body = convertStatus(data.data);
|
||||
let request = NoteHelpers.normalizeComposeOptions(ctx.request.body);
|
||||
const note = await NoteHelpers.createNote(request, user)
|
||||
.then(p => NoteConverter.encode(p, user));
|
||||
ctx.body = convertStatus(note);
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
ctx.status = 401;
|
||||
|
|
|
@ -42,4 +42,20 @@ namespace MastodonEntity {
|
|||
quote: Status | null;
|
||||
bookmarked: boolean;
|
||||
};
|
||||
|
||||
export type StatusCreationRequest = {
|
||||
text?: string,
|
||||
media_ids?: string[],
|
||||
poll?: {
|
||||
options: string[],
|
||||
expires_in: number,
|
||||
multiple: boolean
|
||||
},
|
||||
in_reply_to_id?: string,
|
||||
sensitive?: boolean,
|
||||
spoiler_text?: string,
|
||||
visibility?: string,
|
||||
language?: string,
|
||||
scheduled_at?: Date
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js";
|
||||
import { Metas, NoteFavorites, NoteReactions, Notes, UserNotePinings, Users } from "@/models/index.js";
|
||||
import {
|
||||
DriveFiles,
|
||||
Metas,
|
||||
NoteFavorites,
|
||||
NoteReactions,
|
||||
Notes,
|
||||
RegistryItems,
|
||||
UserNotePinings,
|
||||
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,7 +17,7 @@ import { ILocalUser, User } 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 createNote, { extractMentionedUsers } from "@/services/note/create.js";
|
||||
import deleteNote from "@/services/note/delete.js";
|
||||
import { genId } from "@/misc/gen-id.js";
|
||||
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
||||
|
@ -16,6 +25,13 @@ import { UserConverter } from "@/server/api/mastodon/converters/user.js";
|
|||
import { AccountCache, LinkPaginationObject, UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
||||
import { addPinned, removePinned } from "@/services/i/pin.js";
|
||||
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
|
||||
import { convertId, IdType } from "@/misc/convert-id.js";
|
||||
import querystring from "node:querystring";
|
||||
import qs from "qs";
|
||||
import { awaitAll } from "@/prelude/await-all.js";
|
||||
import { IsNull } from "typeorm";
|
||||
import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js";
|
||||
import mfm from "mfm-js";
|
||||
|
||||
export class NoteHelpers {
|
||||
public static async getDefaultReaction(): Promise<string> {
|
||||
|
@ -204,4 +220,76 @@ export class NoteHelpers {
|
|||
|
||||
return notes.reverse();
|
||||
}
|
||||
|
||||
public static async createNote(request: MastodonEntity.StatusCreationRequest, user: ILocalUser): Promise<Note> {
|
||||
const files = request.media_ids && request.media_ids.length > 0
|
||||
? DriveFiles.findByIds(request.media_ids)
|
||||
: [];
|
||||
|
||||
const reply = request.in_reply_to_id ? await getNote(request.in_reply_to_id, user) : undefined;
|
||||
const visibility = request.visibility ?? UserHelpers.getDefaultNoteVisibility(user);
|
||||
|
||||
const data = {
|
||||
createdAt: new Date(),
|
||||
files: files,
|
||||
poll: request.poll
|
||||
? {
|
||||
choices: request.poll.options,
|
||||
multiple: request.poll.multiple,
|
||||
expiresAt: request.poll.expires_in && request.poll.expires_in > 0 ? new Date(new Date().getTime() + (request.poll.expires_in * 1000)) : null,
|
||||
}
|
||||
: undefined,
|
||||
text: request.text,
|
||||
reply: reply,
|
||||
cw: request.spoiler_text,
|
||||
visibility: visibility,
|
||||
visibleUsers: Promise.resolve(visibility).then(p => p === 'specified' ? this.extractMentions(request.text ?? '', user) : undefined)
|
||||
}
|
||||
|
||||
return createNote(user, await awaitAll(data));
|
||||
}
|
||||
|
||||
public static async extractMentions(text: string, user: ILocalUser): Promise<User[]> {
|
||||
return extractMentionedUsers(user, mfm.parse(text)!);
|
||||
}
|
||||
|
||||
public static normalizeComposeOptions(body: any): MastodonEntity.StatusCreationRequest {
|
||||
const result: MastodonEntity.StatusCreationRequest = {};
|
||||
|
||||
body = qs.parse(querystring.stringify(body));
|
||||
|
||||
if (body.status !== null)
|
||||
result.text = body.status;
|
||||
if (body.spoiler_text !== null)
|
||||
result.spoiler_text = body.spoiler_text;
|
||||
if (body.visibility !== null)
|
||||
result.visibility = VisibilityConverter.decode(body.visibility);
|
||||
if (body.language !== null)
|
||||
result.language = body.language;
|
||||
if (body.scheduled_at !== null)
|
||||
result.scheduled_at = new Date(Date.parse(body.scheduled_at));
|
||||
if (body.in_reply_to_id)
|
||||
result.in_reply_to_id = convertId(body.in_reply_to_id, IdType.IceshrimpId);
|
||||
if (body.media_ids)
|
||||
result.media_ids = body.media_ids && body.media_ids.length > 0
|
||||
? this.normalizeToArray(body.media_ids)
|
||||
.map(p => convertId(p, IdType.IceshrimpId))
|
||||
: undefined;
|
||||
|
||||
if (body.poll) {
|
||||
result.poll = {
|
||||
expires_in: parseInt(body.poll.expires_in, 10),
|
||||
options: body.poll.options,
|
||||
multiple: !!body.poll.multiple,
|
||||
}
|
||||
}
|
||||
|
||||
result.sensitive = !!body.sensitive;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static normalizeToArray<T>(subject: T | T[]) {
|
||||
return Array.isArray(subject) ? subject : [subject];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -164,12 +164,12 @@ export class UserHelpers {
|
|||
public static async verifyCredentials(user: ILocalUser): Promise<MastodonEntity.Account> {
|
||||
const acct = UserConverter.encode(user);
|
||||
const profile = UserProfiles.findOneByOrFail({userId: user.id});
|
||||
const privacy = RegistryItems.findOneBy({domain: IsNull(), userId: user.id, key: 'defaultNoteVisibility', scope: '{client,base}'});
|
||||
const privacy = this.getDefaultNoteVisibility(user);
|
||||
return acct.then(acct => {
|
||||
const source = {
|
||||
note: acct.note,
|
||||
fields: acct.fields,
|
||||
privacy: privacy.then(p => VisibilityConverter.encode(p?.value ?? 'public')),
|
||||
privacy: privacy.then(p => VisibilityConverter.encode(p)),
|
||||
sensitive: profile.then(p => p.alwaysMarkNsfw),
|
||||
language: profile.then(p => p.lang ?? ''),
|
||||
};
|
||||
|
@ -485,4 +485,8 @@ export class UserHelpers {
|
|||
users: [],
|
||||
};
|
||||
}
|
||||
|
||||
public static getDefaultNoteVisibility(user: ILocalUser): Promise<string> {
|
||||
return RegistryItems.findOneBy({domain: IsNull(), userId: user.id, key: 'defaultNoteVisibility', scope: '{client,base}'}).then(p => p?.value ?? 'public')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue