@@ -71,13 +71,10 @@ func (k *Keeper) RegisterNamespace(goCtx context.Context, msg *types.MsgRegister
7171}
7272
7373// CreatePost adds a new post to the specified (existing) namespace.
74- // The signer must have permission to create posts in that namespace.
74+ // Post creation is unrestricted: any valid signer may create a post in any
75+ // registered namespace. Duplicate (namespace, payload) pairs are rejected
76+ // because post_id is derived from them.
7577func (k * Keeper ) CreatePost (goCtx context.Context , msg * types.MsgCreatePost ) (* types.MsgCreatePostResponse , error ) {
76- policyId := k .GetPolicyId (goCtx )
77- if policyId == "" {
78- return nil , types .ErrInvalidPolicyId
79- }
80-
8178 ctx := sdk .UnwrapSDKContext (goCtx )
8279
8380 namespaceId := getNamespaceId (msg .Namespace )
@@ -90,18 +87,9 @@ func (k *Keeper) CreatePost(goCtx context.Context, msg *types.MsgCreatePost) (*t
9087 return nil , err
9188 }
9289
93- hasPermission , err := hasPermission (goCtx , k , policyId , namespaceId , types .CreatePostPermission , creatorDID , msg .Creator )
94- if err != nil {
95- return nil , err
96- }
97- if ! hasPermission {
98- return nil , types .ErrInvalidPostCreator
99- }
100-
10190 postId := types .GeneratePostId (namespaceId , msg .Payload )
10291
103- existingPost := k .getPost (goCtx , namespaceId , postId )
104- if existingPost != nil {
92+ if existing := k .getPost (goCtx , namespaceId , postId ); existing != nil {
10593 return nil , types .ErrPostAlreadyExists
10694 }
10795
@@ -127,6 +115,60 @@ func (k *Keeper) CreatePost(goCtx context.Context, msg *types.MsgCreatePost) (*t
127115 return & types.MsgCreatePostResponse {}, nil
128116}
129117
118+ // UpdatePost overwrites the payload of an existing post while preserving the
119+ // post_id. Authorization: the signer must either be the original creator, or
120+ // be a `collaborator` on the post's namespace (checked via the module's ACP
121+ // policy `update_post` permission).
122+ func (k * Keeper ) UpdatePost (goCtx context.Context , msg * types.MsgUpdatePost ) (* types.MsgUpdatePostResponse , error ) {
123+ ctx := sdk .UnwrapSDKContext (goCtx )
124+
125+ policyId := k .GetPolicyId (goCtx )
126+ if policyId == "" {
127+ return nil , types .ErrInvalidPolicyId
128+ }
129+
130+ namespaceId := getNamespaceId (msg .Namespace )
131+ if ! k .hasNamespace (goCtx , namespaceId ) {
132+ return nil , types .ErrNamespaceNotFound
133+ }
134+
135+ existing := k .getPost (goCtx , namespaceId , msg .PostId )
136+ if existing == nil {
137+ return nil , types .ErrPostNotFound
138+ }
139+
140+ updaterDID , err := k .GetAcpKeeper ().GetActorDID (ctx , msg .Creator )
141+ if err != nil {
142+ return nil , err
143+ }
144+
145+ if updaterDID != existing .CreatorDid {
146+ allowed , err := hasPermission (goCtx , k , policyId , namespaceId , types .UpdatePostPermission , updaterDID , msg .Creator )
147+ if err != nil {
148+ return nil , err
149+ }
150+ if ! allowed {
151+ return nil , types .ErrInvalidPostUpdater
152+ }
153+ }
154+
155+ existing .Payload = msg .Payload
156+ k .SetPost (goCtx , * existing )
157+
158+ b64Payload := base64 .StdEncoding .EncodeToString (existing .Payload )
159+ if err := ctx .EventManager ().EmitTypedEvent (& types.EventPostUpdated {
160+ NamespaceId : namespaceId ,
161+ PostId : existing .Id ,
162+ UpdaterDid : updaterDID ,
163+ Payload : b64Payload ,
164+ Artifact : msg .Artifact ,
165+ }); err != nil {
166+ return nil , err
167+ }
168+
169+ return & types.MsgUpdatePostResponse {}, nil
170+ }
171+
130172// AddCollaborator adds a new collaborator to the specified namespace.
131173// The signer must have permission to manage collaborators of that namespace object.
132174func (k * Keeper ) AddCollaborator (goCtx context.Context , msg * types.MsgAddCollaborator ) (* types.MsgAddCollaboratorResponse , error ) {
0 commit comments