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