Skip to content

Commit

Permalink
Fixed a bug with post reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed May 13, 2020
1 parent 5f84b18 commit 0d46c65
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
26 changes: 18 additions & 8 deletions x/posts/internal/types/models/reactions/post_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,34 @@ func (reactions PostReactions) ContainsReactionFrom(user sdk.Address, value stri
// given user with the specified code inside the reactions slice.
// NOTE: The value can be either an emoji or a shortcode.
func (reactions PostReactions) IndexOfByUserAndValue(owner sdk.Address, value string) int {
reactEmoji, err := emoji.LookupEmoji(value)
isEmoji := err == nil
var reactEmoji *emoji.Emoji
if ej, err := emoji.LookupEmoji(value); err == nil {
reactEmoji = &ej
} else if ej, err := emoji.LookupEmojiByCode(value); err == nil {
reactEmoji = &ej
}

for index, reaction := range reactions {
if reaction.Owner.Equals(owner) {
// The given value is a shortcode, so check only that
if !isEmoji && reaction.Value == value {
return index
}
if reactEmoji != nil {
// Check the emoji value
if reaction.Value == reactEmoji.Value {
return index
}

// The given value is an emoji, so we need to check is any of its shortcode match this reaction value
if isEmoji {
// Check the emoji shortcodes
for _, code := range reactEmoji.Shortcodes {
if reaction.Value == code {
return index
}
}
}

if reactEmoji == nil {
if value == reaction.Value {
return index
}
}
}
}
return -1
Expand Down
28 changes: 28 additions & 0 deletions x/posts/internal/types/models/reactions/post_reaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ func TestPostReactions_IndexOfByUserAndValue(t *testing.T) {
value: "reaction-2",
expIndex: -1,
},
{
name: "Exiting reaction search by code",
reactions: reactions.PostReactions{reactions.NewPostReaction("reaction", user)},
owner: user,
value: "reaction",
expIndex: 0,
},
{
name: "Exiting emoji reaction stored by value search by code",
reactions: reactions.PostReactions{reactions.NewPostReaction("🔥", user)},
owner: user,
value: ":fire:",
expIndex: 0,
},
{
name: "Exiting emoji reaction stored by code search by code",
reactions: reactions.PostReactions{reactions.NewPostReaction(":fire:", user)},
owner: user,
value: ":fire:",
expIndex: 0,
},
{
name: "Exiting emoji reaction stored by code search by value",
reactions: reactions.PostReactions{reactions.NewPostReaction(":fire:", user)},
owner: user,
value: "🔥",
expIndex: 0,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 0d46c65

Please sign in to comment.