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

Reaction registration implementation #120

Merged
merged 26 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c6ecca8
restart the branch after a discussion with @RiccardoM on cyclic depen…
Mar 17, 2020
53f444b
created reaction type
Mar 17, 2020
c23dfa2
Merge branch 'master' of github.com:desmos-labs/desmos into leonardo/…
Mar 17, 2020
f15056d
added method to reaction type, keeper method and relative tests, crea…
Mar 17, 2020
71ba4f7
added querier, handler, cli and rest commands querier, handler, msg t…
Mar 18, 2020
ad266ff
adding simulation tests
Mar 19, 2020
48835ab
added registered reactions to genesis + tests
Mar 19, 2020
7850814
- aadded unicode checks
Mar 20, 2020
fbcb95d
- small lint fixes
Mar 20, 2020
12941eb
minor fixes
Mar 20, 2020
ad0f0c8
fixed tests, added checks for post reactions to see if they are regis…
Mar 20, 2020
4685f9a
solved cli tests and (maybe) simulation tests
Mar 23, 2020
3f11920
solving sim tests
Mar 23, 2020
196bea4
fixed sim-after import tests
Mar 24, 2020
a692f26
fixed sim tests
Mar 24, 2020
eeeab28
Updated docs
leobragaz Mar 24, 2020
c48bc5d
fixed errors and suggestions
Mar 26, 2020
bc5fdce
Merge remote-tracking branch 'origin/leonardo/reactions-registration-…
Mar 26, 2020
0b64eee
Merge branch 'master' of github.com:desmos-labs/desmos into leonardo/…
Mar 26, 2020
4db3361
Update x/posts/internal/keeper/keeper_reactions.go
RiccardoM Mar 26, 2020
8266d84
Update CHANGELOG.md
RiccardoM Mar 26, 2020
3d083d3
Update x/posts/internal/keeper/keeper_reactions.go
RiccardoM Mar 26, 2020
4d15cae
Update x/posts/legacy/v0.2.0/types.go
RiccardoM Mar 26, 2020
90ada69
Update x/posts/internal/keeper/keeper_reactions.go
RiccardoM Mar 26, 2020
d232e5a
fixed last suggestions
Mar 26, 2020
ee6e599
Merge remote-tracking branch 'origin/leonardo/reactions-registration-…
Mar 26, 2020
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
Expand Up @@ -4,6 +4,7 @@
- Added the support for posting empty-message posts with medias (#110)
- Implemented the support for hashtags in posts (#96)
- Updated the post create CLI command in posts (#117)
- Implemented the support for registering new reactions (#94)

## Bug fixes
- Fixed a bug inside the migration procedure of the `magpie` module (#106)
Expand Down
11 changes: 6 additions & 5 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package params

// Default simulation operation weights for messages
const (
DefaultWeightMsgCreatePost int = 100
DefaultWeightMsgEditPost int = 100
DefaultWeightMsgAddReaction int = 100
DefaultWeightMsgRemoveReaction int = 100
DefaultWeightMsgAnswerPoll int = 100
DefaultWeightMsgCreatePost int = 100
DefaultWeightMsgEditPost int = 100
DefaultWeightMsgAddReaction int = 100
DefaultWeightMsgRemoveReaction int = 100
DefaultWeightMsgAnswerPoll int = 100
DefaultWeightMsgRegisterReaction int = 100
)
131 changes: 124 additions & 7 deletions cli_test/cli_posts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,33 +494,48 @@ func TestDesmosCLIPostsReactions(t *testing.T) {
// Save key addresses for later use
fooAddr := f.KeyAddress(keyFoo)

subspace := "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"
reactions := posts.Reactions{
posts.Reaction{":thumbsup:", "U+1F44D", subspace, fooAddr},
posts.Reaction{":blush:", "U+1F60A", subspace, fooAddr},
posts.Reaction{":thumbsdown:", "U+1F44E", subspace, fooAddr},
}

// Create a post
success, _, sterr := f.TxPostsCreate("4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e",
"message", fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Register reactions
for _, reaction := range reactions {
success, _, sterr = f.TxPostsRegisterReaction(reaction.ShortCode, reaction.Value, reaction.Subspace, reaction.Creator, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)
}

// __________________________________________________________________________________
// add-reaction

// Add a reaction
success, _, sterr = f.TxPostsAddReaction(1, "👍", fooAddr, "-y")
success, _, sterr = f.TxPostsAddReaction(1, ":thumbsup:", fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Make sure the reaction is added
storedPost := f.QueryPost(1)
require.Len(t, storedPost.Reactions, 1)
require.Equal(t, storedPost.Reactions[0], posts.NewReaction("👍", fooAddr))
require.Equal(t, storedPost.Reactions[0], posts.NewPostReaction(":thumbsup:", fooAddr))

// Test --dry-run
success, _, _ = f.TxPostsAddReaction(1, "😊", fooAddr, "--dry-run")
success, _, _ = f.TxPostsAddReaction(1, ":blush:", fooAddr, "--dry-run")
require.True(t, success)

// Test --generate-only
success, stdout, stderr := f.TxPostsAddReaction(1, "👎", fooAddr, "--generate-only=true")
success, stdout, stderr := f.TxPostsAddReaction(1, ":thumbsdown:", fooAddr, "--generate-only=true")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
Expand All @@ -536,7 +551,7 @@ func TestDesmosCLIPostsReactions(t *testing.T) {
// remove-reaction

// Remove a reaction
success, _, sterr = f.TxPostsRemoveReaction(1, "👍", fooAddr, "-y")
success, _, sterr = f.TxPostsRemoveReaction(1, ":thumbsup:", fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)
Expand All @@ -546,11 +561,11 @@ func TestDesmosCLIPostsReactions(t *testing.T) {
require.Empty(t, storedPost.Reactions)

// Test --dry-run
success, _, _ = f.TxPostsRemoveReaction(1, "😊", fooAddr, "--dry-run")
success, _, _ = f.TxPostsRemoveReaction(1, ":blush:", fooAddr, "--dry-run")
require.True(t, success)

// Test --generate-only
success, stdout, stderr = f.TxPostsRemoveReaction(1, "👎", fooAddr, "--generate-only=true")
success, stdout, stderr = f.TxPostsRemoveReaction(1, ":thumbsdown:", fooAddr, "--generate-only=true")
require.Empty(t, stderr)
require.True(t, success)
msg = unmarshalStdTx(f.T, stdout)
Expand All @@ -564,3 +579,105 @@ func TestDesmosCLIPostsReactions(t *testing.T) {

f.Cleanup()
}

func TestDesmosCLIRegisterReaction(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test using an emoji as value such as 🎉 ?

t.Parallel()
f := InitFixtures(t)

// Start Desmosd server
proc := f.GDStart()
defer proc.Stop(false)

// Save key addresses for later use
fooAddr := f.KeyAddress(keyFoo)

// Later usage variables
subspace := "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"
shortCode := ":like:"
value := "https://like.jpg"
fooAcc := f.QueryAccount(fooAddr)
startTokens := sdk.TokensFromConsensusPower(140)
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Register a reaction
success, _, sterr := f.TxPostsRegisterReaction(shortCode, value, subspace, fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Make sure the reaction is registered
registeredReactions := f.QueryReactions()
require.NotEmpty(t, registeredReactions)
require.Equal(t, registeredReactions, posts.Reactions{posts.NewReaction(fooAddr, shortCode, value, subspace)})

// Test --dry-run
success, _, _ = f.TxPostsRegisterReaction(shortCode, value, subspace, fooAddr, "--dry-run")
require.True(t, success)

// Test --generate-only
success, stdout, stderr := f.TxPostsRegisterReaction(shortCode, value, subspace, fooAddr, "--generate-only")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

// Check state didn't change
registeredReactions = f.QueryReactions()
require.Len(t, registeredReactions, 1)

f.Cleanup()

}

func TestDesmosCLIRegisterReactionEmojiValue(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

// Start Desmosd server
proc := f.GDStart()
defer proc.Stop(false)

// Save key addresses for later use
fooAddr := f.KeyAddress(keyFoo)

// Later usage variables
subspace := "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"
shortCode := ":like:"
value := "🎉"
fooAcc := f.QueryAccount(fooAddr)
startTokens := sdk.TokensFromConsensusPower(140)
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Register a reaction
success, _, sterr := f.TxPostsRegisterReaction(shortCode, value, subspace, fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Make sure the reaction is registered
registeredReactions := f.QueryReactions()
require.NotEmpty(t, registeredReactions)
require.Equal(t, registeredReactions, posts.Reactions{posts.NewReaction(fooAddr, shortCode, value, subspace)})

// Test --dry-run
success, _, _ = f.TxPostsRegisterReaction(shortCode, value, subspace, fooAddr, "--dry-run")
require.True(t, success)

// Test --generate-only
success, stdout, stderr := f.TxPostsRegisterReaction(shortCode, value, subspace, fooAddr, "--generate-only")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

// Check state didn't change
registeredReactions = f.QueryReactions()
require.Len(t, registeredReactions, 1)

f.Cleanup()

}
19 changes: 19 additions & 0 deletions cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ func (f *Fixtures) TxPostsRemoveReaction(id int, reaction string, from sdk.AccAd
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

// TxPostsRegisterReaction is desmoscli tx posts register-reaction
func (f *Fixtures) TxPostsRegisterReaction(shortCode, value, subspace string, from sdk.AccAddress, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf(`%s tx posts register-reaction %s %s %s --keyring-backend=test --from=%s %v`,
f.DesmosliBinary, shortCode, value, subspace, from, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

//___________________________________________________________________________________
// desmoscli query account

Expand Down Expand Up @@ -746,6 +753,18 @@ func (f *Fixtures) QueryPost(id int, flags ...string) posts.PostQueryResponse {
return storedPost
}

// QueryReactions returns registered reactions
func (f *Fixtures) QueryReactions(flags ...string) posts.Reactions {
cmd := fmt.Sprintf("%s query posts registered-reactions --output=json %s", f.DesmosliBinary, f.Flags())
res, errStr := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
require.Empty(f.T, errStr)
cdc := app.MakeCodec()
var registeredReactions posts.Reactions
err := cdc.UnmarshalJSON([]byte(res), &registeredReactions)
require.NoError(f.T, err)
return registeredReactions
}

//___________________________________________________________________________________
// executors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# `MsgLikePost`
This message allows you to like a post that is already existing on the chain.
# `MsgAddPostReaction`
This message allows you to add a reaction to a post that is already existing on the chain.

## Structure
```json
Expand All @@ -16,14 +16,14 @@ This message allows you to like a post that is already existing on the chain.
### Attributes
| Attribute | Type | Description |
| :-------: | :----: | :-------- |
| `value` String | Value of the reaction |
| `value` | String | Value of the reaction |
| `user` | String | Desmos address of the user adding the reaction to the post |
| `post_id` | String | ID of the post to which add the reaction |

## Example
```json
{
"type": "desmos/MsgLikePost",
"type": "desmos/MsgAddPostReaction",
"value": {
"value": "like",
"liker": "desmos1w3fe8zq5jrxd4nz49hllg75sw7m24qyc7tnaax",
Expand Down
44 changes: 44 additions & 0 deletions docs/developers/msgs/register-reaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# `MsgRegisterReaction`
This message allows you to register a new reaction.
If you want to know more about the `Reaction` type, you can do so inside the [`Reaction` type documentation page](../../types/reaction.md)

## Structure
```json
{
"type": "desmos/MsgRegisterReaction",
"value": {
"short_code": "<reaction short code>",
"value": "<url (identifing gif or image) or unicode (identifing emoji)>",
"subspace": "<Reaction subspace>",
"creator": "<Desmos address that's registering the reaction>"
}
}
```

### Attributes
| Attribute | Type | Description |
| :-------: | :----: | :-------- |
| `short_code` | String | Short code that identifies the reaction (e.g. `":smile:"`) |
| `value` | String | Value can be a URL or a UNICODE identifing gif, images, emoji |
| `subspace` | String | Required string that identifies the subspace inside which the reaction will be registered |
| `creator` | String | Desmos address of the user that is registering the reaction |

## Example
```json
{
"type": "desmos/MsgRegisterReaction",
"value": {
"shortcode": ":smile:",
"value": "U+1F604",
"subspace": "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e",
"creator": "desmos13s7p4jx3rj5pxjzlecxdvua68ex0sg7rug0pt3"
}
}
```

## Message action
The action associated to this message is the following:

```
register_reaction
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# `MsgUnlikePost`
With this message you can remove the like from a previously liked post.
# `MsgRemovePostReaction`
With this message you can remove a reaction from a post you have previously reacted to.

## Structure
```json
Expand Down
5 changes: 3 additions & 2 deletions docs/developers/perform-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Here is the list of currently available [messages](developer-faq.md#what-is-a-me
### Posts
* [`MsgCreatePost`](msgs/create-post.md): allows you to create a new post or a comment for an existing post.
* [`MsgEditPost`](msgs/edit-post.md): allows you to edit a previously created post message.
* [`MsgAddPostReaction`](msgs/add-reaction.md): allows you to add a reaction to an existing post.
* [`MsgRemovePostReaction`](msgs/remove-reaction.md): allows you to remove a reaction from a post.
* [`MsgAddPostReaction`](msgs/add-post-reaction.md): allows you to add a reaction to an existing post.
* [`MsgRemovePostReaction`](msgs/remove-post-reaction.md): allows you to remove a reaction from a post.
* [`MsgAnswerPoll`](msgs/answer-poll.md): allows you to answer a post's poll.
* [`MsgRegisterReaction`](msgs/register-reaction.md): allows you to register a reaction.
18 changes: 18 additions & 0 deletions docs/developers/queries/reactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Query registered reactions
This query endpoint allows you to retrieve the list of registered reactions.

**CLI**
```bash
desmoscli query posts registered-reactions

# Example
# desmoscli query posts registered-reactions
```

**REST**
```
/reactions

# Example
# curl https://morpheus4000.desmos.network/reactions
```
1 change: 1 addition & 0 deletions docs/developers/query-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Aside from [performing transactions](perform-transactions.md) you can also query
- [Query a post](queries/post.md)
- [Query the stored posts](queries/posts.md)
- [Query the post's poll answers](queries/poll-answers.md)
- [Query registered reactions](queries/reactions.md)

## Sessions
- [Query a session](queries/session.md)
36 changes: 36 additions & 0 deletions docs/types/reaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Reaction
Inside Desmos, the reactions is the fastest way users can react to posts.

Everyday each one of us use reactions inside the most popular social networks.
Reactions let you immediately express what you think about the post/photo/video you've just watched.

Each new reaction that will be registered inside Desmos will become part of the set of reactions supported by the system.
Once you have registered your favorite GIF/image/emoji, you will be allowed to use them to react to every post inside Desmos.
Remember that a reaction can be registered only once per `subspace`, so if you ever try to register a previously
registered reaction, your transaction will not be valid.

## Contained data
A reaction is made of different parts. Following you will find out what are those and how they can be used.

### `ShortCode`
The `ShortCode` identifies the actual reaction short code.
Short codes are codes used on various websites to speed up reaction insertion using a keyboard.
These begin and end with a colon, and contain the literal name of the reaction itself.
For example, it can look something like `:emoji-shortcode:`.
When registering a new reaction, the shot code must be validated by the following regEx: `:[a-z]([a-z\d_])*:`.
[Here](https://www.webfx.com/tools/emoji-cheat-sheet/) the list of all available short codes.

### `Value`
The `Value` of a reaction identifies whether the reaction is a GIF an image or an emoji.
Value can be a `URL` with the path of the GIF/image your using as a reaction or it can be a `UNICODE`
that identifies a specific emoji.
`URL`'s will be validated by the following regEx: `^(?:http(s)?://)[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$`.
`Unicode` must be one of the following [list](https://unicode.org/emoji/charts/full-emoji-list.html).

### `Subspace`
The `Subspace` field identifies the application inside which the reaction has been registered.
Currently the subspace must be a SHA256 hash of the previously plain-text value.

### `Creator`
The `Creator` field is used to specify the Bech32 address of the creator of the reaction.
In order for a creator address to be valid, it must begin with the `desmos` Bech32 human-readable part.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
github.com/tendermint/go-amino v0.15.1
github.com/tendermint/tendermint v0.33.0
github.com/tendermint/tm-db v0.4.0
github.com/tmdvs/Go-Emoji-Utils v1.1.0
)
Loading