Skip to content

Commit

Permalink
Add CommentOperation.JsonMetadata
Browse files Browse the repository at this point in the history
Had to move ContentMetadata from apis/database into types.
  • Loading branch information
tchap committed Aug 17, 2017
1 parent 9e161b7 commit efaa109
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 84 deletions.
105 changes: 27 additions & 78 deletions apis/database/data.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package database

import (
// Stdlib
"encoding/json"
"strconv"
"strings"

// RPC
"github.com/go-steem/rpc/types"
)
Expand Down Expand Up @@ -57,85 +52,39 @@ type Block struct {
}

type Content struct {
Id *types.ID `json:"id"`
RootTitle string `json:"root_title"`
Active *types.Time `json:"active"`
AbsRshares *types.Int `json:"abs_rshares"`
PendingPayoutValue string `json:"pending_payout_value"`
TotalPendingPayoutValue string `json:"total_pending_payout_value"`
Category string `json:"category"`
Title string `json:"title"`
LastUpdate *types.Time `json:"last_update"`
Stats string `json:"stats"`
Body string `json:"body"`
Created *types.Time `json:"created"`
Replies []*Content `json:"replies"`
Permlink string `json:"permlink"`
JsonMetadata *ContentMetadata `json:"json_metadata"`
Children *types.Int `json:"children"`
NetRshares *types.Int `json:"net_rshares"`
URL string `json:"url"`
ActiveVotes []*VoteState `json:"active_votes"`
ParentPermlink string `json:"parent_permlink"`
CashoutTime *types.Time `json:"cashout_time"`
TotalPayoutValue string `json:"total_payout_value"`
ParentAuthor string `json:"parent_author"`
ChildrenRshares2 *types.Int `json:"children_rshares2"`
Author string `json:"author"`
Depth *types.Int `json:"depth"`
TotalVoteWeight *types.Int `json:"total_vote_weight"`
Id *types.ID `json:"id"`
RootTitle string `json:"root_title"`
Active *types.Time `json:"active"`
AbsRshares *types.Int `json:"abs_rshares"`
PendingPayoutValue string `json:"pending_payout_value"`
TotalPendingPayoutValue string `json:"total_pending_payout_value"`
Category string `json:"category"`
Title string `json:"title"`
LastUpdate *types.Time `json:"last_update"`
Stats string `json:"stats"`
Body string `json:"body"`
Created *types.Time `json:"created"`
Replies []*Content `json:"replies"`
Permlink string `json:"permlink"`
JsonMetadata *types.ContentMetadata `json:"json_metadata"`
Children *types.Int `json:"children"`
NetRshares *types.Int `json:"net_rshares"`
URL string `json:"url"`
ActiveVotes []*VoteState `json:"active_votes"`
ParentPermlink string `json:"parent_permlink"`
CashoutTime *types.Time `json:"cashout_time"`
TotalPayoutValue string `json:"total_payout_value"`
ParentAuthor string `json:"parent_author"`
ChildrenRshares2 *types.Int `json:"children_rshares2"`
Author string `json:"author"`
Depth *types.Int `json:"depth"`
TotalVoteWeight *types.Int `json:"total_vote_weight"`
}

func (content *Content) IsStory() bool {
return content.ParentAuthor == ""
}

type ContentMetadata struct {
Flag bool
Users []string
Tags []string
Image []string
}

type ContentMetadataRaw struct {
Users types.StringSlice `json:"users"`
Tags types.StringSlice `json:"tags"`
Image types.StringSlice `json:"image"`
}

func (metadata *ContentMetadata) UnmarshalJSON(data []byte) error {
unquoted, err := strconv.Unquote(string(data))
if err != nil {
return err
}

switch unquoted {
case "true":
metadata.Flag = true
return nil
case "false":
metadata.Flag = false
return nil
}

if len(unquoted) == 0 {
var value ContentMetadata
metadata = &value
return nil
}

var raw ContentMetadataRaw
if err := json.NewDecoder(strings.NewReader(unquoted)).Decode(&raw); err != nil {
return err
}

metadata.Users = raw.Users
metadata.Tags = raw.Tags
metadata.Image = raw.Image

return nil
}

type VoteState struct {
Voter string `json:"voter"`
Weight *types.Int `json:"weight"`
Expand Down
67 changes: 61 additions & 6 deletions types/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
// Stdlib
"encoding/json"
"strconv"
"strings"

// RPC
"github.com/go-steem/rpc/encoding/transaction"
Expand Down Expand Up @@ -287,12 +289,13 @@ func (op *AccountWitnessProxyOperation) Data() interface{} {
// In case Title is filled in and ParentAuthor is empty, it is a new post.
// The post category can be read from ParentPermlink.
type CommentOperation struct {
Author string `json:"author"`
Title string `json:"title"`
Permlink string `json:"permlink"`
ParentAuthor string `json:"parent_author"`
ParentPermlink string `json:"parent_permlink"`
Body string `json:"body"`
Author string `json:"author"`
Title string `json:"title"`
Permlink string `json:"permlink"`
ParentAuthor string `json:"parent_author"`
ParentPermlink string `json:"parent_permlink"`
Body string `json:"body"`
JsonMetadata *ContentMetadata `json:"json_metadata"`
}

func (op *CommentOperation) Type() OpType {
Expand All @@ -307,6 +310,58 @@ func (op *CommentOperation) IsStoryOperation() bool {
return op.ParentAuthor == ""
}

type ContentMetadata struct {
Flag bool
Users []string
Tags []string
Image []string
App string
Format string
}

type ContentMetadataRaw struct {
Users StringSlice `json:"users"`
Tags StringSlice `json:"tags"`
Image StringSlice `json:"image"`
App string `json:"app"`
Format string `json:"format"`
}

func (metadata *ContentMetadata) UnmarshalJSON(data []byte) error {
unquoted, err := strconv.Unquote(string(data))
if err != nil {
return err
}

switch unquoted {
case "true":
metadata.Flag = true
return nil
case "false":
metadata.Flag = false
return nil
}

if len(unquoted) == 0 {
var value ContentMetadata
metadata = &value
return nil
}

var raw ContentMetadataRaw
if err := json.NewDecoder(strings.NewReader(unquoted)).Decode(&raw); err != nil {
return err
}

metadata.Users = raw.Users
metadata.Tags = raw.Tags
metadata.Image = raw.Image
metadata.App = raw.App
metadata.Format = raw.Format

return nil
}

// FC_REFLECT( steemit::chain::vote_operation,
// (voter)
// (author)
Expand Down

0 comments on commit efaa109

Please sign in to comment.