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 2 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +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.0
## Changes
- Implemented invariants for posts and profile modules (#90)
Expand Down
2 changes: 2 additions & 0 deletions x/genutil/client/cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
v030 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.3.0"
v040 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.4.0"
v050 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.5.0"
v060 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.6.0"
"github.com/spf13/cobra"
tm "github.com/tendermint/tendermint/types"
)
Expand All @@ -27,6 +28,7 @@ var migrationMap = map[string]types.MigrationCallback{
"v0.3.0": v030.Migrate,
"v0.4.0": v040.Migrate,
"v0.5.0": v050.Migrate,
"v0.6.0": v060.Migrate,
}

const (
Expand Down
34 changes: 34 additions & 0 deletions x/genutil/legacy/v0.6.0/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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"
)

func migratePostModule(cdc *codec.Codec, appState genutil.AppMap) genutil.AppMap {
v040Codec := codec.New()
codec.RegisterCrypto(v040Codec)

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

appState[v040posts.ModuleName] = cdc.MustMarshalJSON(
v060posts.Migrate(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 = migratePostModule(v060Codec, appState)

return appState
}
2 changes: 2 additions & 0 deletions x/magpie/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (

var (
// functions aliases

RandomizedGenState = simulation.RandomizedGenState
WeightedOperations = simulation.WeightedOperations
SimulateMsgCreateSession = simulation.SimulateMsgCreateSession
Expand All @@ -45,6 +46,7 @@ var (
NewQuerier = keeper.NewQuerier

// variable aliases

RandomNamespaces = simulation.RandomNamespaces
SessionLengthKey = types.SessionLengthKey
LastSessionIDStoreKey = types.LastSessionIDStoreKey
Expand Down
2 changes: 2 additions & 0 deletions x/posts/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (

var (
// functions aliases

NewPostMedia = common.NewPostMedia
ValidateURI = common.ValidateURI
NewPostMedias = common.NewPostMedias
Expand Down Expand Up @@ -139,6 +140,7 @@ var (
RegisterModelsCodec = models.RegisterModelsCodec

// variable aliases

RandomMimeTypes = simulation.RandomMimeTypes
RandomHosts = simulation.RandomHosts
ModuleCdc = types.ModuleCdc
Expand Down
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
Loading