This repository has no description
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}
108
109type IssueCount struct {
110 Open int
111 Closed int
112}
113
114type PullCount struct {
115 Open int
116 Merged int
117 Closed int
118 Deleted int
119}
120
121type RepoLabel struct {
122 Id int64
123 RepoDid syntax.DID
124 LabelAt syntax.ATURI
125}
126
127var reservedRepoNames = map[string]struct{}{
128 "self": {},
129}
130
131func ValidateRepoName(name string) error {
132 if len(name) == 0 {
133 return fmt.Errorf("Repository name cannot be empty")
134 }
135 if len(name) > 100 {
136 return fmt.Errorf("Repository name must be 100 characters or fewer")
137 }
138
139 // check for path traversal attempts
140 if strings.Contains(name, "/") || strings.Contains(name, "\\") {
141 return fmt.Errorf("Repository name contains invalid path characters")
142 }
143
144 // check for sequences that could be used for traversal when normalized
145 if strings.HasPrefix(name, ".") || strings.HasSuffix(name, ".") {
146 return fmt.Errorf("Repository name contains invalid path sequence")
147 }
148
149 // then continue with character validation
150 for _, char := range name {
151 if !((char >= 'a' && char <= 'z') ||
152 (char >= 'A' && char <= 'Z') ||
153 (char >= '0' && char <= '9') ||
154 char == '-' || char == '_' || char == '.') {
155 return fmt.Errorf("Repository name can only contain alphanumeric characters, periods, hyphens, and underscores")
156 }
157 }
158
159 // additional check to prevent multiple sequential dots
160 if strings.Contains(name, "..") {
161 return fmt.Errorf("Repository name cannot contain sequential dots")
162 }
163
164 if _, reserved := reservedRepoNames[strings.ToLower(name)]; reserved {
165 return fmt.Errorf("Repository name %q is reserved", name)
166 }
167
168 // if all checks pass
169 return nil
170}
171
172func StripGitExt(name string) string {
173 return strings.TrimSuffix(name, ".git")
174}
175
176type RepoGroup struct {
177 Repo *Repo
178 Issues []Issue
179}
180
181type BlobContentType int
182
183const (
184 BlobContentTypeCode BlobContentType = iota
185 BlobContentTypeMarkup
186 BlobContentTypeImage
187 BlobContentTypeSvg
188 BlobContentTypeVideo
189 BlobContentTypeSubmodule
190)
191
192func (ty BlobContentType) IsCode() bool { return ty == BlobContentTypeCode }
193func (ty BlobContentType) IsMarkup() bool { return ty == BlobContentTypeMarkup }
194func (ty BlobContentType) IsImage() bool { return ty == BlobContentTypeImage }
195func (ty BlobContentType) IsSvg() bool { return ty == BlobContentTypeSvg }
196func (ty BlobContentType) IsVideo() bool { return ty == BlobContentTypeVideo }
197func (ty BlobContentType) IsSubmodule() bool { return ty == BlobContentTypeSubmodule }
198
199type BlobView struct {
200 HasTextView bool // can show as code/text
201 HasRenderedView bool // can show rendered (markup/image/video/submodule)
202 HasRawView bool // can download raw (everything except submodule)
203 FileTooLarge bool // file too large (ignored for image files)
204
205 // current display mode
206 ShowingRendered bool // currently in rendered mode
207
208 // content type flags
209 ContentType BlobContentType
210
211 // Content data
212 Contents string
213 ContentSrc string // URL for media files
214 Lines int
215 SizeHint uint64
216}
217
218// if both views are available, then show a toggle between them
219func (b BlobView) ShowToggle() bool {
220 return b.HasTextView && b.HasRenderedView
221}
222
223func (b BlobView) IsUnsupported() bool {
224 // no view available, only raw
225 return !(b.HasRenderedView || b.HasTextView)
226}
227
228func (b BlobView) ShowingText() bool {
229 return !b.ShowingRendered
230}