this repo has no description
1package db 2 3import ( 4 "database/sql" 5 "fmt" 6 "strings" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" 9 "tangled.org/core/api/tangled" 10 "tangled.org/core/appview/models" 11) 12 13// ValidateReferenceLinks resolves refLinks to Issue/PR/IssueComment/PullComment ATURIs. 14// It will ignore missing refLinks. 15func ValidateReferenceLinks(e Execer, refLinks []models.ReferenceLink) ([]syntax.ATURI, error) { 16 var ( 17 issueRefs []models.ReferenceLink 18 pullRefs []models.ReferenceLink 19 ) 20 for _, ref := range refLinks { 21 switch ref.Kind { 22 case models.RefKindIssue: 23 issueRefs = append(issueRefs, ref) 24 case models.RefKindPull: 25 pullRefs = append(pullRefs, ref) 26 } 27 } 28 issueUris, err := findIssueReferences(e, issueRefs) 29 if err != nil { 30 return nil, fmt.Errorf("find issue references: %w", err) 31 } 32 pullUris, err := findPullReferences(e, pullRefs) 33 if err != nil { 34 return nil, fmt.Errorf("find pull references: %w", err) 35 } 36 37 return append(issueUris, pullUris...), nil 38} 39 40func findIssueReferences(e Execer, refLinks []models.ReferenceLink) ([]syntax.ATURI, error) { 41 if len(refLinks) == 0 { 42 return nil, nil 43 } 44 vals := make([]string, len(refLinks)) 45 args := make([]any, 0, len(refLinks)*4) 46 for i, ref := range refLinks { 47 vals[i] = "(?, ?, ?, ?)" 48 args = append(args, ref.Handle, ref.Repo, ref.SubjectId, ref.CommentId) 49 } 50 query := fmt.Sprintf( 51 `with input(owner_did, name, issue_id, comment_id) as ( 52 values %s 53 ) 54 select 55 i.did, i.rkey, 56 c.did, c.rkey 57 from input inp 58 join repos r 59 on r.did = inp.owner_did 60 and r.name = inp.name 61 join issues i 62 on i.repo_at = r.at_uri 63 and i.issue_id = inp.issue_id 64 left join issue_comments c 65 on inp.comment_id is not null 66 and c.issue_at = i.at_uri 67 and c.id = inp.comment_id 68 `, 69 strings.Join(vals, ","), 70 ) 71 rows, err := e.Query(query, args...) 72 if err != nil { 73 return nil, err 74 } 75 defer rows.Close() 76 77 var uris []syntax.ATURI 78 79 for rows.Next() { 80 // Scan rows 81 var issueOwner, issueRkey string 82 var commentOwner, commentRkey sql.NullString 83 var uri syntax.ATURI 84 if err := rows.Scan(&issueOwner, &issueRkey, &commentOwner, &commentRkey); err != nil { 85 return nil, err 86 } 87 if commentOwner.Valid && commentRkey.Valid { 88 uri = syntax.ATURI(fmt.Sprintf( 89 "at://%s/%s/%s", 90 commentOwner.String, 91 tangled.RepoIssueCommentNSID, 92 commentRkey.String, 93 )) 94 } else { 95 uri = syntax.ATURI(fmt.Sprintf( 96 "at://%s/%s/%s", 97 issueOwner, 98 tangled.RepoIssueNSID, 99 issueRkey, 100 )) 101 } 102 uris = append(uris, uri) 103 } 104 if err := rows.Err(); err != nil { 105 return nil, fmt.Errorf("iterate rows: %w", err) 106 } 107 108 return uris, nil 109} 110 111func findPullReferences(e Execer, refLinks []models.ReferenceLink) ([]syntax.ATURI, error) { 112 if len(refLinks) == 0 { 113 return nil, nil 114 } 115 vals := make([]string, len(refLinks)) 116 args := make([]any, 0, len(refLinks)*4) 117 for i, ref := range refLinks { 118 vals[i] = "(?, ?, ?, ?)" 119 args = append(args, ref.Handle, ref.Repo, ref.SubjectId, ref.CommentId) 120 } 121 query := fmt.Sprintf( 122 `with input(owner_did, name, pull_id, comment_id) as ( 123 values %s 124 ) 125 select 126 p.owner_did, p.rkey, 127 c.comment_at 128 from input inp 129 join repos r 130 on r.did = inp.owner_did 131 and r.name = inp.name 132 join pulls p 133 on p.repo_at = r.at_uri 134 and p.pull_id = inp.pull_id 135 left join pull_comments c 136 on inp.comment_id is not null 137 and c.repo_at = r.at_uri and c.pull_id = p.pull_id 138 and c.id = inp.comment_id 139 `, 140 strings.Join(vals, ","), 141 ) 142 rows, err := e.Query(query, args...) 143 if err != nil { 144 return nil, err 145 } 146 defer rows.Close() 147 148 var uris []syntax.ATURI 149 150 for rows.Next() { 151 // Scan rows 152 var pullOwner, pullRkey string 153 var commentUri sql.NullString 154 var uri syntax.ATURI 155 if err := rows.Scan(&pullOwner, &pullRkey, &commentUri); err != nil { 156 return nil, err 157 } 158 if commentUri.Valid { 159 // no-op 160 uri = syntax.ATURI(commentUri.String) 161 } else { 162 uri = syntax.ATURI(fmt.Sprintf( 163 "at://%s/%s/%s", 164 pullOwner, 165 tangled.RepoPullNSID, 166 pullRkey, 167 )) 168 } 169 uris = append(uris, uri) 170 } 171 return uris, nil 172} 173 174func putReferences(tx *sql.Tx, fromAt syntax.ATURI, references []syntax.ATURI) error { 175 err := deleteReferences(tx, fromAt) 176 if err != nil { 177 return fmt.Errorf("delete old reference_links: %w", err) 178 } 179 if len(references) == 0 { 180 return nil 181 } 182 183 values := make([]string, 0, len(references)) 184 args := make([]any, 0, len(references)*2) 185 for _, ref := range references { 186 values = append(values, "(?, ?)") 187 args = append(args, fromAt, ref) 188 } 189 _, err = tx.Exec( 190 fmt.Sprintf( 191 `insert into reference_links (from_at, to_at) 192 values %s`, 193 strings.Join(values, ","), 194 ), 195 args..., 196 ) 197 if err != nil { 198 return fmt.Errorf("insert new reference_links: %w", err) 199 } 200 return nil 201} 202 203func deleteReferences(tx *sql.Tx, fromAt syntax.ATURI) error { 204 _, err := tx.Exec(`delete from reference_links where from_at = ?`, fromAt) 205 return err 206} 207 208func GetReferencesAll(e Execer, filters ...filter) (map[syntax.ATURI][]syntax.ATURI, error) { 209 var ( 210 conditions []string 211 args []any 212 ) 213 for _, filter := range filters { 214 conditions = append(conditions, filter.Condition()) 215 args = append(args, filter.Arg()...) 216 } 217 218 whereClause := "" 219 if conditions != nil { 220 whereClause = " where " + strings.Join(conditions, " and ") 221 } 222 223 rows, err := e.Query( 224 fmt.Sprintf( 225 `select from_at, to_at from reference_links %s`, 226 whereClause, 227 ), 228 args..., 229 ) 230 if err != nil { 231 return nil, fmt.Errorf("query reference_links: %w", err) 232 } 233 defer rows.Close() 234 235 result := make(map[syntax.ATURI][]syntax.ATURI) 236 237 for rows.Next() { 238 var from, to syntax.ATURI 239 if err := rows.Scan(&from, &to); err != nil { 240 return nil, fmt.Errorf("scan row: %w", err) 241 } 242 243 result[from] = append(result[from], to) 244 } 245 if err := rows.Err(); err != nil { 246 return nil, fmt.Errorf("iterate rows: %w", err) 247 } 248 249 return result, nil 250}