From 279431fcdfe9997aa2faf3b626f04865db2cafb7 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 24 Feb 2023 09:41:53 +0000 Subject: [PATCH] use getDatabase in frontend --- frontend/src/routes/(admin)/oauth/authorize/index.tsx | 7 ++++--- .../routes/(frontend)/[accountId]/[statusId]/index.tsx | 10 ++++++++-- frontend/src/routes/(frontend)/[accountId]/index.tsx | 3 ++- frontend/src/routes/(frontend)/[accountId]/layout.tsx | 5 +++-- .../(frontend)/[accountId]/with_replies/index.tsx | 3 ++- frontend/src/routes/(frontend)/explore/index.tsx | 3 ++- frontend/src/routes/(frontend)/public/index.tsx | 3 ++- frontend/src/routes/(frontend)/public/local/index.tsx | 3 ++- 8 files changed, 25 insertions(+), 12 deletions(-) diff --git a/frontend/src/routes/(admin)/oauth/authorize/index.tsx b/frontend/src/routes/(admin)/oauth/authorize/index.tsx index 7b6c3f8..657bc4e 100644 --- a/frontend/src/routes/(admin)/oauth/authorize/index.tsx +++ b/frontend/src/routes/(admin)/oauth/authorize/index.tsx @@ -8,12 +8,13 @@ import { Avatar } from '~/components/avatar' import { getPersonByEmail } from 'wildebeest/backend/src/activitypub/actors' import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml' import { buildRedirect } from 'wildebeest/functions/oauth/authorize' +import { getDatabase } from 'wildebeest/backend/src/database' export const clientLoader = loader$, { DATABASE: D1Database }>(async ({ platform, query, html }) => { const client_id = query.get('client_id') || '' let client: Client | null = null try { - client = await getClientById(platform.DATABASE, client_id) + client = await getClientById(getDatabase(platform as any), client_id) } catch (e: unknown) { const error = e as { stack: string; cause: string } console.warn(error.stack, error.cause) @@ -48,10 +49,10 @@ export const userLoader = loader$< throw html(500, getErrorHtml("The Access JWT doesn't contain an email")) } - const person = await getPersonByEmail(platform.DATABASE, payload.email) + const person = await getPersonByEmail(getDatabase(platform as any), payload.email) if (person === null) { const isFirstLogin = true - const res = await buildRedirect(platform.DATABASE, request as Request, isFirstLogin, jwt.value) + const res = await buildRedirect(getDatabase(platform as any), request as Request, isFirstLogin, jwt.value) if (res.status === 302) { throw redirect(302, res.headers.get('location') || '') } else { diff --git a/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx b/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx index 0b8307a..c0e2e86 100644 --- a/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/[statusId]/index.tsx @@ -1,4 +1,5 @@ import { component$ } from '@builder.io/qwik' +import { getDatabase } from 'wildebeest/backend/src/database' import { MastodonStatus, StatusContext } from '~/types' import Status from '~/components/Status' import * as statusAPI from 'wildebeest/functions/api/v1/statuses/[id]' @@ -17,7 +18,12 @@ export const statusLoader = loader$< const domain = new URL(request.url).hostname let statusText = '' try { - const statusResponse = await statusAPI.handleRequestGet(platform.DATABASE, params.statusId, domain, {} as Person) + const statusResponse = await statusAPI.handleRequestGet( + getDatabase(platform as any), + params.statusId, + domain, + {} as Person + ) statusText = await statusResponse.text() } catch (e: unknown) { const error = e as { stack: string; cause: string } @@ -31,7 +37,7 @@ export const statusLoader = loader$< const statusTextContent = await getTextContent(status.content) try { - const contextResponse = await contextAPI.handleRequest(domain, platform.DATABASE, params.statusId) + const contextResponse = await contextAPI.handleRequest(domain, getDatabase(platform as any), params.statusId) const contextText = await contextResponse.text() const context = JSON.parse(contextText ?? null) as StatusContext | null if (!context) { diff --git a/frontend/src/routes/(frontend)/[accountId]/index.tsx b/frontend/src/routes/(frontend)/[accountId]/index.tsx index 90e549d..0a551ed 100644 --- a/frontend/src/routes/(frontend)/[accountId]/index.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/index.tsx @@ -1,4 +1,5 @@ import { $, component$ } from '@builder.io/qwik' +import { getDatabase } from 'wildebeest/backend/src/database' import { loader$ } from '@builder.io/qwik-city' import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml' import type { MastodonStatus } from '~/types' @@ -21,7 +22,7 @@ export const statusesLoader = loader$< const handle = parseHandle(accountId) accountId = handle.localPart - const response = await getLocalStatuses(request as Request, platform.DATABASE, handle, 0, false) + const response = await getLocalStatuses(request as Request, getDatabase(platform as any), handle, 0, false) statuses = await response.json>() } catch { throw html( diff --git a/frontend/src/routes/(frontend)/[accountId]/layout.tsx b/frontend/src/routes/(frontend)/[accountId]/layout.tsx index 0a65ecd..8a7891e 100644 --- a/frontend/src/routes/(frontend)/[accountId]/layout.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/layout.tsx @@ -11,6 +11,7 @@ import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml' import { getDocumentHead } from '~/utils/getDocumentHead' import * as statusAPI from 'wildebeest/functions/api/v1/statuses/[id]' import { useAccountUrl } from '~/utils/useAccountUrl' +import { getDatabase } from 'wildebeest/backend/src/database' export const accountPageLoader = loader$< Promise<{ account: MastodonAccount; accountHandle: string; isValidStatus: boolean }>, @@ -24,14 +25,14 @@ export const accountPageLoader = loader$< const accountId = url.pathname.split('/')[1] try { - const statusResponse = await statusAPI.handleRequestGet(platform.DATABASE, params.statusId, domain) + const statusResponse = await statusAPI.handleRequestGet(getDatabase(platform as any), params.statusId, domain) const statusText = await statusResponse.text() isValidStatus = !!statusText } catch { isValidStatus = false } - account = await getAccount(domain, accountId, platform.DATABASE) + account = await getAccount(domain, accountId, getDatabase(platform as any)) } catch { throw html( 500, diff --git a/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx b/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx index 5ffce57..2436092 100644 --- a/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx +++ b/frontend/src/routes/(frontend)/[accountId]/with_replies/index.tsx @@ -6,6 +6,7 @@ import type { MastodonStatus } from '~/types' import { StatusesPanel } from '~/components/StatusesPanel/StatusesPanel' import { getLocalStatuses } from 'wildebeest/functions/api/v1/accounts/[id]/statuses' import { parseHandle } from 'wildebeest/backend/src/utils/parse' +import { getDatabase } from 'wildebeest/backend/src/database' export const statusesLoader = loader$< Promise<{ @@ -22,7 +23,7 @@ export const statusesLoader = loader$< const handle = parseHandle(accountId) accountId = handle.localPart - const response = await getLocalStatuses(request as Request, platform.DATABASE, handle, 0, true) + const response = await getLocalStatuses(request as Request, getDatabase(platform as any), handle, 0, true) statuses = await response.json>() } catch { throw html( diff --git a/frontend/src/routes/(frontend)/explore/index.tsx b/frontend/src/routes/(frontend)/explore/index.tsx index e3678c8..14bb9a0 100644 --- a/frontend/src/routes/(frontend)/explore/index.tsx +++ b/frontend/src/routes/(frontend)/explore/index.tsx @@ -1,4 +1,5 @@ import { $, component$ } from '@builder.io/qwik' +import { getDatabase } from 'wildebeest/backend/src/database' import { DocumentHead, loader$ } from '@builder.io/qwik-city' import * as timelines from 'wildebeest/functions/api/v1/timelines/public' import { StatusesPanel } from '~/components/StatusesPanel/StatusesPanel' @@ -10,7 +11,7 @@ export const statusesLoader = loader$, { DATABASE: D1D async ({ platform, html }) => { try { // TODO: use the "trending" API endpoint here. - const response = await timelines.handleRequest(platform.domain, platform.DATABASE) + const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any)) const results = await response.text() // Manually parse the JSON to ensure that Qwik finds the resulting objects serializable. return JSON.parse(results) as MastodonStatus[] diff --git a/frontend/src/routes/(frontend)/public/index.tsx b/frontend/src/routes/(frontend)/public/index.tsx index 2fee643..ffa4fdd 100644 --- a/frontend/src/routes/(frontend)/public/index.tsx +++ b/frontend/src/routes/(frontend)/public/index.tsx @@ -6,12 +6,13 @@ import StickyHeader from '~/components/StickyHeader/StickyHeader' import { getDocumentHead } from '~/utils/getDocumentHead' import { StatusesPanel } from '~/components/StatusesPanel/StatusesPanel' import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml' +import { getDatabase } from 'wildebeest/backend/src/database' export const statusesLoader = loader$, { DATABASE: D1Database; domain: string }>( async ({ platform, html }) => { try { // TODO: use the "trending" API endpoint here. - const response = await timelines.handleRequest(platform.domain, platform.DATABASE) + const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any)) const results = await response.text() // Manually parse the JSON to ensure that Qwik finds the resulting objects serializable. return JSON.parse(results) as MastodonStatus[] diff --git a/frontend/src/routes/(frontend)/public/local/index.tsx b/frontend/src/routes/(frontend)/public/local/index.tsx index d523ccd..70d9a53 100644 --- a/frontend/src/routes/(frontend)/public/local/index.tsx +++ b/frontend/src/routes/(frontend)/public/local/index.tsx @@ -1,4 +1,5 @@ import { $, component$ } from '@builder.io/qwik' +import { getDatabase } from 'wildebeest/backend/src/database' import { MastodonStatus } from '~/types' import * as timelines from 'wildebeest/functions/api/v1/timelines/public' import { DocumentHead, loader$ } from '@builder.io/qwik-city' @@ -11,7 +12,7 @@ export const statusesLoader = loader$, { DATABASE: D1D async ({ platform, html }) => { try { // TODO: use the "trending" API endpoint here. - const response = await timelines.handleRequest(platform.domain, platform.DATABASE, { local: true }) + const response = await timelines.handleRequest(platform.domain, getDatabase(platform as any), { local: true }) const results = await response.text() // Manually parse the JSON to ensure that Qwik finds the resulting objects serializable. return JSON.parse(results) as MastodonStatus[]