This repository has no description
0

Configure Feed

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

core / appview / models / repo.go
5.2 kB 231 lines
1package models 2 3import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" 9 securejoin "github.com/cyphar/filepath-securejoin" 10 "tangled.org/core/api/tangled" 11) 12 13type Repo struct { 14 Id int64 15 Did string 16 Name string 17 Knot string 18 Rkey string 19 Created time.Time 20 Description string 21 Website string 22 Topics []string 23 Spindle string 24 Labels []string 25 RepoDid string 26 27 // optionally, populate this when querying for reverse mappings 28 RepoStats *RepoStats 29 30 // optional 31 Source string 32} 33 34func (r *Repo) AsRecord() tangled.Repo { 35 var source, spindle, description, website *string 36 37 if r.Source != "" { 38 source = &r.Source 39 } 40 41 if r.Spindle != "" { 42 spindle = &r.Spindle 43 } 44 45 if r.Description != "" { 46 description = &r.Description 47 } 48 49 if r.Website != "" { 50 website = &r.Website 51 } 52 53 var repoDid *string 54 if r.RepoDid != "" { 55 repoDid = &r.RepoDid 56 } 57 58 return tangled.Repo{ 59 Knot: r.Knot, 60 Name: r.cosmeticName(), 61 Description: description, 62 Website: website, 63 Topics: r.Topics, 64 CreatedAt: r.Created.Format(time.RFC3339), 65 Source: source, 66 Spindle: spindle, 67 Labels: r.Labels, 68 RepoDid: repoDid, 69 } 70} 71 72func (r *Repo) cosmeticName() *string { 73 if r.Name == "" || r.Name == r.Rkey { 74 return nil 75 } 76 return &r.Name 77} 78 79func (r Repo) RepoAt() syntax.ATURI { 80 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey)) 81} 82 83func (r Repo) RepoIdentifier() string { 84 if r.RepoDid != "" { 85 return r.RepoDid 86 } 87 p, _ := securejoin.SecureJoin(r.Did, r.Rkey) 88 return p 89} 90 91func (r Repo) PinIdentifier() string { 92 if r.RepoDid != "" { 93 return r.RepoDid 94 } 95 return string(r.RepoAt()) 96} 97 98func (r Repo) TopicStr() string { 99 return strings.Join(r.Topics, " ") 100} 101 102type RepoStats struct { 103 Language string 104 StarCount int 105 IssueCount IssueCount 106 PullCount PullCount 107 ForkCount int 108} 109 110type IssueCount struct { 111 Open int 112 Closed int 113} 114 115type PullCount struct { 116 Open int 117 Merged int 118 Closed int 119 Deleted int 120} 121 122type RepoLabel struct { 123 Id int64 124 RepoDid syntax.DID 125 LabelAt syntax.ATURI 126} 127 128var reservedRepoNames = map[string]struct{}{ 129 "self": {}, 130} 131 132func ValidateRepoName(name string) error { 133 if len(name) == 0 { 134 return fmt.Errorf("Repository name cannot be empty") 135 } 136 if len(name) > 100 { 137 return fmt.Errorf("Repository name must be 100 characters or fewer") 138 } 139 140 // check for path traversal attempts 141 if strings.Contains(name, "/") || strings.Contains(name, "\\") { 142 return fmt.Errorf("Repository name contains invalid path characters") 143 } 144 145 // check for sequences that could be used for traversal when normalized 146 if strings.HasPrefix(name, ".") || strings.HasSuffix(name, ".") { 147 return fmt.Errorf("Repository name contains invalid path sequence") 148 } 149 150 // then continue with character validation 151 for _, char := range name { 152 if !((char >= 'a' && char <= 'z') || 153 (char >= 'A' && char <= 'Z') || 154 (char >= '0' && char <= '9') || 155 char == '-' || char == '_' || char == '.') { 156 return fmt.Errorf("Repository name can only contain alphanumeric characters, periods, hyphens, and underscores") 157 } 158 } 159 160 // additional check to prevent multiple sequential dots 161 if strings.Contains(name, "..") { 162 return fmt.Errorf("Repository name cannot contain sequential dots") 163 } 164 165 if _, reserved := reservedRepoNames[strings.ToLower(name)]; reserved { 166 return fmt.Errorf("Repository name %q is reserved", name) 167 } 168 169 // if all checks pass 170 return nil 171} 172 173func StripGitExt(name string) string { 174 return strings.TrimSuffix(name, ".git") 175} 176 177type RepoGroup struct { 178 Repo *Repo 179 Issues []Issue 180} 181 182type BlobContentType int 183 184const ( 185 BlobContentTypeCode BlobContentType = iota 186 BlobContentTypeMarkup 187 BlobContentTypeImage 188 BlobContentTypeSvg 189 BlobContentTypeVideo 190 BlobContentTypeSubmodule 191) 192 193func (ty BlobContentType) IsCode() bool { return ty == BlobContentTypeCode } 194func (ty BlobContentType) IsMarkup() bool { return ty == BlobContentTypeMarkup } 195func (ty BlobContentType) IsImage() bool { return ty == BlobContentTypeImage } 196func (ty BlobContentType) IsSvg() bool { return ty == BlobContentTypeSvg } 197func (ty BlobContentType) IsVideo() bool { return ty == BlobContentTypeVideo } 198func (ty BlobContentType) IsSubmodule() bool { return ty == BlobContentTypeSubmodule } 199 200type BlobView struct { 201 HasTextView bool // can show as code/text 202 HasRenderedView bool // can show rendered (markup/image/video/submodule) 203 HasRawView bool // can download raw (everything except submodule) 204 FileTooLarge bool // file too large (ignored for image files) 205 206 // current display mode 207 ShowingRendered bool // currently in rendered mode 208 209 // content type flags 210 ContentType BlobContentType 211 212 // Content data 213 Contents string 214 ContentSrc string // URL for media files 215 Lines int 216 SizeHint uint64 217} 218 219// if both views are available, then show a toggle between them 220func (b BlobView) ShowToggle() bool { 221 return b.HasTextView && b.HasRenderedView 222} 223 224func (b BlobView) IsUnsupported() bool { 225 // no view available, only raw 226 return !(b.HasRenderedView || b.HasTextView) 227} 228 229func (b BlobView) ShowingText() bool { 230 return !b.ShowingRendered 231}