now passes linting
This commit is contained in:
30
toot/toot.go
30
toot/toot.go
@@ -5,30 +5,37 @@ import "encoding/json"
|
||||
import "errors"
|
||||
import "strings"
|
||||
import "github.com/sneak/feta/jsonapis"
|
||||
import "github.com/davecgh/go-spew/spew"
|
||||
|
||||
//import "github.com/davecgh/go-spew/spew"
|
||||
import "github.com/rs/zerolog/log"
|
||||
|
||||
//import "encoding/hex"
|
||||
import mh "github.com/multiformats/go-multihash"
|
||||
import mhopts "github.com/multiformats/go-multihash/opts"
|
||||
|
||||
type TootHash string
|
||||
// Hash is a type for storing a string-based base58 multihash of a
|
||||
// toot's identity
|
||||
type Hash string
|
||||
|
||||
// Toot is an object we use internally for storing a discovered toot
|
||||
type Toot struct {
|
||||
Original []byte
|
||||
Parsed *jsonapis.APISerializedToot
|
||||
Hash TootHash
|
||||
Hash Hash
|
||||
FromHost string
|
||||
}
|
||||
|
||||
func NewTootCollectionFromMastodonAPIResponse(in []byte, hostname string) (*[]Toot, error) {
|
||||
// NewTootCollectionFromMastodonAPIResponse takes a byte array from a masto
|
||||
// api response and provides you with a nice array of pointers to parsed
|
||||
// toots
|
||||
func NewTootCollectionFromMastodonAPIResponse(in []byte, hostname string) ([]*Toot, error) {
|
||||
var rt []json.RawMessage
|
||||
err := json.Unmarshal(in, &rt)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to parse api response")
|
||||
}
|
||||
|
||||
var tc []Toot
|
||||
var tc []*Toot
|
||||
|
||||
// iterate over rawtoots from api
|
||||
for _, item := range rt {
|
||||
@@ -47,11 +54,9 @@ func NewTootCollectionFromMastodonAPIResponse(in []byte, hostname string) (*[]To
|
||||
t.Original = o
|
||||
t.FromHost = hostname
|
||||
t.calcHash()
|
||||
tc = append(tc, *t)
|
||||
tc = append(tc, t)
|
||||
}
|
||||
spew.Dump(tc)
|
||||
panic("")
|
||||
return &tc, nil
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
func (t *Toot) String() string {
|
||||
@@ -76,6 +81,11 @@ func (t *Toot) multiHash(in []byte) string {
|
||||
return h.B58String()
|
||||
}
|
||||
|
||||
// DiskStoragePath is a helper function on a Toot that allows it to provide
|
||||
// a storage path on disk. This should probably be moved into the FSStorage
|
||||
// backend instead. FIXME
|
||||
// It's here because it's a pure function that just formats its own toot attributes
|
||||
// into a string.
|
||||
func (t *Toot) DiskStoragePath() string {
|
||||
// FIXME make this error if fields are missing
|
||||
// '/YYYYMMDD/example.com/username/YYYY-MM-DD.HHMMSS.username@fromHost.multihash.json'
|
||||
@@ -103,5 +113,5 @@ func (t *Toot) identityHashInput() string {
|
||||
|
||||
func (t *Toot) calcHash() {
|
||||
hi := t.identityHashInput()
|
||||
t.Hash = TootHash(t.multiHash([]byte(hi)))
|
||||
t.Hash = Hash(t.multiHash([]byte(hi)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user