1package db
2
3import (
4 "sort"
5 "time"
6)
7
8type ProfileTimelineEvent struct {
9 EventAt time.Time
10 Type string
11 *Issue
12 *Pull
13 *Repo
14}
15
16func MakeProfileTimeline(e Execer, forDid string) ([]ProfileTimelineEvent, error) {
17 timeline := []ProfileTimelineEvent{}
18 limit := 30
19
20 pulls, err := GetPullsByOwnerDid(e, forDid)
21 if err != nil {
22 return timeline, err
23 }
24
25 for _, pull := range pulls {
26 repo, err := GetRepoByAtUri(e, string(pull.RepoAt))
27 if err != nil {
28 return timeline, err
29 }
30
31 timeline = append(timeline, ProfileTimelineEvent{
32 EventAt: pull.Created,
33 Type: "pull",
34 Pull: &pull,
35 Repo: repo,
36 })
37 }
38
39 issues, err := GetIssuesByOwnerDid(e, forDid)
40 if err != nil {
41 return timeline, err
42 }
43
44 for _, issue := range issues {
45 repo, err := GetRepoByAtUri(e, string(issue.RepoAt))
46 if err != nil {
47 return timeline, err
48 }
49
50 timeline = append(timeline, ProfileTimelineEvent{
51 EventAt: *issue.Created,
52 Type: "issue",
53 Issue: &issue,
54 Repo: repo,
55 })
56 }
57
58 repos, err := GetAllReposByDid(e, forDid)
59 if err != nil {
60 return timeline, err
61 }
62
63 for _, repo := range repos {
64 timeline = append(timeline, ProfileTimelineEvent{
65 EventAt: repo.Created,
66 Type: "repo",
67 Repo: &repo,
68 })
69 }
70
71 sort.Slice(timeline, func(i, j int) bool {
72 return timeline[i].EventAt.After(timeline[j].EventAt)
73 })
74
75 if len(timeline) > limit {
76 timeline = timeline[:limit]
77 }
78
79 return timeline, nil
80}