forked from leanote/leanote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpgradeService.go
182 lines (153 loc) · 4.72 KB
/
UpgradeService.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
package service
import (
"github.com/leanote/leanote/app/info"
. "github.com/leanote/leanote/app/lea"
"github.com/leanote/leanote/app/db"
"gopkg.in/mgo.v2/bson"
"time"
)
type UpgradeService struct {
}
// 添加了PublicTime, RecommendTime
func (this *UpgradeService) UpgradeBlog() bool {
notes := []info.Note{}
db.ListByQ(db.Notes, bson.M{"IsBlog": true}, ¬es)
// PublicTime, RecommendTime = UpdatedTime
for _, note := range notes {
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())
}
}
return true
}
// 11-5自定义博客升级, 将aboutMe移至pages
/*
<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") != "" {
return false, "Leanote have been upgraded"
}
// 1. aboutMe -> page
userBlogs := []info.UserBlog{}
db.ListByQ(db.UserBlogs, bson.M{}, &userBlogs)
for _, userBlog := range userBlogs {
blogService.AddOrUpdateSingle(userBlog.UserId.Hex(), "", "About Me", userBlog.AboutMe)
}
// 2. 默认主题, 给admin用户
themeService.UpgradeThemeBeta2()
// 3. UrlTitles
// 3.1 note
notes := []info.Note{}
db.ListByQ(db.Notes, bson.M{}, ¬es)
for _, note := range notes {
data := bson.M{}
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)
}
data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note")
db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, data)
Log(noteId)
}
// 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{}
data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook")
db.UpdateByIdAndUserIdMap2(db.Notebooks, notebook.NotebookId, notebook.UserId, data)
Log(notebookId)
}
// 3.3 single
/*
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)
}
*/
// 删除索引
db.ShareNotes.DropIndex("UserId", "ToUserId", "NoteId")
ok = true
msg = "success"
configService.UpdateGlobalStringConfig(userId, "UpgradeBetaToBeta2", "1")
return
}
// 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{}
db.ListByQWithFields(db.Notebooks, bson.M{}, []string{"UserId"}, ¬ebooks)
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{}
db.ListByQWithFields(db.Notes, bson.M{}, []string{"UserId"}, ¬es)
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"
}
// user
db.UpdateByQField(db.Users, bson.M{}, "Usn", 200000)
// notebook
db.UpdateByQField(db.Notebooks, bson.M{}, "IsDeleted", false)
this.setNotebookUsn();
// note
// 1-N
db.UpdateByQField(db.Notes, bson.M{}, "IsDeleted", false)
this.setNoteUsn();
// tag
// 1-N
/// tag, 要重新插入, 将之前的Tag表迁移到NoteTag中
this.moveTag();
configService.UpdateGlobalStringConfig(userId, "UpgradeBetaToBeta4", "1")
return true, ""
}