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>

+28 -25
+21 -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 + l.Error("lookup did to fetch keys", "did", did, "error", err) 440 + return fmt.Errorf("lookup did to fetch keys: %w", err) 441 441 } 442 442 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) 443 + serviceEndpoint, ok := id.Services["atproto_pds"] 444 + if !ok { 445 + l.Warn("did identity did not contain atproto_pds service while adding their keys", "did", did) 452 446 return nil 453 447 } 454 448 455 - plaintext, err := io.ReadAll(resp.Body) 449 + xrpcc := indigoxrpc.Client{Host: serviceEndpoint.URL} 450 + resp, err := comatproto.RepoListRecords(context.Background(), &xrpcc, tangled.PublicKeyNSID, "", 50, did, false) 456 451 if err != nil { 457 - l.Error("error reading response body", "error", err) 458 - return fmt.Errorf("error reading response body: %w", err) 452 + return fmt.Errorf("fetching public keys for did: %w", err) 459 453 } 460 454 461 - for key := range strings.SplitSeq(string(plaintext), "\n") { 462 - if key == "" { 455 + for _, record := range resp.Records { 456 + if record == nil { 457 + continue 458 + } 459 + key := record.Value.Val.(*tangled.PublicKey) 460 + if key == nil { 463 461 continue 464 462 } 465 463 pk := db.PublicKey{ 466 - Did: did, 464 + Did: did, 465 + PublicKey: *key, 467 466 } 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) 467 + err = h.db.AddPublicKey(pk) 468 + if err != nil { 469 + return fmt.Errorf("adding public key to db: %w", err) 472 470 } 473 471 } 474 472 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) 213 218 } 214 219 215 220 return nil