Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blossom): add bud-09 reporting handler. #29

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions blossom/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (bs BlossomServer) handleUploadCheck(w http.ResponseWriter, r *http.Request
func (bs BlossomServer) handleUpload(w http.ResponseWriter, r *http.Request) {
auth, err := readAuthorization(r)
if err != nil {
blossomError(w, "invalid \"Authorization\": "+err.Error(), 400)
blossomError(w, "invalid \"Authorization\": "+err.Error(), 404)
return
}
if auth == nil {
Expand Down Expand Up @@ -206,7 +206,6 @@ func (bs BlossomServer) handleGetBlob(w http.ResponseWriter, r *http.Request) {
}

blossomError(w, "file not found", 404)
return
}

func (bs BlossomServer) handleHasBlob(w http.ResponseWriter, r *http.Request) {
Expand All @@ -228,8 +227,6 @@ func (bs BlossomServer) handleHasBlob(w http.ResponseWriter, r *http.Request) {
blossomError(w, "file not found", 404)
return
}

return
}

func (bs BlossomServer) handleList(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -331,6 +328,38 @@ func (bs BlossomServer) handleDelete(w http.ResponseWriter, r *http.Request) {
}
}

func (bs BlossomServer) handleReport(w http.ResponseWriter, r *http.Request) {
var body []byte
_, err := r.Body.Read(body)
if err != nil {
blossomError(w, "can't read request body", 400)
return
}

var evt *nostr.Event
if err := json.Unmarshal(body, evt); err != nil {
blossomError(w, "can't parse event", 400)
return
}

if isValid, _ := evt.CheckSignature(); !isValid {
blossomError(w, "invalid report event is provided", 400)
return
}

if evt.Kind != nostr.KindReporting {
blossomError(w, "invalid report event is provided", 400)
return
}

for _, rr := range bs.ReceiveReport {
if err := rr(r.Context(), evt); err != nil {
blossomError(w, "failed to receive report: "+err.Error(), 500)
return
}
}
}

func (bs BlossomServer) handleMirror(w http.ResponseWriter, r *http.Request) {
}

Expand Down
14 changes: 11 additions & 3 deletions blossom/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type BlossomServer struct {
ServiceURL string
Store BlobIndex

StoreBlob []func(ctx context.Context, sha256 string, body []byte) error
LoadBlob []func(ctx context.Context, sha256 string) (io.ReadSeeker, error)
DeleteBlob []func(ctx context.Context, sha256 string) error
StoreBlob []func(ctx context.Context, sha256 string, body []byte) error
LoadBlob []func(ctx context.Context, sha256 string) (io.ReadSeeker, error)
DeleteBlob []func(ctx context.Context, sha256 string) error
ReceiveReport []func(ctx context.Context, reportEvt *nostr.Event) error

RejectUpload []func(ctx context.Context, auth *nostr.Event, size int, ext string) (bool, string, int)
RejectGet []func(ctx context.Context, auth *nostr.Event, sha256 string) (bool, string, int)
Expand Down Expand Up @@ -61,6 +62,13 @@ func New(rl *khatru.Relay, serviceURL string) *BlossomServer {
}
}

if r.URL.Path == "/report" {
if r.Method == "PUT" {
bs.handleReport(w, r)
return
}
}

base.ServeHTTP(w, r)
})

Expand Down