@@ -38,7 +38,8 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
3838 async pushDocUpdates (
3939 workspaceId : string ,
4040 docId : string ,
41- updates : Uint8Array [ ]
41+ updates : Uint8Array [ ] ,
42+ editorId ?: string
4243 ) {
4344 if ( ! updates . length ) {
4445 return 0 ;
@@ -82,6 +83,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
8283 blob : Buffer . from ( update ) ,
8384 seq,
8485 createdAt : new Date ( createdAt ) ,
86+ createdBy : editorId || null ,
8587 } ;
8688 } ) ,
8789 } ) ;
@@ -113,6 +115,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
113115 return rows . map ( row => ( {
114116 bin : row . blob ,
115117 timestamp : row . createdAt . getTime ( ) ,
118+ editor : row . createdBy || undefined ,
116119 } ) ) ;
117120 }
118121
@@ -216,6 +219,12 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
216219 const histories = await this . db . snapshotHistory . findMany ( {
217220 select : {
218221 timestamp : true ,
222+ createdByUser : {
223+ select : {
224+ name : true ,
225+ avatarUrl : true ,
226+ } ,
227+ } ,
219228 } ,
220229 where : {
221230 workspaceId,
@@ -230,7 +239,10 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
230239 take : query . limit ,
231240 } ) ;
232241
233- return histories . map ( h => h . timestamp . getTime ( ) ) ;
242+ return histories . map ( h => ( {
243+ timestamp : h . timestamp . getTime ( ) ,
244+ editor : h . createdByUser ,
245+ } ) ) ;
234246 }
235247
236248 async getDocHistory ( workspaceId : string , docId : string , timestamp : number ) {
@@ -253,13 +265,15 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
253265 docId,
254266 bin : history . blob ,
255267 timestamp,
268+ editor : history . createdBy || undefined ,
256269 } ;
257270 }
258271
259272 override async rollbackDoc (
260273 spaceId : string ,
261274 docId : string ,
262- timestamp : number
275+ timestamp : number ,
276+ editorId ?: string
263277 ) : Promise < void > {
264278 await using _lock = await this . lockDocForUpdate ( spaceId , docId ) ;
265279 const toSnapshot = await this . getDocHistory ( spaceId , docId , timestamp ) ;
@@ -274,7 +288,14 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
274288 }
275289
276290 // force create a new history record after rollback
277- await this . createDocHistory ( fromSnapshot , true ) ;
291+ await this . createDocHistory (
292+ {
293+ ...fromSnapshot ,
294+ // override the editor to the one who requested the rollback
295+ editor : editorId ,
296+ } ,
297+ true
298+ ) ;
278299 // WARN:
279300 // we should never do the snapshot updating in recovering,
280301 // which is not the solution in CRDT.
@@ -331,6 +352,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
331352 id : snapshot . docId ,
332353 timestamp : new Date ( snapshot . timestamp ) ,
333354 blob : Buffer . from ( snapshot . bin ) ,
355+ createdBy : snapshot . editor ,
334356 expiredAt : new Date (
335357 Date . now ( ) + ( await this . options . historyMaxAge ( snapshot . spaceId ) )
336358 ) ,
@@ -374,6 +396,8 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
374396 docId,
375397 bin : snapshot . blob ,
376398 timestamp : snapshot . updatedAt . getTime ( ) ,
399+ // creator and editor may null if their account is deleted
400+ editor : snapshot . updatedBy || snapshot . createdBy || undefined ,
377401 } ;
378402 }
379403
@@ -396,10 +420,10 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
396420 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
397421 try {
398422 const result : { updatedAt : Date } [ ] = await this . db . $queryRaw `
399- INSERT INTO "snapshots" ("workspace_id", "guid", "blob", "created_at", "updated_at")
400- VALUES (${ spaceId } , ${ docId } , ${ bin } , DEFAULT, ${ updatedAt } )
423+ INSERT INTO "snapshots" ("workspace_id", "guid", "blob", "created_at", "updated_at", "created_by", "updated_by" )
424+ VALUES (${ spaceId } , ${ docId } , ${ bin } , DEFAULT, ${ updatedAt } , ${ snapshot . editor } , ${ snapshot . editor } )
401425 ON CONFLICT ("workspace_id", "guid")
402- DO UPDATE SET "blob" = ${ bin } , "updated_at" = ${ updatedAt }
426+ DO UPDATE SET "blob" = ${ bin } , "updated_at" = ${ updatedAt } , "updated_by" = ${ snapshot . editor }
403427 WHERE "snapshots"."workspace_id" = ${ spaceId } AND "snapshots"."guid" = ${ docId } AND "snapshots"."updated_at" <= ${ updatedAt }
404428 RETURNING "snapshots"."workspace_id" as "workspaceId", "snapshots"."guid" as "id", "snapshots"."updated_at" as "updatedAt"
405429 ` ;
0 commit comments