This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
model.go
180 lines (161 loc) · 3.38 KB
/
model.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"html/template"
"strings"
"time"
)
type ArticleMetadata struct {
Name string
Author string
IsPage bool
Title string
Tags []string
FeaturedPicURL string
Summary string
CreatedTime int64
ModifiedTime int64
Hits int64
}
type Article struct {
Metadata ArticleMetadata
Text template.HTML
Comments []*Comment
}
func (meta *ArticleMetadata) CreatedTimeRFC3339() string {
return TimeRFC3339(meta.CreatedTime)
}
func (meta *ArticleMetadata) ModifiedTimeRFC3339() string {
return TimeRFC3339(meta.ModifiedTime)
}
func (meta *ArticleMetadata) CreatedTimeHumanReading() string {
return TimeHumanReading(meta.CreatedTime)
}
func (meta *ArticleMetadata) GetCreatedTime() time.Time {
t := time.Unix(meta.CreatedTime, 0).Local()
return t
}
func (meta *ArticleMetadata) GetShortMonth(t1 time.Time) string {
return t1.Month().String()[0:3]
}
func (meta *ArticleMetadata) ModifiedTimeHumanReading() string {
return TimeHumanReading(meta.ModifiedTime)
}
func (meta *ArticleMetadata) TagRawList() string {
return strings.Join(meta.Tags, ", ")
}
func (meta *ArticleMetadata) HasFeaturedPic() bool {
if len(meta.FeaturedPicURL) == 0 {
return false
}
return true
}
func (meta *ArticleMetadata) HasSummary() bool {
if len(meta.Summary) == 0 {
return false
}
return true
}
func (m *ArticleMetadata) BuildFromJson(json interface{}) {
var jsonMap map[string]interface{}
jsonMap = json.(map[string]interface{})
for k, v := range jsonMap {
switch vv := v.(type) {
case string:
switch k {
case "Name":
m.Name = vv
case "Author":
m.Author = vv
case "Title":
m.Title = vv
case "FeaturedPicURL":
m.FeaturedPicURL = vv
case "Summary":
m.Summary = vv
}
case bool:
if k == "IsPage" {
m.IsPage = bool(vv)
}
case float64:
if k == "CreatedTime" {
m.CreatedTime = int64(vv)
} else if k == "ModifiedTime" {
m.ModifiedTime = int64(vv)
} else if k == "Hits" {
m.Hits = int64(vv)
}
default:
if k == "Tags" {
m.Tags = []string{}
if vv != nil {
tags := vv.([]interface{})
for _, t := range tags {
if t.(string) != "" {
m.Tags = append(m.Tags, t.(string))
}
}
}
}
}
}
}
type CommentIndexItem struct {
Name string
CommentNames []string
}
type CommentMetadata struct {
Name string
Author string
ArticleName string
UAgent string
URL string
IP string
Email string
EmailHash string
CreatedTime int64
}
func (m *CommentMetadata) BuildFromJson(json interface{}) {
var jsonMap map[string]interface{}
jsonMap = json.(map[string]interface{})
for k, v := range jsonMap {
switch vv := v.(type) {
case string:
str := vv
switch k {
case "Name":
m.Name = str
case "Author":
m.Author = str
case "URL":
m.URL = str
case "IP":
m.IP = str
case "Email":
m.Email = str
case "EmailHash":
m.EmailHash = str
case "UAgent":
m.UAgent = str
case "ArticleName":
m.ArticleName = str
}
case float64:
if k == "CreatedTime" {
m.CreatedTime = int64(vv)
}
default:
}
}
}
func (meta *CommentMetadata) CreatedTimeHumanReading() string {
return TimeHumanReading(meta.CreatedTime)
}
type Comment struct {
Metadata CommentMetadata
Text template.HTML
}
type TagWrapper struct {
Name string
Count int
}