// ToBlog or Not func (this *NotebookService) ToBlog(userId, notebookId string, isBlog bool) bool { // 笔记本 db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"IsBlog": isBlog}) // 更新笔记 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 } 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 }
// 更新notebook func (this *NotebookService) UpdateNotebook(userId, notebookId string, needUpdate bson.M) bool { needUpdate["UpdatedTime"] = time.Now() // 如果有IsBlog之类的, 需要特殊处理 if isBlog, ok := needUpdate["IsBlog"]; ok { // 设为blog/取消 if is, ok2 := isBlog.(bool); ok2 { q := bson.M{"UserId": bson.ObjectIdHex(userId), "NotebookId": bson.ObjectIdHex(notebookId)} db.UpdateByQMap(db.Notes, q, bson.M{"IsBlog": is}) // 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}) } } } return db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, needUpdate) }
// 更新模板内容 func (this *ThemeService) UpdateTplContent(userId, themeId, filename, content string) (ok bool, msg string) { basePath := this.GetThemeAbsolutePath(userId, themeId) path := basePath + "/" + filename if strings.Contains(filename, ".html") { Log(">>") if ok, msg = this.ValidateTheme(basePath, filename, content); ok { // 模板 if ok, msg = this.mustTpl(filename, content); ok { ok = PutFileStrContent(path, content) } } return } else if filename == "theme.json" { // 主题配置, 判断是否是正确的json theme, err := this.parseConfig(content) if err != nil { return false, fmt.Sprintf("%v", err) } // 正确, 更新theme信息 ok = db.UpdateByQMap(db.Themes, bson.M{"_id": bson.ObjectIdHex(themeId), "UserId": bson.ObjectIdHex(userId)}, bson.M{ "Name": theme.Name, "Version": theme.Version, "Author": theme.Author, "AuthorUrl": theme.AuthorUrl, "Info": theme.Info, }) if ok { ok = PutFileStrContent(path, content) } return } ok = PutFileStrContent(path, content) return }
//----------- // API func (this *SessionService) GetUserId(sessionId string) string { session := this.Get(sessionId) // 更新updateTime, 避免过期 db.UpdateByQMap(db.Sessions, bson.M{"SessionId": sessionId}, bson.M{"UpdatedTime": time.Now()}) return session.UserId }
// 推荐博客 func (this *BlogService) SetRecommend(noteId string, isRecommend bool) bool { data := bson.M{"IsRecommend": isRecommend} if isRecommend { data["RecommendTime"] = time.Now() } return db.UpdateByQMap(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId), "IsBlog": true}, data) }
// 删除标签 // 也删除所有的笔记含该标签的 // 返回noteId => usn func (this *TagService) DeleteTag(userId string, tag string) map[string]int { usn := userService.IncrUsn(userId) if db.UpdateByQMap(db.NoteTags, bson.M{"UserId": bson.ObjectIdHex(userId), "Tag": tag}, bson.M{"Usn": usn, "IsDeleted": true}) { return noteService.UpdateNoteToDeleteTag(userId, tag) } return map[string]int{} }
// 分页与排序 func (this *BlogService) UpdateUserBlogPaging(userId string, perPageSize int, sortField string, isAsc bool) (ok bool, msg string) { if ok, msg = Vds(map[string]string{"perPageSize": strconv.Itoa(perPageSize), "sortField": sortField}); !ok { return } ok = db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"PerPageSize": perPageSize, "SortField": sortField, "IsAsc": isAsc}) return }
//--------- // 第三方添加账号 func (this *UserService) ThirdAddUser(userId, email, pwd string) (ok bool, msg string) { // 判断email是否存在 if this.IsExistsUser(email) { msg = "该用户已存在" return } ok = db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"Email": email, "Pwd": Md5(pwd)}) return }
// 通用方法 func (this *ConfigService) updateGlobalConfig(userId, key string, value interface{}, isArr, isMap, isArrMap bool) bool { // 判断是否存在 if _, ok := this.GlobalAllConfigs[key]; !ok { // 需要添加 config := info.Config{ConfigId: bson.NewObjectId(), UserId: bson.ObjectIdHex(userId), Key: key, IsArr: isArr, IsMap: isMap, IsArrMap: isArrMap, UpdatedTime: time.Now(), } if isArr { v, _ := value.([]string) config.ValueArr = v this.GlobalArrayConfigs[key] = v } else if isMap { v, _ := value.(map[string]string) config.ValueMap = v this.GlobalMapConfigs[key] = v } else if isArrMap { v, _ := value.([]map[string]string) config.ValueArrMap = v this.GlobalArrMapConfigs[key] = v } else { v, _ := value.(string) config.ValueStr = v this.GlobalStringConfigs[key] = v } return db.Insert(db.Configs, config) } else { i := bson.M{"UpdatedTime": time.Now()} this.GlobalAllConfigs[key] = value if isArr { v, _ := value.([]string) i["ValueArr"] = v this.GlobalArrayConfigs[key] = v } else if isMap { v, _ := value.(map[string]string) i["ValueMap"] = v this.GlobalMapConfigs[key] = v } else if isArrMap { v, _ := value.([]map[string]string) i["ValueArrMap"] = v this.GlobalArrMapConfigs[key] = v } else { v, _ := value.(string) i["ValueStr"] = v this.GlobalStringConfigs[key] = v } return db.UpdateByQMap(db.Configs, bson.M{"UserId": bson.ObjectIdHex(userId), "Key": key}, i) } }
// 更新username func (this *UserService) UpdateUsername(userId, username string) (bool, string) { if userId == "" || username == "" || username == "admin" { // admin用户是内置的, 不能设置 return false, "usernameIsExisted" } usernameRaw := username // 原先的, 可能是同一个, 但有大小写 username = strings.ToLower(username) // 先判断是否存在 userIdO := bson.ObjectIdHex(userId) if db.Has(db.Users, bson.M{"Username": username, "_id": bson.M{"$ne": userIdO}}) { return false, "usernameIsExisted" } ok := db.UpdateByQMap(db.Users, bson.M{"_id": userIdO}, bson.M{"Username": username, "UsernameRaw": usernameRaw}) return ok, "" }
// 删除标签, 供API调用 func (this *TagService) DeleteTagApi(userId string, tag string, usn int) (ok bool, msg string, toUsn int) { noteTag := info.NoteTag{} db.GetByQ(db.NoteTags, bson.M{"UserId": bson.ObjectIdHex(userId), "Tag": tag}, ¬eTag) if noteTag.TagId == "" { return false, "notExists", 0 } if noteTag.Usn > usn { return false, "conflict", 0 } toUsn = userService.IncrUsn(userId) if db.UpdateByQMap(db.NoteTags, bson.M{"UserId": bson.ObjectIdHex(userId), "Tag": tag}, bson.M{"Usn": usn, "IsDeleted": true}) { return true, "", toUsn } return false, "", 0 }
// 查看是否有子notebook // 先查看该notebookId下是否有notes, 没有则删除 func (this *NotebookService) DeleteNotebook(userId, notebookId string) (bool, string) { if db.Count(db.Notebooks, bson.M{"ParentNotebookId": bson.ObjectIdHex(notebookId), "UserId": bson.ObjectIdHex(userId)}) == 0 { // 无 if db.Count(db.Notes, bson.M{"NotebookId": bson.ObjectIdHex(notebookId), "UserId": bson.ObjectIdHex(userId), "IsTrash": false}) == 0 { // 不包含trash // 不是真删除 1/20, 为了同步笔记本 ok := db.UpdateByQMap(db.Notebooks, bson.M{"_id": bson.ObjectIdHex(notebookId)}, bson.M{"IsDeleted": true, "Usn": userService.IncrUsn(userId)}) return ok, "" // return db.DeleteByIdAndUserId(db.Notebooks, notebookId, userId), "" } return false, "笔记本下有笔记" } else { return false, "笔记本下有子笔记本" } }
// 注册后验证邮箱 func (this *UserService) ActiveEmail(token string) (ok bool, msg, email string) { tokenInfo := info.Token{} if ok, msg, tokenInfo = tokenService.VerifyToken(token, info.TokenActiveEmail); ok { // 修改之后的邮箱 email = tokenInfo.Email userInfo := this.GetUserInfoByEmail(email) if userInfo.UserId == "" { ok = false msg = "不存在该用户" return } // 修改之, 并将verified = true ok = db.UpdateByQMap(db.Users, bson.M{"_id": userInfo.UserId}, bson.M{"Verified": true}) return } ok = false msg = "该链接已过期" return }
// 修改邮箱 // 在此之前, 验证token是否过期 // 验证email是否有人注册了 func (this *UserService) UpdateEmail(token string) (ok bool, msg, email string) { tokenInfo := info.Token{} if ok, msg, tokenInfo = tokenService.VerifyToken(token, info.TokenUpdateEmail); ok { // 修改之后的邮箱 email = tokenInfo.Email // 先验证该email是否被注册了 if userService.IsExistsUser(email) { ok = false msg = "该邮箱已注册" return } // 修改之, 并将verified = true ok = db.UpdateByQMap(db.Users, bson.M{"_id": tokenInfo.UserId}, bson.M{"Email": email, "Verified": true}) return } ok = false msg = "该链接已过期" return }
// 删除tag // 返回所有note的Usn func (this *NoteService) UpdateNoteToDeleteTag(userId string, targetTag string) map[string]int { query := bson.M{"UserId": bson.ObjectIdHex(userId), "Tags": bson.M{"$in": []string{targetTag}}} notes := []info.Note{} db.ListByQ(db.Notes, query, ¬es) ret := map[string]int{} for _, note := range notes { tags := note.Tags if tags == nil { continue } for i, tag := range tags { if tag == targetTag { tags = tags tags = append(tags[:i], tags[i+1:]...) break } } usn := userService.IncrUsn(userId) db.UpdateByQMap(db.Notes, bson.M{"_id": note.NoteId}, bson.M{"Usn": usn, "Tags": tags}) ret[note.NoteId.Hex()] = usn } return ret }
func (this *SessionService) Update(sessionId, key string, value interface{}) bool { return db.UpdateByQMap(db.Sessions, bson.M{"SessionId": sessionId}, bson.M{key: value, "UpdatedTime": time.Now()}) }
func (this *BlogService) UpdateUserBlogStyle(userId string, userBlog info.UserBlogStyle) bool { return db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)}, userBlog) }
// 修改之UserBlogBase func (this *BlogService) UpdateUserBlogBase(userId string, userBlog info.UserBlogBase) bool { ok := db.UpdateByQMap(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)}, userBlog) return ok }
// 左侧是否隐藏 func (this *UserService) UpdateLeftIsMin(userId string, leftIsMin bool) bool { return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"LeftIsMin": leftIsMin}) }
// 宽度 func (this *UserService) UpdateColumnWidth(userId string, notebookWidth, noteListWidth, mdEditorWidth int) bool { return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"NotebookWidth": notebookWidth, "NoteListWidth": noteListWidth, "MdEditorWidth": mdEditorWidth}) }