mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 13:59:16 +01:00
feat: Add Search endpoint
This commit is contained in:
parent
aa0813fef8
commit
553b558c1a
3 changed files with 138 additions and 24 deletions
|
|
@ -1,7 +1,14 @@
|
|||
import { applyConfig } from "@api";
|
||||
import { MeiliIndexType, meilisearch } from "@meilisearch";
|
||||
import { parseRequest } from "@request";
|
||||
import { errorResponse, jsonResponse } from "@response";
|
||||
import { getFromRequest } from "~database/entities/User";
|
||||
import { client } from "~database/datasource";
|
||||
import { statusAndUserRelations, statusToAPI } from "~database/entities/Status";
|
||||
import {
|
||||
getFromRequest,
|
||||
userRelations,
|
||||
userToAPI,
|
||||
} from "~database/entities/User";
|
||||
import type { APIRouteMeta } from "~types/api";
|
||||
|
||||
export const meta: APIRouteMeta = applyConfig({
|
||||
|
|
@ -29,9 +36,9 @@ export default async (req: Request): Promise<Response> => {
|
|||
resolve,
|
||||
following,
|
||||
account_id,
|
||||
max_id,
|
||||
min_id,
|
||||
limit,
|
||||
// max_id,
|
||||
// min_id,
|
||||
limit = 20,
|
||||
offset,
|
||||
} = await parseRequest<{
|
||||
q?: string;
|
||||
|
|
@ -52,9 +59,81 @@ export default async (req: Request): Promise<Response> => {
|
|||
);
|
||||
}
|
||||
|
||||
if (limit < 1 || limit > 40) {
|
||||
return errorResponse("Limit must be between 1 and 40", 400);
|
||||
}
|
||||
|
||||
let accountResults: { id: string }[] = [];
|
||||
let statusResults: { id: string }[] = [];
|
||||
|
||||
if (!type || type === "accounts") {
|
||||
accountResults = (
|
||||
await meilisearch.index(MeiliIndexType.Accounts).search<{
|
||||
id: string;
|
||||
}>(q, {
|
||||
limit: Number(limit) || 10,
|
||||
offset: Number(offset) || 0,
|
||||
sort: ["createdAt:desc"],
|
||||
})
|
||||
).hits;
|
||||
}
|
||||
|
||||
if (!type || type === "statuses") {
|
||||
statusResults = (
|
||||
await meilisearch.index(MeiliIndexType.Statuses).search<{
|
||||
id: string;
|
||||
}>(q, {
|
||||
limit: Number(limit) || 10,
|
||||
offset: Number(offset) || 0,
|
||||
sort: ["createdAt:desc"],
|
||||
})
|
||||
).hits;
|
||||
}
|
||||
|
||||
const accounts = await client.user.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: accountResults.map(hit => hit.id),
|
||||
},
|
||||
relationshipSubjects: {
|
||||
some: {
|
||||
subjectId: user?.id,
|
||||
following: following ? true : undefined,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
include: userRelations,
|
||||
});
|
||||
|
||||
const statuses = await client.status.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: statusResults.map(hit => hit.id),
|
||||
},
|
||||
author: {
|
||||
relationshipSubjects: {
|
||||
some: {
|
||||
subjectId: user?.id,
|
||||
following: following ? true : undefined,
|
||||
},
|
||||
},
|
||||
},
|
||||
authorId: account_id ? account_id : undefined,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
include: statusAndUserRelations,
|
||||
});
|
||||
|
||||
return jsonResponse({
|
||||
accounts: [],
|
||||
statuses: [],
|
||||
accounts: accounts.map(account => userToAPI(account)),
|
||||
statuses: await Promise.all(
|
||||
statuses.map(status => statusToAPI(status))
|
||||
),
|
||||
hashtags: [],
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue