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