forked from
tangled.org/core
Monorepo for Tangled — https://tangled.org
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
26 // optionally, populate this when querying for reverse mappings
27 RepoStats *RepoStats
28
29 // optional
30 Source string
31}
32
33func (r *Repo) AsRecord() tangled.Repo {
34 var source, spindle, description, website *string
35
36 if r.Source != "" {
37 source = &r.Source
38 }
39
40 if r.Spindle != "" {
41 spindle = &r.Spindle
42 }
43
44 if r.Description != "" {
45 description = &r.Description
46 }
47
48 if r.Website != "" {
49 website = &r.Website
50 }
51
52 return tangled.Repo{
53 Knot: r.Knot,
54 Name: r.Name,
55 Description: description,
56 Website: website,
57 Topics: r.Topics,
58 CreatedAt: r.Created.Format(time.RFC3339),
59 Source: source,
60 Spindle: spindle,
61 Labels: r.Labels,
62 }
63}
64
65func (r Repo) RepoAt() syntax.ATURI {
66 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey))
67}
68
69func (r Repo) DidSlashRepo() string {
70 p, _ := securejoin.SecureJoin(r.Did, r.Name)
71 return p
72}
73
74func (r Repo) TopicStr() string {
75 return strings.Join(r.Topics, " ")
76}
77
78type RepoStats struct {
79 Language string
80 StarCount int
81 IssueCount IssueCount
82 PullCount PullCount
83}
84
85type IssueCount struct {
86 Open int
87 Closed int
88}
89
90type PullCount struct {
91 Open int
92 Merged int
93 Closed int
94 Deleted int
95}
96
97type RepoLabel struct {
98 Id int64
99 RepoAt syntax.ATURI
100 LabelAt syntax.ATURI
101}
102
103type RepoGroup struct {
104 Repo *Repo
105 Issues []Issue
106}
107
108type BlobContentType int
109
110const (
111 BlobContentTypeCode BlobContentType = iota
112 BlobContentTypeMarkup
113 BlobContentTypeImage
114 BlobContentTypeSvg
115 BlobContentTypeVideo
116 BlobContentTypeSubmodule
117)
118
119func (ty BlobContentType) IsCode() bool { return ty == BlobContentTypeCode }
120func (ty BlobContentType) IsMarkup() bool { return ty == BlobContentTypeMarkup }
121func (ty BlobContentType) IsImage() bool { return ty == BlobContentTypeImage }
122func (ty BlobContentType) IsSvg() bool { return ty == BlobContentTypeSvg }
123func (ty BlobContentType) IsVideo() bool { return ty == BlobContentTypeVideo }
124func (ty BlobContentType) IsSubmodule() bool { return ty == BlobContentTypeSubmodule }
125
126type BlobView struct {
127 HasTextView bool // can show as code/text
128 HasRenderedView bool // can show rendered (markup/image/video/submodule)
129 HasRawView bool // can download raw (everything except submodule)
130
131 // current display mode
132 ShowingRendered bool // currently in rendered mode
133 ShowingText bool // currently in text/code mode
134
135 // content type flags
136 ContentType BlobContentType
137
138 // Content data
139 Contents string
140 ContentSrc string // URL for media files
141 Lines int
142 SizeHint uint64
143}
144
145// if both views are available, then show a toggle between them
146func (b BlobView) ShowToggle() bool {
147 return b.HasTextView && b.HasRenderedView
148}
149
150func (b BlobView) IsUnsupported() bool {
151 // no view available, only raw
152 return !(b.HasRenderedView || b.HasTextView)
153}