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

Discussion implementation #48

Merged
merged 6 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
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
31 changes: 26 additions & 5 deletions x/posts/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/viper"
"strconv"
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved

"github.com/spf13/cobra"

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

var (
flagParentId = "parent-id"
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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 +58,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
}

parentID, err := types.ParsePostID(viper.GetString(flagParentId))
if err != nil {
return err
}

msg := types.NewMsgCreatePost(args[0], parentID, from)
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