Monorepo for Tangled
0
fork

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" ··· 434 434 func (h *Knot) fetchAndAddKeys(ctx context.Context, did string) error { 435 435 l := log.FromContext(ctx) 436 436 437 - keysEndpoint, err := url.JoinPath(h.c.AppViewEndpoint, "keys", did) 437 + id, err := h.resolver.Directory().LookupDID(ctx, syntax.DID(did)) 438 438 if err != nil { 439 - l.Error("error building endpoint url", "did", did, "error", err.Error()) 440 - return fmt.Errorf("error building endpoint url: %w", err) 439 + return fmt.Errorf("lookup did to fetch keys: %w", err) 441 440 } 442 441 443 - resp, err := http.Get(keysEndpoint) 444 - if err != nil { 445 - l.Error("error getting keys", "did", did, "error", err) 446 - return fmt.Errorf("error getting keys: %w", err) 447 - } 448 - defer resp.Body.Close() 449 - 450 - if resp.StatusCode == http.StatusNotFound { 451 - l.Info("no keys found for did", "did", did) 442 + serviceEndpoint, ok := id.Services["atproto_pds"] 443 + if !ok { 444 + l.Warn("did identity did not contain atproto_pds service while adding their keys", "did", did) 452 445 return nil 453 446 } 454 447 455 - plaintext, err := io.ReadAll(resp.Body) 448 + xrpcc := indigoxrpc.Client{Host: serviceEndpoint.URL} 449 + resp, err := comatproto.RepoListRecords(context.Background(), &xrpcc, tangled.PublicKeyNSID, "", 50, did, false) 456 450 if err != nil { 457 - l.Error("error reading response body", "error", err) 458 - return fmt.Errorf("error reading response body: %w", err) 451 + return fmt.Errorf("fetching public keys for did: %w", err) 459 452 } 460 453 461 - for key := range strings.SplitSeq(string(plaintext), "\n") { 462 - if key == "" { 454 + for _, record := range resp.Records { 455 + if record == nil { 456 + continue 457 + } 458 + key := record.Value.Val.(*tangled.PublicKey) 459 + if key == nil { 463 460 continue 464 461 } 465 462 pk := db.PublicKey{ 466 - Did: did, 463 + Did: did, 464 + PublicKey: *key, 467 465 } 468 - pk.Key = key 469 - if err := h.db.AddPublicKey(pk); err != nil { 470 - l.Error("failed to add public key", "error", err) 471 - return fmt.Errorf("failed to add public key: %w", err) 466 + err = h.db.AddPublicKey(pk) 467 + if err != nil { 468 + return fmt.Errorf("adding public key to db: %w", err) 472 469 } 473 470 } 474 471 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