Signed-off-by: Anirudh Oppiliappan anirudh@tangled.sh
+19
-6
appview/db/issues.go
+19
-6
appview/db/issues.go
···
3
3
import (
4
4
"database/sql"
5
5
"fmt"
6
+
mathrand "math/rand/v2"
6
7
"strings"
7
8
"time"
8
9
···
109
110
}
110
111
111
112
comment := Comment{
112
-
OwnerDid: ownerDid,
113
-
RepoAt: repoAt,
114
-
Rkey: rkey,
115
-
Body: record.Body,
116
-
Issue: issueId,
117
-
Created: &created,
113
+
OwnerDid: ownerDid,
114
+
RepoAt: repoAt,
115
+
Rkey: rkey,
116
+
Body: record.Body,
117
+
Issue: issueId,
118
+
CommentId: mathrand.IntN(1000000),
119
+
Created: &created,
118
120
}
119
121
120
122
return comment, nil
···
624
626
return err
625
627
}
626
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
+
627
640
func CloseIssue(e Execer, repoAt syntax.ATURI, issueId int) error {
628
641
_, err := e.Exec(`update issues set open = 0 where repo_at = ? and issue_id = ?`, repoAt, issueId)
629
642
return err
+61
appview/ingester.go
+61
appview/ingester.go
···
74
74
err = i.ingestString(e)
75
75
case tangled.RepoIssueNSID:
76
76
err = i.ingestIssue(ctx, e)
77
+
case tangled.RepoIssueCommentNSID:
78
+
err = i.ingestIssueComment(e)
77
79
}
78
80
l = i.Logger.With("nsid", e.Commit.Collection)
79
81
}
···
832
834
833
835
return fmt.Errorf("unknown operation: %s", e.Commit.Operation)
834
836
}
837
+
838
+
func (i *Ingester) ingestIssueComment(e *models.Event) error {
839
+
did := e.Did
840
+
rkey := e.Commit.RKey
841
+
842
+
var err error
843
+
844
+
l := i.Logger.With("handler", "ingestIssueComment", "nsid", e.Commit.Collection, "did", did, "rkey", rkey)
845
+
l.Info("ingesting record")
846
+
847
+
ddb, ok := i.Db.Execer.(*db.DB)
848
+
if !ok {
849
+
return fmt.Errorf("failed to index issue comment record, invalid db cast")
850
+
}
851
+
852
+
switch e.Commit.Operation {
853
+
case models.CommitOperationCreate:
854
+
raw := json.RawMessage(e.Commit.Record)
855
+
record := tangled.RepoIssueComment{}
856
+
err = json.Unmarshal(raw, &record)
857
+
if err != nil {
858
+
l.Error("invalid record", "err", err)
859
+
return err
860
+
}
861
+
862
+
comment, err := db.IssueCommentFromRecord(ddb, did, rkey, record)
863
+
if err != nil {
864
+
l.Error("failed to parse comment from record", "err", err)
865
+
return err
866
+
}
867
+
868
+
sanitizer := markup.NewSanitizer()
869
+
if sb := strings.TrimSpace(sanitizer.SanitizeDefault(comment.Body)); sb == "" {
870
+
return fmt.Errorf("body is empty after HTML sanitization")
871
+
}
872
+
873
+
err = db.NewIssueComment(ddb, &comment)
874
+
if err != nil {
875
+
l.Error("failed to create issue comment", "err", err)
876
+
return err
877
+
}
878
+
879
+
return nil
880
+
881
+
case models.CommitOperationUpdate:
882
+
// TODO: implement comment updates
883
+
return nil
884
+
885
+
case models.CommitOperationDelete:
886
+
if err := db.DeleteCommentByRkey(ddb, did, rkey); err != nil {
887
+
l.Error("failed to delete", "err", err)
888
+
return fmt.Errorf("failed to delete issue comment record: %w", err)
889
+
}
890
+
891
+
return nil
892
+
}
893
+
894
+
return fmt.Errorf("unknown operation: %s", e.Commit.Operation)
895
+
}