mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-01 12:44:25 +01:00
Merge branch 'main' into feat-zap-counter
This commit is contained in:
19
src/app.ts
19
src/app.ts
@@ -6,7 +6,6 @@ import { cors, logger, serveStatic } from 'hono/middleware';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { cron } from '@/cron.ts';
|
||||
import { startFirehose } from '@/firehose.ts';
|
||||
import { Time } from '@/utils.ts';
|
||||
|
||||
import {
|
||||
accountController,
|
||||
@@ -42,7 +41,11 @@ import {
|
||||
nameRequestsController,
|
||||
} from '@/controllers/api/ditto.ts';
|
||||
import { emptyArrayController, emptyObjectController, notImplementedController } from '@/controllers/api/fallback.ts';
|
||||
import { instanceController } from '@/controllers/api/instance.ts';
|
||||
import {
|
||||
instanceDescriptionController,
|
||||
instanceV1Controller,
|
||||
instanceV2Controller,
|
||||
} from '@/controllers/api/instance.ts';
|
||||
import { markersController, updateMarkersController } from '@/controllers/api/markers.ts';
|
||||
import { mediaController } from '@/controllers/api/media.ts';
|
||||
import { mutesController } from '@/controllers/api/mutes.ts';
|
||||
@@ -103,7 +106,6 @@ import { indexController } from '@/controllers/site.ts';
|
||||
import { nodeInfoController, nodeInfoSchemaController } from '@/controllers/well-known/nodeinfo.ts';
|
||||
import { nostrController } from '@/controllers/well-known/nostr.ts';
|
||||
import { auth98Middleware, requireProof, requireRole } from '@/middleware/auth98Middleware.ts';
|
||||
import { cacheMiddleware } from '@/middleware/cacheMiddleware.ts';
|
||||
import { cspMiddleware } from '@/middleware/cspMiddleware.ts';
|
||||
import { requireSigner } from '@/middleware/requireSigner.ts';
|
||||
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
|
||||
@@ -129,7 +131,7 @@ type AppContext = Context<AppEnv>;
|
||||
type AppMiddleware = MiddlewareHandler<AppEnv>;
|
||||
type AppController = Handler<AppEnv, any, HonoInput, Response | Promise<Response>>;
|
||||
|
||||
const app = new Hono<AppEnv>();
|
||||
const app = new Hono<AppEnv>({ strict: false });
|
||||
|
||||
const debug = Debug('ditto:http');
|
||||
|
||||
@@ -141,14 +143,12 @@ if (Conf.cronEnabled) {
|
||||
}
|
||||
|
||||
app.use('/api/*', logger(debug));
|
||||
app.use('/relay/*', logger(debug));
|
||||
app.use('/.well-known/*', logger(debug));
|
||||
app.use('/users/*', logger(debug));
|
||||
app.use('/nodeinfo/*', logger(debug));
|
||||
app.use('/oauth/*', logger(debug));
|
||||
|
||||
app.get('/api/v1/streaming', streamingController);
|
||||
app.get('/api/v1/streaming/', streamingController);
|
||||
app.get('/relay', relayController);
|
||||
|
||||
app.use(
|
||||
@@ -166,7 +166,9 @@ app.get('/.well-known/nostr.json', nostrController);
|
||||
|
||||
app.get('/nodeinfo/:version', nodeInfoSchemaController);
|
||||
|
||||
app.get('/api/v1/instance', cacheMiddleware({ cacheName: 'web', expires: Time.minutes(5) }), instanceController);
|
||||
app.get('/api/v1/instance', instanceV1Controller);
|
||||
app.get('/api/v2/instance', instanceV2Controller);
|
||||
app.get('/api/v1/instance/extended_description', instanceDescriptionController);
|
||||
|
||||
app.get('/api/v1/apps/verify_credentials', appCredentialsController);
|
||||
app.post('/api/v1/apps', createAppController);
|
||||
@@ -297,6 +299,9 @@ app.get('/api/v1/conversations', emptyArrayController);
|
||||
app.get('/api/v1/lists', emptyArrayController);
|
||||
|
||||
app.use('/api/*', notImplementedController);
|
||||
app.use('/.well-known/*', notImplementedController);
|
||||
app.use('/nodeinfo/*', notImplementedController);
|
||||
app.use('/oauth/*', notImplementedController);
|
||||
|
||||
const publicFiles = serveStatic({ root: './public/' });
|
||||
const staticFiles = serveStatic({ root: './static/' });
|
||||
|
||||
@@ -85,7 +85,13 @@ const adminAccountsController: AppController = async (c) => {
|
||||
}
|
||||
|
||||
const events = await store.query([{ kinds: [30382], authors: [Conf.pubkey], '#n': n, ...params }], { signal });
|
||||
const pubkeys = new Set<string>(events.map(({ pubkey }) => pubkey));
|
||||
|
||||
const pubkeys = new Set<string>(
|
||||
events
|
||||
.map(({ tags }) => tags.find(([name]) => name === 'd')?.[1])
|
||||
.filter((pubkey): pubkey is string => !!pubkey),
|
||||
);
|
||||
|
||||
const authors = await store.query([{ kinds: [0], authors: [...pubkeys] }])
|
||||
.then((events) => hydrateEvents({ store, events, signal }));
|
||||
|
||||
@@ -100,10 +106,14 @@ const adminAccountsController: AppController = async (c) => {
|
||||
}
|
||||
|
||||
const filter: NostrFilter = { kinds: [0], ...params };
|
||||
|
||||
if (local) {
|
||||
filter.search = `domain:${Conf.url.host}`;
|
||||
}
|
||||
const events = await store.query([filter], { signal });
|
||||
|
||||
const events = await store.query([filter], { signal })
|
||||
.then((events) => hydrateEvents({ store, events, signal }));
|
||||
|
||||
const accounts = await Promise.all(events.map(renderAdminAccount));
|
||||
return paginated(c, events, accounts);
|
||||
};
|
||||
|
||||
@@ -3,7 +3,9 @@ import { Conf } from '@/config.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
import { getInstanceMetadata } from '@/utils/instance.ts';
|
||||
|
||||
const instanceController: AppController = async (c) => {
|
||||
const version = '0.0.0 (compatible; Ditto 0.0.1)';
|
||||
|
||||
const instanceV1Controller: AppController = async (c) => {
|
||||
const { host, protocol } = Conf.url;
|
||||
const meta = await getInstanceMetadata(await Storages.db(), c.req.raw.signal);
|
||||
|
||||
@@ -54,7 +56,7 @@ const instanceController: AppController = async (c) => {
|
||||
urls: {
|
||||
streaming_api: `${wsProtocol}//${host}`,
|
||||
},
|
||||
version: '0.0.0 (compatible; Ditto 0.0.1)',
|
||||
version,
|
||||
email: meta.email,
|
||||
nostr: {
|
||||
pubkey: Conf.pubkey,
|
||||
@@ -67,4 +69,86 @@ const instanceController: AppController = async (c) => {
|
||||
});
|
||||
};
|
||||
|
||||
export { instanceController };
|
||||
const instanceV2Controller: AppController = async (c) => {
|
||||
const { host, protocol } = Conf.url;
|
||||
const meta = await getInstanceMetadata(await Storages.db(), c.req.raw.signal);
|
||||
|
||||
/** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */
|
||||
const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:';
|
||||
|
||||
return c.json({
|
||||
domain: host,
|
||||
title: meta.name,
|
||||
version,
|
||||
source_url: 'https://gitlab.com/soapbox-pub/ditto',
|
||||
description: meta.about,
|
||||
usage: {
|
||||
users: {
|
||||
active_month: 0,
|
||||
},
|
||||
},
|
||||
thumbnail: {
|
||||
url: meta.picture,
|
||||
blurhash: '',
|
||||
versions: {
|
||||
'@1x': meta.picture,
|
||||
'@2x': meta.picture,
|
||||
},
|
||||
},
|
||||
languages: [
|
||||
'en',
|
||||
],
|
||||
configuration: {
|
||||
urls: {
|
||||
streaming: `${wsProtocol}//${host}`,
|
||||
},
|
||||
vapid: {
|
||||
public_key: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
|
||||
},
|
||||
accounts: {
|
||||
max_featured_tags: 10,
|
||||
max_pinned_statuses: 5,
|
||||
},
|
||||
statuses: {
|
||||
max_characters: Conf.postCharLimit,
|
||||
max_media_attachments: 4,
|
||||
characters_reserved_per_url: 23,
|
||||
},
|
||||
media_attachments: {
|
||||
supported_mime_types: [],
|
||||
image_size_limit: 16777216,
|
||||
image_matrix_limit: 33177600,
|
||||
video_size_limit: 103809024,
|
||||
video_frame_rate_limit: 120,
|
||||
video_matrix_limit: 8294400,
|
||||
},
|
||||
polls: {
|
||||
max_options: 4,
|
||||
max_characters_per_option: 50,
|
||||
min_expiration: 300,
|
||||
max_expiration: 2629746,
|
||||
},
|
||||
translation: {
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
registrations: {
|
||||
enabled: false,
|
||||
approval_required: false,
|
||||
message: null,
|
||||
url: null,
|
||||
},
|
||||
rules: [],
|
||||
});
|
||||
};
|
||||
|
||||
const instanceDescriptionController: AppController = async (c) => {
|
||||
const meta = await getInstanceMetadata(await Storages.db(), c.req.raw.signal);
|
||||
|
||||
return c.json({
|
||||
content: meta.about,
|
||||
updated_at: new Date((meta.event?.created_at ?? 0) * 1000).toISOString(),
|
||||
});
|
||||
};
|
||||
|
||||
export { instanceDescriptionController, instanceV1Controller, instanceV2Controller };
|
||||
|
||||
@@ -122,6 +122,7 @@ const oauthController: AppController = (c) => {
|
||||
return c.text('Missing `redirect_uri` query param.', 422);
|
||||
}
|
||||
|
||||
const state = c.req.query('state');
|
||||
const redirectUri = maybeDecodeUri(encodedUri);
|
||||
|
||||
return c.html(`<!DOCTYPE html>
|
||||
@@ -162,6 +163,7 @@ const oauthController: AppController = (c) => {
|
||||
<form id="oauth_form" action="/oauth/authorize" method="post">
|
||||
<input type="text" placeholder="bunker://..." name="bunker_uri" autocomplete="off" required>
|
||||
<input type="hidden" name="redirect_uri" id="redirect_uri" value="${escape(redirectUri)}">
|
||||
<input type="hidden" name="state" value="${escape(state ?? '')}">
|
||||
<button type="submit">Authorize</button>
|
||||
</form>
|
||||
<p>Sign in with a Nostr bunker app. Please configure the app to use this relay: ${Conf.relay}</p>
|
||||
@@ -187,6 +189,7 @@ function maybeDecodeUri(uri: string): string {
|
||||
const oauthAuthorizeSchema = z.object({
|
||||
bunker_uri: z.string().url().refine((v) => v.startsWith('bunker://')),
|
||||
redirect_uri: z.string().url(),
|
||||
state: z.string().optional(),
|
||||
});
|
||||
|
||||
/** Controller the OAuth form is POSTed to. */
|
||||
@@ -199,7 +202,7 @@ const oauthAuthorizeController: AppController = async (c) => {
|
||||
}
|
||||
|
||||
// Parsed FormData values.
|
||||
const { bunker_uri, redirect_uri: redirectUri } = result.data;
|
||||
const { bunker_uri, redirect_uri: redirectUri, state } = result.data;
|
||||
|
||||
const bunker = new URL(bunker_uri);
|
||||
|
||||
@@ -209,17 +212,26 @@ const oauthAuthorizeController: AppController = async (c) => {
|
||||
relays: bunker.searchParams.getAll('relay'),
|
||||
});
|
||||
|
||||
const url = addCodeToRedirectUri(redirectUri, token);
|
||||
if (redirectUri === 'urn:ietf:wg:oauth:2.0:oob') {
|
||||
return c.text(token);
|
||||
}
|
||||
|
||||
const url = addCodeToRedirectUri(redirectUri, token, state);
|
||||
|
||||
return c.redirect(url);
|
||||
};
|
||||
|
||||
/** Append the given `code` as a query param to the `redirect_uri`. */
|
||||
function addCodeToRedirectUri(redirectUri: string, code: string): string {
|
||||
function addCodeToRedirectUri(redirectUri: string, code: string, state?: string): string {
|
||||
const url = new URL(redirectUri);
|
||||
const q = new URLSearchParams();
|
||||
|
||||
q.set('code', code);
|
||||
|
||||
if (state) {
|
||||
q.set('state', state);
|
||||
}
|
||||
|
||||
url.search = q.toString();
|
||||
|
||||
return url.toString();
|
||||
|
||||
@@ -164,9 +164,9 @@ export async function getTrendingTags(store: NStore, tagName: string): Promise<T
|
||||
|
||||
const tags = label.tags.filter(([name]) => name === tagName);
|
||||
|
||||
const now = new Date();
|
||||
const lastWeek = new Date(now.getTime() - Time.days(7));
|
||||
const dates = generateDateRange(lastWeek, now).reverse();
|
||||
const labelDate = new Date(label.created_at * 1000);
|
||||
const lastWeek = new Date(labelDate.getTime() - Time.days(7));
|
||||
const dates = generateDateRange(lastWeek, labelDate).reverse();
|
||||
|
||||
return Promise.all(tags.map(async ([_, value]) => {
|
||||
const filters = dates.map((date) => ({
|
||||
|
||||
@@ -59,7 +59,7 @@ export function cron() {
|
||||
Deno.cron(
|
||||
'update trending pubkeys',
|
||||
'0 * * * *',
|
||||
() => updateTrendingTags('#p', 'p', [1, 6, 7, 9735], 40, Conf.relay),
|
||||
() => updateTrendingTags('#p', 'p', [1, 3, 6, 7, 9735], 40, Conf.relay),
|
||||
);
|
||||
Deno.cron(
|
||||
'update trending zapped events',
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { NDatabase, NIP50, NKinds, NostrEvent, NostrFilter, NSchema as n, NStore } from '@nostrify/nostrify';
|
||||
import { Stickynotes } from '@soapbox/stickynotes';
|
||||
import { Kysely } from 'kysely';
|
||||
import { nip27 } from 'nostr-tools';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DittoTables } from '@/db/DittoTables.ts';
|
||||
@@ -220,7 +221,7 @@ class EventsDB implements NStore {
|
||||
case 0:
|
||||
return EventsDB.buildUserSearchContent(event);
|
||||
case 1:
|
||||
return event.content;
|
||||
return nip27.replaceAll(event.content, () => '');
|
||||
case 30009:
|
||||
return EventsDB.buildTagsSearchContent(event.tags.filter(([t]) => t !== 'alt'));
|
||||
case 30360:
|
||||
|
||||
Reference in New Issue
Block a user