refactor(api): ♻️ Move to Hono for HTTP

This commit is contained in:
Jesse Wierzbinski 2024-05-06 07:16:33 +00:00
parent 2237be3689
commit 826a260e90
No known key found for this signature in database
155 changed files with 7226 additions and 6077 deletions

View file

@ -3,10 +3,12 @@ import {
type InferInsertModel,
type SQL,
and,
count,
desc,
eq,
inArray,
isNotNull,
sql,
} from "drizzle-orm";
import { htmlToText } from "html-to-text";
import type * as Lysand from "lysand-types";
@ -161,6 +163,19 @@ export class Note {
return new User(this.status.author);
}
static async getCount() {
return (
await db
.select({
count: count(),
})
.from(Notes)
.where(
sql`EXISTS (SELECT 1 FROM "Users" WHERE "Users"."id" = ${Notes.authorId} AND "Users"."instanceId" IS NULL)`,
)
)[0].count;
}
async getReplyChildren() {
return await Note.manyFromSql(eq(Notes.replyId, this.status.id));
}

View file

@ -2,7 +2,17 @@ import { idValidator } from "@api";
import { getBestContentType, urlToContentFormat } from "@content_types";
import { addUserToMeilisearch } from "@meilisearch";
import { proxyUrl } from "@response";
import { type SQL, and, desc, eq, inArray } from "drizzle-orm";
import {
type SQL,
and,
count,
countDistinct,
desc,
eq,
gte,
inArray,
isNull,
} from "drizzle-orm";
import { htmlToText } from "html-to-text";
import type * as Lysand from "lysand-types";
import {
@ -20,6 +30,7 @@ import { db } from "~drizzle/db";
import {
EmojiToUser,
NoteToMentions,
Notes,
UserToPinnedNotes,
Users,
} from "~drizzle/schema";
@ -102,6 +113,37 @@ export class User {
return uri || new URL(`/users/${id}`, baseUrl).toString();
}
static async getCount() {
return (
await db
.select({
count: count(),
})
.from(Users)
.where(isNull(Users.instanceId))
)[0].count;
}
static async getActiveInPeriod(milliseconds: number) {
return (
await db
.select({
count: countDistinct(Users),
})
.from(Users)
.leftJoin(Notes, eq(Users.id, Notes.authorId))
.where(
and(
isNull(Users.instanceId),
gte(
Notes.createdAt,
new Date(Date.now() - milliseconds).toISOString(),
),
),
)
)[0].count;
}
async pin(note: Note) {
return (
await db

View file

@ -260,3 +260,23 @@ export const signedFetch = async (
},
});
};
// Export all schemas as a single object
export default {
Note: schemas.Note,
User: schemas.User,
Reaction: schemas.Reaction,
Poll: schemas.Poll,
Vote: schemas.Vote,
VoteResult: schemas.VoteResult,
Report: schemas.Report,
ServerMetadata: schemas.ServerMetadata,
Like: schemas.Like,
Dislike: schemas.Dislike,
Follow: schemas.Follow,
FollowAccept: schemas.FollowAccept,
FollowReject: schemas.FollowReject,
Announce: schemas.Announce,
Undo: schemas.Undo,
Entity: schemas.Entity,
};

View file

@ -2,6 +2,8 @@ import { dualLogger } from "@loggers";
import { errorResponse, jsonResponse, response } from "@response";
import type { MatchedRoute } from "bun";
import { type Config, config } from "config-manager";
import type { Hono } from "hono";
import type { RouterRoute } from "hono/types";
import { LogLevel, type LogManager, type MultiLogManager } from "log-manager";
import { RequestParser } from "request-parser";
import type { ZodType, z } from "zod";
@ -11,7 +13,7 @@ import { type AuthData, getFromRequest } from "~database/entities/User";
import type { User } from "~packages/database-interface/user";
type MaybePromise<T> = T | Promise<T>;
type HttpVerb = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS";
export type HttpVerb = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS";
export type RouteHandler<
RouteMeta extends APIRouteMetadata,
@ -54,8 +56,11 @@ export interface APIRouteMetadata {
export interface APIRouteExports {
meta: APIRouteMetadata;
schema: z.AnyZodObject;
default: RouteHandler<APIRouteMetadata, z.AnyZodObject>;
schemas?: {
query?: z.AnyZodObject;
body?: z.AnyZodObject;
};
default: (app: Hono) => RouterRoute;
}
export const processRoute = async (