forked from
tangled.org/core
Monorepo for Tangled
1package db
2
3import (
4 "database/sql"
5 "fmt"
6 "log"
7 "net/url"
8 "slices"
9 "strings"
10 "time"
11
12 "github.com/bluesky-social/indigo/atproto/syntax"
13 "tangled.org/core/appview/models"
14 "tangled.org/core/orm"
15)
16
17const TimeframeMonths = 7
18
19func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) {
20 timeline := models.ProfileTimeline{
21 ByMonth: make([]models.ByMonth, TimeframeMonths),
22 }
23 now := time.Now()
24 timeframe := fmt.Sprintf("-%d months", TimeframeMonths)
25
26 pulls, err := GetPullsByOwnerDid(e, forDid, timeframe)
27 if err != nil {
28 return nil, fmt.Errorf("error getting pulls by owner did: %w", err)
29 }
30
31 // group pulls by month
32 for _, pull := range pulls {
33 monthsAgo := monthsBetween(pull.Created, now)
34
35 if monthsAgo >= TimeframeMonths {
36 // shouldn't happen; but times are weird
37 continue
38 }
39
40 idx := monthsAgo
41 items := &timeline.ByMonth[idx].PullEvents.Items
42
43 *items = append(*items, &pull)
44 }
45
46 issues, err := GetIssues(
47 e,
48 orm.FilterEq("did", forDid),
49 orm.FilterGte("created", time.Now().AddDate(0, -TimeframeMonths, 0)),
50 )
51 if err != nil {
52 return nil, fmt.Errorf("error getting issues by owner did: %w", err)
53 }
54
55 for _, issue := range issues {
56 monthsAgo := monthsBetween(issue.Created, now)
57
58 if monthsAgo >= TimeframeMonths {
59 // shouldn't happen; but times are weird
60 continue
61 }
62
63 idx := monthsAgo
64 items := &timeline.ByMonth[idx].IssueEvents.Items
65
66 *items = append(*items, &issue)
67 }
68
69 repos, err := GetRepos(e, orm.FilterEq("did", forDid))
70 if err != nil {
71 return nil, fmt.Errorf("error getting all repos by did: %w", err)
72 }
73
74 for _, repo := range repos {
75 // TODO: get this in the original query; requires COALESCE because nullable
76 var sourceRepo *models.Repo
77 if repo.Source != "" {
78 sourceRepo, err = GetRepoByAtUri(e, repo.Source)
79 if err != nil {
80 // the source repo was not found, skip this bit
81 log.Println("profile", "err", err)
82 }
83 }
84
85 monthsAgo := monthsBetween(repo.Created, now)
86
87 if monthsAgo >= TimeframeMonths {
88 // shouldn't happen; but times are weird
89 continue
90 }
91
92 idx := monthsAgo
93
94 items := &timeline.ByMonth[idx].RepoEvents
95 *items = append(*items, models.RepoEvent{
96 Repo: &repo,
97 Source: sourceRepo,
98 })
99 }
100
101 punchcard, err := MakePunchcard(
102 e,
103 orm.FilterEq("did", forDid),
104 orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)),
105 )
106 if err != nil {
107 return nil, fmt.Errorf("error getting commits by did: %w", err)
108 }
109 for _, punch := range punchcard.Punches {
110 if punch.Date.After(now) {
111 continue
112 }
113
114 monthsAgo := monthsBetween(punch.Date, now)
115 if monthsAgo >= TimeframeMonths {
116 // shouldn't happen; but times are weird
117 continue
118 }
119
120 idx := monthsAgo
121 timeline.ByMonth[idx].Commits += punch.Count
122 }
123
124 return &timeline, nil
125}
126
127func monthsBetween(from, to time.Time) int {
128 years := to.Year() - from.Year()
129 months := int(to.Month() - from.Month())
130 return years*12 + months
131}
132
133func UpsertProfile(tx *sql.Tx, profile *models.Profile) error {
134 defer tx.Rollback()
135
136 // update links
137 _, err := tx.Exec(`delete from profile_links where did = ?`, profile.Did)
138 if err != nil {
139 return err
140 }
141 // update vanity stats
142 _, err = tx.Exec(`delete from profile_stats where did = ?`, profile.Did)
143 if err != nil {
144 return err
145 }
146
147 // update pinned repos
148 _, err = tx.Exec(`delete from profile_pinned_repositories where did = ?`, profile.Did)
149 if err != nil {
150 return err
151 }
152
153 includeBskyValue := 0
154 if profile.IncludeBluesky {
155 includeBskyValue = 1
156 }
157
158 _, err = tx.Exec(
159 `insert or replace into profile (
160 did,
161 avatar,
162 description,
163 include_bluesky,
164 location,
165 pronouns,
166 preferred_handle
167 )
168 values (?, ?, ?, ?, ?, ?, ?)`,
169 profile.Did,
170 profile.Avatar,
171 profile.Description,
172 includeBskyValue,
173 profile.Location,
174 profile.Pronouns,
175 string(profile.PreferredHandle),
176 )
177
178 if err != nil {
179 log.Println("profile", "err", err)
180 return err
181 }
182
183 for _, link := range profile.Links {
184 if link == "" {
185 continue
186 }
187
188 _, err := tx.Exec(
189 `insert into profile_links (did, link) values (?, ?)`,
190 profile.Did,
191 link,
192 )
193
194 if err != nil {
195 log.Println("profile_links", "err", err)
196 return err
197 }
198 }
199
200 for _, v := range profile.Stats {
201 if v.Kind == "" {
202 continue
203 }
204
205 _, err := tx.Exec(
206 `insert into profile_stats (did, kind) values (?, ?)`,
207 profile.Did,
208 v.Kind,
209 )
210
211 if err != nil {
212 log.Println("profile_stats", "err", err)
213 return err
214 }
215 }
216
217 for _, pin := range profile.PinnedRepos {
218 if pin == "" {
219 continue
220 }
221
222 _, err := tx.Exec(
223 `insert into profile_pinned_repositories (did, at_uri) values (?, ?)`,
224 profile.Did,
225 pin,
226 )
227
228 if err != nil {
229 log.Println("profile_pinned_repositories", "err", err)
230 return err
231 }
232 }
233
234 return tx.Commit()
235}
236
237func GetProfiles(e Execer, filters ...orm.Filter) (map[string]*models.Profile, error) {
238 var conditions []string
239 var args []any
240 for _, filter := range filters {
241 conditions = append(conditions, filter.Condition())
242 args = append(args, filter.Arg()...)
243 }
244
245 whereClause := ""
246 if conditions != nil {
247 whereClause = " where " + strings.Join(conditions, " and ")
248 }
249
250 profilesQuery := fmt.Sprintf(
251 `select
252 id,
253 did,
254 description,
255 include_bluesky,
256 location,
257 pronouns,
258 preferred_handle
259 from
260 profile
261 %s`,
262 whereClause,
263 )
264 rows, err := e.Query(profilesQuery, args...)
265 if err != nil {
266 return nil, err
267 }
268 defer rows.Close()
269
270 profileMap := make(map[string]*models.Profile)
271 for rows.Next() {
272 var profile models.Profile
273 var includeBluesky int
274 var pronouns sql.Null[string]
275 var preferredHandle sql.Null[string]
276
277 err = rows.Scan(&profile.ID, &profile.Did, &profile.Description, &includeBluesky, &profile.Location, &pronouns, &preferredHandle)
278 if err != nil {
279 return nil, err
280 }
281
282 if includeBluesky != 0 {
283 profile.IncludeBluesky = true
284 }
285
286 if pronouns.Valid {
287 profile.Pronouns = pronouns.V
288 }
289
290 if preferredHandle.Valid {
291 profile.PreferredHandle = syntax.Handle(preferredHandle.V)
292 }
293
294 profileMap[profile.Did] = &profile
295 }
296 if err = rows.Err(); err != nil {
297 return nil, err
298 }
299
300 // populate profile links
301 inClause := strings.TrimSuffix(strings.Repeat("?, ", len(profileMap)), ", ")
302 args = make([]any, len(profileMap))
303 i := 0
304 for did := range profileMap {
305 args[i] = did
306 i++
307 }
308
309 linksQuery := fmt.Sprintf("select link, did from profile_links where did in (%s)", inClause)
310 rows, err = e.Query(linksQuery, args...)
311 if err != nil {
312 return nil, err
313 }
314 defer rows.Close()
315
316 idxs := make(map[string]int)
317 for did := range profileMap {
318 idxs[did] = 0
319 }
320 for rows.Next() {
321 var link, did string
322 if err = rows.Scan(&link, &did); err != nil {
323 return nil, err
324 }
325
326 idx := idxs[did]
327 profileMap[did].Links[idx] = link
328 idxs[did] = idx + 1
329 }
330
331 pinsQuery := fmt.Sprintf("select at_uri, did from profile_pinned_repositories where did in (%s)", inClause)
332 rows, err = e.Query(pinsQuery, args...)
333 if err != nil {
334 return nil, err
335 }
336 defer rows.Close()
337
338 idxs = make(map[string]int)
339 for did := range profileMap {
340 idxs[did] = 0
341 }
342 for rows.Next() {
343 var link syntax.ATURI
344 var did string
345 if err = rows.Scan(&link, &did); err != nil {
346 return nil, err
347 }
348
349 idx := idxs[did]
350 profileMap[did].PinnedRepos[idx] = link
351 idxs[did] = idx + 1
352 }
353
354 return profileMap, nil
355}
356
357func GetDidByPreferredHandle(e Execer, handle syntax.Handle) (syntax.DID, error) {
358 var did string
359 err := e.QueryRow(
360 `select did from profile where preferred_handle = ?`,
361 string(handle),
362 ).Scan(&did)
363 if err != nil {
364 return "", err
365 }
366 return syntax.DID(did), nil
367}
368
369func GetProfile(e Execer, did string) (*models.Profile, error) {
370 var profile models.Profile
371 var pronouns sql.Null[string]
372 var avatar sql.Null[string]
373 var preferredHandle sql.Null[string]
374
375 profile.Did = did
376
377 includeBluesky := 0
378
379 err := e.QueryRow(
380 `select avatar, description, include_bluesky, location, pronouns, preferred_handle from profile where did = ?`,
381 did,
382 ).Scan(&avatar, &profile.Description, &includeBluesky, &profile.Location, &pronouns, &preferredHandle)
383 if err == sql.ErrNoRows {
384 return nil, nil
385 }
386
387 if err != nil {
388 return nil, err
389 }
390
391 if includeBluesky != 0 {
392 profile.IncludeBluesky = true
393 }
394
395 if pronouns.Valid {
396 profile.Pronouns = pronouns.V
397 }
398
399 if avatar.Valid {
400 profile.Avatar = avatar.V
401 }
402
403 if preferredHandle.Valid {
404 profile.PreferredHandle = syntax.Handle(preferredHandle.V)
405 }
406
407 rows, err := e.Query(`select link from profile_links where did = ?`, did)
408 if err != nil {
409 return nil, err
410 }
411 defer rows.Close()
412 i := 0
413 for rows.Next() {
414 if err := rows.Scan(&profile.Links[i]); err != nil {
415 return nil, err
416 }
417 i++
418 }
419
420 rows, err = e.Query(`select kind from profile_stats where did = ?`, did)
421 if err != nil {
422 return nil, err
423 }
424 defer rows.Close()
425 i = 0
426 for rows.Next() {
427 if err := rows.Scan(&profile.Stats[i].Kind); err != nil {
428 return nil, err
429 }
430 value, err := GetVanityStat(e, profile.Did, profile.Stats[i].Kind)
431 if err != nil {
432 return nil, err
433 }
434 profile.Stats[i].Value = value
435 i++
436 }
437
438 rows, err = e.Query(`select at_uri from profile_pinned_repositories where did = ?`, did)
439 if err != nil {
440 return nil, err
441 }
442 defer rows.Close()
443 i = 0
444 for rows.Next() {
445 if err := rows.Scan(&profile.PinnedRepos[i]); err != nil {
446 return nil, err
447 }
448 i++
449 }
450
451 return &profile, nil
452}
453
454func GetVanityStat(e Execer, did string, stat models.VanityStatKind) (uint64, error) {
455 query := ""
456 var args []any
457 switch stat {
458 case models.VanityStatMergedPRCount:
459 query = `select count(id) from pulls where owner_did = ? and state = ?`
460 args = append(args, did, models.PullMerged)
461 case models.VanityStatClosedPRCount:
462 query = `select count(id) from pulls where owner_did = ? and state = ?`
463 args = append(args, did, models.PullClosed)
464 case models.VanityStatOpenPRCount:
465 query = `select count(id) from pulls where owner_did = ? and state = ?`
466 args = append(args, did, models.PullOpen)
467 case models.VanityStatOpenIssueCount:
468 query = `select count(id) from issues where did = ? and open = 1`
469 args = append(args, did)
470 case models.VanityStatClosedIssueCount:
471 query = `select count(id) from issues where did = ? and open = 0`
472 args = append(args, did)
473 case models.VanityStatRepositoryCount:
474 query = `select count(id) from repos where did = ?`
475 args = append(args, did)
476 case models.VanityStatStarCount:
477 query = `select count(id) from stars where subject_at like 'at://' || ? || '%'`
478 args = append(args, did)
479 case models.VanityStatNone:
480 return 0, nil
481 default:
482 return 0, fmt.Errorf("invalid vanity stat kind: %s", stat)
483 }
484
485 var result uint64
486 err := e.QueryRow(query, args...).Scan(&result)
487 if err != nil {
488 return 0, err
489 }
490
491 return result, nil
492}
493
494func ValidateProfile(e Execer, profile *models.Profile) error {
495 // ensure description is not too long
496 if len(profile.Description) > 256 {
497 return fmt.Errorf("Entered bio is too long.")
498 }
499
500 // ensure description is not too long
501 if len(profile.Location) > 40 {
502 return fmt.Errorf("Entered location is too long.")
503 }
504
505 // ensure pronouns are not too long
506 if len(profile.Pronouns) > 40 {
507 return fmt.Errorf("Entered pronouns are too long.")
508 }
509
510 if profile.PreferredHandle != "" {
511 if _, err := syntax.ParseHandle(string(profile.PreferredHandle)); err != nil {
512 return fmt.Errorf("Invalid preferred handle format.")
513 }
514
515 claimant, err := GetDidByPreferredHandle(e, profile.PreferredHandle)
516 if err == nil && string(claimant) != profile.Did {
517 return fmt.Errorf("Preferred handle is already claimed by another user.")
518 }
519 }
520
521 // ensure links are in order
522 err := validateLinks(profile)
523 if err != nil {
524 return err
525 }
526
527 // ensure all pinned repos are either own repos or collaborating repos
528 repos, err := GetRepos(e, orm.FilterEq("did", profile.Did))
529 if err != nil {
530 log.Printf("getting repos for %s: %s", profile.Did, err)
531 }
532
533 collaboratingRepos, err := CollaboratingIn(e, profile.Did)
534 if err != nil {
535 log.Printf("getting collaborating repos for %s: %s", profile.Did, err)
536 }
537
538 var validRepos []syntax.ATURI
539 for _, r := range repos {
540 validRepos = append(validRepos, r.RepoAt())
541 }
542 for _, r := range collaboratingRepos {
543 validRepos = append(validRepos, r.RepoAt())
544 }
545
546 for _, pinned := range profile.PinnedRepos {
547 if pinned == "" {
548 continue
549 }
550 if !slices.Contains(validRepos, pinned) {
551 return fmt.Errorf("Invalid pinned repo: `%s, does not belong to own or collaborating repos", pinned)
552 }
553 }
554
555 return nil
556}
557
558func validateLinks(profile *models.Profile) error {
559 for i, link := range profile.Links {
560 if link == "" {
561 continue
562 }
563
564 parsedURL, err := url.Parse(link)
565 if err != nil {
566 return fmt.Errorf("Invalid URL '%s': %v\n", link, err)
567 }
568
569 if parsedURL.Scheme == "" {
570 if strings.HasPrefix(link, "//") {
571 profile.Links[i] = "https:" + link
572 } else {
573 profile.Links[i] = "https://" + link
574 }
575 continue
576 } else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
577 return fmt.Errorf("Warning: URL '%s' has unusual scheme: %s\n", link, parsedURL.Scheme)
578 }
579
580 // catch relative paths
581 if parsedURL.Host == "" {
582 return fmt.Errorf("Warning: URL '%s' appears to be a relative path\n", link)
583 }
584 }
585 return nil
586}