Monorepo for Tangled
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.Name,
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) RepoAt() syntax.ATURI {
73 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey))
74}
75
76func (r Repo) RepoIdentifier() string {
77 if r.RepoDid != "" {
78 return r.RepoDid
79 }
80 p, _ := securejoin.SecureJoin(r.Did, r.Name)
81 return p
82}
83
84func (r Repo) TopicStr() string {
85 return strings.Join(r.Topics, " ")
86}
87
88type RepoStats struct {
89 Language string
90 StarCount int
91 IssueCount IssueCount
92 PullCount PullCount
93}
94
95type IssueCount struct {
96 Open int
97 Closed int
98}
99
100type PullCount struct {
101 Open int
102 Merged int
103 Closed int
104 Deleted int
105}
106
107type RepoLabel struct {
108 Id int64
109 RepoAt syntax.ATURI
110 LabelAt syntax.ATURI
111 RepoDid string
112}
113
114type RepoGroup struct {
115 Repo *Repo
116 Issues []Issue
117}
118
119type BlobContentType int
120
121const (
122 BlobContentTypeCode BlobContentType = iota
123 BlobContentTypeMarkup
124 BlobContentTypeImage
125 BlobContentTypeSvg
126 BlobContentTypeVideo
127 BlobContentTypeSubmodule
128)
129
130func (ty BlobContentType) IsCode() bool { return ty == BlobContentTypeCode }
131func (ty BlobContentType) IsMarkup() bool { return ty == BlobContentTypeMarkup }
132func (ty BlobContentType) IsImage() bool { return ty == BlobContentTypeImage }
133func (ty BlobContentType) IsSvg() bool { return ty == BlobContentTypeSvg }
134func (ty BlobContentType) IsVideo() bool { return ty == BlobContentTypeVideo }
135func (ty BlobContentType) IsSubmodule() bool { return ty == BlobContentTypeSubmodule }
136
137type BlobView struct {
138 HasTextView bool // can show as code/text
139 HasRenderedView bool // can show rendered (markup/image/video/submodule)
140 HasRawView bool // can download raw (everything except submodule)
141
142 // current display mode
143 ShowingRendered bool // currently in rendered mode
144
145 // content type flags
146 ContentType BlobContentType
147
148 // Content data
149 Contents string
150 ContentSrc string // URL for media files
151 Lines int
152 SizeHint uint64
153}
154
155// if both views are available, then show a toggle between them
156func (b BlobView) ShowToggle() bool {
157 return b.HasTextView && b.HasRenderedView
158}
159
160func (b BlobView) IsUnsupported() bool {
161 // no view available, only raw
162 return !(b.HasRenderedView || b.HasTextView)
163}
164
165func (b BlobView) ShowingText() bool {
166 return !b.ShowingRendered
167}