github.com/lino-network/lino@v0.6.11/x/post/types/msg.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"unicode/utf8"
     6  
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  
     9  	"github.com/lino-network/lino/types"
    10  )
    11  
    12  // CreatePostMsg contains information to create a post
    13  // required stateful validation:
    14  // createdBy is a developer, if not author.
    15  type CreatePostMsg struct {
    16  	Author    types.AccountKey `json:"author"`
    17  	PostID    string           `json:"post_id"`
    18  	Title     string           `json:"title"`
    19  	Content   string           `json:"content"`
    20  	CreatedBy types.AccountKey `json:"created_by"`
    21  	Preauth   bool             `json:"preauth"`
    22  }
    23  
    24  var _ types.Msg = CreatePostMsg{}
    25  
    26  // Route - implements sdk.Msg
    27  func (msg CreatePostMsg) Route() string { return RouterKey }
    28  
    29  // Type - implements sdk.Msg
    30  func (msg CreatePostMsg) Type() string { return "CreatePostMsg" }
    31  
    32  // GetSigners - implements sdk.Msg
    33  func (msg CreatePostMsg) GetSigners() []sdk.AccAddress {
    34  	if msg.Preauth {
    35  		return []sdk.AccAddress{sdk.AccAddress(msg.Author)}
    36  	}
    37  	return []sdk.AccAddress{sdk.AccAddress(msg.CreatedBy)}
    38  }
    39  
    40  // GetSignBytes - implements sdk.Msg
    41  func (msg CreatePostMsg) GetSignBytes() []byte {
    42  	return getSignBytes(msg)
    43  }
    44  
    45  // GetPermission - implements types.Msg
    46  func (msg CreatePostMsg) GetPermission() types.Permission {
    47  	if msg.Preauth {
    48  		return types.TransactionPermission
    49  	}
    50  	if msg.CreatedBy == msg.Author {
    51  		return types.TransactionPermission
    52  	}
    53  	return types.AppOrAffiliatedPermission
    54  }
    55  
    56  // GetConsumeAmount - implements types.Msg
    57  func (msg CreatePostMsg) GetConsumeAmount() types.Coin {
    58  	return types.NewCoinFromInt64(0)
    59  }
    60  
    61  // ValidateBasic - implements sdk.Msg
    62  func (msg CreatePostMsg) ValidateBasic() sdk.Error {
    63  	err := checkPostBasic(msg.PostID, msg.Author, msg.Title, msg.Content)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	if !msg.CreatedBy.IsValid() {
    68  		return ErrInvalidCreatedBy()
    69  	}
    70  	return nil
    71  }
    72  
    73  // String implements Stringer
    74  func (msg CreatePostMsg) String() string {
    75  	return fmt.Sprintf(
    76  		"Post.CreatePostMsg{author:%v, postID:%v, title:%v, content:%v, created_by:%v}",
    77  		msg.Author, msg.PostID, msg.Title, msg.Content, msg.CreatedBy)
    78  }
    79  
    80  // UpdatePostMsg - update post
    81  type UpdatePostMsg struct {
    82  	Author  types.AccountKey `json:"author"`
    83  	PostID  string           `json:"post_id"`
    84  	Title   string           `json:"title"`
    85  	Content string           `json:"content"`
    86  }
    87  
    88  var _ types.Msg = UpdatePostMsg{}
    89  
    90  // Route - implements sdk.Msg
    91  func (msg UpdatePostMsg) Route() string { return RouterKey }
    92  
    93  // Type - implements sdk.Msg
    94  func (msg UpdatePostMsg) Type() string { return "UpdatePostMsg" }
    95  
    96  // GetPermission - implements types.Msg
    97  func (msg UpdatePostMsg) GetPermission() types.Permission {
    98  	return types.TransactionPermission
    99  }
   100  
   101  // ValidateBasic - implements sdk.Msg
   102  func (msg UpdatePostMsg) ValidateBasic() sdk.Error {
   103  	err := checkPostBasic(msg.PostID, msg.Author, msg.Title, msg.Content)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	return nil
   108  }
   109  
   110  func (msg UpdatePostMsg) String() string {
   111  	return fmt.Sprintf("Post.UpdatePostMsg{author:%v, postID:%v, title:%v, content:%v}",
   112  		msg.Author, msg.PostID, msg.Title, msg.Content)
   113  }
   114  
   115  // GetSignBytes - implements sdk.Msg
   116  func (msg UpdatePostMsg) GetSignBytes() []byte {
   117  	return getSignBytes(msg)
   118  }
   119  
   120  // GetSigners - implements sdk.Msg
   121  func (msg UpdatePostMsg) GetSigners() []sdk.AccAddress {
   122  	return []sdk.AccAddress{sdk.AccAddress(msg.Author)}
   123  }
   124  
   125  // GetConsumeAmount - implements types.Msg
   126  func (msg UpdatePostMsg) GetConsumeAmount() types.Coin {
   127  	return types.NewCoinFromInt64(0)
   128  }
   129  
   130  // DeletePostMsg - sent from a user to a post
   131  type DeletePostMsg struct {
   132  	Author types.AccountKey `json:"author"`
   133  	PostID string           `json:"post_id"`
   134  }
   135  
   136  var _ types.Msg = DeletePostMsg{}
   137  
   138  // Route - implements sdk.Msg
   139  func (msg DeletePostMsg) Route() string { return RouterKey }
   140  
   141  // Type - implements sdk.Msg
   142  func (msg DeletePostMsg) Type() string { return "DeletePostMsg" }
   143  
   144  // ValidateBasic - implements sdk.Msg
   145  func (msg DeletePostMsg) ValidateBasic() sdk.Error {
   146  	if len(msg.PostID) == 0 {
   147  		return ErrNoPostID()
   148  	}
   149  	if !msg.Author.IsValid() {
   150  		return ErrInvalidAuthor()
   151  	}
   152  	return nil
   153  }
   154  
   155  // GetPermission - implements types.Msg
   156  func (msg DeletePostMsg) GetPermission() types.Permission {
   157  	return types.TransactionPermission
   158  }
   159  
   160  // GetSignBytes - implements sdk.Msg
   161  func (msg DeletePostMsg) GetSignBytes() []byte {
   162  	return getSignBytes(msg)
   163  }
   164  
   165  // GetSigners - implements sdk.Msg
   166  func (msg DeletePostMsg) GetSigners() []sdk.AccAddress {
   167  	return []sdk.AccAddress{sdk.AccAddress(msg.Author)}
   168  }
   169  
   170  // GetConsumeAmount - implements types.Msg
   171  func (msg DeletePostMsg) GetConsumeAmount() types.Coin {
   172  	return types.NewCoinFromInt64(0)
   173  }
   174  
   175  func (msg DeletePostMsg) String() string {
   176  	return fmt.Sprintf("Post.DeletePostMsg{author:%v, postID:%v}", msg.Author, msg.PostID)
   177  }
   178  
   179  // DonateMsg - sent from a user to a post
   180  type DonateMsg struct {
   181  	Username types.AccountKey `json:"username"`
   182  	Amount   types.LNO        `json:"amount"`
   183  	Author   types.AccountKey `json:"author"`
   184  	PostID   string           `json:"post_id"`
   185  	FromApp  types.AccountKey `json:"from_app"`
   186  	Memo     string           `json:"memo"`
   187  }
   188  
   189  var _ types.Msg = DonateMsg{}
   190  
   191  // NewDonateMsg - constructs a donate msg
   192  func NewDonateMsg(
   193  	user string, amount types.LNO, author string,
   194  	postID string, fromApp string, memo string) DonateMsg {
   195  	return DonateMsg{
   196  		Username: types.AccountKey(user),
   197  		Amount:   amount,
   198  		Author:   types.AccountKey(author),
   199  		PostID:   postID,
   200  		FromApp:  types.AccountKey(fromApp),
   201  		Memo:     memo,
   202  	}
   203  }
   204  
   205  // Route - implements sdk.Msg
   206  func (msg DonateMsg) Route() string { return RouterKey }
   207  
   208  // Type - implements sdk.Msg
   209  func (msg DonateMsg) Type() string { return "DonateMsg" }
   210  
   211  // ValidateBasic - implements sdk.Msg
   212  func (msg DonateMsg) ValidateBasic() sdk.Error {
   213  	// Ensure permlink  exists
   214  	if !msg.Username.IsValid() {
   215  		return ErrInvalidUsername()
   216  	}
   217  	if !msg.Author.IsValid() || len(msg.PostID) == 0 {
   218  		return ErrInvalidTarget()
   219  	}
   220  	if msg.FromApp != "" && !msg.FromApp.IsValid() {
   221  		return ErrInvalidApp()
   222  	}
   223  	if msg.Username == msg.Author {
   224  		return ErrCannotDonateToSelf(msg.Username)
   225  	}
   226  	_, err := types.LinoToCoin(msg.Amount)
   227  	if err != nil {
   228  		return err
   229  	}
   230  	if utf8.RuneCountInString(msg.Memo) > types.MaximumMemoLength {
   231  		return ErrInvalidMemo()
   232  	}
   233  	return nil
   234  }
   235  
   236  // GetPermission - implements types.Msg
   237  func (msg DonateMsg) GetPermission() types.Permission {
   238  	return types.TransactionPermission
   239  }
   240  
   241  // GetSignBytes - implements sdk.Msg
   242  func (msg DonateMsg) GetSignBytes() []byte {
   243  	return getSignBytes(msg)
   244  }
   245  
   246  // GetSigners - implements sdk.Msg
   247  func (msg DonateMsg) GetSigners() []sdk.AccAddress {
   248  	return []sdk.AccAddress{sdk.AccAddress(msg.Username)}
   249  }
   250  
   251  func (msg DonateMsg) String() string {
   252  	return fmt.Sprintf(
   253  		"Post.DonateMsg{donation from: %v, amount: %v, post author:%v, post id: %v}",
   254  		msg.Username, msg.Amount, msg.Author, msg.PostID)
   255  }
   256  
   257  // GetConsumeAmount - implements types.Msg
   258  func (msg DonateMsg) GetConsumeAmount() types.Coin {
   259  	coin, _ := types.LinoToCoin(msg.Amount)
   260  	return coin
   261  }
   262  
   263  // IDADonateMsg - IDA Donation sent from a user to a post
   264  type IDADonateMsg struct {
   265  	Username types.AccountKey `json:"username"`
   266  	App      types.AccountKey `json:"app"`
   267  	Amount   types.IDAStr     `json:"amount"`
   268  	Author   types.AccountKey `json:"author"`
   269  	PostID   string           `json:"post_id"`
   270  	Memo     string           `json:"memo"`
   271  	Signer   types.AccountKey `json:"singer"`
   272  }
   273  
   274  var _ types.Msg = DonateMsg{}
   275  
   276  // Route - implements sdk.Msg
   277  func (msg IDADonateMsg) Route() string { return RouterKey }
   278  
   279  // Type - implements sdk.Msg
   280  func (msg IDADonateMsg) Type() string { return "IDADonateMsg" }
   281  
   282  // ValidateBasic - implements sdk.Msg
   283  func (msg IDADonateMsg) ValidateBasic() sdk.Error {
   284  	if !msg.Username.IsValid() {
   285  		return ErrInvalidUsername()
   286  	}
   287  	if !msg.Signer.IsValid() {
   288  		return ErrInvalidUsername()
   289  	}
   290  	if !msg.App.IsValid() {
   291  		return ErrInvalidApp()
   292  	}
   293  	if !msg.Author.IsValid() || len(msg.PostID) == 0 {
   294  		return ErrInvalidTarget()
   295  	}
   296  	if msg.Username == msg.Author {
   297  		return ErrCannotDonateToSelf(msg.Author)
   298  	}
   299  
   300  	_, err := msg.Amount.ToMiniIDA()
   301  	if err != nil {
   302  		return err
   303  	}
   304  
   305  	if utf8.RuneCountInString(msg.Memo) > types.MaximumMemoLength {
   306  		return ErrInvalidMemo()
   307  	}
   308  	return nil
   309  }
   310  
   311  // GetPermission - implements types.Msg
   312  func (msg IDADonateMsg) GetPermission() types.Permission {
   313  	return types.AppOrAffiliatedPermission
   314  }
   315  
   316  // GetSignBytes - implements sdk.Msg
   317  func (msg IDADonateMsg) GetSignBytes() []byte {
   318  	return getSignBytes(msg)
   319  }
   320  
   321  // GetSigners - implements sdk.Msg
   322  func (msg IDADonateMsg) GetSigners() []sdk.AccAddress {
   323  	return []sdk.AccAddress{sdk.AccAddress(msg.Signer)}
   324  }
   325  
   326  func (msg IDADonateMsg) String() string {
   327  	return fmt.Sprintf(
   328  		"Post.IDADonateMsg{donation from:%v, app:%v, amount: %v, author:%v, pid:%v, memo:%v}",
   329  		msg.Username, msg.App, msg.Amount, msg.Author, msg.PostID, msg.Memo)
   330  }
   331  
   332  // GetConsumeAmount - implements types.Msg
   333  // TODO(yumin): outdated.
   334  func (msg IDADonateMsg) GetConsumeAmount() types.Coin {
   335  	return types.NewCoinFromInt64(0)
   336  }
   337  
   338  // utils
   339  func getSignBytes(msg sdk.Msg) []byte {
   340  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   341  }
   342  
   343  func checkPostBasic(postID string, author types.AccountKey, title, content string) sdk.Error {
   344  	if len(postID) == 0 {
   345  		return ErrNoPostID()
   346  	}
   347  	if len(postID) > types.MaximumLengthOfPostID {
   348  		return ErrPostIDTooLong()
   349  	}
   350  	if !author.IsValid() {
   351  		return ErrInvalidAuthor()
   352  	}
   353  	if utf8.RuneCountInString(title) > types.MaxPostTitleLength {
   354  		return ErrPostTitleExceedMaxLength()
   355  	}
   356  	if utf8.RuneCountInString(content) > types.MaxPostContentLength {
   357  		return ErrPostContentExceedMaxLength()
   358  	}
   359  	return nil
   360  }