an app.bsky.* indexer

unmarshal CBOR

Changed files
+33 -1
cmd
monarch
+33 -1
cmd/monarch/handlers.go
···
package main
import (
+
"bytes"
"context"
"encoding/json"
+
"errors"
"fmt"
+
"io"
+
appbsky "github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/ipfs/go-cid"
"gorm.io/gorm"
···
syntax.NSID("app.bsky.labeler.service"): true,
}
+
type bskycbor interface {
+
UnmarshalCBOR(io.Reader) error
+
}
+
func (hs *HandlerService) HandleUpsert(ctx context.Context, repo string, rev string, path string, rec *[]byte, cid *cid.Cid, action Action) error {
uri, err := syntax.ParseATURI(fmt.Sprintf("at://%s/%s", repo, path))
if err != nil {
···
return nil
}
+
var out any
+
switch uri.Collection() {
+
case syntax.NSID("app.bsky.actor.profile"):
+
out = appbsky.ActorProfile{}
+
case syntax.NSID("app.bsky.feed.generator"):
+
out = appbsky.FeedGenerator{}
+
case syntax.NSID("app.bsky.labeler.service"):
+
out = appbsky.LabelerService{}
+
}
+
+
model, ok := out.(bskycbor)
+
if !ok {
+
return errors.New("collection model cannot unmarshal CBOR")
+
}
+
+
if err := model.UnmarshalCBOR(bytes.NewReader(*rec)); err != nil {
+
return fmt.Errorf("error cbor unmarshaling: %w", err)
+
}
+
+
body, err := json.Marshal(model)
+
if err != nil {
+
return fmt.Errorf("error json marshaling: %w", err)
+
}
+
switch action {
case ActionCreate:
if err := hs.store.Create(&Record{
Repo: repo,
Collection: string(uri.Collection()),
Key: string(uri.RecordKey()),
-
Data: json.RawMessage(*rec),
+
Data: json.RawMessage(body),
}).Error; err != nil {
return err
}