Commit 6309030
authored
fix(dotcom): clean up guest file_state rows when unsharing group-owned files (tldraw#9049)
In order to stop a file lingering in an ex-guest's recents (and
continuing to sync to them) after its owner revokes access, this PR
fixes the `delete_file_states` trigger so it cleans up both guest
records when a **group-owned** file is unshared. Fixes tldraw#9037.
Two records make a shared file visible to a guest, and neither was
cleaned up on unshare:
- **`file_state`** (sync/visit state). The trigger (from `000_seed.sql`)
decides which rows belong to "guests" with `OLD."ownerId" != "userId"`.
The groups backend (`023_groups.sql`) introduced group-owned files where
`ownerId` is `NULL` (per the `ownerId` XOR `owningGroupId` constraint).
For those files `NULL != "userId"` evaluates to `NULL`, so the `DELETE`
matched nothing and guest `file_state` rows survived — leaving the
now-private file syncing to ex-guests. Legacy (user-owned) files were
unaffected, which is why this only bites group files.
- **`group_file`** (the file link created when a visitor opens a shared
file, in their home group — home group id == user id). For
groups-migrated users the sidebar file list is built from these links
(`TldrawApp.getMyFiles`), so even with `file_state` cleaned up the
revoked file's name kept showing in an ex-guest's recent files. Nothing
deleted these links on unshare for any ownership model.
Migration `034` replaces the function so it cleans up both, for both
ownership models:
- `file_state`: delete states for anyone who isn't the legacy owner (`IS
DISTINCT FROM`, so a `NULL` `ownerId` no longer accidentally protects a
guest) or a current member of the owning group (`NOT EXISTS` against
`group_user`, a no-op for legacy files);
- `group_file`: delete the file's links except the owning group's own
row, the legacy owner's home-group link, and links of current
owning-group members — the same access rule, with the same NULL-safety.
The trigger binding (`file_shared_update`) is unchanged. Note: viewing
access itself is enforced at read time (and duplication needs source
read access since tldraw#9042) — this PR is about cleaning up the stale
per-user records that kept revoked files syncing and listed.
> [!NOTE]
> This migration started life as `033` and was amended in place, then
renamed to `034` after tldraw#9029's `033_rename_group_admin_role_to_member`
merged. The persistent preview DB has the stale
`033_fix_unshare_group_file_cleanup` in its ledger, so the
`reset-preview-db` label flow needs to run before re-testing on the
preview deploy.
### Change type
- [x] `bugfix`
### Test plan
This bug is only visible on **group-owned** files, so you need the
groups flag and two accounts (an owner and a guest). The cleanest way to
see the fix is to run the identical flow on current tldraw.com (the bug)
and on this PR's preview deploy (the fix), side by side.
**Setup (both accounts)**
1. Open the preview deploy at https://pr-9049-preview-deploy.tldraw.com/
and sign in.
2. Turn on the groups flag for each account: go to
https://pr-9049-preview-deploy.tldraw.com/admin, search for the email,
and enrol it in groups.
**Current tldraw.com behaviour (the bug)**
Run this flow on production https://tldraw.com/ (where the fix isn't
deployed):
1. As the owner, create a group, create a file **in that group**, and
enable sharing.
2. As the guest, open the share link — the file appears in their recent
files (creates a guest `file_state` and a `group_file` link).
3. As the owner, set sharing to "no access" (`shared = false`).
4. ❌ The file **stays** in the guest's recent files and keeps syncing to
them (its name is still visible). This is the regression.
**With this PR (the fix)**
Repeat the exact same flow on
https://pr-9049-preview-deploy.tldraw.com/:
1. Owner creates a group, creates a file in it, enables sharing.
2. Guest opens the share link — file appears in their recent files.
3. Owner sets sharing to "no access".
4. ✅ Both guest records are removed: the file stops syncing to the guest
**and** disappears from their recent files. Group members and the owner
keep their access and their recents entries.
(Sanity check, optional: a **legacy** personal file that's unshared
already cleaned up its `file_state` before this PR, but its `group_file`
links from migrated guests also linger — so unsharing a personal shared
file now also clears the guest's recents entry.)
Verified end to end on the local dev stack (two accounts, share → visit
→ unshare): the guest's `file_state` and `group_file` rows are deleted
by the trigger, the guest gets "Invite only" on the file URL, and the
file disappears from their recent files; the owner keeps access and
their recents entry.
- [x] Unit tests
`apps/dotcom/zero-cache/delete_file_states.test.ts` is a focused
postgres integration test that loads the real shipped function body from
migration `034` (rather than a hand-copied duplicate) and covers
group-owned unshare, legacy unshare, and a still-shared control — for
both `file_state` and `group_file` cleanup. It also runs the original
buggy function body as a regression guard, proving the old function left
the group guest's state and link behind and the new one removes them.
The trigger is `plpgsql`, so it needs a real postgres — the suite is
opt-in via `ZERO_CACHE_TEST_POSTGRES_URL` (a **direct** connection
string; the suite is also hardened against transaction-pooled
connections by schema-qualifying everything it owns and running verbatim
SQL under `SET LOCAL search_path` in transactions) and skips cleanly
(exit 0) when unset, so CI stays green. `zero-cache` had no vitest
project, so this PR also adds `apps/dotcom/zero-cache/vitest.config.ts`.
Verified locally: 7/7 pass against the dev postgres; skips with exit 0
without a DB; `yarn typecheck` passes.
### Release notes
- Fix tldraw.com files lingering in a guest's recent files (and
continuing to sync to them) after the owner revokes access to a
group-owned file.
### Code changes
| Section | LOC change |
| -------------- | ---------- |
| Tests | +250 / -0 |
| Apps | +56 / -0 |
| Config/tooling | +15 / -0 |1 parent 98e46ae commit 6309030
2 files changed
Lines changed: 338 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
Lines changed: 58 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
0 commit comments