From 74f665f9e06ed0f6c7281578a41e2798c32f342e Mon Sep 17 00:00:00 2001 From: Evan Jarrett Date: Wed, 15 Oct 2025 10:27:11 -0500 Subject: [PATCH] fix getRecord --- pkg/hold/pds/crew.go | 52 +++++++++++++++++++++++++++----------------- pkg/hold/pds/xrpc.go | 9 ++++---- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/pkg/hold/pds/crew.go b/pkg/hold/pds/crew.go index 612c39e..1bcbfdd 100644 --- a/pkg/hold/pds/crew.go +++ b/pkg/hold/pds/crew.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "strings" "time" "github.com/bluesky-social/indigo/repo" @@ -64,40 +65,51 @@ func (p *HoldPDS) AddCrewMember(ctx context.Context, memberDID, role string, per } // GetCrewMember retrieves a crew member by their record key -func (p *HoldPDS) GetCrewMember(ctx context.Context, rkey string) (*CrewRecord, error) { +func (p *HoldPDS) GetCrewMember(ctx context.Context, rkey string) (cid.Cid, *CrewRecord, error) { path := fmt.Sprintf("%s/%s", CrewCollection, rkey) - _, rec, err := p.repo.GetRecord(ctx, path) + // Get the record bytes and decode manually (indigo doesn't know our custom type) + recordCID, recBytes, err := p.repo.GetRecordBytes(ctx, path) if err != nil { - return nil, fmt.Errorf("failed to get crew record: %w", err) + return cid.Undef, nil, fmt.Errorf("failed to get crew record: %w", err) } - crewRecord, ok := rec.(*CrewRecord) - if !ok { - return nil, fmt.Errorf("record is not a CrewRecord") + // Decode the CBOR bytes into our CrewRecord type + var crewRecord CrewRecord + if err := crewRecord.UnmarshalCBOR(bytes.NewReader(*recBytes)); err != nil { + return cid.Undef, nil, fmt.Errorf("failed to decode crew record: %w", err) } - return crewRecord, nil + return recordCID, &crewRecord, nil } -// ListCrewMembers returns all crew members -func (p *HoldPDS) ListCrewMembers(ctx context.Context) ([]*CrewRecord, error) { - var crew []*CrewRecord +// CrewMemberWithKey pairs a crew record with its rkey and CID +type CrewMemberWithKey struct { + Rkey string + Cid cid.Cid + Record *CrewRecord +} + +// ListCrewMembers returns all crew members with their rkeys +func (p *HoldPDS) ListCrewMembers(ctx context.Context) ([]*CrewMemberWithKey, error) { + var crew []*CrewMemberWithKey err := p.repo.ForEach(ctx, CrewCollection, func(k string, v cid.Cid) error { - // Get the record bytes and decode manually (indigo doesn't know our custom type) - _, recBytes, err := p.repo.GetRecordBytes(ctx, k) + // Extract rkey from full path (k is like "io.atcr.hold.crew/3m37dr2ddit22") + parts := strings.Split(k, "/") + rkey := parts[len(parts)-1] + + // Get the full record using GetCrewMember + recordCID, crewRecord, err := p.GetCrewMember(ctx, rkey) if err != nil { return err } - // Decode the CBOR bytes into our CrewRecord type - var crewRecord CrewRecord - if err := crewRecord.UnmarshalCBOR(bytes.NewReader(*recBytes)); err != nil { - return err - } - - crew = append(crew, &crewRecord) + crew = append(crew, &CrewMemberWithKey{ + Rkey: rkey, + Cid: recordCID, + Record: crewRecord, + }) return nil }) @@ -105,7 +117,7 @@ func (p *HoldPDS) ListCrewMembers(ctx context.Context) ([]*CrewRecord, error) { // If the collection doesn't exist yet (empty repo or no records created), // return empty list instead of error if err.Error() == "mst: not found" { - return []*CrewRecord{}, nil + return []*CrewMemberWithKey{}, nil } return nil, fmt.Errorf("failed to list crew members: %w", err) } diff --git a/pkg/hold/pds/xrpc.go b/pkg/hold/pds/xrpc.go index c341be4..6540ae0 100644 --- a/pkg/hold/pds/xrpc.go +++ b/pkg/hold/pds/xrpc.go @@ -175,7 +175,7 @@ func (h *XRPCHandler) HandleGetRecord(w http.ResponseWriter, r *http.Request) { return } - crewRecord, err := h.pds.GetCrewMember(r.Context(), rkey) + recordCID, crewRecord, err := h.pds.GetCrewMember(r.Context(), rkey) if err != nil { http.Error(w, fmt.Sprintf("failed to get record: %v", err), http.StatusNotFound) return @@ -183,6 +183,7 @@ func (h *XRPCHandler) HandleGetRecord(w http.ResponseWriter, r *http.Request) { response := map[string]any{ "uri": fmt.Sprintf("at://%s/%s/%s", h.pds.DID(), collection, rkey), + "cid": recordCID.String(), "value": crewRecord, } @@ -224,10 +225,10 @@ func (h *XRPCHandler) HandleListRecords(w http.ResponseWriter, r *http.Request) records := make([]map[string]any, len(crew)) for i, member := range crew { - // TODO: Get actual rkey from somewhere records[i] = map[string]any{ - "uri": fmt.Sprintf("at://%s/%s/%s", h.pds.DID(), collection, member.Member), - "value": member, + "uri": fmt.Sprintf("at://%s/%s/%s", h.pds.DID(), collection, member.Rkey), + "cid": member.Cid.String(), + "value": member.Record, } }