-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_site.go
137 lines (113 loc) · 3.17 KB
/
model_site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ambient
import (
"fmt"
"sort"
"time"
)
// Site represents the site information that is in storage.
type Site struct {
Title string `json:"title"` // Title of the site.
Content string `json:"content"` // Home or default content.
Scheme string `json:"scheme"` // http or https
URL string `json:"url"` // URL without scheme and without trailing slash.
Updated time.Time `json:"updated"` // Save time the data was saved (not only changed).
Posts map[string]Post `json:"posts"` // List of posts.
PluginStorage map[string]PluginData `json:"plugins"` // List of plugins, whether they are found, enabled, and what fields they support.
}
// PluginData represents the plugin storage information.
type PluginData struct {
Enabled bool `json:"enabled"`
Version string `json:"version"`
Grants PluginGrants `json:"grants"`
Settings PluginSettings `json:"settings"`
}
// PluginGrants represents an unordered map of grants.
type PluginGrants map[Grant]bool
// PluginSettings represents an unordered map of settings.
type PluginSettings map[string]interface{}
// Correct will fill in the missing defaults.
func (s *Site) Correct() {
// Set the defaults for the site object.
// Save to storage. Ensure the posts exists first so it doesn't error.
if s.Posts == nil {
s.Posts = make(map[string]Post)
}
if s.PluginStorage == nil {
s.PluginStorage = make(map[string]PluginData)
}
}
// SiteURL returns the URL with the scheme.
func (s Site) SiteURL() string {
return fmt.Sprintf("%v://%v", s.Scheme, s.URL)
}
// PublishedPosts returns published posts (no pages).
func (s Site) PublishedPosts() []Post {
arr := make(PostList, 0)
for _, v := range s.Posts {
if v.Published && !v.Page {
arr = append(arr, v)
}
}
sort.Sort(sort.Reverse(arr))
return arr
}
// PublishedPages returns published pages (no posts).
func (s Site) PublishedPages() []Post {
arr := make(PostList, 0)
for _, v := range s.Posts {
if v.Published && v.Page {
arr = append(arr, v)
}
}
sort.Sort(sort.Reverse(arr))
return arr
}
// PostsAndPages returns list of posts and pages with IDs.
func (s Site) PostsAndPages(onlyPublished bool) PostWithIDList {
arr := make(PostWithIDList, 0)
for k, v := range s.Posts {
if onlyPublished && !v.Published {
continue
}
p := PostWithID{Post: v, ID: k}
arr = append(arr, p)
}
sort.Sort(sort.Reverse(arr))
return arr
}
// Tags returns a list of tags.
func (s Site) Tags(onlyPublished bool) TagList {
// Get unique values.
m := make(map[string]Tag)
for _, v := range s.Posts {
if onlyPublished && !v.Published {
continue
}
for _, t := range v.Tags {
m[t.Name] = t
}
}
// Create unsorted tag list.
arr := make(TagList, 0)
for _, v := range m {
arr = append(arr, v)
}
// Sort by name.
sort.Sort(arr)
return arr
}
// PostBySlug returns a post by slug/URL.
func (s Site) PostBySlug(slug string) PostWithID {
// TODO: This needs to be optimized.
var p PostWithID
for k, v := range s.Posts {
if v.URL == slug {
p = PostWithID{
Post: v,
ID: k,
}
break
}
}
return p
}