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, pin) 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 pin, 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 pin string
344 var did string
345 if err = rows.Scan(&pin, &did); err != nil {
346 return nil, err
347 }
348
349 idx := idxs[did]
350 profileMap[did].PinnedRepos[idx] = pin
351 idxs[did] = idx + 1
352 }
353
354 return profileMap, nil
355}
356
357func GetPreferredHandle(e Execer, did string) (syntax.Handle, error) {
358 var h sql.Null[string]
359 err := e.QueryRow(
360 `select preferred_handle from profile where did = ?`,
361 did,
362 ).Scan(&h)
363 if err != nil {
364 return "", err
365 }
366 if !h.Valid || h.V == "" {
367 return "", sql.ErrNoRows
368 }
369 return syntax.Handle(h.V), nil
370}
371
372func GetDidByPreferredHandle(e Execer, handle syntax.Handle) (syntax.DID, error) {
373 var did string
374 err := e.QueryRow(
375 `select did from profile where preferred_handle = ?`,
376 string(handle),
377 ).Scan(&did)
378 if err != nil {
379 return "", err
380 }
381 return syntax.DID(did), nil
382}
383
384func GetProfile(e Execer, did string) (*models.Profile, error) {
385 var profile models.Profile
386 var pronouns sql.Null[string]
387 var avatar sql.Null[string]
388 var preferredHandle sql.Null[string]
389
390 profile.Did = did
391
392 includeBluesky := 0
393
394 err := e.QueryRow(
395 `select avatar, description, include_bluesky, location, pronouns, preferred_handle from profile where did = ?`,
396 did,
397 ).Scan(&avatar, &profile.Description, &includeBluesky, &profile.Location, &pronouns, &preferredHandle)
398 if err == sql.ErrNoRows {
399 return nil, nil
400 }
401
402 if err != nil {
403 return nil, err
404 }
405
406 if includeBluesky != 0 {
407 profile.IncludeBluesky = true
408 }
409
410 if pronouns.Valid {
411 profile.Pronouns = pronouns.V
412 }
413
414 if avatar.Valid {
415 profile.Avatar = avatar.V
416 }
417
418 if preferredHandle.Valid {
419 profile.PreferredHandle = syntax.Handle(preferredHandle.V)
420 }
421
422 rows, err := e.Query(`select link from profile_links where did = ?`, did)
423 if err != nil {
424 return nil, err
425 }
426 defer rows.Close()
427 i := 0
428 for rows.Next() {
429 if err := rows.Scan(&profile.Links[i]); err != nil {
430 return nil, err
431 }
432 i++
433 }
434
435 rows, err = e.Query(`select kind from profile_stats where did = ?`, did)
436 if err != nil {
437 return nil, err
438 }
439 defer rows.Close()
440 i = 0
441 for rows.Next() {
442 if err := rows.Scan(&profile.Stats[i].Kind); err != nil {
443 return nil, err
444 }
445 value, err := GetVanityStat(e, profile.Did, profile.Stats[i].Kind)
446 if err != nil {
447 return nil, err
448 }
449 profile.Stats[i].Value = value
450 i++
451 }
452
453 rows, err = e.Query(`select pin from profile_pinned_repositories where did = ?`, did)
454 if err != nil {
455 return nil, err
456 }
457 defer rows.Close()
458 i = 0
459 for rows.Next() {
460 if err := rows.Scan(&profile.PinnedRepos[i]); err != nil {
461 return nil, err
462 }
463 i++
464 }
465
466 return &profile, nil
467}
468
469func GetVanityStat(e Execer, did string, stat models.VanityStatKind) (uint64, error) {
470 query := ""
471 var args []any
472 switch stat {
473 case models.VanityStatMergedPRCount:
474 query = `select count(id) from pulls where owner_did = ? and state = ?`
475 args = append(args, did, models.PullMerged)
476 case models.VanityStatClosedPRCount:
477 query = `select count(id) from pulls where owner_did = ? and state = ?`
478 args = append(args, did, models.PullClosed)
479 case models.VanityStatOpenPRCount:
480 query = `select count(id) from pulls where owner_did = ? and state = ?`
481 args = append(args, did, models.PullOpen)
482 case models.VanityStatOpenIssueCount:
483 query = `select count(id) from issues where did = ? and open = 1`
484 args = append(args, did)
485 case models.VanityStatClosedIssueCount:
486 query = `select count(id) from issues where did = ? and open = 0`
487 args = append(args, did)
488 case models.VanityStatRepositoryCount:
489 query = `select count(id) from repos where did = ?`
490 args = append(args, did)
491 case models.VanityStatStarCount:
492 query = `select count(id) from stars where subject_at like 'at://' || ? || '%'`
493 args = append(args, did)
494 case models.VanityStatNone:
495 return 0, nil
496 default:
497 return 0, fmt.Errorf("invalid vanity stat kind: %s", stat)
498 }
499
500 var result uint64
501 err := e.QueryRow(query, args...).Scan(&result)
502 if err != nil {
503 return 0, err
504 }
505
506 return result, nil
507}
508
509func ValidateProfile(e Execer, profile *models.Profile) error {
510 // ensure description is not too long
511 if len(profile.Description) > 256 {
512 return fmt.Errorf("Entered bio is too long.")
513 }
514
515 // ensure description is not too long
516 if len(profile.Location) > 40 {
517 return fmt.Errorf("Entered location is too long.")
518 }
519
520 // ensure pronouns are not too long
521 if len(profile.Pronouns) > 40 {
522 return fmt.Errorf("Entered pronouns are too long.")
523 }
524
525 if profile.PreferredHandle != "" {
526 if _, err := syntax.ParseHandle(string(profile.PreferredHandle)); err != nil {
527 return fmt.Errorf("Invalid preferred handle format.")
528 }
529
530 claimant, err := GetDidByPreferredHandle(e, profile.PreferredHandle)
531 if err == nil && string(claimant) != profile.Did {
532 return fmt.Errorf("Preferred handle is already claimed by another user.")
533 }
534 }
535
536 // ensure links are in order
537 err := validateLinks(profile)
538 if err != nil {
539 return err
540 }
541
542 repos, err := GetRepos(e, orm.FilterEq("did", profile.Did))
543 if err != nil {
544 log.Printf("getting repos for %s: %s", profile.Did, err)
545 }
546
547 collaboratingRepos, err := CollaboratingIn(e, profile.Did)
548 if err != nil {
549 log.Printf("getting collaborating repos for %s: %s", profile.Did, err)
550 }
551
552 // ensure all pinned repos are either own repos or collaborating repos
553 allRepos := append(repos, collaboratingRepos...)
554
555 for _, pinned := range profile.PinnedRepos {
556 if pinned == "" {
557 continue
558 }
559 matched := slices.ContainsFunc(allRepos, func(r models.Repo) bool {
560 if strings.HasPrefix(pinned, "did:") {
561 return pinned == r.RepoDid
562 }
563 return pinned == string(r.RepoAt())
564 })
565 if !matched {
566 return fmt.Errorf("Invalid pinned repo: `%s`, does not belong to own or collaborating repos", pinned)
567 }
568 }
569
570 return nil
571}
572
573func validateLinks(profile *models.Profile) error {
574 for i, link := range profile.Links {
575 if link == "" {
576 continue
577 }
578
579 parsedURL, err := url.Parse(link)
580 if err != nil {
581 return fmt.Errorf("Invalid URL '%s': %v\n", link, err)
582 }
583
584 if parsedURL.Scheme == "" {
585 if strings.HasPrefix(link, "//") {
586 profile.Links[i] = "https:" + link
587 } else {
588 profile.Links[i] = "https://" + link
589 }
590 continue
591 } else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
592 return fmt.Errorf("Warning: URL '%s' has unusual scheme: %s\n", link, parsedURL.Scheme)
593 }
594
595 // catch relative paths
596 if parsedURL.Host == "" {
597 return fmt.Errorf("Warning: URL '%s' appears to be a relative path\n", link)
598 }
599 }
600 return nil
601}