Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(saas): address map save, navigation and checkout erasure review

Timothy Yine533c7847142
diff --git a/apps/cloud/app/components/CloudMapSettings.vue b/apps/cloud/app/components/CloudMapSettings.vue
index 9b7faa01..c4b5c3b7 100644
--- a/apps/cloud/app/components/CloudMapSettings.vue
+++ b/apps/cloud/app/components/CloudMapSettings.vue
@@ -4,10 +4,9 @@ const { locale } = useI18n()
const store = useSettingsStore()
const context = useEntryContext()
const t = (en: string, zh: string) => (locale.value === 'zh-Hans' ? zh : en)
-const { data, error, refresh } = await useFetch(
- `/api/sites/${props.siteId}/map`,
- { key: `site-maps-${props.siteId}` },
-)
+const { data, error } = await useFetch(`/api/sites/${props.siteId}/map`, {
+ key: `site-maps-${props.siteId}`,
+})
const own = ref(!!data.value?.config)
const draft = ref({
provider: 'maplibre',
@@ -26,14 +25,15 @@ async function save() {
message.value = ''
failed.value = false
try {
- await $fetch(`/api/sites/${props.siteId}/map`, {
+ const result = await $fetch(`/api/sites/${props.siteId}/map`, {
method: 'PUT',
body: {
revision: data.value.revision,
config: own.value ? draft.value : null,
},
})
- await refresh()
+ data.value = { ...data.value, ...result }
+ error.value = null
if (context.value?.siteId === props.siteId && data.value)
store.data.map = { ...data.value.effective }
message.value = t('Map settings saved.', '地图设置已保存。')
diff --git a/apps/site/app/router.options.ts b/apps/site/app/router.options.ts
index 5d38fce0..f2eb6711 100644
--- a/apps/site/app/router.options.ts
+++ b/apps/site/app/router.options.ts
@@ -10,10 +10,17 @@ export default <RouterConfig>{
return false
}
- if (from.name === 'slug' && (to.path === '/photos' || to.path === '/')) {
+ if (
+ from.name === 'slug' &&
+ !to.hash &&
+ (to.path === '/photos' ||
+ to.path === '/' ||
+ to.path.startsWith('/albums/'))
+ ) {
return false
}
+ if (to.hash) return { el: to.hash }
return { top: 0 }
},
}
diff --git a/packages/platform/src/account-purge.ts b/packages/platform/src/account-purge.ts
index 82009822..107843dd 100644
--- a/packages/platform/src/account-purge.ts
+++ b/packages/platform/src/account-purge.ts
@@ -34,7 +34,7 @@ export async function purgeAccounts(
try {
const billing = await db
.prepare(
- "SELECT s.id FROM platform_payment_subscriptions s JOIN platform_workspaces w ON w.id=s.workspace_id JOIN platform_payment_checkouts c ON c.id=s.checkout_id WHERE (w.owner_user_id=? OR c.user_id=?) AND s.status NOT IN('canceled','expired') UNION ALL SELECT c.id FROM platform_payment_checkouts c JOIN platform_workspaces w ON w.id=c.workspace_id WHERE (w.owner_user_id=? OR c.user_id=?) AND c.state IN('creating','open','uncertain') LIMIT 1",
+ "SELECT s.id FROM platform_payment_subscriptions s JOIN platform_workspaces w ON w.id=s.workspace_id JOIN platform_payment_checkouts c ON c.id=s.checkout_id WHERE (w.owner_user_id=? OR c.user_id=?) AND s.status NOT IN('canceled','expired') UNION ALL SELECT c.id FROM platform_payment_checkouts c JOIN platform_workspaces w ON w.id=c.workspace_id WHERE (w.owner_user_id=? OR c.user_id=?) AND (c.state IN('creating','open','uncertain') OR (c.state='expired' AND c.error IN('payment_confirmation_unresolved','payment_checkout_abandoned'))) LIMIT 1",
)
.bind(id, id, id, id)
.first()
diff --git a/packages/platform/src/dodo-payments.ts b/packages/platform/src/dodo-payments.ts
index e3614dff..8eb4b986 100644
--- a/packages/platform/src/dodo-payments.ts
+++ b/packages/platform/src/dodo-payments.ts
@@ -1497,6 +1497,7 @@ export async function closeDodoAccountPayments(
secret: string,
userId: number,
transport: typeof fetch = fetch,
+ now = Date.now(),
) {
const rows = await allRows<any>(
db
@@ -1523,12 +1524,51 @@ export async function closeDodoAccountPayments(
.prepare(
"UPDATE platform_payment_subscriptions SET status='canceled',checked_at=? WHERE id=?",
)
- .bind(Date.now(), row.id)
+ .bind(now, row.id)
+ .run()
+ }
+ // Locally abandoned sessions can still be paid through an old browser tab.
+ // Only release their erasure fence after expiry AND a fresh no-payment response.
+ const abandoned = await allRows<{
+ id: string
+ provider_id: string
+ provider_key: string
+ environment: DodoEnvironment
+ }>(
+ db
+ .prepare(
+ "SELECT c.id,c.provider_id,c.provider_key,c.environment FROM platform_payment_checkouts c JOIN platform_workspaces w ON w.id=c.workspace_id WHERE c.provider='dodo' AND (w.owner_user_id=? OR c.user_id=?) AND c.state='expired' AND c.error='payment_checkout_abandoned' AND c.created_at<? ORDER BY c.created_at LIMIT 10",
+ )
+ .bind(userId, userId, now - 86400000),
+ )
+ for (const checkout of abandoned) {
+ const { client, revision } = await dodoClient(
+ db,
+ secret,
+ checkout.environment,
+ transport,
+ false,
+ checkout.provider_key,
+ )
+ const session = await dodoRequest(() =>
+ client.checkoutSessions.retrieve(rawDodoId(checkout.provider_id)),
+ )
+ if (
+ session.id !== rawDodoId(checkout.provider_id) ||
+ session.payment_id ||
+ session.payment_status
+ )
+ continue
+ await db
+ .prepare(
+ `UPDATE platform_payment_checkouts SET error='payment_checkout_expired',updated_at=? WHERE id=? AND state='expired' AND error='payment_checkout_abandoned' AND NOT EXISTS(SELECT 1 FROM platform_payment_subscriptions WHERE checkout_id=platform_payment_checkouts.id) AND NOT EXISTS(SELECT 1 FROM platform_payment_locks WHERE workspace_id=platform_payment_checkouts.workspace_id AND expires_at>?) AND ${activeFence}`,
+ )
+ .bind(now, checkout.id, now, checkout.provider_key, revision)
.run()
}
return !(await db
.prepare(
- "SELECT c.id FROM platform_payment_checkouts c JOIN platform_workspaces w ON w.id=c.workspace_id WHERE c.provider='dodo' AND (w.owner_user_id=? OR c.user_id=?) AND (c.state IN('creating','open','uncertain') OR (c.state='expired' AND c.error='payment_confirmation_unresolved')) LIMIT 1",
+ "SELECT c.id FROM platform_payment_checkouts c JOIN platform_workspaces w ON w.id=c.workspace_id WHERE c.provider='dodo' AND (w.owner_user_id=? OR c.user_id=?) AND (c.state IN('creating','open','uncertain') OR (c.state='expired' AND c.error IN('payment_confirmation_unresolved','payment_checkout_abandoned'))) LIMIT 1",
)
.bind(userId, userId)
.first())
diff --git a/packages/platform/src/maps.ts b/packages/platform/src/maps.ts
index 5e1798a9..9c62fda6 100644
--- a/packages/platform/src/maps.ts
+++ b/packages/platform/src/maps.ts
@@ -74,10 +74,10 @@ export async function mapDefaults(db: PlatformD1) {
)
.first<{ config_json: string; revision: number }>()
return {
- config: {
+ config: parsePublicMap({
...EMPTY_MAP,
...JSON.parse(row!.config_json),
- } as PublicMapConfig,
+ }),
revision: row!.revision,
}
}
diff --git a/tests/cloud-map-save.test.ts b/tests/cloud-map-save.test.ts
new file mode 100644
index 00000000..b5881b94
--- /dev/null
+++ b/tests/cloud-map-save.test.ts
@@ -0,0 +1,78 @@
+import test from 'node:test'
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import vm from 'node:vm'
+import ts from 'typescript'
+import { ref } from 'vue'
+import { EMPTY_MAP } from '../packages/platform/src/maps.ts'
+test('successful map PUT updates revision, role and the live map without a follow-up GET', async () => {
+ const data = ref({
+ config: null as Record<string, string> | null,
+ effective: { ...EMPTY_MAP },
+ revision: 4,
+ role: 'admin',
+ })
+ const error = ref(null)
+ const store = { data: { map: { ...EMPTY_MAP } } }
+ let puts = 0,
+ refreshes = 0
+ const source = readFileSync(
+ 'apps/cloud/app/components/CloudMapSettings.vue',
+ 'utf8',
+ )
+ .split('<script setup lang="ts">')[1]!
+ .split('</script>')[0]!
+ const code = ts.transpileModule(source, {
+ compilerOptions: {
+ module: ts.ModuleKind.ESNext,
+ target: ts.ScriptTarget.ES2022,
+ },
+ }).outputText
+ const form = (await vm.runInNewContext(
+ `(async()=>{${code};return {save,own,draft,busy,message,failed}})()`,
+ {
+ ref,
+ defineProps: () => ({ siteId: 1 }),
+ useI18n: () => ({ locale: ref('en') }),
+ useSettingsStore: () => store,
+ useEntryContext: () => ref({ siteId: 1 }),
+ useFetch: async () => ({
+ data,
+ error,
+ refresh: async () => {
+ refreshes++
+ throw Error('GET unavailable')
+ },
+ }),
+ $fetch: async (
+ _url: string,
+ options: { body: { config: Record<string, string> } },
+ ) => {
+ puts++
+ return {
+ config: options.body.config,
+ effective: options.body.config,
+ revision: 5,
+ }
+ },
+ },
+ )) as {
+ save: () => Promise<void>
+ own: { value: boolean }
+ draft: { value: Record<string, string> }
+ busy: { value: boolean }
+ message: { value: string }
+ failed: { value: boolean }
+ }
+ form.own.value = true
+ form.draft.value['maplibre.token'] = 'new_public_browser_key'
+ await form.save()
+ assert.equal(puts, 1)
+ assert.equal(refreshes, 0)
+ assert.equal(data.value.revision, 5)
+ assert.equal(data.value.role, 'admin')
+ assert.equal(store.data.map['maplibre.token'], 'new_public_browser_key')
+ assert.equal(form.busy.value, false)
+ assert.equal(form.failed.value, false)
+ assert.equal(form.message.value, 'Map settings saved.')
+})
diff --git a/tests/dodo-payments.test.ts b/tests/dodo-payments.test.ts
index 57321001..10c405bc 100644
--- a/tests/dodo-payments.test.ts
+++ b/tests/dodo-payments.test.ts
@@ -3372,7 +3372,12 @@ test('details-stage cancellation releases the local slot and preserves late paym
const abandoned = await f.db
.prepare('SELECT * FROM platform_payment_checkouts WHERE id=?')
.bind(checkout.id)
- .first<any>()
+ .first<{
+ state: string
+ error: string
+ checkout_url: string | null
+ provider_id: string
+ }>()
assert.equal(abandoned.state, 'expired')
assert.equal(abandoned.error, 'payment_checkout_abandoned')
assert.equal(abandoned.checkout_url, null)
@@ -3390,7 +3395,7 @@ test('details-stage cancellation releases the local slot and preserves late paym
)
const grant = await f.db
.prepare('SELECT * FROM platform_workspace_plans WHERE workspace_id=1')
- .first<any>()
+ .first<{ source: string }>()
assert.equal(grant.source, 'payment')
} finally {
await f.mf.dispose()
@@ -4085,7 +4090,7 @@ test('details-stage abandonment requires a fresh empty provider session and no r
'SELECT state,checkout_url FROM platform_payment_checkouts WHERE id=?',
)
.bind(checkout.id)
- .first<any>()
+ .first<{ state: string; checkout_url: string | null }>()
assert.equal(row.state, 'open')
assert.equal(row.checkout_url, checkout.url)
assert.ok((await paymentStatus(f.db, 1, 1)).pending)
@@ -4093,3 +4098,103 @@ test('details-stage abandonment requires a fresh empty provider session and no r
await f.mf.dispose()
}
})
+
+test('account erasure retains payable abandoned sessions until expiry is verified without a payment', async () => {
+ const f = await fixture()
+ try {
+ await syncDodoProducts(f.db, root, f.transport)
+ const checkout = await createPaymentCheckout(
+ f.db,
+ root,
+ 1,
+ input(),
+ origin,
+ f.transport,
+ f.now,
+ )
+ const { cancelDodoCheckout, closeDodoAccountPayments } =
+ await import('../packages/platform/src/dodo-payments.ts')
+ await cancelDodoCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ checkout.id,
+ f.transport,
+ f.now + 1000,
+ )
+ const calls = f.calls.length
+ assert.equal(
+ await closeDodoAccountPayments(f.db, root, 1, f.transport, f.now + 2000),
+ false,
+ )
+ assert.equal(
+ f.calls.length,
+ calls,
+ 'a still-payable session must remain fenced',
+ )
+ await f.db
+ .prepare(
+ "INSERT INTO platform_account_deletions(user_id,generation,source,reason,marked_at,purge_at,previous_suspended,previous_reason) VALUES(1,'abandon-purge','manual','Delete account',?,?,0,'')",
+ )
+ .bind(f.now, f.now + 1000)
+ .run()
+ const { purgeAccounts } =
+ await import('../packages/platform/src/account-purge.ts')
+ let billingCalls = 0,
+ domainCalls = 0
+ await purgeAccounts(
+ f.db,
+ {
+ billing: async () => {
+ billingCalls++
+ return false
+ },
+ domains: async () => {
+ domainCalls++
+ return true
+ },
+ storage: async () => true,
+ },
+ f.now + 2000,
+ )
+ assert.equal(billingCalls, 1)
+ assert.equal(domainCalls, 0)
+ assert.ok(
+ await f.db.prepare('SELECT id FROM platform_users WHERE id=1').first(),
+ )
+ const session = f.sessions.get(checkout.url.split('/').pop()!)!
+ const expiredAt = f.now + 86400000 + 2000
+ session.payment_id = 'pay_late_unresolved'
+ assert.equal(
+ await closeDodoAccountPayments(f.db, root, 1, f.transport, expiredAt),
+ false,
+ )
+ session.payment_id = null
+ session.payment_status = 'processing'
+ assert.equal(
+ await closeDodoAccountPayments(f.db, root, 1, f.transport, expiredAt),
+ false,
+ )
+ session.payment_status = null
+ const unavailable: typeof fetch = async () =>
+ Response.json({}, { status: 503 })
+ await assert.rejects(
+ closeDodoAccountPayments(f.db, root, 1, unavailable, expiredAt),
+ )
+ assert.equal(
+ await closeDodoAccountPayments(f.db, root, 1, f.transport, expiredAt),
+ true,
+ )
+ const row = await f.db
+ .prepare(
+ 'SELECT error,provider_id FROM platform_payment_checkouts WHERE id=?',
+ )
+ .bind(checkout.id)
+ .first<{ error: string; provider_id: string }>()
+ assert.equal(row.error, 'payment_checkout_expired')
+ assert.ok(row.provider_id)
+ } finally {
+ await f.mf.dispose()
+ }
+})
diff --git a/tests/saas-maps.test.ts b/tests/saas-maps.test.ts
index ca11f8bd..b70db12e 100644
--- a/tests/saas-maps.test.ts
+++ b/tests/saas-maps.test.ts
@@ -99,6 +99,16 @@ test('public map defaults, per-site overrides, membership and optimistic revisio
}),
{ code: 'map_style_invalid' },
)
+ await db
+ .prepare('UPDATE platform_map_settings SET config_json=? WHERE id=1')
+ .bind(
+ JSON.stringify({
+ ...defaults,
+ 'mapbox.token': 'sk.must_never_be_public',
+ }),
+ )
+ .run()
+ await assert.rejects(siteMaps(db, 2), { code: 'map_public_token_required' })
} finally {
await mf.dispose()
}
diff --git a/tests/site-viewer-navigation.test.ts b/tests/site-viewer-navigation.test.ts
index 89848168..e5531724 100644
--- a/tests/site-viewer-navigation.test.ts
+++ b/tests/site-viewer-navigation.test.ts
@@ -5,7 +5,7 @@ import vm from 'node:vm'
import ts from 'typescript'
import { computed, reactive, ref, watch, effectScope, nextTick } from 'vue'
function compile(source: string, context: Record<string, unknown>) {
- const exports: Record<string, any> = {}
+ const exports: Record<string, unknown> = {}
vm.runInNewContext(
ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS },
@@ -18,7 +18,13 @@ test('Site viewer route keeps scroll on open/close and preserves normal navigati
const router = compile(
readFileSync('apps/site/app/router.options.ts', 'utf8'),
{},
- ).default
+ ).default as {
+ scrollBehavior(
+ to: { name?: string; path?: string; hash?: string },
+ from: { name?: string; path?: string },
+ saved: unknown,
+ ): unknown
+ }
assert.equal(
router.scrollBehavior({ name: 'slug' }, { path: '/' }, null),
false,
@@ -39,6 +45,22 @@ test('Site viewer route keeps scroll on open/close and preserves normal navigati
),
{ top: 0 },
)
+ assert.equal(
+ router.scrollBehavior({ path: '/albums/summer' }, { name: 'slug' }, null),
+ false,
+ )
+ assert.deepEqual(
+ JSON.parse(
+ JSON.stringify(
+ router.scrollBehavior(
+ { path: '/photos', hash: '#summer' },
+ { path: '/' },
+ null,
+ ),
+ ),
+ ),
+ { el: '#summer' },
+ )
const saved = { left: 0, top: 900 }
assert.equal(
router.scrollBehavior({ path: '/' }, { name: 'slug' }, saved),