From 1cf2d9880c0dfcc7905947265c2330b84e99f97a Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 17 Jan 2024 12:33:05 -0500 Subject: [PATCH] feat: add /s5/account/pins.bin endpoint --- api/s5.go | 1 + api/s5/http.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/api/s5.go b/api/s5.go index 5915326..cc7655c 100644 --- a/api/s5.go +++ b/api/s5.go @@ -35,5 +35,6 @@ func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Hand "POST /s5/account/login": h.AccountLogin, "GET /s5/account": s5.AuthMiddleware(h.AccountInfo, portal), "GET /s5/account/stats": s5.AuthMiddleware(h.AccountStats, portal), + "GET /s5/account/pins.bin": s5.AuthMiddleware(h.AccountPins, portal), } } diff --git a/api/s5/http.go b/api/s5/http.go index 5604d4f..f355daa 100644 --- a/api/s5/http.go +++ b/api/s5/http.go @@ -13,6 +13,7 @@ import ( "git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/interfaces" emailverifier "github.com/AfterShip/email-verifier" + "github.com/vmihailenco/msgpack/v5" "go.sia.tech/jape" "go.uber.org/zap" "io" @@ -31,6 +32,7 @@ const ( errAccountGenerateChallenge = "Error generating challenge" errAccountRegister = "Error registering account" errAccountLogin = "Error logging in account" + errFailedToGetPins = "Failed to get pins" ) var ( @@ -46,6 +48,7 @@ var ( errPubkeyAlreadyExists = errors.New("Pubkey already exists") errPubkeyNotExist = errors.New("Pubkey does not exist") errAccountLoginErr = errors.New(errAccountLogin) + errFailedToGetPinsErr = errors.New(errFailedToGetPins) ) type HttpHandler struct { @@ -553,6 +556,51 @@ func (h *HttpHandler) AccountStats(jc jape.Context) { jc.Encode(info) } +func (h *HttpHandler) AccountPins(jc jape.Context) { + var cursor uint64 + + if jc.DecodeForm("cursor", &cursor) != nil { + return + } + + errored := func(err error) { + _ = jc.Error(errFailedToGetPinsErr, http.StatusInternalServerError) + h.portal.Logger().Error(errFailedToGetPins, zap.Error(err)) + } + + pins, err := h.portal.Accounts().AccountPins(jc.Request.Context().Value(AuthUserIDKey).(uint64), cursor) + + if err != nil { + errored(err) + return + } + + pinsList := make([][]byte, len(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) + + if err != nil { + errored(err) + return + } + + jc.Custom(jc.Request, result) + + jc.ResponseWriter.WriteHeader(http.StatusOK) + _, _ = jc.ResponseWriter.Write(result) +} + func setAuthCookie(jwt string, jc jape.Context) { authCookie := http.Cookie{ Name: "s5-auth-token",