@@ -21,6 +21,7 @@ import {
21
21
SendDwnRequest ,
22
22
PermissionsApi ,
23
23
AgentPermissionsApi ,
24
+ getRecordProtocolRole
24
25
} from '@web5/agent' ;
25
26
26
27
import { Convert , isEmptyObject , NodeStream , removeUndefinedProperties , Stream } from '@web5/common' ;
@@ -183,6 +184,9 @@ export type RecordDeleteParams = {
183
184
184
185
/** The timestamp indicating when the record was deleted. */
185
186
dateModified ?: DwnMessageDescriptor [ DwnInterface . RecordsDelete ] [ 'messageTimestamp' ] ;
187
+
188
+ /** The protocol role under which this record will be deleted. */
189
+ protocolRole ?: string ;
186
190
} ;
187
191
188
192
/**
@@ -311,7 +315,6 @@ export class Record implements RecordModel {
311
315
/** Tags of the record */
312
316
get tags ( ) { return this . _recordsWriteDescriptor ?. tags ; }
313
317
314
-
315
318
// Getters for for properties that depend on the current state of the Record.
316
319
/** DID that is the logical author of the Record. */
317
320
get author ( ) : string { return this . _author ; }
@@ -703,7 +706,7 @@ export class Record implements RecordModel {
703
706
*
704
707
* @beta
705
708
*/
706
- async update ( { dateModified, data, ...params } : RecordUpdateParams ) : Promise < DwnResponseStatus > {
709
+ async update ( { dateModified, data, protocolRole , ...params } : RecordUpdateParams ) : Promise < DwnResponseStatus > {
707
710
708
711
if ( this . deleted ) {
709
712
throw new Error ( 'Record: Cannot revive a deleted record.' ) ;
@@ -718,6 +721,7 @@ export class Record implements RecordModel {
718
721
...descriptor ,
719
722
...params ,
720
723
parentContextId,
724
+ protocolRole : protocolRole ?? this . _protocolRole , // Use the current protocolRole if not provided.
721
725
messageTimestamp : dateModified , // Map Record class `dateModified` property to DWN SDK `messageTimestamp`
722
726
recordId : this . _recordId
723
727
} ;
@@ -786,7 +790,7 @@ export class Record implements RecordModel {
786
790
787
791
// Only update the local Record instance mutable properties if the record was successfully (over)written.
788
792
this . _authorization = responseMessage . authorization ;
789
- this . _protocolRole = params . protocolRole ;
793
+ this . _protocolRole = updateMessage . protocolRole ;
790
794
mutableDescriptorProperties . forEach ( property => {
791
795
this . _descriptor [ property ] = responseMessage . descriptor [ property ] ;
792
796
} ) ;
@@ -834,15 +838,19 @@ export class Record implements RecordModel {
834
838
store
835
839
} ;
836
840
837
- if ( this . deleted ) {
838
- // if we have a delete message we can just use it
841
+ // Check to see if the provided protocolRole within the deleteParams is different from the current protocolRole.
842
+ const differentRole = deleteParams ?. protocolRole ? getRecordProtocolRole ( this . rawMessage ) !== deleteParams . protocolRole : false ;
843
+ // If the record is already in a deleted state but the protocolRole is different, we need to construct a delete message with the new protocolRole
844
+ // otherwise we can just use the existing delete message.
845
+ if ( this . deleted && ! differentRole ) {
839
846
deleteOptions . rawMessage = this . rawMessage as DwnMessage [ DwnInterface . RecordsDelete ] ;
840
847
} else {
841
848
// otherwise we construct a delete message given the `RecordDeleteParams`
842
849
deleteOptions . messageParams = {
843
850
prune : prune ,
844
851
recordId : this . _recordId ,
845
852
messageTimestamp : dateModified ,
853
+ protocolRole : deleteParams ?. protocolRole ?? this . _protocolRole // if no protocolRole is provided, use the current protocolRole
846
854
} ;
847
855
}
848
856
@@ -1023,7 +1031,7 @@ export class Record implements RecordModel {
1023
1031
private async readRecordData ( { target, isRemote } : { target : string , isRemote : boolean } ) {
1024
1032
const readRequest : ProcessDwnRequest < DwnInterface . RecordsRead > = {
1025
1033
author : this . _connectedDid ,
1026
- messageParams : { filter : { recordId : this . id } } ,
1034
+ messageParams : { filter : { recordId : this . id } , protocolRole : this . _protocolRole } ,
1027
1035
messageType : DwnInterface . RecordsRead ,
1028
1036
target,
1029
1037
} ;
0 commit comments