From c976ec31bee6f89fe81aff7f023961d1a64e0f4b Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 24 Jan 2024 12:29:25 -0500 Subject: [PATCH] fix: create an AccountPinResponse with a custom msgpack encoder --- api/s5/http.go | 15 ++------------- api/s5/messages.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/api/s5/http.go b/api/s5/http.go index 8072eaf..45e6eaa 100644 --- a/api/s5/http.go +++ b/api/s5/http.go @@ -615,20 +615,9 @@ func (h *HttpHandler) AccountPins(jc jape.Context) { return } - pinsList := make([][]byte, len(pins)) + pinResponse := &AccountPinResponse{Cursor: cursor, Pins: pins} - for i, pin := range pins { - hash, err := hex.DecodeString(pin.Upload.Hash) - - if err != nil { - errored(err) - return - } - - pinsList[i] = encoding.MultihashFromBytes(hash, types.HashTypeBlake3).FullBytes() - } - - result, err := msgpack.Marshal(pinsList) + result, err := msgpack.Marshal(pinResponse) if err != nil { errored(err) diff --git a/api/s5/messages.go b/api/s5/messages.go index 36fad33..e25c8f7 100644 --- a/api/s5/messages.go +++ b/api/s5/messages.go @@ -1,5 +1,17 @@ package s5 +import ( + "encoding/hex" + "git.lumeweb.com/LumeWeb/libs5-go/encoding" + "git.lumeweb.com/LumeWeb/libs5-go/types" + "git.lumeweb.com/LumeWeb/portal/db/models" + "github.com/vmihailenco/msgpack/v5" +) + +var ( + _ msgpack.CustomEncoder = (*AccountPinResponse)(nil) +) + type AccountRegisterRequest struct { Pubkey string `json:"pubkey"` Response string `json:"response"` @@ -78,3 +90,39 @@ type DebugStorageLocation struct { type DebugStorageLocationsResponse struct { Locations []DebugStorageLocation `json:"locations"` } + +type AccountPinResponse struct { + Pins []models.Pin + Cursor uint64 +} + +func (a AccountPinResponse) EncodeMsgpack(enc *msgpack.Encoder) error { + err := enc.EncodeInt(0) + if err != nil { + return err + } + + err = enc.EncodeInt(int64(a.Cursor)) + if err != nil { + return err + } + + pinsList := make([][]byte, len(a.Pins)) + + for i, pin := range a.Pins { + hash, err := hex.DecodeString(pin.Upload.Hash) + + if err != nil { + return err + } + + pinsList[i] = encoding.MultihashFromBytes(hash, types.HashTypeBlake3).FullBytes() + } + + err = enc.Encode(pinsList) + if err != nil { + return err + } + + return nil +}