2014-10-22 16:20:45 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2015-11-13 17:58:41 +08:00
|
|
|
"github.com/leanote/leanote/app/db"
|
2014-10-22 16:20:45 +08:00
|
|
|
"github.com/leanote/leanote/app/info"
|
|
|
|
. "github.com/leanote/leanote/app/lea"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
2015-03-31 14:27:26 +08:00
|
|
|
"time"
|
2014-10-22 16:20:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UpgradeService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加了PublicTime, RecommendTime
|
|
|
|
func (this *UpgradeService) UpgradeBlog() bool {
|
|
|
|
notes := []info.Note{}
|
|
|
|
db.ListByQ(db.Notes, bson.M{"IsBlog": true}, ¬es)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
// PublicTime, RecommendTime = UpdatedTime
|
|
|
|
for _, note := range notes {
|
2014-11-09 16:24:19 +08:00
|
|
|
if note.IsBlog && note.PublicTime.Year() < 100 {
|
|
|
|
db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, bson.M{"PublicTime": note.UpdatedTime, "RecommendTime": note.UpdatedTime})
|
|
|
|
Log(note.NoteId.Hex())
|
|
|
|
}
|
2014-10-22 16:20:45 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-10-22 16:20:45 +08:00
|
|
|
return true
|
2014-11-09 16:24:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 11-5自定义博客升级, 将aboutMe移至pages
|
2014-11-12 17:32:03 +08:00
|
|
|
/*
|
|
|
|
<li>Migrate "About me" to single(a single post)</li>
|
|
|
|
<li>Add some default themes to administrator</li>
|
|
|
|
<li>Generate "UrlTitle" for all notes. "UrlTitle" is a friendly url for post</li>
|
|
|
|
<li>Generate "UrlTitle" for all notebooks</li>
|
|
|
|
<li>Generate "UrlTitle" for all singles</li>
|
|
|
|
*/
|
|
|
|
func (this *UpgradeService) UpgradeBetaToBeta2(userId string) (ok bool, msg string) {
|
|
|
|
if configService.GetGlobalStringConfig("UpgradeBetaToBeta2") != "" {
|
2015-03-31 14:27:26 +08:00
|
|
|
return false, "Leanote have been upgraded"
|
2014-11-09 16:24:19 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-09 16:24:19 +08:00
|
|
|
// 1. aboutMe -> page
|
|
|
|
userBlogs := []info.UserBlog{}
|
|
|
|
db.ListByQ(db.UserBlogs, bson.M{}, &userBlogs)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-09 16:24:19 +08:00
|
|
|
for _, userBlog := range userBlogs {
|
|
|
|
blogService.AddOrUpdateSingle(userBlog.UserId.Hex(), "", "About Me", userBlog.AboutMe)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 2. 默认主题, 给admin用户
|
|
|
|
themeService.UpgradeThemeBeta2()
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 3. UrlTitles
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 3.1 note
|
|
|
|
notes := []info.Note{}
|
|
|
|
db.ListByQ(db.Notes, bson.M{}, ¬es)
|
|
|
|
for _, note := range notes {
|
2015-11-13 17:58:41 +08:00
|
|
|
data := bson.M{}
|
2014-11-12 17:32:03 +08:00
|
|
|
noteId := note.NoteId.Hex()
|
|
|
|
// PublicTime, RecommendTime = UpdatedTime
|
|
|
|
if note.IsBlog && note.PublicTime.Year() < 100 {
|
|
|
|
data["PublicTime"] = note.UpdatedTime
|
|
|
|
data["RecommendTime"] = note.UpdatedTime
|
|
|
|
Log("Time " + noteId)
|
|
|
|
}
|
2015-11-28 14:16:10 +08:00
|
|
|
data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note", noteId)
|
2014-11-12 17:32:03 +08:00
|
|
|
db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, data)
|
|
|
|
Log(noteId)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 3.2
|
|
|
|
Log("notebook")
|
|
|
|
notebooks := []info.Notebook{}
|
|
|
|
db.ListByQ(db.Notebooks, bson.M{}, ¬ebooks)
|
|
|
|
for _, notebook := range notebooks {
|
|
|
|
notebookId := notebook.NotebookId.Hex()
|
|
|
|
data := bson.M{}
|
2015-11-28 14:16:10 +08:00
|
|
|
data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook", notebookId)
|
2014-11-12 17:32:03 +08:00
|
|
|
db.UpdateByIdAndUserIdMap2(db.Notebooks, notebook.NotebookId, notebook.UserId, data)
|
|
|
|
Log(notebookId)
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 3.3 single
|
|
|
|
/*
|
2015-11-13 17:58:41 +08:00
|
|
|
singles := []info.BlogSingle{}
|
|
|
|
db.ListByQ(db.BlogSingles, bson.M{}, &singles)
|
|
|
|
for _, single := range singles {
|
|
|
|
singleId := single.SingleId.Hex()
|
|
|
|
blogService.UpdateSingleUrlTitle(single.UserId.Hex(), singleId, single.Title)
|
|
|
|
Log(singleId)
|
|
|
|
}
|
2014-11-12 17:32:03 +08:00
|
|
|
*/
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 删除索引
|
|
|
|
db.ShareNotes.DropIndex("UserId", "ToUserId", "NoteId")
|
2014-11-09 16:24:19 +08:00
|
|
|
ok = true
|
|
|
|
msg = "success"
|
2014-11-12 17:32:03 +08:00
|
|
|
configService.UpdateGlobalStringConfig(userId, "UpgradeBetaToBeta2", "1")
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-09 16:24:19 +08:00
|
|
|
return
|
|
|
|
}
|
2015-03-31 14:27:26 +08:00
|
|
|
|
|
|
|
// Usn设置
|
|
|
|
// 客户端 api
|
|
|
|
|
|
|
|
func (this *UpgradeService) moveTag() {
|
|
|
|
usnI := 1
|
|
|
|
tags := []info.Tag{}
|
|
|
|
db.ListByQ(db.Tags, bson.M{}, &tags)
|
|
|
|
for _, eachTag := range tags {
|
|
|
|
tagTitles := eachTag.Tags
|
|
|
|
now := time.Now()
|
|
|
|
if tagTitles != nil && len(tagTitles) > 0 {
|
|
|
|
for _, tagTitle := range tagTitles {
|
|
|
|
noteTag := info.NoteTag{}
|
|
|
|
noteTag.TagId = bson.NewObjectId()
|
|
|
|
noteTag.Count = 1
|
|
|
|
noteTag.Tag = tagTitle
|
|
|
|
noteTag.UserId = eachTag.UserId
|
|
|
|
noteTag.CreatedTime = now
|
|
|
|
noteTag.UpdatedTime = now
|
|
|
|
noteTag.Usn = usnI
|
|
|
|
noteTag.IsDeleted = false
|
|
|
|
db.Insert(db.NoteTags, noteTag)
|
|
|
|
usnI++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *UpgradeService) setNotebookUsn() {
|
|
|
|
usnI := 1
|
|
|
|
notebooks := []info.Notebook{}
|
2015-11-13 17:58:41 +08:00
|
|
|
db.ListByQWithFields(db.Notebooks, bson.M{}, []string{"UserId"}, ¬ebooks)
|
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
for _, notebook := range notebooks {
|
|
|
|
db.UpdateByQField(db.Notebooks, bson.M{"_id": notebook.NotebookId}, "Usn", usnI)
|
|
|
|
usnI++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *UpgradeService) setNoteUsn() {
|
|
|
|
usnI := 1
|
|
|
|
notes := []info.Note{}
|
2015-11-13 17:58:41 +08:00
|
|
|
db.ListByQWithFields(db.Notes, bson.M{}, []string{"UserId"}, ¬es)
|
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
for _, note := range notes {
|
|
|
|
db.UpdateByQField(db.Notes, bson.M{"_id": note.NoteId}, "Usn", usnI)
|
|
|
|
usnI++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 升级为Api, beta.4
|
|
|
|
func (this *UpgradeService) Api(userId string) (ok bool, msg string) {
|
|
|
|
if configService.GetGlobalStringConfig("UpgradeBetaToBeta4") != "" {
|
|
|
|
return false, "Leanote have been upgraded"
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
// user
|
|
|
|
db.UpdateByQField(db.Users, bson.M{}, "Usn", 200000)
|
|
|
|
|
|
|
|
// notebook
|
|
|
|
db.UpdateByQField(db.Notebooks, bson.M{}, "IsDeleted", false)
|
2015-11-13 17:58:41 +08:00
|
|
|
this.setNotebookUsn()
|
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
// note
|
|
|
|
// 1-N
|
|
|
|
db.UpdateByQField(db.Notes, bson.M{}, "IsDeleted", false)
|
2015-11-13 17:58:41 +08:00
|
|
|
this.setNoteUsn()
|
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
// tag
|
|
|
|
// 1-N
|
|
|
|
/// tag, 要重新插入, 将之前的Tag表迁移到NoteTag中
|
2015-11-13 17:58:41 +08:00
|
|
|
this.moveTag()
|
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
configService.UpdateGlobalStringConfig(userId, "UpgradeBetaToBeta4", "1")
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-03-31 14:27:26 +08:00
|
|
|
return true, ""
|
|
|
|
}
|