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 if strings.Contains(name, "/") || strings.Contains(name, "\\") {
140 return fmt.Errorf("Repository name contains invalid path characters")
141 }
142
143 if strings.HasPrefix(name, ".") || strings.HasSuffix(name, ".") {
144 return fmt.Errorf("Repository name contains invalid path sequence")
145 }
146
147 for _, char := range name {
148 if !((char >= 'a' && char <= 'z') ||
149 (char >= 'A' && char <= 'Z') ||
150 (char >= '0' && char <= '9') ||
151 char == '-' || char == '_' || char == '.') {
152 return fmt.Errorf("Repository name can only contain alphanumeric characters, periods, hyphens, and underscores")
153 }
154 }
155
156 if strings.Contains(name, "..") {
157 return fmt.Errorf("Repository name cannot contain sequential dots")
158 }
159
160 if _, reserved := reservedRepoNames[strings.ToLower(name)]; reserved {
161 return fmt.Errorf("Repository name %q is reserved", name)
162 }
163
164 return nil
165}
166
167func StripGitExt(name string) string {
168 return strings.TrimSuffix(name, ".git")
169}
170
171type RepoGroup struct {
172 Repo *Repo
173 Issues []Issue
174}
175
176type BlobContentType int
177
178const (
179 BlobContentTypeCode BlobContentType = iota
180 BlobContentTypeMarkup
181 BlobContentTypeImage
182 BlobContentTypeSvg
183 BlobContentTypeVideo
184 BlobContentTypeSubmodule
185)
186
187func (ty BlobContentType) IsCode() bool { return ty == BlobContentTypeCode }
188func (ty BlobContentType) IsMarkup() bool { return ty == BlobContentTypeMarkup }
189func (ty BlobContentType) IsImage() bool { return ty == BlobContentTypeImage }
190func (ty BlobContentType) IsSvg() bool { return ty == BlobContentTypeSvg }
191func (ty BlobContentType) IsVideo() bool { return ty == BlobContentTypeVideo }
192func (ty BlobContentType) IsSubmodule() bool { return ty == BlobContentTypeSubmodule }
193
194type BlobView struct {
195 HasTextView bool // can show as code/text
196 HasRenderedView bool // can show rendered (markup/image/video/submodule)
197 HasRawView bool // can download raw (everything except submodule)
198 FileTooLarge bool // file too large (ignored for image files)
199
200 // current display mode
201 ShowingRendered bool // currently in rendered mode
202
203 // content type flags
204 ContentType BlobContentType
205
206 // Content data
207 Contents string
208 ContentSrc string // URL for media files
209 Lines int
210 SizeHint uint64
211}
212
213// if both views are available, then show a toggle between them
214func (b BlobView) ShowToggle() bool {
215 return b.HasTextView && b.HasRenderedView
216}
217
218func (b BlobView) IsUnsupported() bool {
219 // no view available, only raw
220 return !(b.HasRenderedView || b.HasTextView)
221}
222
223func (b BlobView) ShowingText() bool {
224 return !b.ShowingRendered
225}