mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor: store middleware
This commit is contained in:
parent
ab7a60c6de
commit
86573a579b
4 changed files with 24 additions and 32 deletions
10
src/app.ts
10
src/app.ts
|
|
@ -78,7 +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 { setUserStore } from '@/middleware/userStore.ts';
|
import { storeMiddleware } from '@/middleware/store.ts';
|
||||||
|
|
||||||
interface AppEnv extends HonoEnv {
|
interface AppEnv extends HonoEnv {
|
||||||
Variables: {
|
Variables: {
|
||||||
|
|
@ -90,8 +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;
|
||||||
/** User Store (pubkey has to be set to use it). */
|
/** Store */
|
||||||
userStore?: NStore;
|
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/v1/media', mediaController);
|
||||||
app.post('/api/v2/media', mediaController);
|
app.post('/api/v2/media', mediaController);
|
||||||
|
|
||||||
app.get('/api/v1/timelines/home', requirePubkey, setUserStore, 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);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ 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,10 +45,7 @@ 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 userStore = c.get('userStore');
|
const store = c.get('store') as NStore;
|
||||||
|
|
||||||
let store: NStore = eventsDB;
|
|
||||||
if (userStore) store = userStore;
|
|
||||||
|
|
||||||
const events = await store
|
const events = await store
|
||||||
.query(filters, { signal })
|
.query(filters, { signal })
|
||||||
|
|
|
||||||
18
src/middleware/store.ts
Normal file
18
src/middleware/store.ts
Normal 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 };
|
||||||
|
|
@ -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 };
|
|
||||||
Loading…
Add table
Reference in a new issue