-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
blogmidware.go
331 lines (286 loc) · 11.9 KB
/
blogmidware.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-2018, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"html/template"
"math"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/b3log/pipe/i18n"
"github.com/b3log/pipe/model"
"github.com/b3log/pipe/service"
"github.com/b3log/pipe/util"
"github.com/dustin/go-humanize"
"github.com/gin-gonic/gin"
)
func resolveBlog(c *gin.Context) {
username := c.Param("username")
if "" == username {
notFound(c)
c.Abort()
return
}
user := service.User.GetUserByName(username)
if nil == user {
notFound(c)
c.Abort()
return
}
userBlog := service.User.GetOwnBlog(user.ID)
if nil == userBlog {
notFound(c)
c.Abort()
return
}
c.Set("userBlog", userBlog)
fillCommon(c)
go service.Statistic.IncViewCount(userBlog.ID)
path := strings.Split(c.Request.RequestURI, username)[1]
path = strings.TrimSpace(path)
if end := strings.Index(path, "?"); 0 < end {
path = path[:end]
}
article := service.Article.GetArticleByPath(path, userBlog.ID)
if nil == article {
c.Next()
return
}
c.Set("article", article)
showArticleAction(c)
c.Abort()
}
func fillCommon(c *gin.Context) {
if "dev" == util.Conf.RuntimeMode {
i18n.Load()
}
userBlogVal, _ := c.Get("userBlog")
userBlog := userBlogVal.(*service.UserBlog)
blogID := userBlog.ID
dataModelVal, _ := c.Get("dataModel")
dataModel := dataModelVal.(*DataModel)
localeSetting := service.Setting.GetSetting(model.SettingCategoryI18n, model.SettingNameI18nLocale, blogID)
i18ns := i18n.GetMessages(localeSetting.Value)
i18nMap := map[string]interface{}{}
for key, value := range i18ns {
i18nMap[strings.Title(key)] = value
i18nMap[key] = value
}
(*dataModel)["I18n"] = i18nMap
settings := service.Setting.GetAllSettings(blogID)
settingMap := map[string]interface{}{}
for _, setting := range settings {
v := setting.Value
if model.SettingNameBasicHeader == setting.Name || model.SettingNameBasicFooter == setting.Name || model.SettingNameBasicNoticeBoard == setting.Name {
mdResult := util.Markdown(v)
v = mdResult.ContentHTML
v = strings.TrimPrefix(v, "<p>")
v = strings.TrimSuffix(v, "</p>")
v = strings.TrimSpace(v)
}
settingMap[strings.Title(setting.Name)] = v
settingMap[setting.Name] = v
}
settingMap[strings.Title(model.SettingNameBasicHeader)] = template.HTML(settingMap[model.SettingNameBasicHeader].(string))
settingMap[strings.Title(model.SettingNameBasicFooter)] = template.HTML(settingMap[model.SettingNameBasicFooter].(string))
settingMap[strings.Title(model.SettingNameBasicNoticeBoard)] = template.HTML(settingMap[model.SettingNameBasicNoticeBoard].(string))
settingMap[strings.Title(model.SettingNameArticleSign)] = template.HTML(settingMap[model.SettingNameArticleSign].(string))
(*dataModel)["Setting"] = settingMap
statistics := service.Statistic.GetAllStatistics(blogID)
statisticMap := map[string]int{}
for _, statistic := range statistics {
count, err := strconv.Atoi(statistic.Value)
if nil != err {
logger.Errorf("statistic [%s] should be an integer, actual is [%v]", statistic.Name, statistic.Value)
}
statisticMap[strings.Title(statistic.Name)] = count
statisticMap[statistic.Name] = count
}
(*dataModel)["Statistic"] = statisticMap
(*dataModel)["FaviconURL"] = settingMap[model.SettingNameBasicFaviconURL]
(*dataModel)["LogoURL"] = settingMap[model.SettingNameBasicLogoURL]
(*dataModel)["BlogURL"] = settingMap[model.SettingNameBasicBlogURL]
(*dataModel)["Title"] = settingMap[model.SettingNameBasicBlogTitle]
(*dataModel)["MetaKeywords"] = settingMap[model.SettingNameBasicMetaKeywords]
(*dataModel)["MetaDescription"] = settingMap[model.SettingNameBasicMetaDescription]
(*dataModel)["Conf"] = util.Conf
(*dataModel)["Year"] = time.Now().Year()
users, _ := service.User.GetBlogUsers(1, blogID)
(*dataModel)["UserCount"] = len(users)
(*dataModel)["Navigations"] = service.Navigation.GetNavigations(blogID)
fillMostUseCategories(&settingMap, dataModel, blogID)
fillMostUseTags(&settingMap, dataModel, blogID)
fillMostViewArticles(c, &settingMap, dataModel, blogID)
fillRecentComments(c, &settingMap, dataModel, blogID)
fillMostCommentArticles(c, &settingMap, dataModel, blogID)
c.Set("dataModel", dataModel)
}
func fillMostUseCategories(settingMap *map[string]interface{}, dataModel *DataModel, blogID uint64) {
categories := service.Category.GetCategories(math.MaxInt8, blogID)
var themeCategories []*model.ThemeCategory
for _, category := range categories {
themeCategory := &model.ThemeCategory{
Title: category.Title,
URL: (*settingMap)[model.SettingNameBasicBlogURL].(string) + util.PathCategories + category.Path,
}
themeCategories = append(themeCategories, themeCategory)
}
(*dataModel)["MostUseCategories"] = themeCategories
}
func fillMostUseTags(settingMap *map[string]interface{}, dataModel *DataModel, blogID uint64) {
tagSize, err := strconv.Atoi((*settingMap)[model.SettingNamePreferenceMostUseTagListSize].(string))
if nil != err {
logger.Errorf("setting [%s] should be an integer, actual is [%v]", model.SettingNamePreferenceMostUseTagListSize,
(*settingMap)[model.SettingNamePreferenceMostUseTagListSize])
tagSize = model.SettingPreferenceMostUseTagListSizeDefault
}
tags := service.Tag.GetTags(tagSize, blogID)
var themeTags []*model.ThemeTag
for _, tag := range tags {
themeTag := &model.ThemeTag{
Title: tag.Title,
URL: (*settingMap)[model.SettingNameBasicBlogURL].(string) + "/tags/" + tag.Title,
}
themeTags = append(themeTags, themeTag)
}
(*dataModel)["MostUseTags"] = themeTags
}
func fillMostViewArticles(c *gin.Context, settingMap *map[string]interface{}, dataModel *DataModel, blogID uint64) {
mostViewArticleSize, err := strconv.Atoi((*settingMap)[model.SettingNamePreferenceMostViewArticleListSize].(string))
if nil != err {
logger.Errorf("setting [%s] should be an integer, actual is [%v]", model.SettingNamePreferenceMostViewArticleListSize,
(*settingMap)[model.SettingNamePreferenceMostViewArticleListSize])
mostViewArticleSize = model.SettingPreferenceMostViewArticleListSizeDefault
}
mostViewArticles := service.Article.GetMostViewArticles(mostViewArticleSize, blogID)
var themeMostViewArticles []*model.ThemeArticle
for _, article := range mostViewArticles {
authorModel := service.User.GetUser(article.AuthorID)
if nil == authorModel {
logger.Errorf("not found author of article [id=%d, authorID=%d]", article.ID, article.AuthorID)
continue
}
author := &model.ThemeAuthor{
Name: authorModel.Name,
URL: getBlogURL(c) + util.PathAuthors + "/" + authorModel.Name,
AvatarURL: authorModel.AvatarURL,
}
themeArticle := &model.ThemeArticle{
Title: article.Title,
URL: (*settingMap)[model.SettingNameBasicBlogURL].(string) + article.Path,
CreatedAt: humanize.Time(article.CreatedAt),
Author: author,
}
themeMostViewArticles = append(themeMostViewArticles, themeArticle)
}
(*dataModel)["MostViewArticles"] = themeMostViewArticles
}
func fillRecentComments(c *gin.Context, settingMap *map[string]interface{}, dataModel *DataModel, blogID uint64) {
recentCommentSize, err := strconv.Atoi((*settingMap)[model.SettingNamePreferenceRecentCommentListSize].(string))
if nil != err {
logger.Errorf("setting [%s] should be an integer, actual is [%v]", model.SettingNamePreferenceRecentCommentListSize,
(*settingMap)[model.SettingNamePreferenceRecentCommentListSize])
recentCommentSize = model.SettingPreferenceRecentCommentListSizeDefault
}
recentComments := service.Comment.GetRecentComments(recentCommentSize, blogID)
var themeRecentComments []*model.ThemeComment
for _, comment := range recentComments {
author := &model.ThemeAuthor{}
if model.SyncCommentAuthorID == comment.AuthorID {
author.URL = comment.AuthorURL
author.Name = comment.AuthorName
author.AvatarURL = comment.AuthorAvatarURL
} else {
commentAuthor := service.User.GetUser(comment.AuthorID)
commentAuthorBlog := service.User.GetOwnBlog(comment.AuthorID)
author.URL = service.Setting.GetSetting(model.SettingCategoryBasic, model.SettingNameBasicBlogURL, commentAuthorBlog.ID).Value + util.PathAuthors + "/" + commentAuthor.Name
author.Name = commentAuthor.Name
author.AvatarURL = commentAuthor.AvatarURL
}
page := service.Comment.GetCommentPage(comment.ArticleID, comment.ID, blogID)
article := service.Article.ConsoleGetArticle(comment.ArticleID)
themeComment := &model.ThemeComment{
Title: util.Markdown(comment.Content).AbstractText,
URL: getBlogURL(c) + article.Path + "?p=" + strconv.Itoa(page) + "#pipeComment" + strconv.Itoa(int(comment.ID)),
CreatedAt: humanize.Time(comment.CreatedAt),
Author: author,
}
themeRecentComments = append(themeRecentComments, themeComment)
}
(*dataModel)["RecentComments"] = themeRecentComments
}
func fillMostCommentArticles(c *gin.Context, settingMap *map[string]interface{}, dataModel *DataModel, blogID uint64) {
mostCommentArticleSize, err := strconv.Atoi((*settingMap)[model.SettingNamePreferenceMostCommentArticleListSize].(string))
if nil != err {
logger.Errorf("setting [%s] should be an integer, actual is [%v]", model.SettingNamePreferenceMostCommentArticleListSize,
(*settingMap)[model.SettingNamePreferenceMostCommentArticleListSize])
mostCommentArticleSize = model.SettingPreferenceMostCommentArticleListSizeDefault
}
mostCommentArticles := service.Article.GetMostCommentArticles(mostCommentArticleSize, blogID)
var themeMostCommentArticles []*model.ThemeArticle
for _, article := range mostCommentArticles {
authorModel := service.User.GetUser(article.AuthorID)
if nil == authorModel {
logger.Errorf("not found author of article [id=%d, authorID=%d]", article.ID, article.AuthorID)
continue
}
author := &model.ThemeAuthor{
Name: authorModel.Name,
URL: getBlogURL(c) + util.PathAuthors + "/" + authorModel.Name,
AvatarURL: authorModel.AvatarURL,
}
themeArticle := &model.ThemeArticle{
Title: article.Title,
URL: (*settingMap)[model.SettingNameBasicBlogURL].(string) + article.Path,
CreatedAt: humanize.Time(article.CreatedAt),
Author: author,
}
themeMostCommentArticles = append(themeMostCommentArticles, themeArticle)
}
(*dataModel)["MostCommentArticles"] = themeMostCommentArticles
}
func getBlogURL(c *gin.Context) string {
dataModel := getDataModel(c)
return dataModel["Setting"].(map[string]interface{})[model.SettingNameBasicBlogURL].(string)
}
func getBlogID(c *gin.Context) uint64 {
userBlogVal, _ := c.Get("userBlog")
return userBlogVal.(*service.UserBlog).ID
}
func getTheme(c *gin.Context) string {
dataModel := getDataModel(c)
return dataModel["Setting"].(map[string]interface{})[model.SettingNameThemeName].(string)
}
func getDataModel(c *gin.Context) DataModel {
dataModelVal, _ := c.Get("dataModel")
return *(dataModelVal.(*DataModel))
}
func getLocale(c *gin.Context) string {
dataModel := getDataModel(c)
return dataModel["Setting"].(map[string]interface{})[model.SettingNameI18nLocale].(string)
}
func notFound(c *gin.Context) {
t, err := template.ParseFiles(filepath.ToSlash(filepath.Join(util.Conf.StaticRoot, "console/dist/init/index.html")))
if nil != err {
logger.Errorf("load 404 page failed: " + err.Error())
c.String(http.StatusNotFound, "load 404 page failed")
return
}
c.Status(http.StatusNotFound)
t.Execute(c.Writer, nil)
}