back interdiff of round #1 and #0

appview/labels: add "subscribe all" button for default labels #597

merged
opened by oppi.li targeting master from push-lxxtrqtnnoxy

quickly subscribe to all default labels.

Signed-off-by: oppiliappan me@oppi.li

files
appview
db
labels
models
pages
templates
repo
settings
repo
state
ERROR
appview/ingester.go

Failed to calculate interdiff for this file.

ERROR
appview/labels/labels.go

Failed to calculate interdiff for this file.

ERROR
appview/pages/pages.go

Failed to calculate interdiff for this file.

ERROR
appview/pages/templates/repo/settings/general.html

Failed to calculate interdiff for this file.

ERROR
appview/repo/repo.go

Failed to calculate interdiff for this file.

ERROR
appview/state/state.go

Failed to calculate interdiff for this file.

NEW
appview/db/db.go
··· 527 527 -- label to subscribe to 528 528 label_at text not null, 529 529 530 - unique (repo_at, label_at), 531 - foreign key (label_at) references label_definitions (at_uri) 530 + unique (repo_at, label_at) 532 531 ); 533 532 534 533 create table if not exists migrations (
NEW
appview/db/repos.go
··· 345 345 return &repo, nil 346 346 } 347 347 348 - func AddRepo(e Execer, repo *models.Repo) error { 349 - _, err := e.Exec( 348 + func AddRepo(tx *sql.Tx, repo *models.Repo) error { 349 + _, err := tx.Exec( 350 350 `insert into repos 351 351 (did, name, knot, rkey, at_uri, description, source) 352 352 values (?, ?, ?, ?, ?, ?, ?)`, 353 353 repo.Did, repo.Name, repo.Knot, repo.Rkey, repo.RepoAt().String(), repo.Description, repo.Source, 354 354 ) 355 - return err 355 + if err != nil { 356 + return fmt.Errorf("failed to insert repo: %w", err) 357 + } 358 + 359 + for _, dl := range repo.Labels { 360 + if err := SubscribeLabel(tx, &models.RepoLabel{ 361 + RepoAt: repo.RepoAt(), 362 + LabelAt: syntax.ATURI(dl), 363 + }); err != nil { 364 + return fmt.Errorf("failed to subscribe to label: %w", err) 365 + } 366 + } 367 + 368 + return nil 356 369 } 357 370 358 371 func RemoveRepo(e Execer, did, name string) error {
NEW
appview/models/label.go
··· 1 1 package models 2 2 3 3 import ( 4 + "context" 4 5 "crypto/sha1" 5 6 "encoding/hex" 7 + "encoding/json" 6 8 "errors" 7 9 "fmt" 8 10 "slices" 9 11 "time" 10 12 13 + "github.com/bluesky-social/indigo/api/atproto" 11 14 "github.com/bluesky-social/indigo/atproto/syntax" 15 + "github.com/bluesky-social/indigo/xrpc" 12 16 "tangled.org/core/api/tangled" 13 17 "tangled.org/core/consts" 18 + "tangled.org/core/idresolver" 14 19 ) 15 20 16 21 type ConcreteType string ··· 471 476 472 477 return defs 473 478 } 479 + 480 + func FetchDefaultDefs(r *idresolver.Resolver) ([]LabelDefinition, error) { 481 + resolved, err := r.ResolveIdent(context.Background(), consts.TangledDid) 482 + if err != nil { 483 + return nil, fmt.Errorf("failed to resolve tangled.sh DID %s: %v", consts.TangledDid, err) 484 + } 485 + pdsEndpoint := resolved.PDSEndpoint() 486 + if pdsEndpoint == "" { 487 + return nil, fmt.Errorf("no PDS endpoint found for tangled.sh DID %s", consts.TangledDid) 488 + } 489 + client := &xrpc.Client{ 490 + Host: pdsEndpoint, 491 + } 492 + 493 + var labelDefs []LabelDefinition 494 + 495 + for _, dl := range DefaultLabelDefs() { 496 + atUri := syntax.ATURI(dl) 497 + parsedUri, err := syntax.ParseATURI(string(atUri)) 498 + if err != nil { 499 + return nil, fmt.Errorf("failed to parse AT-URI %s: %v", atUri, err) 500 + } 501 + record, err := atproto.RepoGetRecord( 502 + context.Background(), 503 + client, 504 + "", 505 + parsedUri.Collection().String(), 506 + parsedUri.Authority().String(), 507 + parsedUri.RecordKey().String(), 508 + ) 509 + if err != nil { 510 + return nil, fmt.Errorf("failed to get record for %s: %v", atUri, err) 511 + } 512 + 513 + if record != nil { 514 + bytes, err := record.Value.MarshalJSON() 515 + if err != nil { 516 + return nil, fmt.Errorf("failed to marshal record value for %s: %v", atUri, err) 517 + } 518 + 519 + raw := json.RawMessage(bytes) 520 + labelRecord := tangled.LabelDefinition{} 521 + err = json.Unmarshal(raw, &labelRecord) 522 + if err != nil { 523 + return nil, fmt.Errorf("invalid record for %s: %w", atUri, err) 524 + } 525 + 526 + labelDef, err := LabelDefinitionFromRecord( 527 + parsedUri.Authority().String(), 528 + parsedUri.RecordKey().String(), 529 + labelRecord, 530 + ) 531 + if err != nil { 532 + return nil, fmt.Errorf("failed to create label definition from record %s: %v", atUri, err) 533 + } 534 + 535 + labelDefs = append(labelDefs, *labelDef) 536 + } 537 + } 538 + 539 + return labelDefs, nil 540 + }