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