Skip to content

Commit

Permalink
Merge pull request #48 from desmos-labs/riccardo/discussion-implement…
Browse files Browse the repository at this point in the history
…ation

Discussion implementation
  • Loading branch information
kwunyeung authored Nov 22, 2019
2 parents d3f8199 + 1e0f46a commit 23e524f
Show file tree
Hide file tree
Showing 22 changed files with 890 additions and 208 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# run:
# # timeout for analysis, e.g. 30s, 5m, default is 1m
# timeout: 5m

linters:
enable:
- bodyclose
Expand Down Expand Up @@ -49,4 +53,4 @@ linters-settings:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
suggest-new: true
3 changes: 2 additions & 1 deletion cmd/desmosd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/client"
"io"

"github.com/cosmos/cosmos-sdk/client"

"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
genaccscli "github.com/cosmos/cosmos-sdk/x/genaccounts/client/cli"
Expand Down
3 changes: 2 additions & 1 deletion cmd/desmosd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
"net"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/x/genaccounts"

"github.com/spf13/cobra"
"github.com/spf13/viper"
tmconfig "github.com/tendermint/tendermint/config"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ require (
github.com/tendermint/go-amino v0.15.1
github.com/tendermint/tendermint v0.32.7
github.com/tendermint/tm-db v0.2.0
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down
9 changes: 5 additions & 4 deletions x/posts/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ type (
Keeper = keeper.Keeper

// Types
PostID = types.PostID
Post = types.Post
Like = types.Like
Likes = types.Likes
PostID = types.PostID
PostIDs = types.PostIDs
Post = types.Post
Like = types.Like
Likes = types.Likes

// Msgs
MsgCreatePost = types.MsgCreatePost
Expand Down
32 changes: 27 additions & 5 deletions x/posts/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/viper"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -32,12 +37,17 @@ func GetTxCmd(_ string, cdc *codec.Codec) *cobra.Command {
return postsTxCmd
}

var (
flagParentID = "parent-id"
flagExternalReference = "external-reference"
)

// GetCmdCreatePost is the CLI command for creating a post
func GetCmdCreatePost(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create [message] [parent-post-id]",
cmd := &cobra.Command{
Use: "create [message] [allows-comments]",
Short: "Create a new post",
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {

cliCtx := context.NewCLIContext().WithCodec(cdc)
Expand All @@ -49,19 +59,31 @@ func GetCmdCreatePost(cdc *codec.Codec) *cobra.Command {
return err
}

parentID, err := types.ParsePostID(args[1])
allowsComments, err := strconv.ParseBool(args[1])
if err != nil {
return err
}

msg := types.NewMsgCreatePost(args[0], parentID, from)
parentID, err := types.ParsePostID(viper.GetString(flagParentID))
if err != nil {
return err
}

externalReference := viper.GetString(flagExternalReference)

msg := types.NewMsgCreatePost(args[0], parentID, allowsComments, externalReference, from)
if err = msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd.Flags().String(flagParentID, "0", "Id of the post to which this one should be an answer to")
cmd.Flags().String(flagExternalReference, "", "External reference to this post")

return flags.GetCommands(cmd)[0]
}

// GetCmdEditPost is the CLI command for editing a post
Expand Down
10 changes: 6 additions & 4 deletions x/posts/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
// --- Tx Handler

type createPostReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Message string `json:"message"`
ParentID string `json:"parent_id"`
BaseReq rest.BaseReq `json:"base_req"`
Message string `json:"message"`
ParentID string `json:"parent_id"`
AllowsComments bool `json:"allows_comments"`
ExternalReference string `json:"external_reference"`
}

func createPostHandler(cliCtx context.CLIContext) http.HandlerFunc {
Expand Down Expand Up @@ -57,7 +59,7 @@ func createPostHandler(cliCtx context.CLIContext) http.HandlerFunc {
return
}

msg := types.NewMsgCreatePost(req.Message, parentID, addr)
msg := types.NewMsgCreatePost(req.Message, parentID, req.AllowsComments, req.ExternalReference, addr)
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand Down
4 changes: 1 addition & 3 deletions x/posts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) GenesisState {
// InitGenesis initializes the chain state based on the given GenesisState
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data GenesisState) []abci.ValidatorUpdate {
for _, post := range data.Posts {
if err := keeper.SavePost(ctx, post); err != nil {
panic(err)
}
keeper.SavePost(ctx, post)
}

for postID, likes := range data.Likes {
Expand Down
17 changes: 9 additions & 8 deletions x/posts/internal/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func testCodec() *codec.Codec {
}

var testPostOwner, _ = sdk.AccAddressFromBech32("cosmos1y54exmx84cqtasvjnskf9f63djuuj68p7hqf47")
var testPost = types.Post{
PostID: types.PostID(3257),
ParentID: types.PostID(502),
Message: "Post message",
Created: 10,
LastEdited: 50,
Owner: testPostOwner,
}
var testPost = types.NewPost(
types.PostID(3257),
types.PostID(0),
"Post message",
false,
"",
10,
testPostOwner,
)
49 changes: 29 additions & 20 deletions x/posts/internal/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/x/posts/internal/types"
Expand Down Expand Up @@ -37,29 +36,41 @@ func handleMsgCreatePost(ctx sdk.Context, keeper Keeper, msg types.MsgCreatePost
),
)

post := types.Post{
PostID: keeper.GetLastPostID(ctx).Next(),
ParentID: msg.ParentID,
Message: msg.Message,
Created: ctx.BlockHeight(),
Owner: msg.Creator,
}
post := types.NewPost(
keeper.GetLastPostID(ctx).Next(),
msg.ParentID,
msg.Message,
msg.AllowsComments,
msg.ExternalReference,
ctx.BlockHeight(),
msg.Creator,
)

// Check for double posting
if _, found := keeper.GetPost(ctx, post.PostID); found {
msg := fmt.Sprintf("Post with id %s already exists", post.PostID.String())
return sdk.ErrUnknownRequest(msg).Result()
return sdk.ErrUnknownRequest(fmt.Sprintf("Post with id %s already exists", post.PostID)).Result()
}

if err := keeper.SavePost(ctx, post); err != nil {
return err.Result()
// If valid, check the parent post
if post.ParentID.Valid() {
parentPost, found := keeper.GetPost(ctx, post.ParentID)
if !found {
return sdk.ErrUnknownRequest(fmt.Sprintf("Parent post with id %s not found", post.ParentID)).Result()
}

if !parentPost.AllowsComments {
return sdk.ErrUnknownRequest(fmt.Sprintf("Post with id %s does not allow comments", parentPost.PostID)).Result()
}
}

keeper.SavePost(ctx, post)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCreatePost,
sdk.NewAttribute(types.AttributeKeyPostID, post.PostID.String()),
sdk.NewAttribute(types.AttributeKeyPostParentID, post.ParentID.String()),
sdk.NewAttribute(types.AttributeKeyCreationTime, strconv.FormatInt(post.Created, 10)),
sdk.NewAttribute(types.AttributeKeyCreationTime, post.Created.String()),
sdk.NewAttribute(types.AttributeKeyPostOwner, post.Owner.String()),
),
)
Expand Down Expand Up @@ -94,22 +105,20 @@ func handleMsgEditPost(ctx sdk.Context, keeper Keeper, msg types.MsgEditPost) sd
}

// Check the validity of the current block height respect to the creation date of the post
if ctx.BlockHeight() < existing.Created {
if existing.Created.GT(sdk.NewInt(ctx.BlockHeight())) {
return sdk.ErrUnknownRequest("Edit date cannot be before creation date").Result()
}

// Edit the post
existing.Message = msg.Message
existing.LastEdited = ctx.BlockHeight()
if err := keeper.SavePost(ctx, existing); err != nil {
return err.Result()
}
existing.LastEdited = sdk.NewInt(ctx.BlockHeight())
keeper.SavePost(ctx, existing)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEditPost,
sdk.NewAttribute(types.AttributeKeyPostID, existing.PostID.String()),
sdk.NewAttribute(types.AttributeKeyPostEditTime, strconv.FormatInt(existing.LastEdited, 10)),
sdk.NewAttribute(types.AttributeKeyPostEditTime, existing.LastEdited.String()),
),
)

Expand Down Expand Up @@ -137,7 +146,7 @@ func handleMsgLike(ctx sdk.Context, keeper Keeper, msg types.MsgLikePost) sdk.Re
}

// Check the like date to make sure it's before the post creation date.
if ctx.BlockHeight() < post.Created {
if post.Created.GT(sdk.NewInt(ctx.BlockHeight())) {
return sdk.ErrUnknownRequest("Like cannot have a creation time before the post itself").Result()
}

Expand Down
Loading

0 comments on commit 23e524f

Please sign in to comment.