forked from
tangled.org/core
Monorepo for Tangled
1package models
2
3import (
4 "fmt"
5
6 "github.com/bluesky-social/indigo/atproto/syntax"
7 "tangled.org/core/api/tangled"
8)
9
10type Profile struct {
11 // ids
12 ID int
13 Did string
14
15 // data
16 Avatar string // CID of the avatar blob
17 Description string
18 IncludeBluesky bool
19 Location string
20 Links [5]string
21 Stats [2]VanityStat
22 PinnedRepos [6]syntax.ATURI
23 Pronouns string
24 PreferredHandle syntax.Handle
25}
26
27func (p Profile) IsLinksEmpty() bool {
28 for _, l := range p.Links {
29 if l != "" {
30 return false
31 }
32 }
33 return true
34}
35
36func (p Profile) IsStatsEmpty() bool {
37 for _, s := range p.Stats {
38 if s.Kind != "" {
39 return false
40 }
41 }
42 return true
43}
44
45func (p Profile) IsPinnedReposEmpty() bool {
46 for _, r := range p.PinnedRepos {
47 if r != "" {
48 return false
49 }
50 }
51 return true
52}
53
54type VanityStatKind string
55
56const (
57 VanityStatMergedPRCount VanityStatKind = "merged-pull-request-count"
58 VanityStatClosedPRCount VanityStatKind = "closed-pull-request-count"
59 VanityStatOpenPRCount VanityStatKind = "open-pull-request-count"
60 VanityStatOpenIssueCount VanityStatKind = "open-issue-count"
61 VanityStatClosedIssueCount VanityStatKind = "closed-issue-count"
62 VanityStatRepositoryCount VanityStatKind = "repository-count"
63 VanityStatStarCount VanityStatKind = "star-count"
64 VanityStatNone VanityStatKind = ""
65)
66
67func ParseVanityStatKind(s string) VanityStatKind {
68 switch s {
69 case "merged-pull-request-count":
70 return VanityStatMergedPRCount
71 case "closed-pull-request-count":
72 return VanityStatClosedPRCount
73 case "open-pull-request-count":
74 return VanityStatOpenPRCount
75 case "open-issue-count":
76 return VanityStatOpenIssueCount
77 case "closed-issue-count":
78 return VanityStatClosedIssueCount
79 case "repository-count":
80 return VanityStatRepositoryCount
81 case "star-count":
82 return VanityStatStarCount
83 default:
84 return VanityStatNone
85 }
86}
87
88func (v VanityStatKind) String() string {
89 switch v {
90 case VanityStatMergedPRCount:
91 return "Merged PRs"
92 case VanityStatClosedPRCount:
93 return "Closed PRs"
94 case VanityStatOpenPRCount:
95 return "Open PRs"
96 case VanityStatOpenIssueCount:
97 return "Open Issues"
98 case VanityStatClosedIssueCount:
99 return "Closed Issues"
100 case VanityStatRepositoryCount:
101 return "Repositories"
102 case VanityStatStarCount:
103 return "Stars Received"
104 default:
105 return ""
106 }
107}
108
109type VanityStat struct {
110 Kind VanityStatKind
111 Value uint64
112}
113
114func (p *Profile) ProfileAt() syntax.ATURI {
115 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", p.Did, tangled.ActorProfileNSID, "self"))
116}
117
118type RepoEvent struct {
119 Repo *Repo
120 Source *Repo
121}
122
123type ProfileTimeline struct {
124 ByMonth []ByMonth
125}
126
127func (p *ProfileTimeline) IsEmpty() bool {
128 if p == nil {
129 return true
130 }
131
132 for _, m := range p.ByMonth {
133 if !m.IsEmpty() {
134 return false
135 }
136 }
137
138 return true
139}
140
141type ByMonth struct {
142 Commits int
143 RepoEvents []RepoEvent
144 IssueEvents IssueEvents
145 PullEvents PullEvents
146}
147
148func (b ByMonth) IsEmpty() bool {
149 return len(b.RepoEvents) == 0 &&
150 len(b.IssueEvents.Items) == 0 &&
151 len(b.PullEvents.Items) == 0 &&
152 b.Commits == 0
153}
154
155type IssueEvents struct {
156 Items []*Issue
157}
158
159type IssueEventStats struct {
160 Open int
161 Closed int
162}
163
164func (i IssueEvents) Stats() IssueEventStats {
165 var open, closed int
166 for _, issue := range i.Items {
167 if issue.Open {
168 open += 1
169 } else {
170 closed += 1
171 }
172 }
173
174 return IssueEventStats{
175 Open: open,
176 Closed: closed,
177 }
178}
179
180type PullEvents struct {
181 Items []*Pull
182}
183
184func (p PullEvents) Stats() PullEventStats {
185 var open, merged, closed int
186 for _, pull := range p.Items {
187 switch pull.State {
188 case PullOpen:
189 open += 1
190 case PullMerged:
191 merged += 1
192 case PullClosed:
193 closed += 1
194 }
195 }
196
197 return PullEventStats{
198 Open: open,
199 Merged: merged,
200 Closed: closed,
201 }
202}
203
204type PullEventStats struct {
205 Closed int
206 Open int
207 Merged int
208}