1package db
2
3import (
4 "fmt"
5 "log"
6 "sort"
7 "time"
8)
9
10type ProfileTimelineEvent struct {
11 EventAt time.Time
12 Type string
13 *Issue
14 *Pull
15 *Repo
16
17 // optional: populate only if Repo is a fork
18 Source *Repo
19}
20
21func MakeProfileTimeline(e Execer, forDid string) ([]ProfileTimelineEvent, error) {
22 timeline := []ProfileTimelineEvent{}
23 limit := 30
24
25 pulls, err := GetPullsByOwnerDid(e, forDid)
26 if err != nil {
27 return timeline, fmt.Errorf("error getting pulls by owner did: %w", err)
28 }
29
30 for _, pull := range pulls {
31 repo, err := GetRepoByAtUri(e, string(pull.RepoAt))
32 if err != nil {
33 return timeline, fmt.Errorf("error getting repo by at uri: %w", err)
34 }
35
36 timeline = append(timeline, ProfileTimelineEvent{
37 EventAt: pull.Created,
38 Type: "pull",
39 Pull: &pull,
40 Repo: repo,
41 })
42 }
43
44 issues, err := GetIssuesByOwnerDid(e, forDid)
45 if err != nil {
46 return timeline, fmt.Errorf("error getting issues by owner did: %w", err)
47 }
48
49 for _, issue := range issues {
50 repo, err := GetRepoByAtUri(e, string(issue.RepoAt))
51 if err != nil {
52 return timeline, fmt.Errorf("error getting repo by at uri: %w", err)
53 }
54
55 timeline = append(timeline, ProfileTimelineEvent{
56 EventAt: *issue.Created,
57 Type: "issue",
58 Issue: &issue,
59 Repo: repo,
60 })
61 }
62
63 repos, err := GetAllReposByDid(e, forDid)
64 if err != nil {
65 return timeline, fmt.Errorf("error getting all repos by did: %w", err)
66 }
67
68 log.Println(repos)
69
70 for _, repo := range repos {
71 var sourceRepo *Repo
72 log.Println("name", repo.Name)
73 if repo.Source != "" {
74 log.Println("source", repo.Source)
75 sourceRepo, err = GetRepoByAtUri(e, repo.Source)
76 if err != nil {
77 return nil, err
78 }
79 }
80
81 timeline = append(timeline, ProfileTimelineEvent{
82 EventAt: repo.Created,
83 Type: "repo",
84 Repo: &repo,
85 Source: sourceRepo,
86 })
87 }
88
89 sort.Slice(timeline, func(i, j int) bool {
90 return timeline[i].EventAt.After(timeline[j].EventAt)
91 })
92
93 if len(timeline) > limit {
94 timeline = timeline[:limit]
95 }
96
97 return timeline, nil
98}