// 删除别人共享给我的笔记 // 先判断我是否有权限, 笔记是否是我创建的 func (this *TrashService) DeleteSharedNote(noteId, userId, myUserId string) bool { note := noteService.GetNote(noteId, userId) if shareService.HasUpdatePerm(userId, myUserId, noteId) && note.CreatedUserId.Hex() == myUserId { return db.UpdateByIdAndUserId(db.Notes, noteId, userId, bson.M{"$set": bson.M{"IsTrash": true, "Usn": userService.IncrUsn(userId)}}) } return false }
// recover func (this *TrashService) recoverNote(noteId, notebookId, userId string) bool { re := db.UpdateByIdAndUserId(db.Notes, noteId, userId, bson.M{"$set": bson.M{"IsTrash": false, "Usn": userService.IncrUsn(userId), "NotebookId": bson.ObjectIdHex(notebookId)}}) return re }
// 移动note // trash, 正常的都可以用 // 1. 要检查下notebookId是否是自己的 // 2. 要判断之前是否是blog, 如果不是, 那么notebook是否是blog? 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, "NotebookId": bson.ObjectIdHex(notebookId), "Usn": userService.IncrUsn(userId), }}) if re { // 更新blog状态 this.updateToNotebookBlog(noteId, notebookId, userId) // recount notebooks' notes number notebookService.ReCountNotebookNumberNotes(notebookId) // 之前不是trash才统计, trash本不在统计中的 if !note.IsTrash && preNotebookId != notebookId { notebookService.ReCountNotebookNumberNotes(preNotebookId) } } return this.GetNote(noteId, userId) } return info.Note{} }
// 新建一个note, 不需要添加历史记录 // 添加历史 func (this *NoteContentHistoryService) AddHistory(noteId, userId string, eachHistory info.EachHistory) { // 检查是否是空 if eachHistory.Content == "" { return } // 先查是否存在历史记录, 没有则添加之 history := info.NoteContentHistory{} db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &history) if history.NoteId == "" { this.newHistory(noteId, userId, eachHistory) } else { // 判断是否超出 maxSize, 如果超出则pop最后一个, 再push之, 不用那么麻烦, 直接update吧, 虽然影响性能 // TODO l := len(history.Histories) if l >= maxSize { // history.Histories = history.Histories[l-maxSize:] // BUG, 致使都是以前的 history.Histories = history.Histories[:maxSize] } newHistory := []info.EachHistory{eachHistory} newHistory = append(newHistory, history.Histories...) // 在开头加了, 最近的在最前 history.Histories = newHistory // 更新之 db.UpdateByIdAndUserId(db.NoteContentHistories, noteId, userId, history) } return }
// 添加或更新标签, 先查下是否存在, 不存在则添加, 存在则更新 // 都要统计下tag的note数 // 什么时候调用? 笔记添加Tag, 删除Tag时 // 删除note时, 都可以调用 // 万能 func (this *TagService) AddOrUpdateTag(userId string, tag string) info.NoteTag { userIdO := bson.ObjectIdHex(userId) noteTag := info.NoteTag{} db.GetByQ(db.NoteTags, bson.M{"UserId": userIdO, "Tag": tag}, ¬eTag) // 存在, 则更新之 if noteTag.TagId != "" { // 统计note数 count := noteService.CountNoteByTag(userId, tag) noteTag.Count = count noteTag.UpdatedTime = time.Now() // noteTag.Usn = userService.IncrUsn(userId), 更新count而已 db.UpdateByIdAndUserId(db.NoteTags, noteTag.TagId.Hex(), userId, noteTag) return noteTag } // 不存在, 则创建之 noteTag.TagId = bson.NewObjectId() noteTag.Count = 1 noteTag.Tag = tag noteTag.UserId = bson.ObjectIdHex(userId) noteTag.CreatedTime = time.Now() noteTag.UpdatedTime = noteTag.CreatedTime noteTag.Usn = userService.IncrUsn(userId) noteTag.IsDeleted = false db.Insert(db.NoteTags, noteTag) return noteTag }
// 如果自己的blog状态是true, 不用改变, // 否则, 如果notebookId的blog是true, 则改为true之 // 返回blog状态 // move, copy时用 func (this *NoteService) updateToNotebookBlog(noteId, notebookId, userId string) bool { if this.IsBlog(noteId) { return true } if notebookService.IsBlog(notebookId) { db.UpdateByIdAndUserId(db.Notes, noteId, userId, bson.M{"$set": bson.M{"IsBlog": true, "PublicTime": time.Now()}}) // life return true } return false }
// 删除note // 应该放在回收站里 // 有trashService func (this *TrashService) DeleteNote(noteId, userId string) bool { // 首先删除其共享 if shareService.DeleteShareNoteAll(noteId, userId) { // 更新note isTrash = true if db.UpdateByIdAndUserId(db.Notes, noteId, userId, bson.M{"$set": bson.M{"IsTrash": true, "Usn": userService.IncrUsn(userId)}}) { // recount notebooks' notes number notebookIdO := noteService.GetNotebookId(noteId) notebookId := notebookIdO.Hex() notebookService.ReCountNotebookNumberNotes(notebookId) return true } } return false }