Skip to content

Commit 98541f1

Browse files
authored
server-side-init followup (tldraw#7066)
accidentally merged tldraw#7058 without these commits ### Change type - [x] `other` <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Triggers a welcome dialog on first-time user init and hardens server-side user creation with a duplicate-check and unified timestamps. > > - **Client (TldrawApp)** > - Show welcome dialog after initializing a brand-new user by updating local session state (`updateLocalSessionState` in `TldrawApp.create`). > - **Sync Worker (TLUserDurableObject)** > - Guard user/group creation inside transaction to avoid duplicate inserts if concurrent requests race. > - Use a single `now` timestamp for `createdAt`/`updatedAt` across inserted rows. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit a90eac1. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent ef0eba1 commit 98541f1

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

apps/dotcom/client/src/tla/app/TldrawApp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { getScratchPersistenceKey } from '../../utils/scratch-persistence-key'
6666
import { TLAppUiContextType } from '../utils/app-ui-events'
6767
import { getDateFormat } from '../utils/dates'
6868
import { createIntl, defineMessages, setupCreateIntl } from '../utils/i18n'
69+
import { updateLocalSessionState } from '../utils/local-session-state'
6970
import { Zero as ZeroPolyfill } from './zero-polyfill'
7071

7172
export const TLDR_FILE_ENDPOINT = `/api/app/tldr`
@@ -782,6 +783,7 @@ export class TldrawApp {
782783
})
783784

784785
opts.trackEvent('create-user', { source: 'app' })
786+
updateLocalSessionState((state) => ({ ...state, shouldShowWelcomeDialog: true }))
785787
}
786788
return { app, userId: user.id }
787789
}

apps/dotcom/sync-worker/src/TLUserDurableObject.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ export class TLUserDurableObject extends DurableObject<Environment> {
9292
assert(clerkUser, 'Clerk user not found')
9393
await this.env.USER_DO_SNAPSHOTS.delete(getUserDoSnapshotKey(this.env, id))
9494
await this.db.transaction().execute(async (tx) => {
95+
// check that user wasn't added by another request in between the auth check and the snapshot deletion
96+
if (await tx.selectFrom('user').where('id', '=', id).select('id').executeTakeFirst()) {
97+
return
98+
}
99+
const now = Date.now()
95100
await tx
96101
.insertInto('user')
97102
.values({
@@ -104,8 +109,8 @@ export class TLUserDurableObject extends DurableObject<Environment> {
104109
exportTheme: 'light',
105110
exportBackground: true,
106111
exportPadding: true,
107-
createdAt: Date.now(),
108-
updatedAt: Date.now(),
112+
createdAt: now,
113+
updatedAt: now,
109114
flags: '',
110115
})
111116
.execute()
@@ -114,8 +119,8 @@ export class TLUserDurableObject extends DurableObject<Environment> {
114119
.values({
115120
id,
116121
name: clerkUser.fullName ?? '',
117-
createdAt: Date.now(),
118-
updatedAt: Date.now(),
122+
createdAt: now,
123+
updatedAt: now,
119124
isDeleted: false,
120125
inviteSecret: null,
121126
})
@@ -125,8 +130,8 @@ export class TLUserDurableObject extends DurableObject<Environment> {
125130
.values({
126131
userId: id,
127132
groupId: id,
128-
createdAt: Date.now(),
129-
updatedAt: Date.now(),
133+
createdAt: now,
134+
updatedAt: now,
130135
role: 'owner',
131136
index: 'a1' as IndexKey,
132137
userName: clerkUser.fullName ?? '',

0 commit comments

Comments
 (0)