Monorepo for Tangled
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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