From c68b9d0c4ddf9674018cc7635a014cd966ca2848 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Thu, 21 Aug 2025 22:40:00 +0300 Subject: [PATCH] appview: ingest issue/comment update and delete Change-Id: woxwwvsuuovmvuqnmmzwyqtoxtnwkxnl Also adds the relevant DB helpers for this. Signed-off-by: Anirudh Oppiliappan --- appview/db/issues.go | 21 +++++++++++++++++ appview/ingester.go | 54 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/appview/db/issues.go b/appview/db/issues.go index a8c3b609..927b7467 100644 --- a/appview/db/issues.go +++ b/appview/db/issues.go @@ -626,6 +626,17 @@ func DeleteComment(e Execer, repoAt syntax.ATURI, issueId, commentId int) error return err } +func UpdateCommentByRkey(e Execer, ownerDid, rkey, newBody string) error { + _, err := e.Exec( + ` + update comments + set body = ?, + edited = strftime('%Y-%m-%dT%H:%M:%SZ', 'now') + where owner_did = ? and rkey = ? + `, newBody, ownerDid, rkey) + return err +} + func DeleteCommentByRkey(e Execer, ownerDid, rkey string) error { _, err := e.Exec( ` @@ -637,6 +648,16 @@ func DeleteCommentByRkey(e Execer, ownerDid, rkey string) error { return err } +func UpdateIssueByRkey(e Execer, ownerDid, rkey, title, body string) error { + _, err := e.Exec(`update issues set title = ?, body = ? where owner_did = ? and rkey = ?`, title, body, ownerDid, rkey) + return err +} + +func DeleteIssueByRkey(e Execer, ownerDid, rkey string) error { + _, err := e.Exec(`delete from issues where owner_did = ? and rkey = ?`, ownerDid, rkey) + return err +} + func CloseIssue(e Execer, repoAt syntax.ATURI, issueId int) error { _, err := e.Exec(`update issues set open = 0 where repo_at = ? and issue_id = ?`, repoAt, issueId) return err diff --git a/appview/ingester.go b/appview/ingester.go index 0f472fd8..15e65ea4 100644 --- a/appview/ingester.go +++ b/appview/ingester.go @@ -665,11 +665,41 @@ func (i *Ingester) ingestIssue(ctx context.Context, e *models.Event) error { return nil case models.CommitOperationUpdate: - // TODO: implement updates + raw := json.RawMessage(e.Commit.Record) + record := tangled.RepoIssue{} + err = json.Unmarshal(raw, &record) + if err != nil { + l.Error("invalid record", "err", err) + return err + } + + body := "" + if record.Body != nil { + body = *record.Body + } + + sanitizer := markup.NewSanitizer() + if st := strings.TrimSpace(sanitizer.SanitizeDescription(record.Title)); st == "" { + return fmt.Errorf("title is empty after HTML sanitization") + } + if sb := strings.TrimSpace(sanitizer.SanitizeDefault(body)); sb == "" { + return fmt.Errorf("body is empty after HTML sanitization") + } + + err = db.UpdateIssueByRkey(ddb, did, rkey, record.Title, body) + if err != nil { + l.Error("failed to update issue", "err", err) + return err + } + return nil case models.CommitOperationDelete: - // TODO: implement issue deletion + if err := db.DeleteIssueByRkey(ddb, did, rkey); err != nil { + l.Error("failed to delete", "err", err) + return fmt.Errorf("failed to delete issue record: %w", err) + } + return nil } @@ -720,7 +750,25 @@ func (i *Ingester) ingestIssueComment(e *models.Event) error { return nil case models.CommitOperationUpdate: - // TODO: implement comment updates + raw := json.RawMessage(e.Commit.Record) + record := tangled.RepoIssueComment{} + err = json.Unmarshal(raw, &record) + if err != nil { + l.Error("invalid record", "err", err) + return err + } + + sanitizer := markup.NewSanitizer() + if sb := strings.TrimSpace(sanitizer.SanitizeDefault(record.Body)); sb == "" { + return fmt.Errorf("body is empty after HTML sanitization") + } + + err = db.UpdateCommentByRkey(ddb, did, rkey, record.Body) + if err != nil { + l.Error("failed to update issue comment", "err", err) + return err + } + return nil case models.CommitOperationDelete: -- 2.43.0