urlTitle 优化, 减少生成次数, 避免使用id作为urlTitle

This commit is contained in:
lealife
2015-11-28 14:16:10 +08:00
parent b25bf0e16c
commit a7eaf6114a
4 changed files with 31 additions and 10 deletions

View File

@ -919,7 +919,7 @@ func (this *BlogService) UpateCateUrlTitle(userId string, cateId, urlTitle strin
"UrlTitle": "", "UrlTitle": "",
}) })
*/ */
url = GetUrTitle(userId, urlTitle, "notebook") url = GetUrTitle(userId, urlTitle, "notebook", cateId)
ok = db.UpdateByIdAndUserIdMap(db.Notebooks, cateId, userId, bson.M{ ok = db.UpdateByIdAndUserIdMap(db.Notebooks, cateId, userId, bson.M{
"UrlTitle": url, "UrlTitle": url,
}) })
@ -935,7 +935,7 @@ func (this *BlogService) UpateBlogUrlTitle(userId string, noteId, urlTitle strin
ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{
"UrlTitle": "", "UrlTitle": "",
}) })
url = GetUrTitle(userId, urlTitle, "note") url = GetUrTitle(userId, urlTitle, "note", noteId)
ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{
"UrlTitle": url, "UrlTitle": url,
}) })
@ -1036,7 +1036,7 @@ func (this *BlogService) UpdateSingleUrlTitle(userId, singleId, urlTitle string)
"UrlTitle": "", "UrlTitle": "",
}) })
*/ */
url = GetUrTitle(userId, urlTitle, "single") url = GetUrTitle(userId, urlTitle, "single", singleId)
ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{ ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{
"UrlTitle": url, "UrlTitle": url,
}) })
@ -1070,7 +1070,7 @@ func (this *BlogService) AddOrUpdateSingle(userId, singleId, title, content stri
UserId: bson.ObjectIdHex(userId), UserId: bson.ObjectIdHex(userId),
Title: title, Title: title,
Content: content, Content: content,
UrlTitle: GetUrTitle(userId, title, "single"), UrlTitle: GetUrTitle(userId, title, "single", singleId),
CreatedTime: time.Now(), CreatedTime: time.Now(),
} }
page.UpdatedTime = page.CreatedTime page.UpdatedTime = page.CreatedTime

View File

@ -156,9 +156,13 @@ func (this *NotebookService) GetNotebooksByNotebookIds(notebookIds []bson.Object
} }
// 添加 // 添加
// [ok]
func (this *NotebookService) AddNotebook(notebook info.Notebook) (bool, info.Notebook) { func (this *NotebookService) AddNotebook(notebook info.Notebook) (bool, info.Notebook) {
notebook.UrlTitle = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook")
if notebook.NotebookId == "" {
notebook.NotebookId = bson.NewObjectId()
}
notebook.UrlTitle = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook", notebook.NotebookId.Hex())
notebook.Usn = userService.IncrUsn(notebook.UserId.Hex()) notebook.Usn = userService.IncrUsn(notebook.UserId.Hex())
now := time.Now() now := time.Now()
notebook.CreatedTime = now notebook.CreatedTime = now

View File

@ -65,7 +65,7 @@ func (this *UpgradeService) UpgradeBetaToBeta2(userId string) (ok bool, msg stri
data["RecommendTime"] = note.UpdatedTime data["RecommendTime"] = note.UpdatedTime
Log("Time " + noteId) Log("Time " + noteId)
} }
data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note") data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note", noteId)
db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, data) db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, data)
Log(noteId) Log(noteId)
} }
@ -77,7 +77,7 @@ func (this *UpgradeService) UpgradeBetaToBeta2(userId string) (ok bool, msg stri
for _, notebook := range notebooks { for _, notebook := range notebooks {
notebookId := notebook.NotebookId.Hex() notebookId := notebook.NotebookId.Hex()
data := bson.M{} data := bson.M{}
data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook") data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook", notebookId)
db.UpdateByIdAndUserIdMap2(db.Notebooks, notebook.NotebookId, notebook.UserId, data) db.UpdateByIdAndUserIdMap2(db.Notebooks, notebook.NotebookId, notebook.UserId, data)
Log(notebookId) Log(notebookId)
} }

View File

@ -2,6 +2,7 @@ package service
import ( import (
"github.com/leanote/leanote/app/db" "github.com/leanote/leanote/app/db"
. "github.com/leanote/leanote/app/lea"
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"net/url" "net/url"
@ -150,12 +151,28 @@ func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int
return urlTitle2 return urlTitle2
} }
// 截取id 24位变成12位
// 先md5, 再取12位
func subIdHalf(id string) string {
idMd5 := Md5(id)
return idMd5[:12]
}
// types == note,notebook,single // types == note,notebook,single
func GetUrTitle(userId string, title string, types string) string { // id noteId, notebookId, singleId 当title没的时候才有用, 用它来替换
func GetUrTitle(userId string, title string, types string, id string) string {
urlTitle := strings.Trim(title, " ") urlTitle := strings.Trim(title, " ")
if urlTitle == "" { if urlTitle == "" {
urlTitle = "Untitled-" + userId if id == "" {
urlTitle = "Untitled-" + userId
} else {
urlTitle = subIdHalf(id)
}
// 不允许title是ObjectId
} else if bson.IsObjectIdHex(title) {
urlTitle = subIdHalf(id)
} }
urlTitle = fixUrlTitle(urlTitle) urlTitle = fixUrlTitle(urlTitle)
return getUniqueUrlTitle(userId, urlTitle, types, 1) return getUniqueUrlTitle(userId, urlTitle, types, 1)
} }