diff --git a/app/controllers/BlogController.go b/app/controllers/BlogController.go
index d0a84f4..172b9d8 100644
--- a/app/controllers/BlogController.go
+++ b/app/controllers/BlogController.go
@@ -4,11 +4,11 @@ import (
"github.com/revel/revel"
"strings"
// "encoding/json"
+ "fmt"
"github.com/leanote/leanote/app/info"
. "github.com/leanote/leanote/app/lea"
"github.com/leanote/leanote/app/lea/blog"
"gopkg.in/mgo.v2/bson"
- "fmt"
// "github.com/leanote/leanote/app/types"
// "io/ioutil"
// "math"
@@ -47,26 +47,25 @@ $.bootstrapCssUrl
$.bootstrapJsUrl
*/
-
func (c Blog) render(templateName string, themePath string) revel.Result {
isPreview := false
- if c.RenderArgs["isPreview"] != nil {
+ if c.RenderArgs["isPreview"] != nil {
themePath2 := c.RenderArgs["themePath"]
if themePath2 == nil {
- return c.E404();
+ return c.E404()
}
isPreview = true
themePath = themePath2.(string)
c.setPreviewUrl()
}
- return blog.RenderTemplate(templateName, c.RenderArgs, revel.BasePath + "/" + themePath, isPreview)
+ return blog.RenderTemplate(templateName, c.RenderArgs, revel.BasePath+"/"+themePath, isPreview)
}
// 404
func (c Blog) e404(themePath string) revel.Result {
// 不知道是谁的404, 则用系统的404
if themePath == "" {
- return c.E404();
+ return c.E404()
}
return c.render("404.html", themePath)
}
@@ -101,24 +100,24 @@ func (c Blog) domain() (ok bool, userBlog info.UserBlog) {
// 渲染模板之
func (c Blog) setPreviewUrl() {
var indexUrl, postUrl, searchUrl, cateUrl, singleUrl, tagsUrl, archiveUrl string
-
+
userId := c.GetUserId()
themeId := c.Session["themeId"]
theme := themeService.GetTheme(userId, themeId)
-
+
siteUrl := configService.GetSiteUrl()
blogUrl := siteUrl + "/preview" // blog.leanote.com
userIdOrEmail := userId
indexUrl = blogUrl + "/" + userIdOrEmail
- cateUrl = blogUrl + "/cate" // /notebookId
-
- postUrl = blogUrl + "/post" // /xxxxx
- searchUrl = blogUrl + "/search/" + userIdOrEmail // blog.leanote.com/search/userId
- singleUrl = blogUrl + "/single" // blog.leanote.com/single/singleId
+ cateUrl = blogUrl + "/cate" // /notebookId
+
+ postUrl = blogUrl + "/post" // /xxxxx
+ searchUrl = blogUrl + "/search/" + userIdOrEmail // blog.leanote.com/search/userId
+ singleUrl = blogUrl + "/single" // blog.leanote.com/single/singleId
archiveUrl = blogUrl + "/archives/" + userIdOrEmail // blog.leanote.com/archive/userId
- tagsUrl = blogUrl + "/tags/" + userIdOrEmail // blog.leanote.com/archive/userId
-
+ tagsUrl = blogUrl + "/tags/" + userIdOrEmail // blog.leanote.com/archive/userId
+
c.RenderArgs["indexUrl"] = indexUrl
c.RenderArgs["cateUrl"] = cateUrl
c.RenderArgs["postUrl"] = postUrl
@@ -127,7 +126,7 @@ func (c Blog) setPreviewUrl() {
c.RenderArgs["archiveUrl"] = archiveUrl
c.RenderArgs["archivesUrl"] = archiveUrl // 别名
c.RenderArgs["tagsUrl"] = tagsUrl
- c.RenderArgs["tagPostsUrl"] = blogUrl + "/tag/" + userIdOrEmail
+ c.RenderArgs["tagPostsUrl"] = blogUrl + "/tag/" + userIdOrEmail
c.RenderArgs["tagUrl"] = c.RenderArgs["tagPostsUrl"]
// themeBaseUrl 本theme的路径url, 可以加载js, css, images之类的
@@ -137,89 +136,55 @@ func (c Blog) setPreviewUrl() {
// 各种地址设置
func (c Blog) setUrl(userBlog info.UserBlog, userInfo info.User) {
// 主页 http://leanote.com/blog/life or http://blog.leanote.com/life or http:// xxxx.leanote.com or aa.com
- var indexUrl, postUrl, searchUrl, cateUrl, singleUrl, tagsUrl, archiveUrl, tagPostsUrl, staticUrl string
host := c.Request.Request.Host
- staticUrl = configService.GetUserUrl(strings.Split(host, ":")[0])
+ var staticUrl = configService.GetUserUrl(strings.Split(host, ":")[0])
// staticUrl == host, 为保证同源!!! 只有host, http://leanote.com, http://blog/leanote.com
// life.leanote.com, lealife.com
siteUrl := configService.GetSiteUrl()
- if userBlog.Domain != "" && configService.AllowCustomDomain() {
- // ok
- indexUrl = configService.GetUserUrl(userBlog.Domain)
- cateUrl = indexUrl + "/cate" // /xxxxx
- postUrl = indexUrl + "/post" // /xxxxx
- searchUrl = indexUrl + "/search" // /xxxxx
- singleUrl = indexUrl + "/single"
- archiveUrl = indexUrl + "/archives"
- tagsUrl = indexUrl + "/tags"
- tagPostsUrl = indexUrl + "/tag"
- } else if userBlog.SubDomain != "" {
- indexUrl = configService.GetUserSubUrl(userBlog.SubDomain)
- cateUrl = indexUrl + "/cate" // /xxxxx
- postUrl = indexUrl + "/post" // /xxxxx
- searchUrl = indexUrl + "/search" // /xxxxx
- singleUrl = indexUrl + "/single"
- archiveUrl = indexUrl + "/archives"
- tagsUrl = indexUrl + "/tags"
- tagPostsUrl = indexUrl + "/tag"
- } else {
- // ok
- blogUrl := configService.GetBlogUrl() // blog.leanote.com
- userIdOrEmail := ""
- if userInfo.Username != "" {
- userIdOrEmail = userInfo.Username
- } else if userInfo.Email != "" {
- userIdOrEmail = userInfo.Email
- } else {
- userIdOrEmail = userInfo.UserId.Hex()
- }
- indexUrl = blogUrl + "/" + userIdOrEmail
- cateUrl = blogUrl + "/cate" // /notebookId
- postUrl = blogUrl + "/post" // /xxxxx
- searchUrl = blogUrl + "/search/" + userIdOrEmail // blog.leanote.com/search/userId
- singleUrl = blogUrl + "/single" // blog.leanote.com/single/singleId
- archiveUrl = blogUrl + "/archives/" + userIdOrEmail // blog.leanote.com/archive/userId
- tagsUrl = blogUrl + "/tags/" + userIdOrEmail
- tagPostsUrl = blogUrl + "/tag/" + userIdOrEmail // blog.leanote.com/archive/userId
- }
-
+ blogUrls := blogService.GetBlogUrls(&userBlog, &userInfo)
// 分类
// 搜索
// 查看
c.RenderArgs["siteUrl"] = siteUrl
- c.RenderArgs["indexUrl"] = indexUrl
- c.RenderArgs["cateUrl"] = cateUrl
- c.RenderArgs["postUrl"] = postUrl
- c.RenderArgs["searchUrl"] = searchUrl
- c.RenderArgs["singleUrl"] = singleUrl // 单页
- c.RenderArgs["archiveUrl"] = archiveUrl
- c.RenderArgs["archivesUrl"] = archiveUrl // 别名
- c.RenderArgs["tagsUrl"] = tagsUrl
- c.RenderArgs["tagPostsUrl"] = tagPostsUrl
- c.RenderArgs["tagUrl"] = c.RenderArgs["tagPostsUrl"] // 别名
+ c.RenderArgs["indexUrl"] = blogUrls.IndexUrl
+ c.RenderArgs["cateUrl"] = blogUrls.CateUrl
+ c.RenderArgs["postUrl"] = blogUrls.PostUrl
+ c.RenderArgs["searchUrl"] = blogUrls.SearchUrl
+ c.RenderArgs["singleUrl"] = blogUrls.SingleUrl // 单页
+ c.RenderArgs["archiveUrl"] = blogUrls.ArchiveUrl
+ c.RenderArgs["archivesUrl"] = blogUrls.ArchiveUrl // 别名
+ c.RenderArgs["tagsUrl"] = blogUrls.TagsUrl
+ c.RenderArgs["tagPostsUrl"] = blogUrls.TagPostsUrl
+ c.RenderArgs["tagUrl"] = blogUrls.TagPostsUrl // 别名
// themeBaseUrl 本theme的路径url, 可以加载js, css, images之类的
c.RenderArgs["themeBaseUrl"] = "/" + userBlog.ThemePath
// 其它static js
c.RenderArgs["jQueryUrl"] = siteUrl + "/js/jquery-1.9.0.min.js"
-
+
c.RenderArgs["prettifyJsUrl"] = siteUrl + "/js/google-code-prettify/prettify.js"
c.RenderArgs["prettifyCssUrl"] = siteUrl + "/js/google-code-prettify/prettify.css"
-
+
c.RenderArgs["blogCommonJsUrl"] = siteUrl + "/public/blog/js/common.js"
-
+
c.RenderArgs["shareCommentCssUrl"] = siteUrl + "/public/blog/css/share_comment.css"
c.RenderArgs["shareCommentJsUrl"] = siteUrl + "/public/blog/js/share_comment.js"
-
+
c.RenderArgs["fontAwesomeUrl"] = staticUrl + "/css/font-awesome-4.2.0/css/font-awesome.css"
-
+
c.RenderArgs["bootstrapCssUrl"] = siteUrl + "/css/bootstrap.css"
c.RenderArgs["bootstrapJsUrl"] = siteUrl + "/js/bootstrap-min.js"
}
// 笔记本分类
// cates = [{title:"xxx", cateId: "xxxx"}, {}]
+func (c Blog) getCateUrlTitle(n *info.Notebook) string {
+ if n.UrlTitle != "" {
+ return n.UrlTitle
+ }
+ return n.NotebookId.Hex()
+}
func (c Blog) getCates(userBlog info.UserBlog) {
notebooks := blogService.ListBlogNotebooks(userBlog.UserId.Hex())
notebooksMap := map[string]info.Notebook{}
@@ -236,7 +201,7 @@ func (c Blog) getCates(userBlog info.UserBlog) {
if cateIds != nil && len(cateIds) > 0 {
for _, cateId := range cateIds {
if n, ok := notebooksMap[cateId]; ok {
- cates[i] = map[string]string{"Title": n.Title, "CateId": n.NotebookId.Hex()}
+ cates[i] = map[string]string{"Title": n.Title, "UrlTitle": c.getCateUrlTitle(&n), "CateId": n.NotebookId.Hex()}
i++
has[cateId] = true
}
@@ -246,7 +211,7 @@ func (c Blog) getCates(userBlog info.UserBlog) {
for _, n := range notebooks {
id := n.NotebookId.Hex()
if !has[id] {
- cates[i] = map[string]string{"Title": n.Title, "CateId": id}
+ cates[i] = map[string]string{"Title": n.Title, "UrlTitle": c.getCateUrlTitle(&n), "CateId": id}
i++
}
}
@@ -279,39 +244,15 @@ func (c Blog) setBlog(userBlog info.UserBlog, userInfo info.User) {
"Logo": userBlog.Logo,
"OpenComment": userBlog.CanComment,
"CommentType": userBlog.CommentType, // leanote, or disqus
- "DisqusId": userBlog.DisqusId,
+ "DisqusId": userBlog.DisqusId,
"ThemeId": userBlog.ThemeId,
"SubDomain": userBlog.SubDomain,
"Domain": userBlog.Domain,
}
c.RenderArgs["blogInfo"] = blogInfo
- /*
- blogInfo := info.BlogInfoCustom{
- UserId: userBlog.UserId.Hex(),
- Username: userInfo.Username,
- UserLogo: userInfo.Logo,
- Title: userBlog.Title,
- SubTitle: userBlog.SubTitle,
- Logo: userBlog.Logo,
- OpenComment: userBlog.CanComment,
- CommentType: userBlog.CommentType, // leanote, or disqus
- ThemeId: userBlog.ThemeId,
- SubDomain: userBlog.SubDomain,
- Domain: userBlog.Domain,
- }
- */
}
func (c Blog) setPaging(pageInfo info.Page) {
- /*
- // 这些分类信息用一个变量
- paging := map[string]interface{}{ // 分页信息
- "CurPage": pageInfo.CurPage, // 当前页
- "TotalPage": pageInfo.TotalPage, // 总页数
- "PerPageSize": pageInfo.PerPageSize, // 每一页的记录数
- "Count": pageInfo.Count, // 总记录数
- }
- */
c.RenderArgs["paging"] = pageInfo
}
@@ -323,13 +264,13 @@ func (c Blog) blogCommon(userId string, userBlog info.UserBlog, userInfo info.Us
return false, userBlog
}
}
-// c.RenderArgs["userInfo"] = userInfo
+ // c.RenderArgs["userInfo"] = userInfo
// 最新笔记
_, recentBlogs := blogService.ListBlogs(userId, "", 1, 5, userBlog.SortField, userBlog.IsAsc)
- c.RenderArgs["recentPosts"] = c.fixBlogs(recentBlogs)
- c.RenderArgs["latestPosts"] = c.RenderArgs["recentPosts"]
- c.RenderArgs["tags"] = blogService.GetBlogTags(userId)
+ c.RenderArgs["recentPosts"] = blogService.FixBlogs(recentBlogs)
+ c.RenderArgs["latestPosts"] = c.RenderArgs["recentPosts"]
+ c.RenderArgs["tags"] = blogService.GetBlogTags(userId)
// 语言, url地址
c.SetLocale()
@@ -339,7 +280,7 @@ func (c Blog) blogCommon(userId string, userBlog info.UserBlog, userInfo info.Us
userBlog = blogService.GetUserBlog(userId)
}
c.setBlog(userBlog, userInfo)
-// c.RenderArgs["userBlog"] = userBlog
+ // c.RenderArgs["userBlog"] = userBlog
// 分类导航
c.getCates(userBlog)
@@ -352,42 +293,14 @@ func (c Blog) blogCommon(userId string, userBlog info.UserBlog, userInfo info.Us
// 当前分类Id, 全设为""
c.RenderArgs["curCateId"] = ""
c.RenderArgs["curSingleId"] = ""
-
+
// 得到主题信息
themeInfo := themeService.GetThemeInfo(userBlog.ThemeId.Hex(), userBlog.Style)
- LogJ(themeInfo)
c.RenderArgs["themeInfo"] = themeInfo
return true, userBlog
}
-// 修复博客, index, cate用到
-func (c Blog) fixBlog(blog info.BlogItem) info.Post {
- blog2 := info.Post{
- NoteId: blog.NoteId.Hex(),
- Title: blog.Title,
- CreatedTime: blog.CreatedTime,
- UpdatedTime: blog.UpdatedTime,
- PublicTime: blog.PublicTime,
- Desc: blog.Desc,
- Abstract: blog.Abstract,
- Content: blog.Content,
- Tags: blog.Tags,
- CommentNum: blog.CommentNum,
- ReadNum: blog.ReadNum,
- LikeNum: blog.LikeNum,
- IsMarkdown: blog.IsMarkdown,
- }
- return blog2
-}
-func (c Blog) fixBlogs(blogs []info.BlogItem) []info.Post {
- blogs2 := make([]info.Post, len(blogs))
- for i, blog := range blogs {
- blogs2[i] = c.fixBlog(blog)
- }
- return blogs2
-}
-
// 404
func (c Blog) E(userIdOrEmail, tag string) revel.Result {
ok, userBlog := c.domain()
@@ -403,8 +316,8 @@ func (c Blog) E(userIdOrEmail, tag string) revel.Result {
userInfo = userService.GetUserInfoByAny(userIdOrEmail)
}
userId = userInfo.UserId.Hex()
- _, userBlog = c.blogCommon(userId, userBlog, userInfo);
-
+ _, userBlog = c.blogCommon(userId, userBlog, userInfo)
+
return c.e404(userBlog.ThemePath)
}
@@ -413,10 +326,10 @@ func (c Blog) Tags(userIdOrEmail string) (re revel.Result) {
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
-
+
userId := ""
if hasDomain {
userId = userBlog.UserId.Hex()
@@ -448,10 +361,10 @@ func (c Blog) Tag(userIdOrEmail, tag string) (re revel.Result) {
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
-
+
userId := ""
if hasDomain {
userId = userBlog.UserId.Hex()
@@ -465,23 +378,23 @@ func (c Blog) Tag(userIdOrEmail, tag string) (re revel.Result) {
userInfo = userService.GetUserInfoByAny(userIdOrEmail)
}
userId = userInfo.UserId.Hex()
-
+
var ok = false
if ok, userBlog = c.blogCommon(userId, userBlog, userInfo); !ok {
return c.e404(userBlog.ThemePath) // 404 TODO 使用用户的404
}
-
+
if hasDomain && tag == "" {
tag = userIdOrEmail
}
-
+
c.RenderArgs["curIsTagPosts"] = true
c.RenderArgs["curTag"] = tag
page := c.GetPage()
pageInfo, blogs := blogService.SearchBlogByTags([]string{tag}, userId, page, userBlog.PerPageSize, userBlog.SortField, userBlog.IsAsc)
c.setPaging(pageInfo)
- c.RenderArgs["posts"] = c.fixBlogs(blogs)
+ c.RenderArgs["posts"] = blogService.FixBlogs(blogs)
tagPostsUrl := c.RenderArgs["tagPostsUrl"].(string)
c.RenderArgs["pagingBaseUrl"] = tagPostsUrl + "/" + tag
@@ -496,7 +409,7 @@ func (c Blog) Archives(userIdOrEmail string, cateId string, year, month int) (re
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
userId := ""
@@ -532,59 +445,52 @@ func (c Blog) Archives(userIdOrEmail string, cateId string, year, month int) (re
}
c.RenderArgs["curYear"] = year
c.RenderArgs["curMonth"] = month
-
return c.render("archive.html", userBlog.ThemePath)
}
// 进入某个用户的博客
-// TODO 可以配置, 排序和数目
var blogPageSize = 5
var searchBlogPageSize = 30
-
// 分类 /cate/xxxxxxxx?notebookId=1212
-func (c Blog) Cate(notebookId string) (re revel.Result) {
+func (c Blog) Cate(userIdOrEmail string, notebookId string) (re revel.Result) {
// 自定义域名
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
- re = c.e404(userBlog.ThemePath);
+ fmt.Println(err)
+ re = c.e404(userBlog.ThemePath)
}
}()
-
- if notebookId == "" {
- return c.e404(userBlog.ThemePath);
- }
- userId := ""
- if hasDomain {
- userId = userBlog.UserId.Hex()
- }
+ userId, userInfo := c.userIdOrEmail(hasDomain, userBlog, userIdOrEmail)
+ notebookId2 := notebookId
var notebook info.Notebook
- notebook = notebookService.GetNotebookById(notebookId)
+ if userId == "" { // 证明没有userIdOrEmail, 只有singleId, 那么直接查
+ notebook = notebookService.GetNotebookById(notebookId)
+ userId = notebook.UserId.Hex()
+ } else {
+ notebook = notebookService.GetNotebookByUserIdAndUrlTitle(userId, notebookId)
+ notebookId2 = notebook.NotebookId.Hex()
+ }
+ var ok = false
+ if ok, userBlog = c.blogCommon(userId, userBlog, userInfo); !ok {
+ return c.e404(userBlog.ThemePath) // 404 TODO 使用用户的404
+ }
if !notebook.IsBlog {
return c.e404("")
}
- if userId != "" && userId != notebook.UserId.Hex() {
- return c.e404("")
- }
- userId = notebook.UserId.Hex()
-
- var ok = false
- if ok, userBlog = c.blogCommon(userId, userBlog, info.User{}); !ok {
- return c.e404(userBlog.ThemePath)
- }
// 分页的话, 需要分页信息, totalPage, curPage
page := c.GetPage()
- pageInfo, blogs := blogService.ListBlogs(userId, notebookId, page, userBlog.PerPageSize, userBlog.SortField, userBlog.IsAsc)
- blogs2 := c.fixBlogs(blogs)
+ pageInfo, blogs := blogService.ListBlogs(userId, notebookId2, page, userBlog.PerPageSize, userBlog.SortField, userBlog.IsAsc)
+ blogs2 := blogService.FixBlogs(blogs)
c.RenderArgs["posts"] = blogs2
c.setPaging(pageInfo)
c.RenderArgs["curCateTitle"] = notebook.Title
- c.RenderArgs["curCateId"] = notebookId
+ c.RenderArgs["curCateId"] = notebookId2
cateUrl := c.RenderArgs["cateUrl"].(string)
c.RenderArgs["pagingBaseUrl"] = cateUrl + "/" + notebookId
c.RenderArgs["curIsCate"] = true
@@ -592,31 +498,37 @@ func (c Blog) Cate(notebookId string) (re revel.Result) {
return c.render("cate.html", userBlog.ThemePath)
}
+func (c Blog) userIdOrEmail(hasDomain bool, userBlog info.UserBlog, userIdOrEmail string) (userId string, userInfo info.User) {
+ userId = ""
+ if hasDomain {
+ userId = userBlog.UserId.Hex()
+ }
+ if userId != "" {
+ userInfo = userService.GetUserInfoByAny(userId)
+ } else {
+ if userIdOrEmail != "" {
+ userInfo = userService.GetUserInfoByAny(userIdOrEmail)
+ } else {
+ return
+ }
+ }
+ userId = userInfo.UserId.Hex()
+ return
+}
+
func (c Blog) Index(userIdOrEmail string) (re revel.Result) {
// 自定义域名
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
- userId := ""
- if hasDomain {
- userId = userBlog.UserId.Hex()
- }
-
- // 用户id为空, 转至博客平台
+ // 用户id为空, 则是admin用户的博客
if userIdOrEmail == "" {
userIdOrEmail = configService.GetAdminUsername()
}
- var userInfo info.User
- if userId != "" {
- userInfo = userService.GetUserInfoByAny(userId)
- } else {
- userInfo = userService.GetUserInfoByAny(userIdOrEmail)
- }
- userId = userInfo.UserId.Hex()
-
+ userId, userInfo := c.userIdOrEmail(hasDomain, userBlog, userIdOrEmail)
var ok = false
if ok, userBlog = c.blogCommon(userId, userBlog, userInfo); !ok {
return c.e404(userBlog.ThemePath) // 404 TODO 使用用户的404
@@ -625,7 +537,7 @@ func (c Blog) Index(userIdOrEmail string) (re revel.Result) {
// 分页的话, 需要分页信息, totalPage, curPage
page := c.GetPage()
pageInfo, blogs := blogService.ListBlogs(userId, "", page, userBlog.PerPageSize, userBlog.SortField, userBlog.IsAsc)
- blogs2 := c.fixBlogs(blogs)
+ blogs2 := blogService.FixBlogs(blogs)
c.RenderArgs["posts"] = blogs2
c.setPaging(pageInfo)
@@ -636,66 +548,36 @@ func (c Blog) Index(userIdOrEmail string) (re revel.Result) {
return c.render("index.html", userBlog.ThemePath)
}
-// 详情
-// 为了上一篇, 下一篇
-func (c Blog) fixNote(note info.Note) map[string]interface{} {
- if note.NoteId == "" {
- return nil
- }
- return map[string]interface{}{
- "NoteId": note.NoteId.Hex(),
- "Title": note.Title,
- "CreatedTime": note.CreatedTime,
- "UpdatedTime": note.UpdatedTime,
- "PublicTime": note.PublicTime,
- "Desc": note.Desc,
- "Tags": note.Tags,
- "CommentNum": note.CommentNum,
- "ReadNum": note.ReadNum,
- "LikeNum": note.LikeNum,
- "IsMarkdown": note.IsMarkdown,
- }
-}
-
-func (c Blog) Post(noteId string) (re revel.Result) {
+func (c Blog) Post(userIdOrEmail, noteId string) (re revel.Result) {
// 自定义域名
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
Log(err)
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
- userId := ""
- if hasDomain {
- userId = userBlog.UserId.Hex()
+
+ userId, userInfo := c.userIdOrEmail(hasDomain, userBlog, userIdOrEmail)
+ var blogInfo info.BlogItem
+ if userId == "" { // 证明没有userIdOrEmail, 只有singleId, 那么直接查
+ blogInfo = blogService.GetBlog(noteId)
+ userId = blogInfo.UserId.Hex()
+ } else {
+ blogInfo = blogService.GetBlogByIdAndUrlTitle(userId, noteId)
+ }
+ var ok = false
+ if ok, userBlog = c.blogCommon(userId, userBlog, userInfo); !ok {
+ return c.e404(userBlog.ThemePath) // 404 TODO 使用用户的404
+ }
+ if blogInfo.NoteId == "" {
+ return c.e404(userBlog.ThemePath) // 404 TODO 使用用户的404
}
- blogInfo := blogService.GetBlog(noteId)
- userInfo := userService.GetUserInfo(blogInfo.UserId.Hex())
- if userId != "" && userInfo.UserId.Hex() != userId {
- panic("error")
- }
-
- userId = userInfo.UserId.Hex()
- _, userBlog = c.blogCommon(userId, userBlog, info.User{})
-
- c.RenderArgs["post"] = c.fixBlog(blogInfo)
+ c.RenderArgs["post"] = blogService.FixBlog(blogInfo)
// c.RenderArgs["userInfo"] = userInfo
c.RenderArgs["curIsPost"] = true
- // 得到访问者id
- /*
- visitUserId := c.GetUserId()
- if visitUserId != "" {
- visitUserInfo := userService.GetUserInfo(visitUserId)
- c.RenderArgs["visitUserInfoJson"] = c.Json(visitUserInfo)
- c.RenderArgs["visitUserInfo"] = visitUserInfo
- } else {
- c.RenderArgs["visitUserInfoJson"] = "{}"
- }
- */
-
// 上一篇, 下一篇
var baseTime interface{}
if userBlog.SortField == "PublicTime" {
@@ -708,44 +590,46 @@ func (c Blog) Post(noteId string) (re revel.Result) {
baseTime = blogInfo.Title
}
- preNote, nextNote := blogService.PreNextBlog(userId, userBlog.SortField, userBlog.IsAsc, baseTime)
- c.RenderArgs["prePost"] = c.fixNote(preNote)
- c.RenderArgs["nextPost"] = c.fixNote(nextNote)
- Log("---------")
+ prePost, nextPost := blogService.PreNextBlog(userId, userBlog.SortField, userBlog.IsAsc, baseTime)
+ c.RenderArgs["prePost"] = prePost
+ c.RenderArgs["nextPost"] = nextPost
return c.render("post.html", userBlog.ThemePath)
}
-func (c Blog) Single(singleId string) (re revel.Result) {
+func (c Blog) Single(userIdOrEmail, singleId string) (re revel.Result) {
// 自定义域名
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
-
- userId := ""
- if hasDomain {
- userId = userBlog.UserId.Hex()
- }
- single := blogService.GetSingle(singleId)
- userInfo := userService.GetUserInfo(single.UserId.Hex())
- if userId != "" && userInfo.UserId.Hex() != userId {
- return c.e404(userBlog.ThemePath)
+ userId, userInfo := c.userIdOrEmail(hasDomain, userBlog, userIdOrEmail)
+ var single info.BlogSingle
+ if userId == "" { // 证明没有userIdOrEmail, 只有singleId, 那么直接查
+ single = blogService.GetSingle(singleId)
+ userId = single.UserId.Hex()
+ } else {
+ single = blogService.GetSingleByUserIdAndUrlTitle(userId, singleId)
+ }
+ var ok = false
+ if ok, userBlog = c.blogCommon(userId, userBlog, userInfo); !ok {
+ return c.e404(userBlog.ThemePath) // 404 TODO 使用用户的404
+ }
+ if single.SingleId == "" {
+ panic("")
}
-
- userId = userInfo.UserId.Hex()
- _, userBlog = c.blogCommon(userId, userBlog, info.User{})
c.RenderArgs["single"] = map[string]interface{}{
"SingleId": single.SingleId.Hex(),
"Title": single.Title,
+ "UrlTitle": single.UrlTitle,
"Content": single.Content,
"CreatedTime": single.CreatedTime,
"UpdatedTime": single.UpdatedTime,
}
- c.RenderArgs["curSingleId"] = singleId
+ c.RenderArgs["curSingleId"] = single.SingleId.Hex()
c.RenderArgs["curIsSingle"] = true
return c.render("single.html", userBlog.ThemePath)
@@ -757,7 +641,7 @@ func (c Blog) Search(userIdOrEmail, keywords string) (re revel.Result) {
hasDomain, userBlog := c.domain()
defer func() {
if err := recover(); err != nil {
- re = c.e404(userBlog.ThemePath);
+ re = c.e404(userBlog.ThemePath)
}
}()
userId := ""
@@ -771,7 +655,7 @@ func (c Blog) Search(userIdOrEmail, keywords string) (re revel.Result) {
} else {
userInfo = userService.GetUserInfoByAny(userIdOrEmail)
}
-// c.RenderArgs["userInfo"] = userInfo
+ // c.RenderArgs["userInfo"] = userInfo
userId = userInfo.UserId.Hex()
var ok = false
if ok, userBlog = c.blogCommon(userId, userBlog, userInfo); !ok {
@@ -782,7 +666,7 @@ func (c Blog) Search(userIdOrEmail, keywords string) (re revel.Result) {
pageInfo, blogs := blogService.SearchBlog(keywords, userId, page, userBlog.PerPageSize, userBlog.SortField, userBlog.IsAsc)
c.setPaging(pageInfo)
- c.RenderArgs["posts"] = c.fixBlogs(blogs)
+ c.RenderArgs["posts"] = blogService.FixBlogs(blogs)
c.RenderArgs["keywords"] = keywords
searchUrl, _ := c.RenderArgs["searchUrl"].(string)
c.RenderArgs["pagingBaseUrl"] = searchUrl + "?keywords=" + keywords
@@ -808,7 +692,7 @@ func (c Blog) GetPostStat(noteId string) revel.Result {
re.Ok = true
statInfo := blogService.GetBlogStat(noteId)
re.Item = statInfo
- return c.RenderJson(re);
+ return c.RenderJson(re)
}
// jsonP
@@ -819,7 +703,7 @@ func (c Blog) GetLikes(noteId string, callback string) revel.Result {
userId := c.GetUserId()
result := map[string]interface{}{}
isILikeIt := false
- if userId != "" {
+ if userId != "" {
isILikeIt = blogService.IsILikeIt(noteId, userId)
result["visitUserInfo"] = userService.GetUserAndBlog(userId)
}
@@ -841,11 +725,11 @@ func (c Blog) GetLikesAndComments(noteId, callback string) revel.Result {
// 我也点过?
isILikeIt := false
- if userId != "" {
+ if userId != "" {
isILikeIt = blogService.IsILikeIt(noteId, userId)
result["visitUserInfo"] = userService.GetUserAndBlog(userId)
}
-
+
// 点赞用户列表
likedUsers, hasMoreLikedUser := blogService.ListLikedUsers(noteId, false)
// 评论
@@ -892,18 +776,21 @@ func (c Blog) GetComments(noteId string) revel.Result {
return c.RenderJson(re)
}
+
// jsonp
func (c Blog) DeleteComment(noteId, commentId string, callback string) revel.Result {
re := info.NewRe()
re.Ok = blogService.DeleteComment(noteId, commentId, c.GetUserId())
return c.RenderJsonP(callback, re)
}
+
// jsonp
func (c Blog) CommentPost(noteId, content, toCommentId string, callback string) revel.Result {
re := info.NewRe()
re.Ok, re.Item = blogService.Comment(noteId, toCommentId, c.GetUserId(), content)
return c.RenderJsonP(callback, re)
}
+
// jsonp
func (c Blog) LikeComment(commentId string, callback string) revel.Result {
re := info.NewRe()
diff --git a/app/controllers/IndexController.go b/app/controllers/IndexController.go
index b6a86a6..bc4b86d 100644
--- a/app/controllers/IndexController.go
+++ b/app/controllers/IndexController.go
@@ -14,6 +14,10 @@ type Index struct {
// leanote展示页, 没有登录的, 或已登录明确要进该页的
func (c Index) Index() revel.Result {
+ if configService.HomePageIsAdminsBlog(){
+ blog := Blog{c.BaseController}
+ return blog.Index(configService.GetAdminUsername());
+ }
c.SetUserInfo()
c.RenderArgs["title"] = "leanote"
c.RenderArgs["openRegister"] = configService.GlobalStringConfigs["openRegister"]
diff --git a/app/controllers/PreviewController.go b/app/controllers/PreviewController.go
index c049f56..03cfca9 100644
--- a/app/controllers/PreviewController.go
+++ b/app/controllers/PreviewController.go
@@ -71,25 +71,25 @@ func (c Preview) Archives(userIdOrEmail string, notebookId string, year, month i
return c.Blog.Archives(c.GetUserId(), notebookId, year, month)
// return blog.RenderTemplate("archive.html", c.RenderArgs, c.getPreviewThemeAbsolutePath(""))
}
-func (c Preview) Cate(notebookId string) revel.Result {
+func (c Preview) Cate(userIdOrEmail, notebookId string) revel.Result {
if !c.getPreviewThemeAbsolutePath("") {
return c.E404()
}
- return c.Blog.Cate(notebookId)
+ return c.Blog.Cate(userIdOrEmail, notebookId)
// return blog.RenderTemplate("cate.html", c.RenderArgs, c.getPreviewThemeAbsolutePath(""))
}
-func (c Preview) Post(noteId string) revel.Result {
+func (c Preview) Post(userIdOrEmail, noteId string) revel.Result {
if !c.getPreviewThemeAbsolutePath("") {
return c.E404()
}
- return c.Blog.Post(noteId)
+ return c.Blog.Post(userIdOrEmail, noteId)
// return blog.RenderTemplate("view.html", c.RenderArgs, c.getPreviewThemeAbsolutePath(""))
}
-func (c Preview) Single(singleId string) revel.Result {
+func (c Preview) Single(userIdOrEmail, singleId string) revel.Result {
if !c.getPreviewThemeAbsolutePath("") {
return c.E404()
}
- return c.Blog.Single(singleId)
+ return c.Blog.Single(userIdOrEmail, singleId)
// return blog.RenderTemplate("single.html", c.RenderArgs, c.getPreviewThemeAbsolutePath(""))
}
func (c Preview) Search(userIdOrEmail, keywords string) revel.Result {
diff --git a/app/controllers/ShareController.go b/app/controllers/ShareController.go
index 15dc17c..9630b0b 100644
--- a/app/controllers/ShareController.go
+++ b/app/controllers/ShareController.go
@@ -4,7 +4,7 @@ import (
"github.com/revel/revel"
// "encoding/json"
// "gopkg.in/mgo.v2/bson"
-// . "github.com/leanote/leanote/app/lea"
+ . "github.com/leanote/leanote/app/lea"
"github.com/leanote/leanote/app/info"
// "github.com/leanote/leanote/app/types"
// "io/ioutil"
@@ -87,6 +87,8 @@ func (c Share) ListNoteShareUserInfo(noteId string) revel.Result {
noteShareUserInfos := shareService.ListNoteShareUserInfo(noteId, c.GetUserId())
c.RenderArgs["noteOrNotebookShareUserInfos"] = noteShareUserInfos
+ c.RenderArgs["noteOrNotebookShareGroupInfos"] = shareService.GetNoteShareGroups(noteId, c.GetUserId())
+
c.RenderArgs["isNote"] = true
c.RenderArgs["noteOrNotebookId"] = note.NoteId.Hex();
c.RenderArgs["title"] = note.Title
@@ -99,6 +101,9 @@ func (c Share) ListNotebookShareUserInfo(notebookId string) revel.Result {
notebookShareUserInfos := shareService.ListNotebookShareUserInfo(notebookId, c.GetUserId())
c.RenderArgs["noteOrNotebookShareUserInfos"] = notebookShareUserInfos
+ c.RenderArgs["noteOrNotebookShareGroupInfos"] = shareService.GetNotebookShareGroups(notebookId, c.GetUserId())
+ LogJ(c.RenderArgs["noteOrNotebookShareGroupInfos"])
+
c.RenderArgs["isNote"] = false
c.RenderArgs["noteOrNotebookId"] = notebook.NotebookId.Hex();
c.RenderArgs["title"] = notebook.Title
@@ -139,4 +144,45 @@ func (c Share) DeleteShareNotebookBySharedUser(notebookId string, fromUserId str
// 删除fromUserId分享给我的所有note, notebook
func (c Share) DeleteUserShareNoteAndNotebook(fromUserId string) revel.Result {
return c.RenderJson(shareService.DeleteUserShareNoteAndNotebook(fromUserId, c.GetUserId()));
+}
+
+//-------------
+// 用户组
+
+
+// 将笔记分享给分组
+func (c Share) AddShareNoteGroup(noteId, groupId string, perm int) revel.Result {
+ re := info.NewRe()
+ re.Ok = shareService.AddShareNoteGroup(c.GetUserId(), noteId, groupId, perm);
+ return c.RenderJson(re);
+}
+// 删除
+func (c Share) DeleteShareNoteGroup(noteId, groupId string) revel.Result {
+ re := info.NewRe()
+ re.Ok = shareService.DeleteShareNoteGroup(c.GetUserId(), noteId, groupId);
+ return c.RenderJson(re);
+}
+// 更新, 也是一样, 先删后加
+func (c Share) UpdateShareNoteGroupPerm(noteId, groupId string, perm int) revel.Result {
+ return c.AddShareNoteGroup(noteId, groupId, perm)
+}
+
+
+//------
+
+// 将笔记分享给分组
+func (c Share) AddShareNotebookGroup(notebookId, groupId string, perm int) revel.Result {
+ re := info.NewRe()
+ re.Ok = shareService.AddShareNotebookGroup(c.GetUserId(), notebookId, groupId, perm);
+ return c.RenderJson(re);
+}
+// 删除
+func (c Share) DeleteShareNotebookGroup(notebookId, groupId string) revel.Result {
+ re := info.NewRe()
+ re.Ok = shareService.DeleteShareNotebookGroup(c.GetUserId(), notebookId, groupId);
+ return c.RenderJson(re);
+}
+// 更新, 也是一样, 先删后加
+func (c Share) UpdateShareNotebookGroupPerm(notebookId, groupId string, perm int) revel.Result {
+ return c.AddShareNotebookGroup(notebookId, groupId, perm)
}
\ No newline at end of file
diff --git a/app/controllers/admin/AdminBlogController.go b/app/controllers/admin/AdminBlogController.go
index 9843938..ca8a48d 100644
--- a/app/controllers/admin/AdminBlogController.go
+++ b/app/controllers/admin/AdminBlogController.go
@@ -16,7 +16,7 @@ type AdminBlog struct {
func (c AdminBlog) Index(sorter, keywords string) revel.Result {
pageNumber := c.GetPage()
sorterField, isAsc := c.getSorter("CreatedTime", false, []string{"title", "userId", "isRecommed", "createdTime"});
- pageInfo, blogs := blogService.ListAllBlogs("", keywords, false, pageNumber, userPageSize, sorterField, isAsc);
+ pageInfo, blogs := blogService.ListAllBlogs("", "", keywords, false, pageNumber, userPageSize, sorterField, isAsc);
c.RenderArgs["pageInfo"] = pageInfo
c.RenderArgs["blogs"] = blogs
c.RenderArgs["keywords"] = keywords
@@ -27,4 +27,4 @@ func (c AdminBlog) SetRecommend(noteId string, recommend bool) revel.Result {
re := info.NewRe()
re.Ok = blogService.SetRecommend(noteId, recommend);
return c.RenderJson(re)
-}
\ No newline at end of file
+}
diff --git a/app/controllers/admin/AdminController.go b/app/controllers/admin/AdminController.go
index dad63d1..c3215a5 100644
--- a/app/controllers/admin/AdminController.go
+++ b/app/controllers/admin/AdminController.go
@@ -30,6 +30,7 @@ func (c Admin) T(t string) revel.Result {
c.RenderArgs["arr"] = configService.GlobalArrayConfigs
c.RenderArgs["map"] = configService.GlobalMapConfigs
c.RenderArgs["arrMap"] = configService.GlobalArrMapConfigs
+ c.RenderArgs["version"] = configService.GetVersion()
return c.RenderTemplate("admin/" + t + ".html")
}
diff --git a/app/controllers/admin/AdminSettingController.go b/app/controllers/admin/AdminSettingController.go
index f36e892..8a4ce51 100644
--- a/app/controllers/admin/AdminSettingController.go
+++ b/app/controllers/admin/AdminSettingController.go
@@ -111,6 +111,15 @@ func (c AdminSetting) OpenRegister(openRegister string) revel.Result {
return c.RenderJson(re)
}
+func (c AdminSetting) HomePage(homePage string) revel.Result {
+ re := info.NewRe()
+ if homePage == "0" {
+ homePage = ""
+ }
+ re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "homePage", homePage)
+ return c.RenderJson(re)
+}
+
func (c AdminSetting) Mongodb(mongodumpPath, mongorestorePath string) revel.Result {
re := info.NewRe()
re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "mongodumpPath", mongodumpPath)
diff --git a/app/controllers/admin/AdminUpgradeController.go b/app/controllers/admin/AdminUpgradeController.go
index 8580acf..3835d68 100644
--- a/app/controllers/admin/AdminUpgradeController.go
+++ b/app/controllers/admin/AdminUpgradeController.go
@@ -17,8 +17,8 @@ func (c AdminUpgrade) UpgradeBlog() revel.Result {
return nil;
}
-func (c AdminUpgrade) UpgradeBetaToSelfBlog() revel.Result {
+func (c AdminUpgrade) UpgradeBetaToBeta2() revel.Result {
re := info.NewRe()
- re.Ok, re.Msg = upgradeService.UpgradeBetaToSelfBlog(c.GetUserId())
+ re.Ok, re.Msg = upgradeService.UpgradeBetaToBeta2(c.GetUserId())
return c.RenderJson(re)
-}
+}
\ No newline at end of file
diff --git a/app/controllers/member/MemberBlogController.go b/app/controllers/member/MemberBlogController.go
index 9dca66c..daa75b5 100644
--- a/app/controllers/member/MemberBlogController.go
+++ b/app/controllers/member/MemberBlogController.go
@@ -8,10 +8,11 @@ import (
"io/ioutil"
"time"
"fmt"
+ "strings"
// "github.com/leanote/leanote/app/lea/blog"
)
-// admin 首页
+// 博客管理
type MemberBlog struct {
MemberBaseController
@@ -33,6 +34,82 @@ func (c MemberBlog) common() info.UserBlog {
return userBlog
}
+
+// 得到sorterField 和 isAsc
+// okSorter = ['email', 'username']
+func (c MemberBlog) getSorter(sorterField string, isAsc bool, okSorter []string) (string, bool){
+ sorter := ""
+ c.Params.Bind(&sorter, "sorter")
+ if sorter == "" {
+ return sorterField, isAsc;
+ }
+
+ // sorter形式 email-up, email-down
+ s2 := strings.Split(sorter, "-")
+ if len(s2) != 2 {
+ return sorterField, isAsc;
+ }
+
+ // 必须是可用的sorter
+ if okSorter != nil && len(okSorter) > 0 {
+ if !InArray(okSorter, s2[0]) {
+ return sorterField, isAsc;
+ }
+ }
+
+ sorterField = strings.Title(s2[0])
+ if s2[1] == "up" {
+ isAsc = true
+ } else {
+ isAsc = false
+ }
+ c.RenderArgs["sorter"] = sorter
+ return sorterField, isAsc;
+}
+
+// 博客列表
+var userPageSize = 15
+func (c MemberBlog) Index(sorter, keywords string) revel.Result {
+ c.RenderArgs["title"] = "Posts"
+ pageNumber := c.GetPage()
+ sorterField, isAsc := c.getSorter("CreatedTime", false, []string{"title", "urlTitle", "updatedTime", "publicTime", "createdTime"});
+ pageInfo, blogs := blogService.ListAllBlogs(c.GetUserId(), "", keywords, false, pageNumber, userPageSize, sorterField, isAsc);
+ c.RenderArgs["pageInfo"] = pageInfo
+ c.RenderArgs["blogs"] = blogs
+ c.RenderArgs["keywords"] = keywords
+
+ userAndBlog := userService.GetUserAndBlog(c.GetUserId())
+ c.RenderArgs["userAndBlog"] = userAndBlog
+
+ return c.RenderTemplate("member/blog/list.html");
+}
+
+// 修改笔记的urlTitle
+func (c MemberBlog) UpdateBlogUrlTitle(noteId, urlTitle string) revel.Result {
+ re := info.NewRe()
+ re.Ok, re.Item = blogService.UpateBlogUrlTitle(c.GetUserId(), noteId, urlTitle)
+ return c.RenderJson(re)
+}
+
+
+// 修改笔记的urlTitle
+func (c MemberBlog) UpdateBlogAbstract(noteId string) revel.Result {
+ c.RenderArgs["title"] = "Update Post Abstract"
+ note := noteService.GetNoteAndContent(noteId, c.GetUserId());
+ if !note.Note.IsBlog {
+ return c.E404();
+ }
+ c.RenderArgs["note"] = note
+ c.RenderArgs["noteId"] = noteId
+ return c.RenderTemplate("member/blog/update_abstract.html");
+}
+func (c MemberBlog) DoUpdateBlogAbstract(noteId, imgSrc, desc, abstract string) revel.Result {
+
+ re := info.NewRe()
+ re.Ok = blogService.UpateBlogAbstract(c.GetUserId(), noteId, imgSrc, desc, abstract)
+ return c.RenderJson(re)
+}
+
// 基本信息设置
func (c MemberBlog) Base() revel.Result {
c.common()
@@ -101,6 +178,12 @@ func (c MemberBlog) UpateCateIds(cateIds []string) revel.Result {
return c.RenderJson(re)
}
+func (c MemberBlog) UpdateCateUrlTitle(cateId, urlTitle string) revel.Result {
+ re := info.NewRe()
+ re.Ok, re.Item = blogService.UpateCateUrlTitle(c.GetUserId(), cateId, urlTitle)
+ return c.RenderJson(re)
+}
+
// 保存之, 包含增加与保存
func (c MemberBlog) DoAddOrUpdateSingle(singleId, title, content string) revel.Result {
re := info.NewRe()
@@ -127,6 +210,14 @@ func (c MemberBlog) DeleteSingle(singleId string) revel.Result {
re.Ok = blogService.DeleteSingle(c.GetUserId(), singleId)
return c.RenderJson(re)
}
+
+// 修改页面标题
+func (c MemberBlog) UpdateSingleUrlTitle(singleId, urlTitle string) revel.Result {
+ re := info.NewRe()
+ re.Ok, re.Item = blogService.UpdateSingleUrlTitle(c.GetUserId(), singleId, urlTitle)
+ return c.RenderJson(re)
+}
+
func (c MemberBlog) Single() revel.Result {
c.common()
c.RenderArgs["title"] = "Cate"
@@ -425,4 +516,4 @@ func (c MemberBlog) SetUserBlogPaging(perPageSize int, sortField string, isAsc b
re := info.NewRe()
re.Ok, re.Msg = blogService.UpdateUserBlogPaging(c.GetUserId(), perPageSize, sortField, isAsc)
return c.RenderRe(re)
-}
\ No newline at end of file
+}
diff --git a/app/controllers/member/MemberGroupController.go b/app/controllers/member/MemberGroupController.go
new file mode 100644
index 0000000..815857d
--- /dev/null
+++ b/app/controllers/member/MemberGroupController.go
@@ -0,0 +1,58 @@
+package member
+
+import (
+ "github.com/revel/revel"
+ "github.com/leanote/leanote/app/info"
+)
+
+// 分组管理
+type MemberGroup struct {
+ MemberBaseController
+}
+
+// 首页, 显示所有分组和用户
+func (c MemberGroup) Index() revel.Result {
+ c.SetUserInfo()
+ c.SetLocale()
+ c.RenderArgs["title"] = "My Group"
+ c.RenderArgs["groups"] = groupService.GetGroupsAndUsers(c.GetUserId())
+ return c.RenderTemplate("member/group/index.html");
+}
+
+// 添加分组
+func (c MemberGroup) AddGroup(title string) revel.Result {
+ re := info.NewRe()
+ re.Ok, re.Item = groupService.AddGroup(c.GetUserId(), title)
+ return c.RenderJson(re)
+}
+
+func (c MemberGroup) UpdateGroupTitle(groupId, title string) revel.Result {
+ re := info.NewRe()
+ re.Ok = groupService.UpdateGroupTitle(c.GetUserId(), groupId, title)
+ return c.RenderJson(re)
+}
+
+func (c MemberGroup) DeleteGroup(groupId string) revel.Result {
+ re := info.NewRe()
+ re.Ok, re.Msg = groupService.DeleteGroup(c.GetUserId(), groupId)
+ return c.RenderJson(re)
+}
+
+// 添加用户
+func (c MemberGroup) AddUser(groupId, email string) revel.Result {
+ re := info.NewRe()
+ userInfo := userService.GetUserInfoByAny(email)
+ if userInfo.UserId == "" {
+ re.Msg = "userNotExists"
+ } else {
+ re.Ok, re.Msg = groupService.AddUser(c.GetUserId(), groupId, userInfo.UserId.Hex())
+ re.Item = userInfo
+ }
+ return c.RenderRe(re)
+}
+
+func (c MemberGroup) DeleteUser(groupId, userId string) revel.Result {
+ re := info.NewRe()
+ re.Ok, re.Msg = groupService.DeleteUser(c.GetUserId(), groupId, userId)
+ return c.RenderRe(re)
+}
diff --git a/app/controllers/member/init.go b/app/controllers/member/init.go
index 2737265..d891db2 100644
--- a/app/controllers/member/init.go
+++ b/app/controllers/member/init.go
@@ -9,6 +9,7 @@ import (
)
var userService *service.UserService
+var groupService *service.GroupService
var noteService *service.NoteService
var trashService *service.TrashService
var notebookService *service.NotebookService
@@ -106,6 +107,7 @@ func InitService() {
trashService = service.TrashS
shareService = service.ShareS
userService = service.UserS
+ groupService = service.GroupS
tagService = service.TagS
blogService = service.BlogS
tokenService = service.TokenS
diff --git a/app/info/BlogCustom.go b/app/info/BlogCustom.go
index 0da02b8..c309c21 100644
--- a/app/info/BlogCustom.go
+++ b/app/info/BlogCustom.go
@@ -23,6 +23,8 @@ type BlogInfoCustom struct {
type Post struct {
NoteId string
Title string
+ UrlTitle string
+ ImgSrc string
CreatedTime time.Time
UpdatedTime time.Time
PublicTime time.Time
diff --git a/app/info/BlogInfo.go b/app/info/BlogInfo.go
index 68eaef3..24dafae 100644
--- a/app/info/BlogInfo.go
+++ b/app/info/BlogInfo.go
@@ -74,9 +74,10 @@ type BlogStat struct {
// 单页
type BlogSingle struct {
- SingleId bson.ObjectId `bson:"_id"`
+ SingleId bson.ObjectId `bson:"_id,omitempty"`
UserId bson.ObjectId `UserId`
Title string `Title`
+ UrlTitle string `UrlTitle` // 2014/11/11
Content string `Content`
UpdatedTime time.Time `UpdatedTime`
CreatedTime time.Time `CreatedTime`
@@ -87,7 +88,7 @@ type BlogSingle struct {
// 点赞记录
type BlogLike struct {
- LikeId bson.ObjectId `bson:"_id"`
+ LikeId bson.ObjectId `bson:"_id,omitempty"`
NoteId bson.ObjectId `NoteId`
UserId bson.ObjectId `UserId`
CreatedTime time.Time `CreatedTime`
@@ -95,7 +96,7 @@ type BlogLike struct {
// 评论
type BlogComment struct {
- CommentId bson.ObjectId `bson:"_id"`
+ CommentId bson.ObjectId `bson:"_id,omitempty"`
NoteId bson.ObjectId `NoteId`
UserId bson.ObjectId `UserId` // UserId回复ToUserId
@@ -114,3 +115,14 @@ type BlogCommentPublic struct {
BlogComment
IsILikeIt bool
}
+
+type BlogUrls struct {
+ IndexUrl string
+ CateUrl string
+ SearchUrl string
+ SingleUrl string
+ PostUrl string
+ ArchiveUrl string
+ TagsUrl string
+ TagPostsUrl string
+}
diff --git a/app/info/GroupInfo.go b/app/info/GroupInfo.go
new file mode 100644
index 0000000..e75b352
--- /dev/null
+++ b/app/info/GroupInfo.go
@@ -0,0 +1,25 @@
+package info
+
+import (
+ "gopkg.in/mgo.v2/bson"
+ "time"
+)
+
+// 分组
+type Group struct {
+ GroupId bson.ObjectId `bson:"_id"` // 谁的
+ UserId bson.ObjectId `UserId` // 所有者Id
+ Title string `Title` // 标题
+ UserCount int `UserCount` // 用户数
+ CreatedTime time.Time `CreatedTime`
+
+ Users []User `Users,omitempty` // 分组下的用户, 不保存, 仅查看
+}
+
+// 分组好友
+type GroupUser struct {
+ GroupUserId bson.ObjectId `bson:"_id"` // 谁的
+ GroupId bson.ObjectId `GroupId` // 分组
+ UserId bson.ObjectId `UserId` // 用户
+ CreatedTime time.Time `CreatedTime`
+}
diff --git a/app/info/NoteInfo.go b/app/info/NoteInfo.go
index e8560e0..a1bce49 100644
--- a/app/info/NoteInfo.go
+++ b/app/info/NoteInfo.go
@@ -20,9 +20,11 @@ type Note struct {
IsTrash bool `IsTrash` // 是否是trash, 默认是false
- IsBlog bool `IsBlog,omitempty` // 是否设置成了blog 2013/12/29 新加
- IsRecommend bool `IsRecommend,omitempty` // 是否为推荐博客 2014/9/24新加
- IsTop bool `IsTop,omitempty` // blog是否置顶
+ IsBlog bool `IsBlog,omitempty` // 是否设置成了blog 2013/12/29 新加
+ UrlTitle string `UrlTitle,omitempty` // 博客的url标题, 为了更友好的url, 在UserId, UrlName下唯一
+ IsRecommend bool `IsRecommend,omitempty` // 是否为推荐博客 2014/9/24新加
+ IsTop bool `IsTop,omitempty` // blog是否置顶
+ HasSelfDefined bool `HasSelfDefined` // 是否已经自定义博客图片, desc, abstract
// 2014/9/28 添加评论社交功能
ReadNum int `ReadNum,omitempty` // 阅读次数 2014/9/28
@@ -35,9 +37,9 @@ type Note struct {
CreatedTime time.Time `CreatedTime`
UpdatedTime time.Time `UpdatedTime`
- RecommendTime time.Time `RecommendTime,omitempty` // 推荐时间
- PublicTime time.Time `PublicTime,omitempty` // 发表时间, 公开为博客则设置
- UpdatedUserId bson.ObjectId `bson:"UpdatedUserId"` // 如果共享了, 并可写, 那么可能是其它他修改了
+ RecommendTime time.Time `RecommendTime,omitempty` // 推荐时间
+ PublicTime time.Time `PublicTime,omitempty` // 发表时间, 公开为博客则设置
+ UpdatedUserId bson.ObjectId `bson:"UpdatedUserId"` // 如果共享了, 并可写, 那么可能是其它他修改了
}
// 内容
diff --git a/app/info/NotebookInfo.go b/app/info/NotebookInfo.go
index 8275c9f..8d0db19 100644
--- a/app/info/NotebookInfo.go
+++ b/app/info/NotebookInfo.go
@@ -13,6 +13,7 @@ type Notebook struct {
ParentNotebookId bson.ObjectId `bson:"ParentNotebookId,omitempty"` // 上级
Seq int `Seq` // 排序
Title string `Title` // 标题
+ UrlTitle string `UrlTitle` // Url标题 2014/11.11加
NumberNotes int `NumberNotes` // 笔记数
IsTrash bool `IsTrash,omitempty` // 是否是trash, 默认是false
IsBlog bool `IsBlog,omitempty` // 是否是Blog 2013/12/29 新加
diff --git a/app/info/ShareNotebookNoteInfo.go b/app/info/ShareNotebookNoteInfo.go
index dfea67b..401b720 100644
--- a/app/info/ShareNotebookNoteInfo.go
+++ b/app/info/ShareNotebookNoteInfo.go
@@ -76,7 +76,9 @@ type SharingNotebookAndNotes struct {
type ShareNotebook struct {
ShareNotebookId bson.ObjectId `bson:"_id,omitempty"` // 必须要设置bson:"_id" 不然mgo不会认为是主键
UserId bson.ObjectId `bson:"UserId"`
- ToUserId bson.ObjectId `bson:"ToUserId"`
+ ToUserId bson.ObjectId `bson:"ToUserId,omitempty"`
+ ToGroupId bson.ObjectId `bson:"ToGroupId,omitempty"` // 分享给的用户组
+ ToGroup Group `ToGroup,omitempty` // 仅仅为了显示, 不存储, 分组信息
NotebookId bson.ObjectId `bson:"NotebookId"`
Seq int `bson:"Seq"` // 排序
Perm int `bson:"Perm"` // 权限, 其下所有notes 0只读, 1可修改
@@ -134,7 +136,9 @@ type ShareNotebooksByUser struct {
type ShareNote struct {
ShareNoteId bson.ObjectId `bson:"_id,omitempty"` // 必须要设置bson:"_id" 不然mgo不会认为是主键
UserId bson.ObjectId `bson:"UserId"`
- ToUserId bson.ObjectId `bson:"ToUserId"`
+ ToUserId bson.ObjectId `bson:"ToUserId,omitempty"`
+ ToGroupId bson.ObjectId `bson:"ToGroupId,omitempty"` // 分享给的用户组
+ ToGroup Group `ToGroup,omitempty` // 仅仅为了显示, 不存储, 分组信息
NoteId bson.ObjectId `bson:"NoteId"`
Perm int `bson:"Perm"` // 权限, 0只读, 1可修改
CreatedTime time.Time `CreatedTime`
diff --git a/app/info/UserInfo.go b/app/info/UserInfo.go
index 8a406d5..7d4e49d 100644
--- a/app/info/UserInfo.go
+++ b/app/info/UserInfo.go
@@ -74,5 +74,7 @@ type UserAndBlog struct {
Logo string `Logo`
BlogTitle string `BlogTitle` // 博客标题
BlogLogo string `BlogLogo` // 博客Logo
- BlogUrl string `BlogUrl` // 博客链接
+ BlogUrl string `BlogUrl` // 博客链接, 主页
+
+ BlogUrls // 各个页面
}
diff --git a/app/info/common.go b/app/info/common.go
index 3ea4521..25ef0e0 100644
--- a/app/info/common.go
+++ b/app/info/common.go
@@ -9,6 +9,7 @@ import (
type Page struct {
CurPage int // 当前页码
TotalPage int // 总页
+ PerPageSize int
Count int // 总记录数
List interface{}
}
@@ -18,5 +19,5 @@ func NewPage(page, perPageSize, count int, list interface{}) Page {
if count > 0 {
totalPage = int(math.Ceil(float64(count) / float64(perPageSize)))
}
- return Page{page, totalPage, count, list}
-}
\ No newline at end of file
+ return Page{page, totalPage, perPageSize, count, list}
+}
diff --git a/app/init.go b/app/init.go
index f491511..a0cb922 100644
--- a/app/init.go
+++ b/app/init.go
@@ -20,6 +20,7 @@ import (
"strconv"
"time"
"encoding/json"
+ "net/url"
)
func init() {
@@ -78,6 +79,10 @@ func init() {
}
return str
}
+ revel.TemplateFuncs["decodeUrlValue"] = func(i string) string {
+ v, _ := url.ParseQuery("a=" + i)
+ return v.Get("a")
+ }
revel.TemplateFuncs["json"] = func(i interface{}) string {
b, _ := json.Marshal(i)
return string(b)
@@ -318,4 +323,4 @@ func init() {
member.InitService()
service.ConfigS.InitGlobalConfigs()
})
-}
\ No newline at end of file
+}
diff --git a/app/service/AuthService.go b/app/service/AuthService.go
index 1d4e66a..6e4fd24 100644
--- a/app/service/AuthService.go
+++ b/app/service/AuthService.go
@@ -35,7 +35,7 @@ func (this *AuthService) Login(emailOrUsername, pwd string) info.User {
func (this *AuthService) Register(email, pwd string) (bool, string) {
// 用户是否已存在
if userService.IsExistsUser(email) {
- return false, email + " 已被注册"
+ return false, "userHasBeenRegistered-" + email
}
user := info.User{UserId: bson.NewObjectId(), Email: email, Username: email, Pwd: Md5(pwd)}
return this.register(user)
@@ -90,7 +90,10 @@ func (this *AuthService) register(user info.User) (bool, string) {
Title: user.Username + " 's Blog",
SubTitle: "love leanote!",
AboutMe: "Hello, I am (^_^)",
+ CanComment: true,
})
+ // 添加一个单页面
+ blogService.AddOrUpdateSingle(user.UserId.Hex(), "", "About Me", "Hello, I am (^_^)")
}
return true, ""
diff --git a/app/service/BlogService.go b/app/service/BlogService.go
index 33a2105..789a519 100644
--- a/app/service/BlogService.go
+++ b/app/service/BlogService.go
@@ -1,16 +1,15 @@
package service
import (
- "github.com/leanote/leanote/app/info"
"github.com/leanote/leanote/app/db"
+ "github.com/leanote/leanote/app/info"
. "github.com/leanote/leanote/app/lea"
-// "github.com/leanote/leanote/app/lea/netutil"
"gopkg.in/mgo.v2/bson"
-// "time"
-// "sort"
+ // "time"
+ // "sort"
+ "strconv"
"strings"
"time"
- "strconv"
)
// blog
@@ -39,20 +38,33 @@ func (this *BlogService) GetBlogStat(noteId string) (stat info.BlogStat) {
stat = info.BlogStat{note.NoteId, note.ReadNum, note.LikeNum, note.CommentNum}
return
}
+
+// 通过id或urlTitle得到博客
+func (this *BlogService) GetBlogByIdAndUrlTitle(userId string, noteIdOrUrlTitle string) (blog info.BlogItem) {
+ if IsObjectId(noteIdOrUrlTitle) {
+ return this.GetBlog(noteIdOrUrlTitle)
+ }
+ note := info.Note{}
+ db.GetByQ(db.Notes, bson.M{"UserId": bson.ObjectIdHex(userId), "UrlTitle": encodeValue(noteIdOrUrlTitle), "IsBlog": true, "IsTrash": false}, ¬e)
+ return this.GetBlogItem(note)
+}
+
// 得到某博客具体信息
func (this *BlogService) GetBlog(noteId string) (blog info.BlogItem) {
note := noteService.GetBlogNote(noteId)
-
+ return this.GetBlogItem(note)
+}
+func (this *BlogService) GetBlogItem(note info.Note) (blog info.BlogItem) {
if note.NoteId == "" || !note.IsBlog {
return
}
-
+
// 内容
noteContent := noteService.GetNoteContent(note.NoteId.Hex(), note.UserId.Hex())
-
+
// 组装成blogItem
- blog = info.BlogItem{note, noteContent.Abstract, noteContent.Content, false, info.User{}}
-
+ blog = info.BlogItem{note, noteContent.Abstract, noteContent.Content, false, info.User{}}
+
return
}
@@ -66,18 +78,18 @@ func (this *BlogService) ListBlogNotebooks(userId string) []info.Notebook {
// 博客列表
// userId 表示谁的blog
func (this *BlogService) ListBlogs(userId, notebookId string, page, pageSize int, sortField string, isAsc bool) (info.Page, []info.BlogItem) {
- count, notes := noteService.ListNotes(userId, notebookId, false, page, pageSize, sortField, isAsc, true);
-
- if(notes == nil || len(notes) == 0) {
+ count, notes := noteService.ListNotes(userId, notebookId, false, page, pageSize, sortField, isAsc, true)
+
+ if notes == nil || len(notes) == 0 {
return info.Page{}, nil
}
-
+
// 得到content, 并且每个都要substring
noteIds := make([]bson.ObjectId, len(notes))
for i, note := range notes {
noteIds[i] = note.NoteId
}
-
+
// 直接得到noteContents表的abstract
// 这里可能是乱序的
noteContents := noteService.ListNoteAbstractsByNoteIds(noteIds) // 返回[info.NoteContent]
@@ -85,7 +97,7 @@ func (this *BlogService) ListBlogs(userId, notebookId string, page, pageSize int
for _, noteContent := range noteContents {
noteContentsMap[noteContent.NoteId] = noteContent
}
-
+
// 组装成blogItem
// 按照notes的顺序
blogs := make([]info.BlogItem, len(noteIds))
@@ -99,9 +111,9 @@ func (this *BlogService) ListBlogs(userId, notebookId string, page, pageSize int
}
blogs[i] = info.BlogItem{note, abstract, content, hasMore, info.User{}}
}
-
+
pageInfo := info.NewPage(page, pageSize, count, nil)
-
+
return pageInfo, blogs
}
@@ -111,25 +123,26 @@ func (this *BlogService) ListBlogs(userId, notebookId string, page, pageSize int
{Tag:xxx, Count: 32}
]
*/
-func (this *BlogService) GetBlogTags(userId string) ([]info.TagCount) {
+func (this *BlogService) GetBlogTags(userId string) []info.TagCount {
// 得到所有博客
tagCounts := []info.TagCount{}
query := bson.M{"UserId": bson.ObjectIdHex(userId), "IsBlog": true}
db.TagCounts.Find(query).Sort("-Count").All(&tagCounts)
return tagCounts
}
+
// 重新计算博客的标签
-// 在设置设置/取消为博客时调用
-func (this *BlogService) ReCountBlogTags(userId string) (bool) {
+// 在设置设置/取消为博客时调用
+func (this *BlogService) ReCountBlogTags(userId string) bool {
// 得到所有博客
notes := []info.Note{}
userIdO := bson.ObjectIdHex(userId)
query := bson.M{"UserId": userIdO, "IsTrash": false, "IsBlog": true}
db.ListByQWithFields(db.Notes, query, []string{"Tags"}, ¬es)
-
- db.DeleteAll(db.TagCounts, bson.M{"UserId": userIdO, "IsBlog": true});
- if(notes == nil || len(notes) == 0) {
- return true
+
+ db.DeleteAll(db.TagCounts, bson.M{"UserId": userIdO, "IsBlog": true})
+ if notes == nil || len(notes) == 0 {
+ return true
}
// 统计所有的Tags和数目
tagsCount := map[string]int{}
@@ -145,11 +158,12 @@ func (this *BlogService) ReCountBlogTags(userId string) (bool) {
}
// 一个个插入
for tag, count := range tagsCount {
- db.Insert(db.TagCounts,
+ db.Insert(db.TagCounts,
info.TagCount{UserId: userIdO, IsBlog: true, Tag: tag, Count: count})
}
return true
}
+
// 归档博客
/*
数据: 按年汇总
@@ -157,14 +171,14 @@ func (this *BlogService) ReCountBlogTags(userId string) (bool) {
archive1,
archive2,
]
-archive的数据类型是
+archive的数据类型是
{
Year: 2014
Posts: []
}
*/
-func (this *BlogService) ListBlogsArchive(userId, notebookId string, year, month int, sortField string, isAsc bool) ([]info.Archive) {
-// _, notes := noteService.ListNotes(userId, notebookId, false, 1, 99999, sortField, isAsc, true);
+func (this *BlogService) ListBlogsArchive(userId, notebookId string, year, month int, sortField string, isAsc bool) []info.Archive {
+ // _, notes := noteService.ListNotes(userId, notebookId, false, 1, 99999, sortField, isAsc, true);
q := bson.M{"UserId": bson.ObjectIdHex(userId), "IsBlog": true, "IsTrash": false}
if notebookId != "" {
q["NotebookId"] = bson.ObjectIdHex(notebookId)
@@ -192,18 +206,18 @@ func (this *BlogService) ListBlogsArchive(userId, notebookId string, year, month
q["PublicTime"] = bson.M{"$gte": leftT, "$lt": rightT}
}
}
-
+
sorter := sortField
if !isAsc {
sorter = "-" + sortField
}
notes := []info.Note{}
db.Notes.Find(q).Sort(sorter).All(¬es)
-
- if(notes == nil || len(notes) == 0) {
+
+ if notes == nil || len(notes) == 0 {
return nil
}
-
+
arcs := []info.Archive{}
// 按年汇总
arcsMap := map[int]info.Archive{}
@@ -225,41 +239,31 @@ func (this *BlogService) ListBlogsArchive(userId, notebookId string, year, month
if everYear == 0 {
everYear = year
}
-
+
if everYear != year {
yearArc := arcsMap[everYear]
yearArc.MonthAchives = arcsMonth
arcs = append(arcs, yearArc)
everYear = year
-
+
// 新的一年
arcsMonth = []info.ArchiveMonth{}
}
-
+
if arcT, ok := arcsMap[year]; ok {
arc = arcT
} else {
arc = info.Archive{Year: year, Posts: []*info.Post{}}
}
- p := &info.Post{
- NoteId: note.NoteId.Hex(),
- Title: note.Title,
- CreatedTime: note.CreatedTime,
- UpdatedTime: note.UpdatedTime,
- PublicTime: note.PublicTime,
- Desc: note.Desc,
- Tags: note.Tags,
- CommentNum: note.CommentNum,
- ReadNum: note.ReadNum,
- LikeNum: note.LikeNum,
- IsMarkdown: note.IsMarkdown,
- }
- arc.Posts = append(arc.Posts, p);
+
+ pt := this.FixNote(note)
+ p := &pt
+ arc.Posts = append(arc.Posts, p)
arcsMap[year] = arc
-
+
// month
lm := len(arcsMonth)
- if(lm == 0 || arcsMonth[lm-1].Month != month) {
+ if lm == 0 || arcsMonth[lm-1].Month != month {
arcsMonth = append(arcsMonth, info.ArchiveMonth{month, []*info.Post{p}})
} else {
arcsMonth[lm-1].Posts = append(arcsMonth[lm-1].Posts, p)
@@ -271,7 +275,7 @@ func (this *BlogService) ListBlogsArchive(userId, notebookId string, year, month
yearArc.MonthAchives = arcsMonth
arcs = append(arcs, yearArc)
}
-
+
return arcs
}
@@ -279,39 +283,39 @@ func (this *BlogService) ListBlogsArchive(userId, notebookId string, year, month
func (this *BlogService) SearchBlogByTags(tags []string, userId string, pageNumber, pageSize int, sortField string, isAsc bool) (pageInfo info.Page, blogs []info.BlogItem) {
notes := []info.Note{}
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
-
+
// 不是trash的
- query := bson.M{"UserId": bson.ObjectIdHex(userId),
- "IsTrash": false,
- "IsBlog": true,
- "Tags": bson.M{"$all": tags}}
-
- q := db.Notes.Find(query);
-
+ query := bson.M{"UserId": bson.ObjectIdHex(userId),
+ "IsTrash": false,
+ "IsBlog": true,
+ "Tags": bson.M{"$all": tags}}
+
+ q := db.Notes.Find(query)
+
// 总记录数
count, _ := q.Count()
if count == 0 {
return
}
-
+
q.Sort(sortFieldR).
Skip(skipNum).
Limit(pageSize).
All(¬es)
-
+
blogs = this.notes2BlogItems(notes)
pageInfo = info.NewPage(pageNumber, pageSize, count, nil)
-
+
return
}
-func (this *BlogService) notes2BlogItems(notes []info.Note) []info.BlogItem{
+func (this *BlogService) notes2BlogItems(notes []info.Note) []info.BlogItem {
// 得到content, 并且每个都要substring
noteIds := make([]bson.ObjectId, len(notes))
for i, note := range notes {
noteIds[i] = note.NoteId
}
-
+
// 直接得到noteContents表的abstract
// 这里可能是乱序的
noteContents := noteService.ListNoteContentByNoteIds(noteIds) // 返回[info.NoteContent]
@@ -319,7 +323,7 @@ func (this *BlogService) notes2BlogItems(notes []info.Note) []info.BlogItem{
for _, noteContent := range noteContents {
noteContentsMap[noteContent.NoteId] = noteContent
}
-
+
// 组装成blogItem
// 按照notes的顺序
blogs := make([]info.BlogItem, len(noteIds))
@@ -335,12 +339,12 @@ func (this *BlogService) notes2BlogItems(notes []info.Note) []info.BlogItem{
return blogs
}
func (this *BlogService) SearchBlog(key, userId string, page, pageSize int, sortField string, isAsc bool) (info.Page, []info.BlogItem) {
- count, notes := noteService.SearchNote(key, userId, page, pageSize, sortField, isAsc, true);
-
- if(notes == nil || len(notes) == 0) {
+ count, notes := noteService.SearchNote(key, userId, page, pageSize, sortField, isAsc, true)
+
+ if notes == nil || len(notes) == 0 {
return info.Page{}, nil
}
-
+
blogs := this.notes2BlogItems(notes)
pageInfo := info.NewPage(page, pageSize, count, nil)
return pageInfo, blogs
@@ -349,18 +353,18 @@ func (this *BlogService) SearchBlog(key, userId string, page, pageSize int, sort
// 上一篇文章, 下一篇文章
// sorterField, baseTime是基准, sorterField=PublicTime, title
// isAsc是用户自定义的排序方式
-func (this *BlogService) PreNextBlog(userId string, sorterField string, isAsc bool, baseTime interface{}) (info.Note, info.Note) {
+func (this *BlogService) PreNextBlog(userId string, sorterField string, isAsc bool, baseTime interface{}) (info.Post, info.Post) {
userIdO := bson.ObjectIdHex(userId)
-
+
var sortFieldT1, sortFieldT2 bson.M
var sortFieldR1, sortFieldR2 string
if !isAsc {
// 降序
/*
-------- pre
------ now
---- next
---
+ ------- pre
+ ----- now
+ --- next
+ --
*/
// 上一篇时间要比它大, 找最小的
sortFieldT1 = bson.M{"$gt": baseTime}
@@ -370,12 +374,12 @@ func (this *BlogService) PreNextBlog(userId string, sorterField string, isAsc bo
sortFieldR2 = "-" + sorterField
} else {
// 升序
-/*
---- pre
------ now
-------- next
----------
-*/
+ /*
+ --- pre
+ ----- now
+ ------- next
+ ---------
+ */
// 上一篇要比它小, 找最大的
sortFieldT1 = bson.M{"$lt": baseTime}
sortFieldR1 = "-" + sorterField
@@ -383,68 +387,71 @@ func (this *BlogService) PreNextBlog(userId string, sorterField string, isAsc bo
sortFieldT2 = bson.M{"$gt": baseTime}
sortFieldR2 = sorterField
}
-
+
// 上一篇, 比基时间要小, 但是是最后一篇, 所以是降序
note := info.Note{}
- query := bson.M{"UserId": userIdO, "IsTrash": false, "IsBlog": true,
+ query := bson.M{"UserId": userIdO, "IsTrash": false, "IsBlog": true,
sorterField: sortFieldT1,
}
- q := db.Notes.Find(query);
+ q := db.Notes.Find(query)
q.Sort(sortFieldR1).Limit(1).One(¬e)
// 下一篇, 比基时间要大, 但是是第一篇, 所以是升序
note2 := info.Note{}
query[sorterField] = sortFieldT2
-// Log(isAsc)
-// LogJ(query)
-// Log(sortFieldR2)
- q = db.Notes.Find(query);
+ // Log(isAsc)
+ // LogJ(query)
+ // Log(sortFieldR2)
+ q = db.Notes.Find(query)
q.Sort(sortFieldR2).Limit(1).One(¬e2)
-
- return note, note2
+
+ return this.FixNote(note), this.FixNote(note2)
}
//-------
// p
// 平台 lea+
// 博客列表
-func (this *BlogService) ListAllBlogs(tag string, keywords string, isRecommend bool, page, pageSize int, sorterField string, isAsc bool) (info.Page, []info.BlogItem) {
+func (this *BlogService) ListAllBlogs(userId, tag string, keywords string, isRecommend bool, page, pageSize int, sorterField string, isAsc bool) (info.Page, []info.BlogItem) {
pageInfo := info.Page{CurPage: page}
notes := []info.Note{}
-
+
skipNum, sortFieldR := parsePageAndSort(page, pageSize, sorterField, isAsc)
-
+
// 不是trash的
- query := bson.M{"IsTrash": false, "IsBlog": true, "Title": bson.M{"$ne":"欢迎来到leanote!"}}
+ query := bson.M{"IsTrash": false, "IsBlog": true, "Title": bson.M{"$ne": "欢迎来到leanote!"}}
if tag != "" {
query["Tags"] = bson.M{"$in": []string{tag}}
}
+ if userId != "" {
+ query["UserId"] = bson.ObjectIdHex(userId)
+ }
// 不是demo的博客
demoUserId := configService.GetGlobalStringConfig("demoUserId")
- if demoUserId != "" {
+ if userId == "" && demoUserId != "" {
query["UserId"] = bson.M{"$ne": bson.ObjectIdHex(demoUserId)}
}
-
+
if isRecommend {
query["IsRecommend"] = isRecommend
}
if keywords != "" {
query["Title"] = bson.M{"$regex": bson.RegEx{".*?" + keywords + ".*", "i"}}
}
- q := db.Notes.Find(query);
-
+ q := db.Notes.Find(query)
+
// 总记录数
count, _ := q.Count()
-
+
q.Sort(sortFieldR).
Skip(skipNum).
Limit(pageSize).
All(¬es)
-
- if(notes == nil || len(notes) == 0) {
+
+ if notes == nil || len(notes) == 0 {
return pageInfo, nil
}
-
+
// 得到content, 并且每个都要substring
noteIds := make([]bson.ObjectId, len(notes))
userIds := make([]bson.ObjectId, len(notes))
@@ -452,21 +459,21 @@ func (this *BlogService) ListAllBlogs(tag string, keywords string, isRecommend b
noteIds[i] = note.NoteId
userIds[i] = note.UserId
}
-
+
// 可以不要的
// 直接得到noteContents表的abstract
// 这里可能是乱序的
/*
- noteContents := noteService.ListNoteAbstractsByNoteIds(noteIds) // 返回[info.NoteContent]
- noteContentsMap := make(map[bson.ObjectId]info.NoteContent, len(noteContents))
- for _, noteContent := range noteContents {
- noteContentsMap[noteContent.NoteId] = noteContent
- }
+ noteContents := noteService.ListNoteAbstractsByNoteIds(noteIds) // 返回[info.NoteContent]
+ noteContentsMap := make(map[bson.ObjectId]info.NoteContent, len(noteContents))
+ for _, noteContent := range noteContents {
+ noteContentsMap[noteContent.NoteId] = noteContent
+ }
*/
-
+
// 得到用户信息
userMap := userService.MapUserInfoAndBlogInfosByUserIds(userIds)
-
+
// 组装成blogItem
// 按照notes的顺序
blogs := make([]info.BlogItem, len(noteIds))
@@ -474,18 +481,17 @@ func (this *BlogService) ListAllBlogs(tag string, keywords string, isRecommend b
hasMore := true
var content string
/*
- if noteContent, ok := noteContentsMap[note.NoteId]; ok {
- content = noteContent.Abstract
- }
+ if noteContent, ok := noteContentsMap[note.NoteId]; ok {
+ content = noteContent.Abstract
+ }
*/
blogs[i] = info.BlogItem{note, "", content, hasMore, userMap[note.UserId]}
}
pageInfo = info.NewPage(page, pageSize, count, nil)
-
+
return pageInfo, blogs
}
-
//------------------------
// 博客设置
func (this *BlogService) fixUserBlog(userBlog *info.UserBlog) {
@@ -494,14 +500,14 @@ func (this *BlogService) fixUserBlog(userBlog *info.UserBlog) {
userBlog.Logo = strings.Trim(userBlog.Logo, "/")
userBlog.Logo = configService.GetSiteUrl() + "/" + userBlog.Logo
}
-
+
if userBlog.SortField == "" {
userBlog.SortField = "PublicTime"
}
if userBlog.PerPageSize <= 0 {
userBlog.PerPageSize = 10
}
-
+
// themePath
if userBlog.Style == "" {
userBlog.Style = defaultStyle
@@ -523,6 +529,7 @@ func (this *BlogService) GetUserBlog(userId string) info.UserBlog {
func (this *BlogService) UpdateUserBlog(userBlog info.UserBlog) bool {
return db.Upsert(db.UserBlogs, bson.M{"_id": userBlog.UserId}, userBlog)
}
+
// 修改之UserBlogBase
func (this *BlogService) UpdateUserBlogBase(userId string, userBlog info.UserBlogBase) bool {
ok := db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)}, userBlog)
@@ -534,12 +541,13 @@ func (this *BlogService) UpdateUserBlogComment(userId string, userBlog info.User
func (this *BlogService) UpdateUserBlogStyle(userId string, userBlog info.UserBlogStyle) bool {
return db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)}, userBlog)
}
-// 分页与排序
+
+// 分页与排序
func (this *BlogService) UpdateUserBlogPaging(userId string, perPageSize int, sortField string, isAsc bool) (ok bool, msg string) {
if ok, msg = Vds(map[string]string{"perPageSize": strconv.Itoa(perPageSize), "sortField": sortField}); !ok {
return
}
- ok = db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)},
+ ok = db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)},
bson.M{"PerPageSize": perPageSize, "SortField": sortField, "IsAsc": isAsc})
return
}
@@ -577,23 +585,23 @@ func (this *BlogService) ListLikedUsers(noteId string, isAll bool) ([]info.UserA
// 默认前5
pageSize := 5
skipNum, sortFieldR := parsePageAndSort(1, pageSize, "CreatedTime", false)
-
+
likes := []info.BlogLike{}
query := bson.M{"NoteId": bson.ObjectIdHex(noteId)}
- q := db.BlogLikes.Find(query);
-
+ q := db.BlogLikes.Find(query)
+
// 总记录数
count, _ := q.Count()
if count == 0 {
return nil, false
}
-
+
if isAll {
q.Sort(sortFieldR).Skip(skipNum).Limit(pageSize).All(&likes)
} else {
q.Sort(sortFieldR).All(&likes)
}
-
+
// 得到所有userIds
userIds := make([]bson.ObjectId, len(likes))
for i, like := range likes {
@@ -601,12 +609,12 @@ func (this *BlogService) ListLikedUsers(noteId string, isAll bool) ([]info.UserA
}
// 得到用户信息
userMap := userService.MapUserAndBlogByUserIds(userIds)
-
- users := make([]info.UserAndBlog, len(likes));
+
+ users := make([]info.UserAndBlog, len(likes))
for i, like := range likes {
users[i] = userMap[like.UserId.Hex()]
}
-
+
return users, count > pageSize
}
@@ -635,14 +643,14 @@ func (this *BlogService) LikeBlog(noteId, userId string) (ok bool, isLike bool)
ok = false
isLike = false
if noteId == "" || userId == "" {
- return
+ return
}
// 判断是否点过赞, 如果点过那么取消点赞
note := noteService.GetNoteById(noteId)
- if !note.IsBlog /*|| note.UserId.Hex() == userId */{
- return
+ if !note.IsBlog /*|| note.UserId.Hex() == userId */ {
+ return
}
-
+
noteIdO := bson.ObjectIdHex(noteId)
userIdO := bson.ObjectIdHex(userId)
var n int
@@ -658,7 +666,7 @@ func (this *BlogService) LikeBlog(noteId, userId string) (ok bool, isLike bool)
isLike = false
}
ok = db.Update(db.Notes, bson.M{"_id": noteIdO}, bson.M{"$inc": bson.M{"LikeNum": n}})
-
+
return
}
@@ -670,16 +678,16 @@ func (this *BlogService) Comment(noteId, toCommentId, userId, content string) (b
if content == "" {
return false, comment
}
-
+
note := noteService.GetNoteById(noteId)
if !note.IsBlog {
return false, comment
}
- comment = info.BlogComment{CommentId: bson.NewObjectId(),
- NoteId: bson.ObjectIdHex(noteId),
- UserId: bson.ObjectIdHex(userId),
- Content: content,
+ comment = info.BlogComment{CommentId: bson.NewObjectId(),
+ NoteId: bson.ObjectIdHex(noteId),
+ UserId: bson.ObjectIdHex(userId),
+ Content: content,
CreatedTime: time.Now(),
}
var comment2 = info.BlogComment{}
@@ -698,44 +706,44 @@ func (this *BlogService) Comment(noteId, toCommentId, userId, content string) (b
// 评论+1
db.Update(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, bson.M{"$inc": bson.M{"CommentNum": 1}})
}
-
+
if userId != note.UserId.Hex() || toCommentId != "" {
go func() {
- this.sendEmail(note, comment2, userId, content);
+ this.sendEmail(note, comment2, userId, content)
}()
}
-
+
return ok, comment
}
// 发送email
func (this *BlogService) sendEmail(note info.Note, comment info.BlogComment, userId, content string) {
- emailService.SendCommentEmail(note, comment, userId, content);
+ emailService.SendCommentEmail(note, comment, userId, content)
/*
- toUserId := note.UserId.Hex()
- // title := "评论提醒"
-
- // 表示回复回复的内容, 那么发送给之前回复的
- if comment.CommentId != "" {
- toUserId = comment.UserId.Hex()
- }
- toUserInfo := userService.GetUserInfo(toUserId)
- sendUserInfo := userService.GetUserInfo(userId)
-
- subject := note.Title + " 收到 " + sendUserInfo.Username + " 的评论";
- if comment.CommentId != "" {
- subject = "您在 " + note.Title + " 发表的评论收到 " + sendUserInfo.Username;
- if userId == note.UserId.Hex() {
- subject += "(作者)";
- }
- subject += " 的评论";
- }
+ toUserId := note.UserId.Hex()
+ // title := "评论提醒"
- body := "{header}评论内容:
" + content + ""; - href := "http://"+ configService.GetBlogDomain() + "/view/" + note.NoteId.Hex() - body += "
" + content + ""; + href := "http://"+ configService.GetBlogDomain() + "/view/" + note.NoteId.Hex() + body += "