Merge branch 'feat-store-middleware' into 'main'

Create User Store middleware and use it in required endpoints

See merge request soapbox-pub/ditto!189
This commit is contained in:
Alex Gleason 2024-04-28 01:45:17 +00:00
commit 42bbc09a91
4 changed files with 29 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import { NostrEvent } from '@nostrify/nostrify'; import { NostrEvent, NStore } from '@nostrify/nostrify';
import { type Context, Env as HonoEnv, type Handler, Hono, Input as HonoInput, type MiddlewareHandler } from 'hono'; import { type Context, Env as HonoEnv, type Handler, Hono, Input as HonoInput, type MiddlewareHandler } from 'hono';
import { cors, logger, serveStatic } from 'hono/middleware'; import { cors, logger, serveStatic } from 'hono/middleware';
@ -78,6 +78,7 @@ import { auth98, requireProof, requireRole } from '@/middleware/auth98.ts';
import { cache } from '@/middleware/cache.ts'; import { cache } from '@/middleware/cache.ts';
import { csp } from '@/middleware/csp.ts'; import { csp } from '@/middleware/csp.ts';
import { adminRelaysController } from '@/controllers/api/ditto.ts'; import { adminRelaysController } from '@/controllers/api/ditto.ts';
import { storeMiddleware } from '@/middleware/store.ts';
interface AppEnv extends HonoEnv { interface AppEnv extends HonoEnv {
Variables: { Variables: {
@ -89,6 +90,8 @@ interface AppEnv extends HonoEnv {
proof?: NostrEvent; proof?: NostrEvent;
/** User associated with the pubkey, if any. */ /** User associated with the pubkey, if any. */
user?: User; user?: User;
/** Store */
store?: NStore;
}; };
} }
@ -170,8 +173,8 @@ app.delete('/api/v1/statuses/:id{[0-9a-f]{64}}', requirePubkey, deleteStatusCont
app.post('/api/v1/media', mediaController); app.post('/api/v1/media', mediaController);
app.post('/api/v2/media', mediaController); app.post('/api/v2/media', mediaController);
app.get('/api/v1/timelines/home', requirePubkey, homeTimelineController); app.get('/api/v1/timelines/home', requirePubkey, storeMiddleware, homeTimelineController);
app.get('/api/v1/timelines/public', publicTimelineController); app.get('/api/v1/timelines/public', storeMiddleware, publicTimelineController);
app.get('/api/v1/timelines/tag/:hashtag', hashtagTimelineController); app.get('/api/v1/timelines/tag/:hashtag', hashtagTimelineController);
app.get('/api/v1/preferences', preferencesController); app.get('/api/v1/preferences', preferencesController);

View File

@ -1,11 +1,10 @@
import { NostrFilter } from '@nostrify/nostrify'; import { NostrFilter, NStore } from '@nostrify/nostrify';
import { z } from 'zod'; import { z } from 'zod';
import { type AppContext, type AppController } from '@/app.ts'; import { type AppContext, type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { getFeedPubkeys } from '@/queries.ts'; import { getFeedPubkeys } from '@/queries.ts';
import { booleanParamSchema } from '@/schema.ts'; import { booleanParamSchema } from '@/schema.ts';
import { eventsDB } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginated, paginationSchema } from '@/utils/api.ts'; import { paginated, paginationSchema } from '@/utils/api.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
@ -46,13 +45,14 @@ const hashtagTimelineController: AppController = (c) => {
/** Render statuses for timelines. */ /** Render statuses for timelines. */
async function renderStatuses(c: AppContext, filters: NostrFilter[]) { async function renderStatuses(c: AppContext, filters: NostrFilter[]) {
const { signal } = c.req.raw; const { signal } = c.req.raw;
const store = c.get('store') as NStore;
const events = await eventsDB const events = await store
.query(filters, { signal }) .query(filters, { signal })
.then((events) => .then((events) =>
hydrateEvents({ hydrateEvents({
events, events,
storage: eventsDB, storage: store,
signal, signal,
}) })
); );

18
src/middleware/store.ts Normal file
View File

@ -0,0 +1,18 @@
import { AppMiddleware } from '@/app.ts';
import { UserStore } from '@/storages/UserStore.ts';
import { eventsDB } from '@/storages.ts';
/** Store middleware. */
const storeMiddleware: AppMiddleware = async (c, next) => {
const pubkey = c.get('pubkey') as string;
if (pubkey) {
const store = new UserStore(pubkey, eventsDB);
c.set('store', store);
} else {
c.set('store', eventsDB);
}
await next();
};
export { storeMiddleware };

View File

@ -29,7 +29,7 @@ export class UserStore implements NStore {
const mutedPubkeys = getTagSet(mutedPubkeysEvent.tags, 'p'); const mutedPubkeys = getTagSet(mutedPubkeysEvent.tags, 'p');
return allEvents.filter((event) => { return allEvents.filter((event) => {
mutedPubkeys.has(event.pubkey) === false; return mutedPubkeys.has(event.pubkey) === false;
}); });
} }