Monorepo for Tangled tangled.org

appview/db: helpers for issue/comment from record

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

anirudh.fi 3fee847d 88baa380

verified
Changed files
+71
appview
+71
appview/db/issues.go
··· 49 49 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", i.OwnerDid, tangled.RepoIssueNSID, i.Rkey)) 50 50 } 51 51 52 + func IssueFromRecord(did, rkey string, record tangled.RepoIssue) Issue { 53 + created, err := time.Parse(time.RFC3339, record.CreatedAt) 54 + if err != nil { 55 + created = time.Now() 56 + } 57 + 58 + body := "" 59 + if record.Body != nil { 60 + body = *record.Body 61 + } 62 + 63 + return Issue{ 64 + RepoAt: syntax.ATURI(record.Repo), 65 + OwnerDid: record.Owner, 66 + Rkey: rkey, 67 + Created: created, 68 + Title: record.Title, 69 + Body: body, 70 + Open: true, // new issues are open by default 71 + } 72 + } 73 + 74 + func ResolveIssueFromAtUri(e Execer, issueUri syntax.ATURI) (syntax.ATURI, int, error) { 75 + ownerDid := issueUri.Authority().String() 76 + issueRkey := issueUri.RecordKey().String() 77 + 78 + var repoAt string 79 + var issueId int 80 + 81 + query := `select repo_at, issue_id from issues where owner_did = ? and rkey = ?` 82 + err := e.QueryRow(query, ownerDid, issueRkey).Scan(&repoAt, &issueId) 83 + if err != nil { 84 + return "", 0, err 85 + } 86 + 87 + return syntax.ATURI(repoAt), issueId, nil 88 + } 89 + 90 + func IssueCommentFromRecord(e Execer, did, rkey string, record tangled.RepoIssueComment) (Comment, error) { 91 + created, err := time.Parse(time.RFC3339, record.CreatedAt) 92 + if err != nil { 93 + created = time.Now() 94 + } 95 + 96 + ownerDid := did 97 + if record.Owner != nil { 98 + ownerDid = *record.Owner 99 + } 100 + 101 + issueUri, err := syntax.ParseATURI(record.Issue) 102 + if err != nil { 103 + return Comment{}, err 104 + } 105 + 106 + repoAt, issueId, err := ResolveIssueFromAtUri(e, issueUri) 107 + if err != nil { 108 + return Comment{}, err 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 121 + } 122 + 52 123 func NewIssue(tx *sql.Tx, issue *Issue) error { 53 124 defer tx.Rollback() 54 125