Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostReaction struct improvements #160

Merged
merged 15 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Version 0.6.0
## Changes
- Implemented tags in post medias (#118)
- Edited PostReaction struct to allow a better integration with middle layer applications (#157)
leobragaz marked this conversation as resolved.
Show resolved Hide resolved

# Version 0.5.1
## Bug fixes
Expand Down
37 changes: 21 additions & 16 deletions x/genutil/legacy/v0.6.0/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,40 @@ package v060
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/genutil"
v040posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.4.0"
v060posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.6.0"
v040profiles "github.com/desmos-labs/desmos/x/profile/legacy/v0.4.0"
v060profiles "github.com/desmos-labs/desmos/x/profile/legacy/v0.6.0"
)

// migrateProfilesModule migrates the state of profiles from v0.4.0 to a v0.6.0 genesis state.
func migrateProfilesModule(cdc *codec.Codec, appState genutil.AppMap) genutil.AppMap {
v040Codec := codec.New()
codec.RegisterCrypto(v040Codec)
// Migrate migrates exported state from v0.5.0 to a v0.6.0 genesis state.
func Migrate(appState genutil.AppMap, _ ...interface{}) genutil.AppMap {
v050Codec := codec.New()
codec.RegisterCrypto(v050Codec)

v060Codec := codec.New()
codec.RegisterCrypto(v060Codec)

// Migrate posts state
if appState[v040posts.ModuleName] != nil {
var genDocs v040posts.GenesisState
v050Codec.MustUnmarshalJSON(appState[v040posts.ModuleName], &genDocs)

appState[v040posts.ModuleName] = v060Codec.MustMarshalJSON(
v060posts.Migrate(genDocs),
)
}

// Migrate profile state
if appState[v040profiles.ModuleName] != nil {
var genDocs v040profiles.GenesisState
v040Codec.MustUnmarshalJSON(appState[v040profiles.ModuleName], &genDocs)
v050Codec.MustUnmarshalJSON(appState[v040profiles.ModuleName], &genDocs)

delete(appState, v040profiles.ModuleName)
appState[v060profiles.ModuleName] = cdc.MustMarshalJSON(
appState[v060profiles.ModuleName] = v060Codec.MustMarshalJSON(
genDocs,
)
}

return appState
}

// Migrate migrates exported state from v0.5.0 to a v0.6.0 genesis state.
func Migrate(appState genutil.AppMap, _ ...interface{}) genutil.AppMap {
v060Codec := codec.New()
codec.RegisterCrypto(v060Codec)

appState = migrateProfilesModule(v060Codec, appState)

return appState
}
4 changes: 2 additions & 2 deletions x/posts/internal/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func handleMsgAddPostReaction(ctx sdk.Context, keeper Keeper, msg types.MsgAddPo
return nil, err
}

postReaction := types.NewPostReaction(reactionShortcode, msg.User)
postReaction := types.NewPostReaction(reactionShortcode, reactionValue, msg.User)
if err := keeper.SavePostReaction(ctx, post.PostID, postReaction); err != nil {
return nil, err
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func handleMsgRemovePostReaction(ctx sdk.Context, keeper Keeper, msg types.MsgRe
}

// Remove the reaction
reaction := types.NewPostReaction(reactionShortcode, msg.User)
reaction := types.NewPostReaction(reactionShortcode, reactionValue, msg.User)
if err := keeper.RemovePostReaction(ctx, post.PostID, reaction); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
Expand Down
30 changes: 19 additions & 11 deletions x/posts/internal/keeper/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,22 @@ func Test_handleMsgAddPostReaction(t *testing.T) {
require.True(t, test.existingPost.Equals(storedPost))

// Check the post reactions
reactValue := test.msg.Reaction
if e, err := emoji.LookupEmoji(reactValue); err == nil {
reactValue = e.Shortcodes[0]
var reactValue, reactShortcode string
if e, err := emoji.LookupEmoji(test.msg.Reaction); err == nil {
reactShortcode = e.Shortcodes[0]
reactValue = e.Value
} else {
e, err := emoji.LookupEmojiByCode(test.msg.Reaction)
if err != nil {
panic(err)
}
reactShortcode = e.Shortcodes[0]
reactValue = e.Value
}

var storedReactions types.PostReactions
k.Cdc.MustUnmarshalBinaryBare(store.Get(types.PostReactionsStoreKey(storedPost.PostID)), &storedReactions)
require.Contains(t, storedReactions, types.NewPostReaction(reactValue, test.msg.User))
require.Contains(t, storedReactions, types.NewPostReaction(reactShortcode, reactValue, test.msg.User))

// Check the registered reactions
registeredReactions := k.GetRegisteredReactions(ctx)
Expand Down Expand Up @@ -421,13 +429,13 @@ func Test_handleMsgRemovePostReaction(t *testing.T) {
require.NoError(t, err)

regReaction := types.NewReaction(user, ":reaction:", "react", testPost.Subspace)
reaction := types.NewPostReaction(":reaction:", user)
emojiShortcodeReaction := types.NewPostReaction(":smile:", user)
reaction := types.NewPostReaction(":reaction:", "react", user)
emojiShortcodeReaction := types.NewPostReaction(":smile:", "react", user)

emoji, err := emoji.LookupEmojiByCode(":+1:")
require.NoError(t, err)

emojiReaction := types.NewPostReaction(emoji.Shortcodes[0], user)
emojiReaction := types.NewPostReaction(emoji.Shortcodes[0], emoji.Value, user)

tests := []struct {
name string
Expand All @@ -454,7 +462,7 @@ func Test_handleMsgRemovePostReaction(t *testing.T) {
existingPost: &post,
existingReaction: &reaction,
registeredReaction: &regReaction,
msg: types.NewMsgRemovePostReaction(post.PostID, user, reaction.Value),
msg: types.NewMsgRemovePostReaction(post.PostID, user, reaction.Shortcode),
error: nil,
expEvent: sdk.NewEvent(
types.EventTypePostReactionRemoved,
Expand All @@ -468,14 +476,14 @@ func Test_handleMsgRemovePostReaction(t *testing.T) {
name: "Removing a reaction using the code works properly (emoji shortcode)",
existingPost: &post,
existingReaction: &emojiShortcodeReaction,
msg: types.NewMsgRemovePostReaction(post.PostID, user, emojiShortcodeReaction.Value),
msg: types.NewMsgRemovePostReaction(post.PostID, user, emojiShortcodeReaction.Shortcode),
error: nil,
expEvent: sdk.NewEvent(
types.EventTypePostReactionRemoved,
sdk.NewAttribute(types.AttributeKeyPostID, post.PostID.String()),
sdk.NewAttribute(types.AttributeKeyPostReactionOwner, user.String()),
sdk.NewAttribute(types.AttributeKeyPostReactionValue, "😄"),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, emojiShortcodeReaction.Value),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, emojiShortcodeReaction.Shortcode),
),
},
{
Expand All @@ -489,7 +497,7 @@ func Test_handleMsgRemovePostReaction(t *testing.T) {
sdk.NewAttribute(types.AttributeKeyPostID, post.PostID.String()),
sdk.NewAttribute(types.AttributeKeyPostReactionOwner, user.String()),
sdk.NewAttribute(types.AttributeKeyPostReactionValue, emoji.Value),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, emojiReaction.Value),
sdk.NewAttribute(types.AttributeKeyReactionShortCode, emojiReaction.Shortcode),
),
},
}
Expand Down
4 changes: 2 additions & 2 deletions x/posts/internal/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestInvariants(t *testing.T) {

answers := []types.AnswerID{types.AnswerID(1), types.AnswerID(2)}

postReaction := types.NewPostReaction(":like:", user)
postReaction := types.NewPostReaction(":like:", "+1", user)
reaction := types.NewReaction(testPostOwner, ":like:", "+1", testPost.Subspace)
answer := types.NewUserAnswer(answers, testPostOwner)

Expand Down Expand Up @@ -106,7 +106,7 @@ func TestInvariants(t *testing.T) {
answers: nil,
postReaction: &postReaction,
reaction: &reaction,
expResponse: "posts: posts reactions refers to non existing posts invariant\nThe following reactions refer to posts that do not exist:\n {\"owner\":\"cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns\",\"value\":\":like:\"}\n\n",
expResponse: "posts: posts reactions refers to non existing posts invariant\nThe following reactions refer to posts that do not exist:\n {\"owner\":\"cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns\",\"shortcode\":\":like:\",\"value\":\"+1\"}\n\n",
expBool: true,
},
{
Expand Down
10 changes: 5 additions & 5 deletions x/posts/internal/keeper/keeper_reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func (k Keeper) SavePostReaction(ctx sdk.Context, postID types.PostID, reaction
k.Cdc.MustUnmarshalBinaryBare(store.Get(key), &reactions)

// Check for double reactions
if reactions.ContainsReactionFrom(reaction.Owner, reaction.Value) {
if reactions.ContainsReactionFrom(reaction.Owner, reaction.Shortcode) {
return fmt.Errorf("%s has already reacted with %s to the post with id %s",
reaction.Owner, reaction.Value, postID)
reaction.Owner, reaction.Shortcode, postID)
}

// Save the new reaction
Expand All @@ -51,12 +51,12 @@ func (k Keeper) RemovePostReaction(ctx sdk.Context, postID types.PostID, reactio
k.Cdc.MustUnmarshalBinaryBare(store.Get(key), &reactions)

// Check if the user exists
if !reactions.ContainsReactionFrom(reaction.Owner, reaction.Value) {
return fmt.Errorf("cannot remove the reaction with value %s from user %s as it does not exist", reaction.Value, reaction.Owner)
if !reactions.ContainsReactionFrom(reaction.Owner, reaction.Shortcode) {
return fmt.Errorf("cannot remove the reaction with value %s from user %s as it does not exist", reaction.Shortcode, reaction.Owner)
}

// Remove and save the reactions list
if newLikes, edited := reactions.RemoveReaction(reaction.Owner, reaction.Value); edited {
if newLikes, edited := reactions.RemoveReaction(reaction.Owner, reaction.Shortcode); edited {
if len(newLikes) == 0 {
store.Delete(key)
} else {
Expand Down
68 changes: 32 additions & 36 deletions x/posts/internal/keeper/keeper_reactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ func TestKeeper_SaveReaction(t *testing.T) {
require.NoError(t, err)

tests := []struct {
name string
storedReaction types.PostReactions
postID types.PostID
reaction types.PostReaction
storedPost types.Post
registeredReaction types.Reaction
error error
expectedStored types.PostReactions
name string
storedReaction types.PostReactions
postID types.PostID
reaction types.PostReaction
storedPost types.Post
error error
expectedStored types.PostReactions
}{
{
name: "Reaction from same user already present returns expError",
storedReaction: types.PostReactions{types.NewPostReaction(":like:", liker)},
storedReaction: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
postID: id,
reaction: types.NewPostReaction(":like:", liker),
reaction: types.NewPostReaction(":like:", "👍", liker),
storedPost: types.NewPost(
id,
testPost.ParentID,
Expand All @@ -47,16 +46,14 @@ func TestKeeper_SaveReaction(t *testing.T) {
testPost.Created,
testPost.Creator,
),
registeredReaction: types.NewReaction(liker, ":like:", "https://smile.jpg",
"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"),
error: fmt.Errorf("cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4 has already reacted with :like: to the post with id 19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af"),
expectedStored: types.PostReactions{types.NewPostReaction(":like:", liker)},
expectedStored: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
},
{
name: "First liker is stored properly",
storedReaction: types.PostReactions{},
postID: id,
reaction: types.NewPostReaction(":like:", liker),
reaction: types.NewPostReaction(":like:", "👍", liker),
storedPost: types.NewPost(
id,
testPost.ParentID,
Expand All @@ -67,16 +64,14 @@ func TestKeeper_SaveReaction(t *testing.T) {
testPost.Created,
testPost.Creator,
),
registeredReaction: types.NewReaction(liker, ":like:", "https://smile.jpg",
"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"),
error: nil,
expectedStored: types.PostReactions{types.NewPostReaction(":like:", liker)},
expectedStored: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
},
{
name: "Second liker is stored properly",
storedReaction: types.PostReactions{types.NewPostReaction(":like:", liker)},
storedReaction: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
postID: id,
reaction: types.NewPostReaction(":like:", otherLiker),
reaction: types.NewPostReaction(":like:", "👍", otherLiker),
storedPost: types.NewPost(
id,
testPost.ParentID,
Expand All @@ -87,12 +82,10 @@ func TestKeeper_SaveReaction(t *testing.T) {
testPost.Created,
testPost.Creator,
),
registeredReaction: types.NewReaction(liker, ":like:", "https://smile.jpg",
"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"),
error: nil,
expectedStored: types.PostReactions{
types.NewPostReaction(":like:", liker),
types.NewPostReaction(":like:", otherLiker),
types.NewPostReaction(":like:", "👍", liker),
types.NewPostReaction(":like:", "👍", otherLiker),
},
},
}
Expand All @@ -108,7 +101,6 @@ func TestKeeper_SaveReaction(t *testing.T) {
}

k.SavePost(ctx, test.storedPost)
k.RegisterReaction(ctx, test.registeredReaction)

err := k.SavePostReaction(ctx, test.postID, test.reaction)
require.Equal(t, test.error, err)
Expand All @@ -131,15 +123,17 @@ func TestKeeper_RemoveReaction(t *testing.T) {
postID types.PostID
liker sdk.AccAddress
value string
shortcode string
error error
expectedStored types.PostReactions
}{
{
name: "PostReaction from same liker is removed properly",
storedLikes: types.PostReactions{types.NewPostReaction(":like:", liker)},
storedLikes: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
postID: id,
liker: liker,
value: ":like:",
shortcode: ":like:",
value: "👍",
error: nil,
expectedStored: types.PostReactions{},
},
Expand All @@ -148,18 +142,20 @@ func TestKeeper_RemoveReaction(t *testing.T) {
storedLikes: types.PostReactions{},
postID: id,
liker: liker,
value: ":like:",
shortcode: ":like:",
value: "👍",
error: fmt.Errorf("cannot remove the reaction with value :like: from user cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4 as it does not exist"),
expectedStored: types.PostReactions{},
},
{
name: "Non existing reaction returns error - Reaction",
storedLikes: types.PostReactions{types.NewPostReaction(":like:", liker)},
storedLikes: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
postID: id,
liker: liker,
value: ":smile:",
shortcode: ":smile:",
value: "😊",
error: fmt.Errorf("cannot remove the reaction with value :smile: from user cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4 as it does not exist"),
expectedStored: types.PostReactions{types.NewPostReaction(":like:", liker)},
expectedStored: types.PostReactions{types.NewPostReaction(":like:", "👍", liker)},
},
}

Expand All @@ -173,7 +169,7 @@ func TestKeeper_RemoveReaction(t *testing.T) {
store.Set(types.PostReactionsStoreKey(test.postID), k.Cdc.MustMarshalBinaryBare(&test.storedLikes))
}

err := k.RemovePostReaction(ctx, test.postID, types.NewPostReaction(test.value, test.liker))
err := k.RemovePostReaction(ctx, test.postID, types.NewPostReaction(test.shortcode, test.value, test.liker))
require.Equal(t, test.error, err)

var stored types.PostReactions
Expand Down Expand Up @@ -210,8 +206,8 @@ func TestKeeper_GetPostReactions(t *testing.T) {
{
name: "Valid list of likes is returned properly",
likes: types.PostReactions{
types.NewPostReaction(":smile:", otherLiker),
types.NewPostReaction(":smile:", liker),
types.NewPostReaction(":smile:", "😊", otherLiker),
types.NewPostReaction(":smile:", "😊", liker),
},
postID: id,
storedPost: testPost,
Expand Down Expand Up @@ -262,11 +258,11 @@ func TestKeeper_GetReactions(t *testing.T) {
name: "Non empty likes data are returned correcly",
likes: map[string]types.PostReactions{
id: {
types.NewPostReaction("reaction", liker1),
types.NewPostReaction("reaction", liker2),
types.NewPostReaction(":smile:", "😊", liker1),
types.NewPostReaction(":smile:", "😊", liker2),
},
id2: {
types.NewPostReaction("reaction", liker1),
types.NewPostReaction(":smile:", "😊", liker1),
},
},
},
Expand Down
Loading