Move translators into their own package

This commit is contained in:
Alex Gleason 2025-02-17 00:41:49 -06:00
parent 65846d062a
commit 28360e0ea8
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
12 changed files with 87 additions and 7 deletions

View file

@ -5,7 +5,8 @@
"./packages/conf", "./packages/conf",
"./packages/db", "./packages/db",
"./packages/ditto", "./packages/ditto",
"./packages/metrics" "./packages/metrics",
"./packages/translators"
], ],
"tasks": { "tasks": {
"start": "deno run -A --env-file --deny-read=.env packages/ditto/server.ts", "start": "deno run -A --env-file --deny-read=.env packages/ditto/server.ts",

View file

@ -0,0 +1,52 @@
import { type Env, Hono } from '@hono/hono';
import type { DittoConf } from '@ditto/conf';
import type { DittoDatabase, DittoTables } from '@ditto/db';
import type { HonoOptions } from '@hono/hono/hono-base';
import type { NostrSigner, NPool, NRelay, NStore, NUploader } from '@nostrify/nostrify';
import type { Kysely } from 'kysely';
interface DittoEnv extends Env {
Variables: {
conf: DittoConf;
user?: {
/** Signer to get the logged-in user's pubkey, relays, and to sign events, or `undefined` if the user isn't logged in. */
signer: NostrSigner;
/** Storage for the user, might filter out unwanted content. */
store: NStore;
};
/** Uploader for the user to upload files. */
uploader?: NUploader;
/** Kysely instance for the database. */
kysely: Kysely<DittoTables>;
/** Main database. */
store: NRelay;
/** Internal Nostr relay for realtime subscriptions. */
pubsub: NRelay;
/** Nostr relay pool. */
pool: NPool<NRelay>;
/** Database object. */
db: DittoDatabase;
/** Normalized pagination params. */
pagination: { since?: number; until?: number; limit: number };
/** Normalized list pagination params. */
listPagination: { offset: number; limit: number };
/** Translation service. */
translator?: DittoTranslator;
signal: AbortSignal;
pipeline: Pick<NStore, 'event'>;
};
}
export class DittoRoute extends Hono<DittoEnv> {
constructor(opts: HonoOptions<DittoEnv> = {}) {
super(opts);
this.init();
}
init(): void {
this.use((c, next) => {
return next();
});
}
}

1
packages/api/mod.ts Normal file
View file

@ -0,0 +1 @@
export { DittoRoute } from './DittoRoute.ts';

View file

@ -7,19 +7,33 @@ import { booleanParamSchema, languageSchema } from '@/schema.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginated } from '@/utils/api.ts'; import { paginated } from '@/utils/api.ts';
import { getTagSet } from '@/utils/tags.ts'; import { getTagSet } from '@/utils/tags.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; import { StatusView } from '@/views/mastodon/StatusView.ts';
import { Hono } from '@hono/hono';
const homeQuerySchema = z.object({ const homeQuerySchema = z.object({
exclude_replies: booleanParamSchema.optional(), exclude_replies: booleanParamSchema.optional(),
only_media: booleanParamSchema.optional(), only_media: booleanParamSchema.optional(),
}); });
export interface TimelineRouteOpts {
}
export class TimelineRoute {
private app: Hono;
constructor(private opts: TimelineRouteOpts) {
this.app = new Hono();
}
}
const homeTimelineController: AppController = async (c) => { const homeTimelineController: AppController = async (c) => {
const params = c.get('pagination'); const { user, pagination } = c.var;
const pubkey = await c.get('signer')?.getPublicKey()!;
const pubkey = await user.signer.getPublicKey()!;
const result = homeQuerySchema.safeParse(c.req.query()); const result = homeQuerySchema.safeParse(c.req.query());
if (!result.success) { if (!result.success) {const homeTimelineController: AppController = async (c) => {
return c.json({ error: 'Bad request', schema: result.error }, 400); return c.json({ error: 'Bad request', schema: result.error }, 400);
} }

View file

@ -6,10 +6,10 @@ let configDBCache: Promise<PleromaConfigDB> | undefined;
export const cspMiddleware = (): AppMiddleware => { export const cspMiddleware = (): AppMiddleware => {
return async (c, next) => { return async (c, next) => {
const { conf, store } = c.var; const { conf } = c.var;
if (!configDBCache) { if (!configDBCache) {
configDBCache = getPleromaConfigs(conf, store); configDBCache = getPleromaConfigs(c.var);
} }
const { host, protocol, origin } = conf.url; const { host, protocol, origin } = conf.url;

View file

@ -0,0 +1,7 @@
{
"name": "@ditto/translators",
"version": "1.1.0",
"exports": {
".": "./mod.ts"
}
}

View file

@ -0,0 +1,5 @@
export { DeepLTranslator } from './DeepLTranslator.ts';
export { LibreTranslateTranslator } from './LibreTranslateTranslator.ts';
export type { LanguageCode } from 'iso-639-1';
export type { DittoTranslator } from './DittoTranslator.ts';