mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Refactors, bugfixing
This commit is contained in:
parent
5812618170
commit
e26d604a54
42 changed files with 370 additions and 376 deletions
|
|
@ -58,13 +58,11 @@ export const attachmentToAPI = (
|
|||
};
|
||||
|
||||
export const getUrl = (name: string, config: Config) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
|
||||
if (config.media.backend === MediaBackendType.LOCAL) {
|
||||
return `${config.http.base_url}/media/${name}`;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison, @typescript-eslint/no-unnecessary-condition
|
||||
return new URL(`/media/${name}`, config.http.base_url).toString();
|
||||
}
|
||||
if (config.media.backend === MediaBackendType.S3) {
|
||||
return `${config.s3.public_url}/${name}`;
|
||||
return new URL(`/${name}`, config.s3.public_url).toString();
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import type { Like, Prisma } from "@prisma/client";
|
||||
import type { Like } from "@prisma/client";
|
||||
import { config } from "config-manager";
|
||||
import { client } from "~database/datasource";
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
import type { Like as LysandLike } from "~types/lysand/Object";
|
||||
import type { StatusWithRelations } from "./Status";
|
||||
import type { UserWithRelations } from "./User";
|
||||
|
|
@ -18,7 +17,7 @@ export const toLysand = (like: Like): LysandLike => {
|
|||
created_at: new Date(like.createdAt).toISOString(),
|
||||
// biome-ignore lint/suspicious/noExplicitAny: to be rewritten
|
||||
object: (like as any).liked?.uri,
|
||||
uri: `${config.http.base_url}/actions/${like.id}`,
|
||||
uri: new URL(`/actions/${like.id}`, config.http.base_url).toString(),
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
import type { LysandObject } from "@prisma/client";
|
||||
import { client } from "~database/datasource";
|
||||
import type { LysandObjectType } from "~types/lysand/Object";
|
||||
|
|
|
|||
|
|
@ -149,7 +149,6 @@ export const federateStatusTo = async (
|
|||
new TextEncoder().encode("request_body"),
|
||||
);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
const userInbox = new URL(user.endpoints.inbox);
|
||||
|
||||
const date = new Date();
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ import type { LysandPublication, Note } from "~types/lysand/Object";
|
|||
import { applicationToAPI } from "./Application";
|
||||
import { attachmentToAPI } from "./Attachment";
|
||||
import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji";
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
import type { UserWithRelations } from "./User";
|
||||
import { fetchRemoteUser, parseMentionsUris, userToAPI } from "./User";
|
||||
import { statusAndUserRelations, userRelations } from "./relations";
|
||||
|
|
@ -118,7 +117,6 @@ export const fetchFromRemote = async (uri: string): Promise<Status | null> => {
|
|||
/**
|
||||
* Return all the ancestors of this post,
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
|
||||
export const getAncestors = async (
|
||||
status: StatusWithRelations,
|
||||
fetcher: UserWithRelations | null,
|
||||
|
|
@ -154,7 +152,6 @@ export const getAncestors = async (
|
|||
* Return all the descendants of this post (recursive)
|
||||
* Temporary implementation, will be replaced with a recursive SQL query when Prisma adds support for it
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
|
||||
export const getDescendants = async (
|
||||
status: StatusWithRelations,
|
||||
fetcher: UserWithRelations | null,
|
||||
|
|
@ -295,7 +292,10 @@ export const createNewStatus = async (data: {
|
|||
isReblog: false,
|
||||
uri:
|
||||
data.uri ||
|
||||
`${config.http.base_url}/statuses/FAKE-${crypto.randomUUID()}`,
|
||||
new URL(
|
||||
`/statuses/FAKE-${crypto.randomUUID()}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
mentions: {
|
||||
connect: mentions.map((mention) => {
|
||||
return {
|
||||
|
|
@ -313,7 +313,12 @@ export const createNewStatus = async (data: {
|
|||
id: status.id,
|
||||
},
|
||||
data: {
|
||||
uri: data.uri || `${config.http.base_url}/statuses/${status.id}`,
|
||||
uri:
|
||||
data.uri ||
|
||||
new URL(
|
||||
`/statuses/${status.id}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
},
|
||||
include: statusAndUserRelations,
|
||||
});
|
||||
|
|
@ -467,13 +472,10 @@ export const statusToAPI = async (
|
|||
card: null,
|
||||
content: status.content,
|
||||
emojis: status.emojis.map((emoji) => emojiToAPI(emoji)),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
favourited: !!(status.likes ?? []).find(
|
||||
(like) => like.likerId === user?.id,
|
||||
),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
favourites_count: (status.likes ?? []).length,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
media_attachments: (status.attachments ?? []).map(
|
||||
(a) => attachmentToAPI(a) as APIAttachment,
|
||||
),
|
||||
|
|
@ -485,7 +487,7 @@ export const statusToAPI = async (
|
|||
?.muting || false
|
||||
: false,
|
||||
pinned: status.pinnedBy.find((u) => u.id === user?.id) ? true : false,
|
||||
// TODO: Add pols
|
||||
// TODO: Add polls
|
||||
poll: null,
|
||||
reblog: status.reblog
|
||||
? await statusToAPI(status.reblog as unknown as StatusWithRelations)
|
||||
|
|
@ -501,9 +503,9 @@ export const statusToAPI = async (
|
|||
sensitive: status.sensitive,
|
||||
spoiler_text: status.spoilerText,
|
||||
tags: [],
|
||||
uri: `${config.http.base_url}/statuses/${status.id}`,
|
||||
uri: new URL(`/statuses/${status.id}`, config.http.base_url).toString(),
|
||||
visibility: "public",
|
||||
url: `${config.http.base_url}/statuses/${status.id}`,
|
||||
url: new URL(`/statuses/${status.id}`, config.http.base_url).toString(),
|
||||
bookmarked: false,
|
||||
quote: status.quotingPost
|
||||
? await statusToAPI(
|
||||
|
|
@ -567,7 +569,7 @@ export const statusToLysand = (status: StatusWithRelations): Note => {
|
|||
created_at: new Date(status.createdAt).toISOString(),
|
||||
id: status.id,
|
||||
author: status.authorId,
|
||||
uri: `${config.http.base_url}/users/${status.authorId}/statuses/${status.id}`,
|
||||
uri: new URL(`/statuses/${status.id}`, config.http.base_url).toString(),
|
||||
contents: [
|
||||
{
|
||||
content: status.content,
|
||||
|
|
|
|||
|
|
@ -4,13 +4,13 @@ import { Prisma } from "@prisma/client";
|
|||
import { type Config, config } from "config-manager";
|
||||
import { htmlToText } from "html-to-text";
|
||||
import { client } from "~database/datasource";
|
||||
import { MediaBackendType } from "~packages/media-manager";
|
||||
import type { APIAccount } from "~types/entities/account";
|
||||
import type { APISource } from "~types/entities/source";
|
||||
import type { LysandUser } from "~types/lysand/Object";
|
||||
import { addEmojiIfNotExists, emojiToAPI, emojiToLysand } from "./Emoji";
|
||||
import { addInstanceIfNotExists } from "./Instance";
|
||||
import { userRelations } from "./relations";
|
||||
import { getUrl } from "./Attachment";
|
||||
|
||||
export interface AuthData {
|
||||
user: UserWithRelations | null;
|
||||
|
|
@ -35,14 +35,7 @@ export type UserWithRelations = Prisma.UserGetPayload<typeof userRelations2>;
|
|||
*/
|
||||
export const getAvatarUrl = (user: User, config: Config) => {
|
||||
if (!user.avatar) return config.defaults.avatar;
|
||||
if (config.media.backend === MediaBackendType.LOCAL) {
|
||||
return `${config.http.base_url}/media/${user.avatar}`;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
}
|
||||
if (config.media.backend === MediaBackendType.S3) {
|
||||
return `${config.s3.public_url}/${user.avatar}`;
|
||||
}
|
||||
return "";
|
||||
return getUrl(user.avatar, config);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -52,14 +45,7 @@ export const getAvatarUrl = (user: User, config: Config) => {
|
|||
*/
|
||||
export const getHeaderUrl = (user: User, config: Config) => {
|
||||
if (!user.header) return config.defaults.header;
|
||||
if (config.media.backend === MediaBackendType.LOCAL) {
|
||||
return `${config.http.base_url}/media/${user.header}`;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
}
|
||||
if (config.media.backend === MediaBackendType.S3) {
|
||||
return `${config.s3.public_url}/${user.header}`;
|
||||
}
|
||||
return "";
|
||||
return getUrl(user.header, config);
|
||||
};
|
||||
|
||||
export const getFromRequest = async (req: Request): Promise<AuthData> => {
|
||||
|
|
@ -224,16 +210,7 @@ export const createNewLocalUser = async (data: {
|
|||
id: user.id,
|
||||
},
|
||||
data: {
|
||||
uri: `${config.http.base_url}/users/${user.id}`,
|
||||
endpoints: {
|
||||
disliked: `${config.http.base_url}/users/${user.id}/disliked`,
|
||||
featured: `${config.http.base_url}/users/${user.id}/featured`,
|
||||
liked: `${config.http.base_url}/users/${user.id}/liked`,
|
||||
followers: `${config.http.base_url}/users/${user.id}/followers`,
|
||||
following: `${config.http.base_url}/users/${user.id}/following`,
|
||||
inbox: `${config.http.base_url}/users/${user.id}/inbox`,
|
||||
outbox: `${config.http.base_url}/users/${user.id}/outbox`,
|
||||
},
|
||||
uri: new URL(`/users/${user.id}`, config.http.base_url).toString(),
|
||||
},
|
||||
include: userRelations,
|
||||
});
|
||||
|
|
@ -399,13 +376,35 @@ export const userToLysand = (user: UserWithRelations): LysandUser => {
|
|||
},
|
||||
],
|
||||
created_at: new Date(user.createdAt).toISOString(),
|
||||
disliked: `${user.uri}/disliked`,
|
||||
featured: `${user.uri}/featured`,
|
||||
liked: `${user.uri}/liked`,
|
||||
followers: `${user.uri}/followers`,
|
||||
following: `${user.uri}/following`,
|
||||
inbox: `${user.uri}/inbox`,
|
||||
outbox: `${user.uri}/outbox`,
|
||||
|
||||
disliked: new URL(
|
||||
`/users/${user.id}/disliked`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
featured: new URL(
|
||||
`/users/${user.id}/featured`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
liked: new URL(
|
||||
`/users/${user.id}/liked`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
followers: new URL(
|
||||
`/users/${user.id}/followers`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
following: new URL(
|
||||
`/users/${user.id}/following`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
inbox: new URL(
|
||||
`/users/${user.id}/inbox`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
outbox: new URL(
|
||||
`/users/${user.id}/outbox`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
indexable: false,
|
||||
username: user.username,
|
||||
avatar: [
|
||||
|
|
@ -444,7 +443,10 @@ export const userToLysand = (user: UserWithRelations): LysandUser => {
|
|||
],
|
||||
})),
|
||||
public_key: {
|
||||
actor: `${config.http.base_url}/users/${user.id}`,
|
||||
actor: new URL(
|
||||
`/users/${user.id}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
public_key: user.publicKey,
|
||||
},
|
||||
extensions: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue