fix: create an AccountPinResponse with a custom msgpack encoder

This commit is contained in:
Derrick Hammer 2024-01-24 12:29:25 -05:00
parent e476ed4476
commit c976ec31be
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 50 additions and 13 deletions

View File

@ -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)

View File

@ -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
}