Skip to content

Commit

Permalink
Soft deleting of files (tldraw#4992)
Browse files Browse the repository at this point in the history
Instead of deleting the files we now soft delete them. I kept the whole
delete logic the same as before, which should allow us to delete the
file from postgres which will trigger all other necessary file deletes.

### Change type

- [ ] `bugfix`
- [ ] `improvement`
- [x] `feature`
- [ ] `api`
- [ ] `other`

### Release notes

- Add soft deleting of files.

---------

Co-authored-by: David Sheldrick <[email protected]>
  • Loading branch information
MitjaBezensek and ds300 authored Nov 25, 2024
1 parent 5bcd587 commit d9448fa
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
9 changes: 5 additions & 4 deletions apps/dotcom/client/src/tla/app/TldrawApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ export class TldrawApp {
const nextRecentFileOrdering = []

for (const fileId of myFileIds) {
const file = myFiles[fileId]
if (!file) continue
const existing = this.lastRecentFileOrdering?.find((f) => f.fileId === fileId)
if (existing) {
nextRecentFileOrdering.push(existing)
continue
}
const file = myFiles[fileId]
if (!file) continue
const state = myStates[fileId]

nextRecentFileOrdering.push({
Expand Down Expand Up @@ -238,6 +238,7 @@ export class TldrawApp {
sharedLinkType: 'edit',
thumbnail: '',
updatedAt: Date.now(),
isDeleted: false,
}
if (typeof fileOrId === 'object') {
Object.assign(file, fileOrId)
Expand Down Expand Up @@ -420,7 +421,7 @@ export class TldrawApp {
this.z.mutate((tx) => {
tx.file_state.delete({ fileId, userId: this.userId })
if (file?.ownerId === this.userId) {
tx.file.delete({ id: fileId })
tx.file.update({ ...file, isDeleted: true })
}
})
}
Expand Down Expand Up @@ -458,7 +459,7 @@ export class TldrawApp {
async getOrCreateFileState(fileId: string) {
let fileState = this.getFileState(fileId)
if (!fileState) {
await this.z.mutate.file_state.create({
this.z.mutate.file_state.create({
fileId,
userId: this.userId,
firstVisitAt: Date.now(),
Expand Down
2 changes: 1 addition & 1 deletion apps/dotcom/client/src/tla/app/zero-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Zero {
query = {
file: this.makeQuery(
'file',
computed('files', () => this.store.getFullData()?.files)
computed('files', () => this.store.getFullData()?.files.filter((f) => !f.isDeleted))
),
file_state: this.makeQuery(
'file_state',
Expand Down
7 changes: 7 additions & 0 deletions apps/dotcom/sync-worker/src/TLDrawDurableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ export class TLDrawDurableObject extends DurableObject {
const file = await this.getAppFileRecord()

if (file) {
if (file.isDeleted) {
return closeSocket(TLSyncErrorCloseEventReason.NOT_FOUND)
}
if (!auth && !file.shared) {
return closeSocket(TLSyncErrorCloseEventReason.NOT_AUTHENTICATED)
}
Expand Down Expand Up @@ -623,6 +626,10 @@ export class TLDrawDurableObject extends DurableObject {
const roomIsReadOnlyForGuests = file.shared && file.sharedLinkType === 'view'

for (const session of room.getSessions()) {
if (file.isDeleted) {
room.closeSession(session.sessionId, TLSyncErrorCloseEventReason.NOT_FOUND)
continue
}
// allow the owner to stay connected
if (session.meta.userId === file.ownerId) continue

Expand Down
1 change: 1 addition & 0 deletions apps/dotcom/zero-cache/docker/006_add_file_soft_delete.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE public.file ADD COLUMN "isDeleted" BOOLEAN NOT NULL DEFAULT FALSE;
1 change: 1 addition & 0 deletions packages/dotcom-shared/src/tlaSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const tlaFileSchema = {
createdAt: { type: 'number' },
updatedAt: { type: 'number' },
isEmpty: { type: 'boolean' },
isDeleted: { type: 'boolean' },
},
primaryKey: ['id'],
relationships: {
Expand Down

0 comments on commit d9448fa

Please sign in to comment.