// 复制图片, 把note的图片都copy给我, 且修改noteContent图片路径 func (this *NoteImageService) CopyNoteImages(fromNoteId, fromUserId, newNoteId, content, toUserId string) string { // 得到fromNoteId的noteImages, 如果为空, 则直接返回content noteImages := []info.NoteImage{} db.ListByQWithFields(db.NoteImages, bson.M{"NoteId": bson.ObjectIdHex(fromNoteId)}, []string{"ImageId"}, ¬eImages) if len(noteImages) == 0 { return content } // <img src="/file/outputImage?fileId=12323232" /> // 把fileId=1232替换成新的 replaceMap := map[string]string{} for _, noteImage := range noteImages { imageId := noteImage.ImageId.Hex() ok, newImageId := fileService.CopyImage(fromUserId, imageId, toUserId) if ok { replaceMap[imageId] = newImageId } } if len(replaceMap) > 0 { // 替换之 reg, _ := regexp.Compile("outputImage\\?fileId=([a-z0-9A-Z]{24})") content = reg.ReplaceAllStringFunc(content, func(each string) string { // each=outputImage?fileId=541bd2f599c37b4f3r000003 fileId := each[len(each)-24:] // 得到后24位, 也即id if replaceFileId, ok := replaceMap[fileId]; ok { return "outputImage?fileId=" + replaceFileId } return each }) } return content }
// 重新计算博客的标签 // 在设置设置/取消为博客时调用 func (this *BlogService) ReCountBlogTags(userId string) bool { // 得到所有博客 notes := []info.Note{} userIdO := bson.ObjectIdHex(userId) query := bson.M{"UserId": userIdO, "IsTrash": false, "IsBlog": true} db.ListByQWithFields(db.Notes, query, []string{"Tags"}, ¬es) db.DeleteAll(db.TagCounts, bson.M{"UserId": userIdO, "IsBlog": true}) if notes == nil || len(notes) == 0 { return true } // 统计所有的Tags和数目 tagsCount := map[string]int{} for _, note := range notes { tags := note.Tags if tags != nil && len(tags) > 0 { for _, tag := range tags { count := tagsCount[tag] count++ tagsCount[tag] = count } } } // 一个个插入 for tag, count := range tagsCount { db.Insert(db.TagCounts, info.TagCount{UserId: userIdO, IsBlog: true, Tag: tag, Count: count}) } return true }
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++ } }
// 得到我所属的所有分组ids func (this *GroupService) GetBelongToGroupIds(userId string) []bson.ObjectId { // 得到UserIds groupUsers := []info.GroupUser{} db.ListByQWithFields(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId)}, []string{"GroupId"}, &groupUsers) if len(groupUsers) == 0 { return nil } groupIds := make([]bson.ObjectId, len(groupUsers)) for i, each := range groupUsers { groupIds[i] = each.GroupId } return groupIds }
// 得到某分组下的用户 func (this *GroupService) GetUsers(groupId string) []info.User { // 得到UserIds groupUsers := []info.GroupUser{} db.ListByQWithFields(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId)}, []string{"UserId"}, &groupUsers) if len(groupUsers) == 0 { return nil } userIds := make([]bson.ObjectId, len(groupUsers)) for i, each := range groupUsers { userIds[i] = each.UserId } // 得到userInfos return userService.ListUserInfosByUserIds(userIds) }
// get all images names // for upgrade func (this *FileService) GetAllImageNamesMap(userId string) (m map[string]bool) { q := bson.M{"UserId": bson.ObjectIdHex(userId)} files := []info.File{} db.ListByQWithFields(db.Files, q, []string{"Name"}, &files) m = make(map[string]bool) if len(files) == 0 { return } for _, file := range files { m[file.Name] = true } return }
// 通过id, userId得到noteIds func (this *NoteImageService) GetNoteIds(imageId string) []bson.ObjectId { noteImages := []info.NoteImage{} db.ListByQWithFields(db.NoteImages, bson.M{"ImageId": bson.ObjectIdHex(imageId)}, []string{"NoteId"}, ¬eImages) if noteImages != nil && len(noteImages) > 0 { noteIds := make([]bson.ObjectId, len(noteImages)) cnt := len(noteImages) for i := 0; i < cnt; i++ { noteIds[i] = noteImages[i].NoteId } return noteIds } return nil }
// 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 }
func (this *NoteService) ListNoteContentByNoteIds(noteIds []bson.ObjectId) (notes []info.Note) { notes = []info.Note{} db.ListByQWithFields(db.Notes, bson.M{"_id": bson.M{"$in": noteIds}}, []string{"_id", "Abstract", "Content"}, ¬es) return }