// 修改博客的图片, 描述, 摘要 func (this *BlogService) UpateBlogAbstract(userId string, noteId, imgSrc, desc, abstract string) (ok bool) { ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ "ImgSrc": imgSrc, "Desc": desc, "HasSelfDefined": true, }) ok = db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, bson.M{ "Abstract": abstract, }) return ok }
// 修改笔记urlTitle func (this *BlogService) UpateBlogUrlTitle(userId string, noteId, urlTitle string) (ok bool, url string) { url = urlTitle // 先清空 ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ "UrlTitle": "", }) url = GetUrTitle(userId, urlTitle, "note") ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ "UrlTitle": url, }) // 返回给前端的是decode url = decodeValue(url) return }
func (this *NoteService) ToBlog(userId, noteId string, isBlog, isTop bool) bool { noteUpdate := bson.M{} if isTop { isBlog = true } if !isBlog { isTop = false } noteUpdate["IsBlog"] = isBlog noteUpdate["IsTop"] = isTop if isBlog { noteUpdate["PublicTime"] = time.Now() } else { noteUpdate["HasSelfDefined"] = false } noteUpdate["Usn"] = userService.IncrUsn(userId) ok := db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, noteUpdate) // 重新计算tags go (func() { this.UpdateNoteContentIsBlog(noteId, userId, isBlog) blogService.ReCountBlogTags(userId) })() return ok }
func (this *TrashService) DeleteTrashApi(noteId, userId string, usn int) (bool, string, int) { note := noteService.GetNote(noteId, userId) if note.NoteId == "" || note.IsDeleted { return false, "notExists", 0 } if note.Usn != usn { return false, "conflict", 0 } // delete note's attachs ok := attachService.DeleteAllAttachs(noteId, userId) // 设置删除位 afterUsn := userService.IncrUsn(userId) ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{"IsDeleted": true, "Usn": afterUsn}) // delete content ok = db.DeleteByIdAndUserId(db.NoteContents, noteId, userId) return ok, "", afterUsn }
// 更新笔记, api func (this *NotebookService) UpdateNotebookApi(userId, notebookId, title, parentNotebookId string, seq, usn int) (bool, string, info.Notebook) { if notebookId == "" { return false, "notebookIdNotExists", info.Notebook{} } // 先判断usn是否和数据库的一样, 如果不一样, 则冲突, 不保存 notebook := this.GetNotebookById(notebookId) // 不存在 if notebook.NotebookId == "" { return false, "notExists", notebook } else if notebook.Usn != usn { return false, "conflict", notebook } notebook.Usn = userService.IncrUsn(userId) notebook.Title = title updates := bson.M{"Title": title, "Usn": notebook.Usn, "Seq": seq, "UpdatedTime": time.Now()} if parentNotebookId != "" && bson.IsObjectIdHex(parentNotebookId) { updates["ParentNotebookId"] = bson.ObjectIdHex(parentNotebookId) } else { updates["ParentNotebookId"] = "" } ok := db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, updates) if ok { return ok, "", this.GetNotebookById(notebookId) } return false, "", notebook }
// 更新或添加 func (this *BlogService) AddOrUpdateSingle(userId, singleId, title, content string) (ok bool) { ok = false if singleId != "" { ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{ "Title": title, "Content": content, "UpdatedTime": time.Now(), }) if ok { // 还要修改UserBlog中的Singles this.updateBlogSingles(userId, false, false, singleId, title, "") } return } // 添加 page := info.BlogSingle{ SingleId: bson.NewObjectId(), UserId: bson.ObjectIdHex(userId), Title: title, Content: content, UrlTitle: GetUrTitle(userId, title, "single"), CreatedTime: time.Now(), } page.UpdatedTime = page.CreatedTime ok = db.Insert(db.BlogSingles, page) // 还要修改UserBlog中的Singles this.updateBlogSingles(userId, false, true, page.SingleId.Hex(), title, page.UrlTitle) return }
// 这里要判断权限, 如果userId != updatedUserId, 那么需要判断权限 // [ok] TODO perm还没测 [del] func (this *NoteService) UpdateNoteTitle(userId, updatedUserId, noteId, title string) bool { // updatedUserId 要修改userId的note, 此时需要判断是否有修改权限 if userId != updatedUserId { if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) { println("NO AUTH") return false } } return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId), "Title": title, "UpdatedTime": time.Now(), "Usn": userService.IncrUsn(userId)}) }
// 排序和设置父 func (this *NotebookService) DragNotebooks(userId string, curNotebookId string, parentNotebookId string, siblings []string) bool { ok := false // 如果没parentNotebookId, 则parentNotebookId设空 if parentNotebookId == "" { ok = db.UpdateByIdAndUserIdMap(db.Notebooks, curNotebookId, userId, bson.M{"ParentNotebookId": "", "Usn": userService.IncrUsn(userId)}) } else { ok = db.UpdateByIdAndUserIdMap(db.Notebooks, curNotebookId, userId, bson.M{"ParentNotebookId": bson.ObjectIdHex(parentNotebookId), "Usn": userService.IncrUsn(userId)}) } if !ok { return false } // 排序 for seq, notebookId := range siblings { if !db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"Seq": seq, "Usn": userService.IncrUsn(userId)}) { return false } } return true }
// 排序 // 传入 notebookId => Seq // 为什么要传入userId, 防止修改其它用户的信息 (恶意) // [ok] func (this *NotebookService) SortNotebooks(userId string, notebookId2Seqs map[string]int) bool { if len(notebookId2Seqs) == 0 { return false } for notebookId, seq := range notebookId2Seqs { if !db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"Seq": seq, "Usn": userService.IncrUsn(userId)}) { return false } } return true }
// 修改笔记本内容 // [ok] TODO perm未测 // hasBeforeUpdateNote 之前是否更新过note其它信息, 如果有更新, usn不用更新 // TODO abstract这里生成 func (this *NoteService) UpdateNoteContent(updatedUserId, noteId, content, abstract string, hasBeforeUpdateNote bool, usn int) (bool, string, int) { // 是否已自定义 note := this.GetNoteById(noteId) if note.NoteId == "" { return false, "notExists", 0 } userId := note.UserId.Hex() // updatedUserId 要修改userId的note, 此时需要判断是否有修改权限 if userId != updatedUserId { if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) { Log("NO AUTH") return false, "noAuth", 0 } } // 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++ if !hasBeforeUpdateNote { // 需要验证 if usn >= 0 && note.Usn != usn { return false, "conflict", 0 } afterUsn = userService.IncrUsn(userId) db.UpdateByIdAndUserIdField(db.Notes, noteId, userId, "Usn", usn) } if db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, data) { // 这里, 添加历史记录 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 }
// 修改urlTitle func (this *BlogService) UpdateSingleUrlTitle(userId, singleId, urlTitle string) (ok bool, url string) { url = urlTitle /* // 先清空 ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{ "UrlTitle": "", }) */ url = GetUrTitle(userId, urlTitle, "single") ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{ "UrlTitle": url, }) if ok { // 还要修改UserBlog中的Singles this.updateBlogSingles(userId, false, false, singleId, "", url) } // 返回给前端的是decode url = decodeValue(url) return }
// ToBlog or Not func (this *NotebookService) ToBlog(userId, notebookId string, isBlog bool) bool { updates := bson.M{"IsBlog": isBlog, "Usn": userService.IncrUsn(userId)} // 笔记本 db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, updates) // 更新笔记 q := bson.M{"UserId": bson.ObjectIdHex(userId), "NotebookId": bson.ObjectIdHex(notebookId)} data := bson.M{"IsBlog": isBlog} if isBlog { data["PublicTime"] = time.Now() } else { data["HasSelfDefined"] = false } // usn data["Usn"] = userService.IncrUsn(userId) db.UpdateByQMap(db.Notes, q, data) // noteContents也更新, 这个就麻烦了, noteContents表没有NotebookId // 先查该notebook下所有notes, 得到id notes := []info.Note{} db.ListByQWithFields(db.Notes, q, []string{"_id"}, ¬es) if len(notes) > 0 { noteIds := make([]bson.ObjectId, len(notes)) for i, each := range notes { noteIds[i] = each.NoteId } db.UpdateByQMap(db.NoteContents, bson.M{"_id": bson.M{"$in": noteIds}}, bson.M{"IsBlog": isBlog}) } // 重新计算tags go (func() { blogService.ReCountBlogTags(userId) })() return true }
// 删除trash func (this *TrashService) DeleteTrash(noteId, userId string) bool { note := noteService.GetNote(noteId, userId) if note.NoteId == "" { return false } // delete note's attachs ok := attachService.DeleteAllAttachs(noteId, userId) // 设置删除位 ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{"IsDeleted": true, "Usn": userService.IncrUsn(userId)}) // delete note // ok = db.DeleteByIdAndUserId(db.Notes, noteId, userId) // delete content ok = db.DeleteByIdAndUserId(db.NoteContents, noteId, userId) // 重新统计tag's count // TODO 这里会改变tag's Usn tagService.reCountTagCount(userId, note.Tags) return ok }
// 更新notebook func (this *NotebookService) UpdateNotebook(userId, notebookId string, needUpdate bson.M) bool { needUpdate["UpdatedTime"] = time.Now() needUpdate["Usn"] = userService.IncrUsn(userId) return db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, needUpdate) }
// 更新笔记本标题 // [ok] func (this *NotebookService) UpdateNotebookTitle(notebookId, userId, title string) bool { usn := userService.IncrUsn(userId) return db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"Title": title, "Usn": usn}) }
// 修改笔记 // 这里没有判断usn func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bson.M, usn int) (bool, string, int) { // 是否存在 note := this.GetNoteById(noteId) if note.NoteId == "" { return false, "notExists", 0 } userId := note.UserId.Hex() // updatedUserId 要修改userId的note, 此时需要判断是否有修改权限 if userId != updatedUserId { if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) { Log("NO AUTH2") return false, "noAuth", 0 } else { Log("HAS AUTH -----------") } } 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["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) // 重新发布成博客 if !note.IsBlog { needUpdate["PublicTime"] = needUpdate["UpdatedTime"] } } } 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"] if notebookIdI != nil { notebookId := notebookIdI.(bson.ObjectId) if notebookId != "" { notebookService.ReCountNotebookNumberNotes(note.NotebookId.Hex()) hasRecount = true notebookService.ReCountNotebookNumberNotes(notebookId.Hex()) } } // 不要多次更新, isTrash = false, = true都要重新统计 if !hasRecount { if _, ok := needUpdate["IsTrash"]; ok { notebookService.ReCountNotebookNumberNotes(note.NotebookId.Hex()) } } return true, "", afterUsn }
// 更新tags // [ok] [del] func (this *NoteService) UpdateTags(noteId string, userId string, tags []string) bool { return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{"Tags": tags, "Usn": userService.IncrUsn(userId)}) }
// 当设置/取消了笔记为博客 func (this *NoteService) UpdateNoteContentIsBlog(noteId, userId string, isBlog bool) { db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, bson.M{"IsBlog": isBlog}) }
// 附件修改, 增加noteIncr func (this *NoteService) IncrNoteUsn(noteId, userId string) int { afterUsn := userService.IncrUsn(userId) db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{"UpdatedTime": time.Now(), "Usn": afterUsn}) return afterUsn }