refactor: store middleware

This commit is contained in:
P. Reis 2024-04-27 20:59:35 -03:00
parent ab7a60c6de
commit 86573a579b
4 changed files with 24 additions and 32 deletions

View file

@ -78,7 +78,7 @@ import { auth98, requireProof, requireRole } from '@/middleware/auth98.ts';
import { cache } from '@/middleware/cache.ts';
import { csp } from '@/middleware/csp.ts';
import { adminRelaysController } from '@/controllers/api/ditto.ts';
import { setUserStore } from '@/middleware/userStore.ts';
import { storeMiddleware } from '@/middleware/store.ts';
interface AppEnv extends HonoEnv {
Variables: {
@ -90,8 +90,8 @@ interface AppEnv extends HonoEnv {
proof?: NostrEvent;
/** User associated with the pubkey, if any. */
user?: User;
/** User Store (pubkey has to be set to use it). */
userStore?: NStore;
/** Store */
store?: NStore;
};
}
@ -173,8 +173,8 @@ app.delete('/api/v1/statuses/:id{[0-9a-f]{64}}', requirePubkey, deleteStatusCont
app.post('/api/v1/media', mediaController);
app.post('/api/v2/media', mediaController);
app.get('/api/v1/timelines/home', requirePubkey, setUserStore, homeTimelineController);
app.get('/api/v1/timelines/public', publicTimelineController);
app.get('/api/v1/timelines/home', requirePubkey, storeMiddleware, homeTimelineController);
app.get('/api/v1/timelines/public', storeMiddleware, publicTimelineController);
app.get('/api/v1/timelines/tag/:hashtag', hashtagTimelineController);
app.get('/api/v1/preferences', preferencesController);

View file

@ -5,7 +5,6 @@ import { type AppContext, type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { getFeedPubkeys } from '@/queries.ts';
import { booleanParamSchema } from '@/schema.ts';
import { eventsDB } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginated, paginationSchema } from '@/utils/api.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
@ -46,10 +45,7 @@ const hashtagTimelineController: AppController = (c) => {
/** Render statuses for timelines. */
async function renderStatuses(c: AppContext, filters: NostrFilter[]) {
const { signal } = c.req.raw;
const userStore = c.get('userStore');
let store: NStore = eventsDB;
if (userStore) store = userStore;
const store = c.get('store') as NStore;
const events = await store
.query(filters, { 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

@ -1,22 +0,0 @@
import { AppMiddleware } from '@/app.ts';
import { UserStore } from '@/storages/UserStore.ts';
import { eventsDB } from '@/storages.ts';
import { HTTPException } from 'hono';
/** User Store middleware.
* Throw a 500 if can't set the `userStore` */
const setUserStore: AppMiddleware = async (c, next) => {
const pubkey = c.get('pubkey') as string;
try {
const store = new UserStore(pubkey, eventsDB);
c.set('userStore', store);
} catch (e) {
console.log(e);
throw new HTTPException(500);
}
await next();
};
export { setUserStore };