Monorepo for Tangled tangled.org
6

Configure Feed

Select the types of activity you want to include in your feed.

"knotserver: add owners public keys to database on startup

Signed-off-by: Will Andrews <did:plc:dadhhalkfcq3gucaq25hjqon>

+27 -25
+20 -23
knotserver/ingester.go
··· 8 8 "net/http" 9 9 "net/url" 10 10 "path/filepath" 11 - "strings" 12 11 13 12 comatproto "github.com/bluesky-social/indigo/api/atproto" 14 13 "github.com/bluesky-social/indigo/atproto/syntax" 15 14 "github.com/bluesky-social/indigo/xrpc" 15 + indigoxrpc "github.com/bluesky-social/indigo/xrpc" 16 16 jmodels "github.com/bluesky-social/jetstream/pkg/models" 17 17 "tangled.org/core/api/tangled" 18 18 "tangled.org/core/appview/models" ··· 450 450 func (h *Knot) fetchAndAddKeys(ctx context.Context, did string) error { 451 451 l := log.FromContext(ctx) 452 452 453 - keysEndpoint, err := url.JoinPath(h.c.AppViewEndpoint, "keys", did) 453 + id, err := h.resolver.Directory().LookupDID(ctx, syntax.DID(did)) 454 454 if err != nil { 455 - l.Error("error building endpoint url", "did", did, "error", err.Error()) 456 - return fmt.Errorf("error building endpoint url: %w", err) 455 + return fmt.Errorf("lookup did to fetch keys: %w", err) 457 456 } 458 457 459 - resp, err := http.Get(keysEndpoint) 460 - if err != nil { 461 - l.Error("error getting keys", "did", did, "error", err) 462 - return fmt.Errorf("error getting keys: %w", err) 463 - } 464 - defer resp.Body.Close() 465 - 466 - if resp.StatusCode == http.StatusNotFound { 467 - l.Info("no keys found for did", "did", did) 458 + serviceEndpoint, ok := id.Services["atproto_pds"] 459 + if !ok { 460 + l.Warn("did identity did not contain atproto_pds service while adding their keys", "did", did) 468 461 return nil 469 462 } 470 463 471 - plaintext, err := io.ReadAll(resp.Body) 464 + xrpcc := indigoxrpc.Client{Host: serviceEndpoint.URL} 465 + resp, err := comatproto.RepoListRecords(context.Background(), &xrpcc, tangled.PublicKeyNSID, "", 50, did, false) 472 466 if err != nil { 473 - l.Error("error reading response body", "error", err) 474 - return fmt.Errorf("error reading response body: %w", err) 467 + return fmt.Errorf("fetching public keys for did: %w", err) 475 468 } 476 469 477 - for key := range strings.SplitSeq(string(plaintext), "\n") { 478 - if key == "" { 470 + for _, record := range resp.Records { 471 + if record == nil { 472 + continue 473 + } 474 + key := record.Value.Val.(*tangled.PublicKey) 475 + if key == nil { 479 476 continue 480 477 } 481 478 pk := db.PublicKey{ 482 - Did: did, 479 + Did: did, 480 + PublicKey: *key, 483 481 } 484 - pk.Key = key 485 - if err := h.db.AddPublicKey(pk); err != nil { 486 - l.Error("failed to add public key", "error", err) 487 - return fmt.Errorf("failed to add public key: %w", err) 482 + err = h.db.AddPublicKey(pk) 483 + if err != nil { 484 + return fmt.Errorf("adding public key to db: %w", err) 488 485 } 489 486 } 490 487 return nil
+7 -2
knotserver/router.go
··· 54 54 } 55 55 56 56 // configure owner 57 - if err = h.configureOwner(); err != nil { 57 + if err = h.configureOwner(ctx); err != nil { 58 58 return nil, err 59 59 } 60 60 h.l.Info("owner set", "did", h.c.Server.Owner) ··· 171 171 }) 172 172 } 173 173 174 - func (h *Knot) configureOwner() error { 174 + func (h *Knot) configureOwner(ctx context.Context) error { 175 175 cfgOwner := h.c.Server.Owner 176 176 177 177 rbacDomain := "thisserver" ··· 210 210 } 211 211 if err := h.e.AddKnotOwner(rbacDomain, cfgOwner); err != nil { 212 212 return fmt.Errorf("failed to add owner to RBAC: %w", err) 213 + } 214 + 215 + err = h.fetchAndAddKeys(ctx, cfgOwner) 216 + if err != nil { 217 + h.l.Error("fetching and adding owners public keys", "error", err, "did", cfgOwner) 213 218 } 214 219 215 220 return nil