/
msg_server_add_editors.go
50 lines (38 loc) · 1.08 KB
/
msg_server_add_editors.go
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
package keeper
import (
"context"
"encoding/json"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v3/x/filetree/types"
)
func (k msgServer) AddEditors(goCtx context.Context, msg *types.MsgAddEditors) (*types.MsgAddEditorsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
file, found := k.GetFiles(ctx, msg.Address, msg.Fileowner)
if !found {
return nil, types.ErrFileNotFound
}
// Only the owner can add editors
isOwner := IsOwner(file, msg.Creator)
if !isOwner {
return nil, types.ErrCannotAllowEdit
}
peacc := file.EditAccess
jeacc := make(map[string]string)
if err := json.Unmarshal([]byte(peacc), &jeacc); err != nil {
return nil, types.ErrCantUnmarshall
}
ids := strings.Split(msg.EditorIds, ",")
keys := strings.Split(msg.EditorKeys, ",")
for i, v := range ids {
jeacc[v] = keys[i]
}
eaccbytes, err := json.Marshal(jeacc)
if err != nil {
return nil, types.ErrCantMarshall
}
newEditors := string(eaccbytes)
file.EditAccess = newEditors
k.SetFiles(ctx, file)
return &types.MsgAddEditorsResponse{}, nil
}