fmt all go file
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
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"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"time"
|
||||
@ -17,6 +17,7 @@ func (this *NoteService) GetNote(noteId, userId string) (note info.Note) {
|
||||
db.GetByIdAndUserId(db.Notes, noteId, userId, ¬e)
|
||||
return
|
||||
}
|
||||
|
||||
// fileService调用
|
||||
// 不能是已经删除了的, life bug, 客户端删除后, 竟然还能在web上打开
|
||||
func (this *NoteService) GetNoteById(noteId string) (note info.Note) {
|
||||
@ -35,14 +36,16 @@ func (this *NoteService) GetNoteByIdAndUserId(noteId, userId string) (note info.
|
||||
db.GetByQ(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId), "UserId": bson.ObjectIdHex(userId), "IsDeleted": false}, ¬e)
|
||||
return
|
||||
}
|
||||
|
||||
// 得到blog, blogService用
|
||||
// 不要传userId, 因为是公开的
|
||||
func (this *NoteService) GetBlogNote(noteId string) (note info.Note) {
|
||||
note = info.Note{}
|
||||
db.GetByQ(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId),
|
||||
db.GetByQ(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId),
|
||||
"IsBlog": true, "IsTrash": false, "IsDeleted": false}, ¬e)
|
||||
return
|
||||
}
|
||||
|
||||
// 通过id, userId得到noteContent
|
||||
func (this *NoteService) GetNoteContent(noteContentId, userId string) (noteContent info.NoteContent) {
|
||||
noteContent = info.NoteContent{}
|
||||
@ -62,11 +65,11 @@ func (this *NoteService) GetNoteAndContent(noteId, userId string) (noteAndConten
|
||||
func (this *NoteService) GetSyncNotes(userId string, afterUsn, maxEntry int) []info.ApiNote {
|
||||
notes := []info.Note{}
|
||||
q := db.Notes.Find(bson.M{
|
||||
"UserId": bson.ObjectIdHex(userId),
|
||||
"Usn": bson.M{"$gt": afterUsn},
|
||||
});
|
||||
"UserId": bson.ObjectIdHex(userId),
|
||||
"Usn": bson.M{"$gt": afterUsn},
|
||||
})
|
||||
q.Sort("Usn").Limit(maxEntry).All(¬es)
|
||||
|
||||
|
||||
return this.ToApiNotes(notes)
|
||||
}
|
||||
|
||||
@ -75,7 +78,7 @@ func (this *NoteService) ToApiNotes(notes []info.Note) []info.ApiNote {
|
||||
// 2, 得到所有图片, 附件信息
|
||||
// 查images表, attachs表
|
||||
if len(notes) > 0 {
|
||||
noteIds := make([]bson.ObjectId, len(notes));
|
||||
noteIds := make([]bson.ObjectId, len(notes))
|
||||
for i, note := range notes {
|
||||
noteIds[i] = note.NoteId
|
||||
}
|
||||
@ -92,24 +95,23 @@ func (this *NoteService) ToApiNotes(notes []info.Note) []info.ApiNote {
|
||||
return []info.ApiNote{}
|
||||
}
|
||||
|
||||
|
||||
// note与apiNote的转换
|
||||
func (this *NoteService) ToApiNote(note *info.Note, files []info.NoteFile) info.ApiNote {
|
||||
apiNote := info.ApiNote{
|
||||
NoteId: note.NoteId.Hex(),
|
||||
NotebookId: note.NotebookId.Hex(),
|
||||
UserId : note.UserId.Hex(),
|
||||
Title : note.Title,
|
||||
Tags : note.Tags,
|
||||
IsMarkdown : note.IsMarkdown,
|
||||
IsBlog : note.IsBlog,
|
||||
IsTrash : note.IsTrash,
|
||||
IsDeleted : note.IsDeleted,
|
||||
Usn : note.Usn,
|
||||
CreatedTime : note.CreatedTime,
|
||||
UpdatedTime : note.UpdatedTime,
|
||||
PublicTime : note.PublicTime,
|
||||
Files: files,
|
||||
NoteId: note.NoteId.Hex(),
|
||||
NotebookId: note.NotebookId.Hex(),
|
||||
UserId: note.UserId.Hex(),
|
||||
Title: note.Title,
|
||||
Tags: note.Tags,
|
||||
IsMarkdown: note.IsMarkdown,
|
||||
IsBlog: note.IsBlog,
|
||||
IsTrash: note.IsTrash,
|
||||
IsDeleted: note.IsDeleted,
|
||||
Usn: note.Usn,
|
||||
CreatedTime: note.CreatedTime,
|
||||
UpdatedTime: note.UpdatedTime,
|
||||
PublicTime: note.PublicTime,
|
||||
Files: files,
|
||||
}
|
||||
return apiNote
|
||||
}
|
||||
@ -121,11 +123,11 @@ func (this *NoteService) ToApiNote(note *info.Note, files []info.NoteFile) info.
|
||||
// 查images表, attachs表
|
||||
// [待测]
|
||||
func (this *NoteService) getFiles(noteIds []bson.ObjectId) map[string][]info.NoteFile {
|
||||
noteImages := noteImageService.getImagesByNoteIds(noteIds);
|
||||
noteImages := noteImageService.getImagesByNoteIds(noteIds)
|
||||
noteAttachs := attachService.getAttachsByNoteIds(noteIds)
|
||||
|
||||
|
||||
noteFilesMap := map[string][]info.NoteFile{}
|
||||
|
||||
|
||||
for _, noteId := range noteIds {
|
||||
noteIdHex := noteId.Hex()
|
||||
noteFiles := []info.NoteFile{}
|
||||
@ -134,36 +136,36 @@ func (this *NoteService) getFiles(noteIds []bson.ObjectId) map[string][]info.Not
|
||||
for _, image := range images {
|
||||
noteFiles = append(noteFiles, info.NoteFile{
|
||||
FileId: image.FileId.Hex(),
|
||||
Type: image.Type,
|
||||
});
|
||||
Type: image.Type,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// attach
|
||||
if attachs, ok := noteAttachs[noteIdHex]; ok {
|
||||
for _, attach := range attachs {
|
||||
noteFiles = append(noteFiles, info.NoteFile{
|
||||
FileId: attach.AttachId.Hex(),
|
||||
Type: attach.Type,
|
||||
Title: attach.Title,
|
||||
FileId: attach.AttachId.Hex(),
|
||||
Type: attach.Type,
|
||||
Title: attach.Title,
|
||||
IsAttach: true,
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
noteFilesMap[noteIdHex] = noteFiles
|
||||
}
|
||||
|
||||
|
||||
return noteFilesMap
|
||||
}
|
||||
|
||||
// 列出note, 排序规则, 还有分页
|
||||
// CreatedTime, UpdatedTime, title 来排序
|
||||
func (this *NoteService) ListNotes(userId, notebookId string,
|
||||
isTrash bool, pageNumber, pageSize int, sortField string, isAsc bool, isBlog bool) (count int, notes []info.Note) {
|
||||
isTrash bool, pageNumber, pageSize int, sortField string, isAsc bool, isBlog bool) (count int, notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
|
||||
|
||||
// 不是trash的
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId), "IsTrash": isTrash, "IsDeleted": false}
|
||||
if isBlog {
|
||||
@ -172,12 +174,12 @@ func (this *NoteService) ListNotes(userId, notebookId string,
|
||||
if notebookId != "" {
|
||||
query["NotebookId"] = bson.ObjectIdHex(notebookId)
|
||||
}
|
||||
|
||||
q := db.Notes.Find(query);
|
||||
|
||||
|
||||
q := db.Notes.Find(query)
|
||||
|
||||
// 总记录数
|
||||
count, _ = q.Count()
|
||||
|
||||
|
||||
q.Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
@ -187,11 +189,11 @@ func (this *NoteService) ListNotes(userId, notebookId string,
|
||||
|
||||
// 通过noteIds来查询
|
||||
// ShareService调用
|
||||
func (this *NoteService) ListNotesByNoteIdsWithPageSort(noteIds []bson.ObjectId, userId string,
|
||||
pageNumber, pageSize int, sortField string, isAsc bool, isBlog bool) (notes []info.Note) {
|
||||
func (this *NoteService) ListNotesByNoteIdsWithPageSort(noteIds []bson.ObjectId, userId string,
|
||||
pageNumber, pageSize int, sortField string, isAsc bool, isBlog bool) (notes []info.Note) {
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
notes = []info.Note{}
|
||||
|
||||
|
||||
// 不是trash
|
||||
db.Notes.
|
||||
Find(bson.M{"_id": bson.M{"$in": noteIds}, "IsTrash": false}).
|
||||
@ -201,24 +203,27 @@ func (this *NoteService) ListNotesByNoteIdsWithPageSort(noteIds []bson.ObjectId,
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
|
||||
// shareService调用
|
||||
func (this *NoteService) ListNotesByNoteIds(noteIds []bson.ObjectId) (notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
|
||||
|
||||
db.Notes.
|
||||
Find(bson.M{"_id": bson.M{"$in": noteIds}}).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
|
||||
// blog需要
|
||||
func (this *NoteService) ListNoteContentsByNoteIds(noteIds []bson.ObjectId) (notes []info.NoteContent) {
|
||||
notes = []info.NoteContent{}
|
||||
|
||||
|
||||
db.NoteContents.
|
||||
Find(bson.M{"_id": bson.M{"$in": noteIds}}).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
|
||||
// 只得到abstract, 不需要content
|
||||
func (this *NoteService) ListNoteAbstractsByNoteIds(noteIds []bson.ObjectId) (notes []info.NoteContent) {
|
||||
notes = []info.NoteContent{}
|
||||
@ -236,9 +241,9 @@ func (this *NoteService) ListNoteContentByNoteIds(noteIds []bson.ObjectId) (note
|
||||
// [ok]
|
||||
|
||||
func (this *NoteService) AddNote(note info.Note, fromApi bool) info.Note {
|
||||
if(note.NoteId.Hex() == "") {
|
||||
noteId := bson.NewObjectId();
|
||||
note.NoteId = noteId;
|
||||
if note.NoteId.Hex() == "" {
|
||||
noteId := bson.NewObjectId()
|
||||
note.NoteId = noteId
|
||||
}
|
||||
note.CreatedTime = time.Now()
|
||||
note.UpdatedTime = note.CreatedTime
|
||||
@ -246,26 +251,26 @@ func (this *NoteService) AddNote(note info.Note, fromApi bool) info.Note {
|
||||
note.UpdatedUserId = note.UserId
|
||||
note.UrlTitle = GetUrTitle(note.UserId.Hex(), note.Title, "note")
|
||||
note.Usn = userService.IncrUsn(note.UserId.Hex())
|
||||
|
||||
|
||||
notebookId := note.NotebookId.Hex()
|
||||
|
||||
|
||||
// api会传IsBlog, web不会传
|
||||
if !fromApi {
|
||||
// 设为blog
|
||||
note.IsBlog = notebookService.IsBlog(notebookId)
|
||||
}
|
||||
// if note.IsBlog {
|
||||
// if note.IsBlog {
|
||||
note.PublicTime = note.UpdatedTime
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
db.Insert(db.Notes, note)
|
||||
|
||||
|
||||
// tag1
|
||||
tagService.AddTags(note.UserId.Hex(), note.Tags)
|
||||
|
||||
|
||||
// recount notebooks' notes number
|
||||
notebookService.ReCountNotebookNumberNotes(notebookId)
|
||||
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
@ -279,19 +284,18 @@ func (this *NoteService) AddSharedNote(note info.Note, myUserId bson.ObjectId) i
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
|
||||
// 添加笔记本内容
|
||||
// [ok]
|
||||
func (this *NoteService) AddNoteContent(noteContent info.NoteContent) info.NoteContent {
|
||||
noteContent.CreatedTime = time.Now()
|
||||
noteContent.UpdatedTime = noteContent.CreatedTime
|
||||
noteContent.UpdatedTime = noteContent.CreatedTime
|
||||
noteContent.UpdatedUserId = noteContent.UserId
|
||||
db.Insert(db.NoteContents, noteContent)
|
||||
|
||||
|
||||
// 更新笔记图片
|
||||
noteImageService.UpdateNoteImages(noteContent.UserId.Hex(), noteContent.NoteId.Hex(), "", noteContent.Content)
|
||||
|
||||
return noteContent;
|
||||
|
||||
return noteContent
|
||||
}
|
||||
|
||||
// API, abstract, desc需要这里获取
|
||||
@ -308,7 +312,7 @@ func (this *NoteService) AddNoteAndContentApi(note info.Note, noteContent info.N
|
||||
note.UpdatedUserId = note.UserId
|
||||
note.UrlTitle = GetUrTitle(note.UserId.Hex(), note.Title, "note")
|
||||
note.Usn = userService.IncrUsn(note.UserId.Hex())
|
||||
|
||||
|
||||
// desc这里获取
|
||||
desc := SubStringHTMLToRaw(noteContent.Content, 50)
|
||||
note.Desc = desc;
|
||||
@ -316,24 +320,24 @@ func (this *NoteService) AddNoteAndContentApi(note info.Note, noteContent info.N
|
||||
// 设为blog
|
||||
notebookId := note.NotebookId.Hex()
|
||||
note.IsBlog = notebookService.IsBlog(notebookId)
|
||||
|
||||
|
||||
if note.IsBlog {
|
||||
note.PublicTime = note.UpdatedTime
|
||||
}
|
||||
|
||||
|
||||
db.Insert(db.Notes, note)
|
||||
|
||||
|
||||
// tag1, 不需要了
|
||||
// tagService.AddTags(note.UserId.Hex(), note.Tags)
|
||||
|
||||
|
||||
// recount notebooks' notes number
|
||||
notebookService.ReCountNotebookNumberNotes(notebookId)
|
||||
|
||||
|
||||
// 这里, 添加到内容中
|
||||
abstract := SubStringHTML(noteContent.Content, 200, "")
|
||||
noteContent.Abstract = abstract
|
||||
this.AddNoteContent(noteContent)
|
||||
|
||||
|
||||
return note
|
||||
}*/
|
||||
|
||||
@ -348,15 +352,15 @@ func (this *NoteService) AddNoteAndContentForController(note info.Note, noteCont
|
||||
Log("HAS AUTH -----------")
|
||||
}
|
||||
}
|
||||
return this.AddNoteAndContent(note, noteContent, bson.ObjectIdHex(updatedUserId));
|
||||
return this.AddNoteAndContent(note, noteContent, bson.ObjectIdHex(updatedUserId))
|
||||
}
|
||||
func (this *NoteService) AddNoteAndContent(note info.Note, noteContent info.NoteContent, myUserId bson.ObjectId) info.Note {
|
||||
if(note.NoteId.Hex() == "") {
|
||||
if note.NoteId.Hex() == "" {
|
||||
noteId := bson.NewObjectId()
|
||||
note.NoteId = noteId
|
||||
}
|
||||
noteContent.NoteId = note.NoteId
|
||||
if note.UserId != myUserId {
|
||||
if note.UserId != myUserId {
|
||||
note = this.AddSharedNote(note, myUserId)
|
||||
} else {
|
||||
note = this.AddNote(note, false)
|
||||
@ -368,12 +372,12 @@ func (this *NoteService) AddNoteAndContent(note info.Note, noteContent info.Note
|
||||
}
|
||||
|
||||
func (this *NoteService) AddNoteAndContentApi(note info.Note, noteContent info.NoteContent, myUserId bson.ObjectId) info.Note {
|
||||
if(note.NoteId.Hex() == "") {
|
||||
if note.NoteId.Hex() == "" {
|
||||
noteId := bson.NewObjectId()
|
||||
note.NoteId = noteId
|
||||
}
|
||||
noteContent.NoteId = note.NoteId
|
||||
if note.UserId != myUserId {
|
||||
if note.UserId != myUserId {
|
||||
note = this.AddSharedNote(note, myUserId)
|
||||
} else {
|
||||
note = this.AddNote(note, true)
|
||||
@ -392,7 +396,7 @@ func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bso
|
||||
if note.NoteId == "" {
|
||||
return false, "notExists", 0
|
||||
}
|
||||
|
||||
|
||||
userId := note.UserId.Hex()
|
||||
// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
|
||||
if userId != updatedUserId {
|
||||
@ -403,34 +407,34 @@ func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bso
|
||||
Log("HAS AUTH -----------")
|
||||
}
|
||||
}
|
||||
|
||||
if usn > 0 && note.Usn != usn {
|
||||
|
||||
if usn > 0 && note.Usn != usn {
|
||||
return false, "conflict", 0
|
||||
}
|
||||
|
||||
|
||||
// 是否已自定义
|
||||
if note.IsBlog && note.HasSelfDefined {
|
||||
delete(needUpdate, "ImgSrc")
|
||||
delete(needUpdate, "Desc")
|
||||
}
|
||||
|
||||
needUpdate["UpdatedUserId"] = bson.ObjectIdHex(updatedUserId);
|
||||
needUpdate["UpdatedTime"] = time.Now();
|
||||
afterUsn := userService.IncrUsn(userId);
|
||||
|
||||
needUpdate["UpdatedUserId"] = bson.ObjectIdHex(updatedUserId)
|
||||
needUpdate["UpdatedTime"] = time.Now()
|
||||
afterUsn := userService.IncrUsn(userId)
|
||||
needUpdate["Usn"] = afterUsn
|
||||
|
||||
|
||||
// 添加tag2
|
||||
// TODO 这个tag去掉, 添加tag另外添加, 不要这个
|
||||
if tags, ok := needUpdate["Tags"]; ok {
|
||||
tagService.AddTagsI(userId, tags)
|
||||
}
|
||||
|
||||
|
||||
// 是否修改了isBlog
|
||||
// 也要修改noteContents的IsBlog
|
||||
if isBlog, ok := needUpdate["IsBlog"]; ok {
|
||||
isBlog2 := isBlog.(bool)
|
||||
if note.IsBlog != isBlog2 {
|
||||
this.UpdateNoteContentIsBlog(noteId, userId, isBlog2);
|
||||
this.UpdateNoteContentIsBlog(noteId, userId, isBlog2)
|
||||
|
||||
// 重新发布成博客
|
||||
if !note.IsBlog {
|
||||
@ -438,17 +442,17 @@ func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bso
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ok := db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, needUpdate)
|
||||
if !ok {
|
||||
return ok, "", 0
|
||||
}
|
||||
|
||||
|
||||
// 重新获取之
|
||||
note = this.GetNoteById(noteId)
|
||||
|
||||
|
||||
hasRecount := false
|
||||
|
||||
|
||||
// 如果修改了notebookId, 则更新notebookId'count
|
||||
// 两方的notebook也要修改
|
||||
notebookIdI := needUpdate["NotebookId"]
|
||||
@ -460,14 +464,14 @@ func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bso
|
||||
notebookService.ReCountNotebookNumberNotes(notebookId.Hex())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 不要多次更新, isTrash = false, = true都要重新统计
|
||||
if !hasRecount {
|
||||
if _, ok := needUpdate["IsTrash"]; ok {
|
||||
notebookService.ReCountNotebookNumberNotes(note.NotebookId.Hex())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true, "", afterUsn
|
||||
}
|
||||
|
||||
@ -479,7 +483,7 @@ func (this *NoteService) UpdateNoteContentIsBlog(noteId, userId string, isBlog b
|
||||
// 附件修改, 增加noteIncr
|
||||
func (this *NoteService) IncrNoteUsn(noteId, userId string) int {
|
||||
afterUsn := userService.IncrUsn(userId)
|
||||
db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
|
||||
db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
|
||||
bson.M{"UpdatedTime": time.Now(), "Usn": afterUsn})
|
||||
return afterUsn
|
||||
}
|
||||
@ -495,7 +499,7 @@ func (this *NoteService) UpdateNoteTitle(userId, updatedUserId, noteId, title st
|
||||
}
|
||||
}
|
||||
|
||||
return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
|
||||
return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
|
||||
bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId), "Title": title, "UpdatedTime": time.Now(), "Usn": userService.IncrUsn(userId)})
|
||||
}
|
||||
|
||||
@ -517,17 +521,17 @@ func (this *NoteService) UpdateNoteContent(updatedUserId, noteId, content, abstr
|
||||
return false, "noAuth", 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// abstract重置
|
||||
data := bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId),
|
||||
"Content": content,
|
||||
"Abstract": abstract,
|
||||
data := bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId),
|
||||
"Content": content,
|
||||
"Abstract": abstract,
|
||||
"UpdatedTime": time.Now()}
|
||||
|
||||
|
||||
if note.IsBlog && note.HasSelfDefined {
|
||||
delete(data, "Abstract")
|
||||
}
|
||||
|
||||
|
||||
// usn, 修改笔记不可能单独修改内容
|
||||
afterUsn := 0
|
||||
// 如果之前没有修改note其它信息, 那么usn++
|
||||
@ -542,14 +546,14 @@ func (this *NoteService) UpdateNoteContent(updatedUserId, noteId, content, abstr
|
||||
|
||||
if db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, data) {
|
||||
// 这里, 添加历史记录
|
||||
noteContentHistoryService.AddHistory(noteId, userId, info.EachHistory{UpdatedUserId: bson.ObjectIdHex(updatedUserId),
|
||||
Content: content,
|
||||
noteContentHistoryService.AddHistory(noteId, userId, info.EachHistory{UpdatedUserId: bson.ObjectIdHex(updatedUserId),
|
||||
Content: content,
|
||||
UpdatedTime: time.Now(),
|
||||
})
|
||||
|
||||
|
||||
// 更新笔记图片
|
||||
noteImageService.UpdateNoteImages(userId, noteId, note.ImgSrc, content)
|
||||
|
||||
|
||||
return true, "", afterUsn
|
||||
}
|
||||
return false, "", 0
|
||||
@ -585,12 +589,12 @@ func (this *NoteService) ToBlog(userId, noteId string, isBlog, isTop bool) bool
|
||||
noteUpdate["HasSelfDefined"] = false
|
||||
}
|
||||
noteUpdate["Usn"] = userService.IncrUsn(userId)
|
||||
|
||||
|
||||
ok := db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, noteUpdate)
|
||||
// 重新计算tags
|
||||
go (func() {
|
||||
this.UpdateNoteContentIsBlog(noteId, userId, isBlog);
|
||||
|
||||
this.UpdateNoteContentIsBlog(noteId, userId, isBlog)
|
||||
|
||||
blogService.ReCountBlogTags(userId)
|
||||
})()
|
||||
return ok
|
||||
@ -604,17 +608,17 @@ func (this *NoteService) MoveNote(noteId, notebookId, userId string) info.Note {
|
||||
if notebookService.IsMyNotebook(notebookId, userId) {
|
||||
note := this.GetNote(noteId, userId)
|
||||
preNotebookId := note.NotebookId.Hex()
|
||||
|
||||
re := db.UpdateByIdAndUserId(db.Notes, noteId, userId,
|
||||
bson.M{"$set": bson.M{"IsTrash": false,
|
||||
|
||||
re := db.UpdateByIdAndUserId(db.Notes, noteId, userId,
|
||||
bson.M{"$set": bson.M{"IsTrash": false,
|
||||
"NotebookId": bson.ObjectIdHex(notebookId),
|
||||
"Usn": userService.IncrUsn(userId),
|
||||
}})
|
||||
|
||||
"Usn": userService.IncrUsn(userId),
|
||||
}})
|
||||
|
||||
if re {
|
||||
// 更新blog状态
|
||||
this.updateToNotebookBlog(noteId, notebookId, userId)
|
||||
|
||||
|
||||
// recount notebooks' notes number
|
||||
notebookService.ReCountNotebookNumberNotes(notebookId)
|
||||
// 之前不是trash才统计, trash本不在统计中的
|
||||
@ -622,13 +626,13 @@ func (this *NoteService) MoveNote(noteId, notebookId, userId string) info.Note {
|
||||
notebookService.ReCountNotebookNumberNotes(preNotebookId)
|
||||
}
|
||||
}
|
||||
|
||||
return this.GetNote(noteId, userId);
|
||||
|
||||
return this.GetNote(noteId, userId)
|
||||
}
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
// 如果自己的blog状态是true, 不用改变,
|
||||
// 如果自己的blog状态是true, 不用改变,
|
||||
// 否则, 如果notebookId的blog是true, 则改为true之
|
||||
// 返回blog状态
|
||||
// move, copy时用
|
||||
@ -637,16 +641,17 @@ func (this *NoteService) updateToNotebookBlog(noteId, notebookId, userId string)
|
||||
return true
|
||||
}
|
||||
if notebookService.IsBlog(notebookId) {
|
||||
db.UpdateByIdAndUserId(db.Notes, noteId, userId,
|
||||
db.UpdateByIdAndUserId(db.Notes, noteId, userId,
|
||||
bson.M{"$set": bson.M{"IsBlog": true, "PublicTime": time.Now()}}) // life
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 判断是否是blog
|
||||
func (this *NoteService) IsBlog(noteId string) bool {
|
||||
note := info.Note{}
|
||||
db.GetByQWithFields(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, []string{"IsBlog"}, ¬e);
|
||||
db.GetByQWithFields(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, []string{"IsBlog"}, ¬e)
|
||||
return note.IsBlog
|
||||
}
|
||||
|
||||
@ -658,25 +663,25 @@ func (this *NoteService) CopyNote(noteId, notebookId, userId string) info.Note {
|
||||
if notebookService.IsMyNotebook(notebookId, userId) {
|
||||
note := this.GetNote(noteId, userId)
|
||||
noteContent := this.GetNoteContent(noteId, userId)
|
||||
|
||||
|
||||
// 重新生成noteId
|
||||
note.NoteId = bson.NewObjectId();
|
||||
note.NoteId = bson.NewObjectId()
|
||||
note.NotebookId = bson.ObjectIdHex(notebookId)
|
||||
|
||||
|
||||
noteContent.NoteId = note.NoteId
|
||||
note = this.AddNoteAndContent(note, noteContent, note.UserId);
|
||||
note = this.AddNoteAndContent(note, noteContent, note.UserId)
|
||||
|
||||
// 更新blog状态
|
||||
isBlog := this.updateToNotebookBlog(note.NoteId.Hex(), notebookId, userId)
|
||||
|
||||
// recount
|
||||
notebookService.ReCountNotebookNumberNotes(notebookId)
|
||||
|
||||
|
||||
note.IsBlog = isBlog
|
||||
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
@ -693,37 +698,37 @@ func (this *NoteService) CopySharedNote(noteId, notebookId, fromUserId, myUserId
|
||||
noteContent := this.GetNoteContent(noteId, fromUserId)
|
||||
|
||||
// 重新生成noteId
|
||||
note.NoteId = bson.NewObjectId();
|
||||
note.NoteId = bson.NewObjectId()
|
||||
note.NotebookId = bson.ObjectIdHex(notebookId)
|
||||
note.UserId = bson.ObjectIdHex(myUserId)
|
||||
note.IsTop = false
|
||||
note.IsBlog = false // 别人的可能是blog
|
||||
|
||||
|
||||
note.ImgSrc = "" // 为什么清空, 因为图片需要复制, 先清空
|
||||
|
||||
|
||||
// content
|
||||
noteContent.NoteId = note.NoteId
|
||||
noteContent.UserId = note.UserId
|
||||
|
||||
|
||||
// 复制图片, 把note的图片都copy给我, 且修改noteContent图片路径
|
||||
noteContent.Content = noteImageService.CopyNoteImages(noteId, fromUserId, note.NoteId.Hex(), noteContent.Content, myUserId)
|
||||
|
||||
|
||||
// 复制附件
|
||||
attachService.CopyAttachs(noteId, note.NoteId.Hex(), myUserId)
|
||||
|
||||
|
||||
// 添加之
|
||||
note = this.AddNoteAndContent(note, noteContent, note.UserId);
|
||||
|
||||
note = this.AddNoteAndContent(note, noteContent, note.UserId)
|
||||
|
||||
// 更新blog状态
|
||||
isBlog := this.updateToNotebookBlog(note.NoteId.Hex(), notebookId, myUserId)
|
||||
|
||||
|
||||
// recount
|
||||
notebookService.ReCountNotebookNumberNotes(notebookId)
|
||||
|
||||
|
||||
note.IsBlog = isBlog
|
||||
return note
|
||||
}
|
||||
|
||||
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
@ -743,31 +748,31 @@ func (this *NoteService) GetNotebookId(noteId string) bson.ObjectId {
|
||||
func (this *NoteService) SearchNote(key, userId string, pageNumber, pageSize int, sortField string, isAsc, isBlog bool) (count int, notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
|
||||
|
||||
// 利用标题和desc, 不用content
|
||||
orQ := []bson.M{
|
||||
bson.M{"Title": bson.M{"$regex": bson.RegEx{".*?" + key + ".*", "i"}}},
|
||||
bson.M{"Desc": bson.M{"$regex": bson.RegEx{".*?" + key + ".*", "i"}}},
|
||||
}
|
||||
// 不是trash的
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"IsTrash": false,
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"IsTrash": false,
|
||||
"IsDeleted": false, // 不能搜索已删除了的
|
||||
"$or": orQ,
|
||||
"$or": orQ,
|
||||
}
|
||||
if isBlog {
|
||||
query["IsBlog"] = true
|
||||
}
|
||||
q := db.Notes.Find(query);
|
||||
|
||||
q := db.Notes.Find(query)
|
||||
|
||||
// 总记录数
|
||||
count, _ = q.Count()
|
||||
|
||||
|
||||
q.Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
All(¬es)
|
||||
|
||||
|
||||
// 如果 < pageSize 那么搜索content, 且id不在这些id之间的
|
||||
if len(notes) < pageSize {
|
||||
notes = this.searchNoteFromContent(notes, userId, key, pageSize, sortFieldR, isBlog)
|
||||
@ -794,19 +799,19 @@ func (this *NoteService) searchNoteFromContent(notes []info.Note, userId, key st
|
||||
Select(bson.M{"_id": true}).
|
||||
All(¬eContents)
|
||||
var lenContent = len(noteContents)
|
||||
if(lenContent == 0) {
|
||||
if lenContent == 0 {
|
||||
return notes
|
||||
}
|
||||
|
||||
|
||||
// 收集ids
|
||||
noteIds2 := make([]bson.ObjectId, lenContent)
|
||||
for i, content := range noteContents {
|
||||
noteIds2[i] = content.NoteId
|
||||
}
|
||||
|
||||
|
||||
// 得到notes
|
||||
notes2 := this.ListNotesByNoteIds(noteIds2)
|
||||
|
||||
|
||||
// 合并之
|
||||
notes = append(notes, notes2...)
|
||||
return notes
|
||||
@ -817,17 +822,17 @@ func (this *NoteService) searchNoteFromContent(notes []info.Note, userId, key st
|
||||
func (this *NoteService) SearchNoteByTags(tags []string, userId string, pageNumber, pageSize int, sortField string, isAsc bool) (count int, notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
|
||||
|
||||
// 不是trash的
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"IsTrash": false,
|
||||
"Tags": bson.M{"$all": tags}}
|
||||
|
||||
q := db.Notes.Find(query);
|
||||
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"IsTrash": false,
|
||||
"Tags": bson.M{"$all": tags}}
|
||||
|
||||
q := db.Notes.Find(query)
|
||||
|
||||
// 总记录数
|
||||
count, _ = q.Count()
|
||||
|
||||
|
||||
q.Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
@ -857,17 +862,17 @@ func (this *NoteService) CountNoteByTag(userId string, tag string) int {
|
||||
if tag == "" {
|
||||
return 0
|
||||
}
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
// "IsTrash": false,
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
// "IsTrash": false,
|
||||
"IsDeleted": false,
|
||||
"Tags": bson.M{"$in": []string{tag}}}
|
||||
"Tags": bson.M{"$in": []string{tag}}}
|
||||
return db.Count(db.Notes, query)
|
||||
}
|
||||
|
||||
// 删除tag
|
||||
// 返回所有note的Usn
|
||||
func (this *NoteService) UpdateNoteToDeleteTag(userId string, targetTag string) map[string]int {
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"Tags": bson.M{"$in": []string{targetTag}}}
|
||||
notes := []info.Note{}
|
||||
db.ListByQ(db.Notes, query, ¬es)
|
||||
@ -881,7 +886,7 @@ func (this *NoteService) UpdateNoteToDeleteTag(userId string, targetTag string)
|
||||
if tag == targetTag {
|
||||
tags = tags
|
||||
tags = append(tags[:i], tags[i+1:]...)
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
usn := userService.IncrUsn(userId)
|
||||
|
Reference in New Issue
Block a user