forked from tangled.org/core
this repo has no description

appview: ingest issue comment records

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.sh>

anirudh.fi 727c2570 9d6ab324

verified
Changed files
+81 -6
appview
+19 -6
appview/db/issues.go
··· 3 import ( 4 "database/sql" 5 "fmt" 6 "strings" 7 "time" 8 ··· 109 } 110 111 comment := Comment{ 112 - OwnerDid: ownerDid, 113 - RepoAt: repoAt, 114 - Rkey: rkey, 115 - Body: record.Body, 116 - Issue: issueId, 117 - Created: &created, 118 } 119 120 return comment, nil ··· 621 deleted = strftime('%Y-%m-%dT%H:%M:%SZ', 'now') 622 where repo_at = ? and issue_id = ? and comment_id = ? 623 `, repoAt, issueId, commentId) 624 return err 625 } 626
··· 3 import ( 4 "database/sql" 5 "fmt" 6 + mathrand "math/rand/v2" 7 "strings" 8 "time" 9 ··· 110 } 111 112 comment := Comment{ 113 + OwnerDid: ownerDid, 114 + RepoAt: repoAt, 115 + Rkey: rkey, 116 + Body: record.Body, 117 + Issue: issueId, 118 + CommentId: mathrand.IntN(1000000), 119 + Created: &created, 120 } 121 122 return comment, nil ··· 623 deleted = strftime('%Y-%m-%dT%H:%M:%SZ', 'now') 624 where repo_at = ? and issue_id = ? and comment_id = ? 625 `, repoAt, issueId, commentId) 626 + return err 627 + } 628 + 629 + func DeleteCommentByRkey(e Execer, ownerDid, rkey string) error { 630 + _, err := e.Exec( 631 + ` 632 + update comments 633 + set body = "", 634 + deleted = strftime('%Y-%m-%dT%H:%M:%SZ', 'now') 635 + where owner_did = ? and rkey = ? 636 + `, ownerDid, rkey) 637 return err 638 } 639
+61
appview/ingester.go
··· 70 err = i.ingestString(e) 71 case tangled.RepoIssueNSID: 72 err = i.ingestIssue(ctx, e) 73 } 74 l = i.Logger.With("nsid", e.Commit.Collection) 75 } ··· 673 674 return fmt.Errorf("unknown operation: %s", e.Commit.Operation) 675 }
··· 70 err = i.ingestString(e) 71 case tangled.RepoIssueNSID: 72 err = i.ingestIssue(ctx, e) 73 + case tangled.RepoIssueCommentNSID: 74 + err = i.ingestIssueComment(e) 75 } 76 l = i.Logger.With("nsid", e.Commit.Collection) 77 } ··· 675 676 return fmt.Errorf("unknown operation: %s", e.Commit.Operation) 677 } 678 + 679 + func (i *Ingester) ingestIssueComment(e *models.Event) error { 680 + did := e.Did 681 + rkey := e.Commit.RKey 682 + 683 + var err error 684 + 685 + l := i.Logger.With("handler", "ingestIssueComment", "nsid", e.Commit.Collection, "did", did, "rkey", rkey) 686 + l.Info("ingesting record") 687 + 688 + ddb, ok := i.Db.Execer.(*db.DB) 689 + if !ok { 690 + return fmt.Errorf("failed to index issue comment record, invalid db cast") 691 + } 692 + 693 + switch e.Commit.Operation { 694 + case models.CommitOperationCreate: 695 + raw := json.RawMessage(e.Commit.Record) 696 + record := tangled.RepoIssueComment{} 697 + err = json.Unmarshal(raw, &record) 698 + if err != nil { 699 + l.Error("invalid record", "err", err) 700 + return err 701 + } 702 + 703 + comment, err := db.IssueCommentFromRecord(ddb, did, rkey, record) 704 + if err != nil { 705 + l.Error("failed to parse comment from record", "err", err) 706 + return err 707 + } 708 + 709 + sanitizer := markup.NewSanitizer() 710 + if sb := strings.TrimSpace(sanitizer.SanitizeDefault(comment.Body)); sb == "" { 711 + return fmt.Errorf("body is empty after HTML sanitization") 712 + } 713 + 714 + err = db.NewIssueComment(ddb, &comment) 715 + if err != nil { 716 + l.Error("failed to create issue comment", "err", err) 717 + return err 718 + } 719 + 720 + return nil 721 + 722 + case models.CommitOperationUpdate: 723 + // TODO: implement comment updates 724 + return nil 725 + 726 + case models.CommitOperationDelete: 727 + if err := db.DeleteCommentByRkey(ddb, did, rkey); err != nil { 728 + l.Error("failed to delete", "err", err) 729 + return fmt.Errorf("failed to delete issue comment record: %w", err) 730 + } 731 + 732 + return nil 733 + } 734 + 735 + return fmt.Errorf("unknown operation: %s", e.Commit.Operation) 736 + }
+1
appview/state/state.go
··· 95 tangled.SpindleNSID, 96 tangled.StringNSID, 97 tangled.RepoIssueNSID, 98 }, 99 nil, 100 slog.Default(),
··· 95 tangled.SpindleNSID, 96 tangled.StringNSID, 97 tangled.RepoIssueNSID, 98 + tangled.RepoIssueCommentNSID, 99 }, 100 nil, 101 slog.Default(),