mirror of
https://github.com/versia-pub/server.git
synced 2026-01-26 12:16:01 +01:00
Modify WebFinger behaviour, add user searching
This commit is contained in:
parent
ae9698c647
commit
d5817e985d
|
|
@ -23,7 +23,7 @@ import { applicationToAPI } from "./Application";
|
|||
import { attachmentToAPI, attachmentToLysand } from "./Attachment";
|
||||
import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji";
|
||||
import type { UserWithRelations } from "./User";
|
||||
import { fetchRemoteUser, parseMentionsUris, userToAPI } from "./User";
|
||||
import { resolveUser, parseMentionsUris, userToAPI } from "./User";
|
||||
import { statusAndUserRelations, userRelations } from "./relations";
|
||||
|
||||
const statusRelations = Prisma.validator<Prisma.StatusDefaultArgs>()({
|
||||
|
|
@ -57,62 +57,9 @@ export const isViewableByUser = (status: Status, user: User | null) => {
|
|||
return user && (status.mentions as User[]).includes(user);
|
||||
};
|
||||
|
||||
export const fetchFromRemote = async (uri: string): Promise<Status | null> => {
|
||||
// Check if already in database
|
||||
/* const existingStatus: StatusWithRelations | null =
|
||||
await client.status.findFirst({
|
||||
where: {
|
||||
uri: uri,
|
||||
},
|
||||
include: statusAndUserRelations,
|
||||
});
|
||||
|
||||
if (existingStatus) return existingStatus;
|
||||
|
||||
const status = await fetch(uri);
|
||||
|
||||
if (status.status === 404) return null;
|
||||
|
||||
const body = (await status.json()) as LysandPublication;
|
||||
|
||||
const content = getBestContentType(body.contents);
|
||||
|
||||
const emojis = await parseEmojis(content?.content || "");
|
||||
|
||||
const author = await fetchRemoteUser(body.author);
|
||||
|
||||
let replyStatus: Status | null = null;
|
||||
let quotingStatus: Status | null = null;
|
||||
|
||||
if (body.replies_to.length > 0) {
|
||||
replyStatus = await fetchFromRemote(body.replies_to[0]);
|
||||
}
|
||||
|
||||
if (body.quotes.length > 0) {
|
||||
quotingStatus = await fetchFromRemote(body.quotes[0]);
|
||||
}
|
||||
|
||||
return await createNewStatus({
|
||||
account: author,
|
||||
content: content?.content || "",
|
||||
content_type: content?.content_type,
|
||||
application: null,
|
||||
// TODO: Add visibility
|
||||
visibility: "public",
|
||||
spoiler_text: body.subject || "",
|
||||
uri: body.uri,
|
||||
sensitive: body.is_sensitive,
|
||||
emojis: emojis,
|
||||
mentions: await parseMentionsUris(body.mentions),
|
||||
reply: replyStatus
|
||||
? {
|
||||
status: replyStatus,
|
||||
user: (replyStatus as StatusWithRelations).author,
|
||||
}
|
||||
: undefined,
|
||||
quote: quotingStatus || undefined,
|
||||
}); */
|
||||
};
|
||||
export const fetchFromRemote = async (
|
||||
uri: string,
|
||||
): Promise<StatusWithRelations | null> => {};
|
||||
|
||||
/**
|
||||
* Return all the ancestors of this post,
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ export const followRequestUser = async (
|
|||
return relationship;
|
||||
};
|
||||
|
||||
export const fetchRemoteUser = async (uri: string) => {
|
||||
export const resolveUser = async (uri: string) => {
|
||||
// Check if user not already in database
|
||||
const foundUser = await client.user.findUnique({
|
||||
where: {
|
||||
|
|
@ -228,6 +228,68 @@ export const fetchRemoteUser = async (uri: string) => {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves a WebFinger identifier to a user.
|
||||
* @param identifier Either a UUID or a username
|
||||
*/
|
||||
export const resolveWebFinger = async (identifier: string, host: string) => {
|
||||
// Check if user not already in database
|
||||
const foundUser = await client.user.findUnique({
|
||||
where: {
|
||||
username: identifier,
|
||||
instance: {
|
||||
base_url: host,
|
||||
},
|
||||
},
|
||||
include: userRelations,
|
||||
});
|
||||
|
||||
if (foundUser) return foundUser;
|
||||
|
||||
const hostWithProtocol = host.startsWith("http") ? host : `https://${host}`;
|
||||
|
||||
const response = await fetch(
|
||||
new URL(
|
||||
`/.well-known/webfinger?${new URLSearchParams({
|
||||
resource: `acct:${identifier}@${host}`,
|
||||
})}`,
|
||||
hostWithProtocol,
|
||||
),
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const data = (await response.json()) as {
|
||||
subject: string;
|
||||
links: {
|
||||
rel: string;
|
||||
type: string;
|
||||
href: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
if (!data.subject || !data.links) {
|
||||
throw new Error(
|
||||
"Invalid WebFinger data (missing subject or links from response)",
|
||||
);
|
||||
}
|
||||
|
||||
const relevantLink = data.links.find((link) => link.rel === "self");
|
||||
|
||||
if (!relevantLink) {
|
||||
throw new Error(
|
||||
"Invalid WebFinger data (missing link with rel: 'self')",
|
||||
);
|
||||
}
|
||||
|
||||
return resolveUser(relevantLink.href);
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches the list of followers associated with the actor and updates the user's followers
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
"name": "lysand",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"version": "0.3.1",
|
||||
"version": "0.4.0",
|
||||
"description": "A project to build a federated social network",
|
||||
"author": {
|
||||
"email": "contact@cpluspatch.com",
|
||||
|
|
|
|||
|
|
@ -3,7 +3,11 @@ import { MeiliIndexType, meilisearch } from "@meilisearch";
|
|||
import { errorResponse, jsonResponse } from "@response";
|
||||
import { client } from "~database/datasource";
|
||||
import { statusToAPI } from "~database/entities/Status";
|
||||
import { userToAPI } from "~database/entities/User";
|
||||
import {
|
||||
resolveUser,
|
||||
resolveWebFinger,
|
||||
userToAPI,
|
||||
} from "~database/entities/User";
|
||||
import {
|
||||
statusAndUserRelations,
|
||||
userRelations,
|
||||
|
|
@ -71,6 +75,42 @@ export default apiRoute<{
|
|||
let statusResults: { id: string }[] = [];
|
||||
|
||||
if (!type || type === "accounts") {
|
||||
// Check if q is matching format username@domain.com or @username@domain.com
|
||||
if (q?.trim().match(/@?[a-zA-Z0-9_]+(@[a-zA-Z0-9_.:]+)/g)) {
|
||||
const [username, domain] = q.trim().split("@");
|
||||
const account = await client.user.findFirst({
|
||||
where: {
|
||||
username,
|
||||
instance: {
|
||||
base_url: domain,
|
||||
},
|
||||
},
|
||||
include: userRelations,
|
||||
});
|
||||
|
||||
if (account) {
|
||||
return jsonResponse({
|
||||
accounts: [userToAPI(account)],
|
||||
statuses: [],
|
||||
hashtags: [],
|
||||
});
|
||||
}
|
||||
|
||||
if (resolve) {
|
||||
const newUser = await resolveWebFinger(username, domain).catch(
|
||||
() => null,
|
||||
);
|
||||
|
||||
if (newUser) {
|
||||
return jsonResponse({
|
||||
accounts: [userToAPI(newUser)],
|
||||
statuses: [],
|
||||
hashtags: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
accountResults = (
|
||||
await meilisearch.index(MeiliIndexType.Accounts).search<{
|
||||
id: string;
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ export default apiRoute(async (req, matchedRoute, extraData) => {
|
|||
note.subject ?? "",
|
||||
[],
|
||||
note.uri,
|
||||
// TODO: Resolve mention,s
|
||||
// TODO: Resolve mentions
|
||||
[],
|
||||
// TODO: Add attachments
|
||||
[],
|
||||
|
|
|
|||
|
|
@ -21,14 +21,10 @@ export default apiRoute<{
|
|||
|
||||
if (!resource) return errorResponse("No resource provided", 400);
|
||||
|
||||
// Check if resource is in the correct format (acct:uuid@domain)
|
||||
if (
|
||||
!resource.match(
|
||||
/^acct:[0-9A-F]{8}-[0-9A-F]{4}-[7][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}@.+/i,
|
||||
)
|
||||
) {
|
||||
// Check if resource is in the correct format (acct:uuid/username@domain)
|
||||
if (!resource.match(/^acct:[a-zA-Z0-9-]+@[a-zA-Z0-9.-:]+$/)) {
|
||||
return errorResponse(
|
||||
"Invalid resource (should be acct:uuid@domain)",
|
||||
"Invalid resource (should be acct:(id or username)@domain)",
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
@ -43,8 +39,17 @@ export default apiRoute<{
|
|||
return errorResponse("User is a remote user", 404);
|
||||
}
|
||||
|
||||
const isUuid = requestedUser
|
||||
.split("@")[0]
|
||||
.match(
|
||||
/[0-9A-F]{8}-[0-9A-F]{4}-[7][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/i,
|
||||
);
|
||||
|
||||
const user = await client.user.findUnique({
|
||||
where: { id: requestedUser.split("@")[0] },
|
||||
where: {
|
||||
id: isUuid ? requestedUser.split("@")[0] : undefined,
|
||||
username: isUuid ? undefined : requestedUser.split("@")[0],
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
|
|
@ -52,7 +57,7 @@ export default apiRoute<{
|
|||
}
|
||||
|
||||
return jsonResponse({
|
||||
subject: `acct:${user.id}@${host}`,
|
||||
subject: `acct:${isUuid ? user.id : user.username}@${host}`,
|
||||
|
||||
links: [
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue