/
keeper_reactions.go
106 lines (85 loc) · 3.84 KB
/
keeper_reactions.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package keeper
import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v2/x/staging/posts/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// SavePostReaction allows to save the given reaction inside the store.
// It assumes that the given reaction is valid and already registered.
// If another reaction from the same user for the same post and with the same value exists, returns an expError.
func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) error {
store := ctx.KVStore(k.storeKey)
key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode)
// Check for double reactions
if store.Has(key) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"%s has already reacted with %s to the post with id %s",
reaction.Owner, reaction.ShortCode, reaction.PostID)
}
// Save the new reaction
store.Set(key, types.MustMarshalPostReaction(k.cdc, reaction))
return nil
}
// DeletePostReaction removes the reaction from the given data.
// If no reaction with the same value was previously added from the given user, an expError
// is returned.
func (k Keeper) DeletePostReaction(ctx sdk.Context, reaction types.PostReaction) error {
store := ctx.KVStore(k.storeKey)
key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode)
if !store.Has(key) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"cannot remove the reaction with value %s from user %s as it does not exist",
reaction.ShortCode, reaction.Owner)
}
store.Delete(key)
return nil
}
// GetAllRegisteredReactions returns all the post reactions
func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction {
var reactions []types.PostReaction
k.IteratePostReactions(ctx, func(_ int64, reaction types.PostReaction) bool {
reactions = append(reactions, reaction)
return false
})
return reactions
}
// GetPostReaction returns the post reaction for the given postID, owner and short code.
// If such reaction does not exist, returns false instead.
func (k Keeper) GetPostReaction(ctx sdk.Context, postID, owner, shortCode string) (types.PostReaction, bool) {
store := ctx.KVStore(k.storeKey)
key := types.PostReactionsStoreKey(postID, owner, shortCode)
if !store.Has(key) {
return types.PostReaction{}, false
}
return types.MustUnmarshalPostReaction(k.cdc, store.Get(key)), true
}
// ___________________________________________________________________________________________________________________
// SaveRegisteredReaction allows to register a new reaction for later reference
func (k Keeper) SaveRegisteredReaction(ctx sdk.Context, reaction types.RegisteredReaction) {
store := ctx.KVStore(k.storeKey)
store.Set(types.RegisteredReactionsStoreKey(reaction.Subspace, reaction.ShortCode), k.cdc.MustMarshal(&reaction))
k.Logger(ctx).Info("registered reaction", "shortcode", reaction.ShortCode, "subspace", reaction.Subspace)
}
// GetRegisteredReaction returns the registered reactions which has the given shortcode
// and is registered to be used inside the given subspace.
// If no reaction could be found, returns false instead.
func (k Keeper) GetRegisteredReaction(
ctx sdk.Context, shortcode string, subspace string,
) (reaction types.RegisteredReaction, exist bool) {
store := ctx.KVStore(k.storeKey)
key := types.RegisteredReactionsStoreKey(subspace, shortcode)
if !store.Has(key) {
return types.RegisteredReaction{}, false
}
k.cdc.MustUnmarshal(store.Get(key), &reaction)
return reaction, true
}
// GetRegisteredReactions returns all the registered reactions
func (k Keeper) GetRegisteredReactions(ctx sdk.Context) []types.RegisteredReaction {
var reactions []types.RegisteredReaction
k.IterateRegisteredReactions(ctx, func(_ int64, reaction types.RegisteredReaction) bool {
reactions = append(reactions, reaction)
return false
})
return reactions
}