From c9ac66b69f6e298f4475a8d19efffb81bd01c6bb Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Wed, 20 Aug 2025 19:37:59 +0300 Subject: [PATCH] appview/db: helpers for issue/comment from record Change-Id: oourxxuolzwltuzsrxsymwwkquyuonuv Signed-off-by: Anirudh Oppiliappan --- appview/db/issues.go | 75 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/appview/db/issues.go b/appview/db/issues.go index bea2c817..09b7781a 100644 --- a/appview/db/issues.go +++ b/appview/db/issues.go @@ -48,6 +48,77 @@ func (i *Issue) AtUri() syntax.ATURI { return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", i.OwnerDid, tangled.RepoIssueNSID, i.Rkey)) } +func IssueFromRecord(did, rkey string, record tangled.RepoIssue) Issue { + created, err := time.Parse(time.RFC3339, record.CreatedAt) + if err != nil { + created = time.Now() + } + + body := "" + if record.Body != nil { + body = *record.Body + } + + return Issue{ + RepoAt: syntax.ATURI(record.Repo), + OwnerDid: record.Owner, + Rkey: rkey, + Created: created, + Title: record.Title, + Body: body, + Open: true, // new issues are open by default + } +} + +func ResolveIssueFromAtUri(e Execer, issueUri syntax.ATURI) (syntax.ATURI, int, error) { + ownerDid := issueUri.Authority().String() + issueRkey := issueUri.RecordKey().String() + + var repoAt string + var issueId int + + query := `select repo_at, issue_id from issues where owner_did = ? and rkey = ?` + err := e.QueryRow(query, ownerDid, issueRkey).Scan(&repoAt, &issueId) + if err != nil { + return "", 0, err + } + + return syntax.ATURI(repoAt), issueId, nil +} + +func IssueCommentFromRecord(e Execer, did, rkey string, record tangled.RepoIssueComment) (Comment, error) { + created, err := time.Parse(time.RFC3339, record.CreatedAt) + if err != nil { + created = time.Now() + } + + ownerDid := did + if record.Owner != nil { + ownerDid = *record.Owner + } + + issueUri, err := syntax.ParseATURI(record.Issue) + if err != nil { + return Comment{}, err + } + + repoAt, issueId, err := ResolveIssueFromAtUri(e, issueUri) + if err != nil { + return Comment{}, err + } + + comment := Comment{ + OwnerDid: ownerDid, + RepoAt: repoAt, + Rkey: rkey, + Body: record.Body, + Issue: issueId, + Created: &created, + } + + return comment, nil +} + func NewIssue(tx *sql.Tx, issue *Issue) error { defer tx.Rollback() @@ -145,9 +216,9 @@ func GetIssues(e Execer, repoAt syntax.ATURI, isOpen bool, page pagination.Page) body, open, comment_count - from + from numbered_issue - where + where row_num between ? and ?`, repoAt, openValue, page.Offset+1, page.Offset+page.Limit) if err != nil { -- 2.43.0