// 返回info.UserAndBlog func (this *UserService) MapUserAndBlogByUserIds(userIds []bson.ObjectId) map[string]info.UserAndBlog { users := []info.User{} db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users) userBlogs := []info.UserBlog{} db.ListByQ(db.UserBlogs, bson.M{"_id": bson.M{"$in": userIds}}, &userBlogs) userBlogMap := make(map[bson.ObjectId]info.UserBlog, len(userBlogs)) for _, user := range userBlogs { userBlogMap[user.UserId] = user } userAndBlogMap := make(map[string]info.UserAndBlog, len(users)) for _, user := range users { this.setUserLogo(&user) userBlog, ok := userBlogMap[user.UserId] if !ok { continue } userAndBlogMap[user.UserId.Hex()] = info.UserAndBlog{ UserId: user.UserId, Username: user.Username, Email: user.Email, Logo: user.Logo, BlogTitle: userBlog.Title, BlogLogo: userBlog.Logo, BlogUrl: blogService.GetUserBlogUrl(&userBlog, user.Username), } } return userAndBlogMap }
// 得到笔记本分享给的groups func (this *ShareService) GetNotebookShareGroups(notebookId, userId string) []info.ShareNotebook { groups := groupService.GetGroupsContainOf(userId) // 得到有分享的分组 shares := []info.ShareNotebook{} db.ListByQ(db.ShareNotebooks, bson.M{"NotebookId": bson.ObjectIdHex(notebookId), "UserId": bson.ObjectIdHex(userId), "ToGroupId": bson.M{"$exists": true}}, &shares) mapShares := map[bson.ObjectId]info.ShareNotebook{} for _, share := range shares { mapShares[share.ToGroupId] = share } LogJ(shares) // 所有的groups都有share, 但没有share的group没有shareId shares2 := make([]info.ShareNotebook, len(groups)) for i, group := range groups { share, ok := mapShares[group.GroupId] if !ok { share = info.ShareNotebook{} } share.ToGroup = group shares2[i] = share } return shares2 }
// 复制笔记时需要复制附件 // noteService调用, 权限已判断 func (this *AttachService) CopyAttachs(noteId, toNoteId, toUserId string) bool { attachs := []info.Attach{} db.ListByQ(db.Attachs, bson.M{"NoteId": bson.ObjectIdHex(noteId)}, &attachs) // 复制之 toNoteIdO := bson.ObjectIdHex(toNoteId) for _, attach := range attachs { attach.AttachId = "" attach.NoteId = toNoteIdO // 文件复制一份 _, ext := SplitFilename(attach.Name) newFilename := NewGuid() + ext dir := "files/" + toUserId + "/attachs" filePath := dir + "/" + newFilename err := os.MkdirAll(revel.BasePath+"/"+dir, 0755) if err != nil { return false } _, err = CopyFile(revel.BasePath+"/"+attach.Path, revel.BasePath+"/"+filePath) if err != nil { return false } attach.Name = newFilename attach.Path = filePath this.AddAttach(attach, false) } return true }
// 得到用户共享的notebooks // 3/19 博客不是deleted func (this *BlogService) ListBlogNotebooks(userId string) []info.Notebook { notebooks := []info.Notebook{} orQ := []bson.M{ bson.M{"IsDeleted": false}, bson.M{"IsDeleted": bson.M{"$exists": false}}, } db.ListByQ(db.Notebooks, bson.M{"UserId": bson.ObjectIdHex(userId), "IsBlog": true, "$or": orQ}, ¬ebooks) return notebooks }
func (this *NoteImageService) getImagesByNoteIds(noteIds []bson.ObjectId) map[string][]info.File { noteNoteImages := []info.NoteImage{} db.ListByQ(db.NoteImages, bson.M{"NoteId": bson.M{"$in": noteIds}}, ¬eNoteImages) // 得到imageId, 再去files表查所有的Files imageIds := []bson.ObjectId{} // 图片1 => N notes imageIdNotes := map[string][]string{} // imageId => [noteId1, noteId2, ...] for _, noteImage := range noteNoteImages { imageId := noteImage.ImageId imageIds = append(imageIds, imageId) imageIdHex := imageId.Hex() noteId := noteImage.NoteId.Hex() if notes, ok := imageIdNotes[imageIdHex]; ok { imageIdNotes[imageIdHex] = append(notes, noteId) } else { imageIdNotes[imageIdHex] = []string{noteId} } } // 得到所有files files := []info.File{} db.ListByQ(db.Files, bson.M{"_id": bson.M{"$in": imageIds}}, &files) // 建立note->file关联 noteImages := make(map[string][]info.File) for _, file := range files { fileIdHex := file.FileId.Hex() // == imageId // 这个fileIdHex有哪些notes呢? if notes, ok := imageIdNotes[fileIdHex]; ok { for _, noteId := range notes { if files, ok2 := noteImages[noteId]; ok2 { noteImages[noteId] = append(files, file) } else { noteImages[noteId] = []info.File{file} } } } } return noteImages }
// 用户信息即可 func (this *UserService) MapUserInfoByUserIds(userIds []bson.ObjectId) map[bson.ObjectId]info.User { users := []info.User{} db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users) userMap := make(map[bson.ObjectId]info.User, len(users)) for _, user := range users { this.setUserLogo(&user) userMap[user.UserId] = user } return userMap }
// list attachs func (this *AttachService) ListAttachs(noteId, userId string) []info.Attach { attachs := []info.Attach{} // 判断是否有权限为笔记添加附件 if !shareService.HasUpdateNotePerm(noteId, userId) { return attachs } db.ListByQ(db.Attachs, bson.M{"NoteId": bson.ObjectIdHex(noteId)}, &attachs) return attachs }
// 添加了PublicTime, RecommendTime func (this *UpgradeService) UpgradeBlog() bool { notes := []info.Note{} db.ListByQ(db.Notes, bson.M{"IsBlog": true}, ¬es) // PublicTime, RecommendTime = UpdatedTime for _, note := range notes { if note.IsBlog && note.PublicTime.Year() < 100 { db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, bson.M{"PublicTime": note.UpdatedTime, "RecommendTime": note.UpdatedTime}) Log(note.NoteId.Hex()) } } return true }
// Delete note to delete attas firstly func (this *AttachService) DeleteAllAttachs(noteId, userId string) bool { note := noteService.GetNoteById(noteId) if note.UserId.Hex() == userId { attachs := []info.Attach{} db.ListByQ(db.Attachs, bson.M{"NoteId": bson.ObjectIdHex(noteId)}, &attachs) for _, attach := range attachs { attach.Path = strings.TrimLeft(attach.Path, "/") os.Remove(revel.BasePath + "/" + attach.Path) } return true } return false }
// api调用, 通过noteIds得到note's attachs, 通过noteId归类返回 func (this *AttachService) getAttachsByNoteIds(noteIds []bson.ObjectId) map[string][]info.Attach { attachs := []info.Attach{} db.ListByQ(db.Attachs, bson.M{"NoteId": bson.M{"$in": noteIds}}, &attachs) noteAttchs := make(map[string][]info.Attach) for _, attach := range attachs { noteId := attach.NoteId.Hex() if itAttachs, ok := noteAttchs[noteId]; ok { noteAttchs[noteId] = append(itAttachs, attach) } else { noteAttchs[noteId] = []info.Attach{attach} } } return noteAttchs }
// 通过ids得到users, 按id的顺序组织users func (this *UserService) GetUserInfosOrderBySeq(userIds []bson.ObjectId) []info.User { users := []info.User{} db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users) usersMap := map[bson.ObjectId]info.User{} for _, user := range users { usersMap[user.UserId] = user } hasAppend := map[bson.ObjectId]bool{} // 为了防止userIds有重复的 users2 := []info.User{} for _, userId := range userIds { if user, ok := usersMap[userId]; ok && !hasAppend[userId] { hasAppend[userId] = true users2 = append(users2, user) } } return users2 }
// appStart时 将全局的配置从数据库中得到作为全局 func (this *ConfigService) InitGlobalConfigs() bool { this.GlobalAllConfigs = map[string]interface{}{} this.GlobalStringConfigs = map[string]string{} this.GlobalArrayConfigs = map[string][]string{} this.GlobalMapConfigs = map[string]map[string]string{} this.GlobalArrMapConfigs = map[string][]map[string]string{} this.adminUsername, _ = revel.Config.String("adminUsername") if this.adminUsername == "" { this.adminUsername = "******" } this.siteUrl, _ = revel.Config.String("site.url") userInfo := userService.GetUserInfoByAny(this.adminUsername) if userInfo.UserId == "" { return false } this.adminUserId = userInfo.UserId.Hex() configs := []info.Config{} db.ListByQ(db.Configs, bson.M{"UserId": userInfo.UserId}, &configs) for _, config := range configs { if config.IsArr { this.GlobalArrayConfigs[config.Key] = config.ValueArr this.GlobalAllConfigs[config.Key] = config.ValueArr } else if config.IsMap { this.GlobalMapConfigs[config.Key] = config.ValueMap this.GlobalAllConfigs[config.Key] = config.ValueMap } else if config.IsArrMap { this.GlobalArrMapConfigs[config.Key] = config.ValueArrMap this.GlobalAllConfigs[config.Key] = config.ValueArrMap } else { this.GlobalStringConfigs[config.Key] = config.ValueStr this.GlobalAllConfigs[config.Key] = config.ValueStr } } return true }
// 获取包含此用户的组对象数组 // 获取该用户所属组, 和我的组 func (this *GroupService) GetGroupsContainOf(userId string) []info.Group { // 我的组 myGroups := this.GetGroups(userId) myGroupMap := map[bson.ObjectId]bool{} for _, group := range myGroups { myGroupMap[group.GroupId] = true } // 所属组 groupIds := this.GetBelongToGroupIds(userId) groups := []info.Group{} db.ListByQ(db.Groups, bson.M{"_id": bson.M{"$in": groupIds}}, &groups) for _, group := range groups { if !myGroupMap[group.GroupId] { myGroups = append(myGroups, group) } } return myGroups }
func (this *UpgradeService) moveTag() { usnI := 1 tags := []info.Tag{} db.ListByQ(db.Tags, bson.M{}, &tags) for _, eachTag := range tags { tagTitles := eachTag.Tags now := time.Now() if tagTitles != nil && len(tagTitles) > 0 { for _, tagTitle := range tagTitles { noteTag := info.NoteTag{} noteTag.TagId = bson.NewObjectId() noteTag.Count = 1 noteTag.Tag = tagTitle noteTag.UserId = eachTag.UserId noteTag.CreatedTime = now noteTag.UpdatedTime = now noteTag.Usn = usnI noteTag.IsDeleted = false db.Insert(db.NoteTags, noteTag) usnI++ } } } }
// 删除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 *GroupService) GetGroups(userId string) []info.Group { // 得到分组s groups := []info.Group{} db.ListByQ(db.Groups, bson.M{"UserId": bson.ObjectIdHex(userId)}, &groups) return groups }
// get albums func (this *AlbumService) GetAlbums(userId string) []info.Album { albums := []info.Album{} db.ListByQ(db.Albums, bson.M{"UserId": bson.ObjectIdHex(userId)}, &albums) return albums }
// 得到默认主题供选择 func (this *ThemeService) GetDefaultThemes() (themes []info.Theme) { themes = []info.Theme{} db.ListByQ(db.Themes, bson.M{"IsDefault": true}, &themes) return }
/* <li>Migrate "About me" to single(a single post)</li> <li>Add some default themes to administrator</li> <li>Generate "UrlTitle" for all notes. "UrlTitle" is a friendly url for post</li> <li>Generate "UrlTitle" for all notebooks</li> <li>Generate "UrlTitle" for all singles</li> */ func (this *UpgradeService) UpgradeBetaToBeta2(userId string) (ok bool, msg string) { if configService.GetGlobalStringConfig("UpgradeBetaToBeta2") != "" { return false, "Leanote have been upgraded" } // 1. aboutMe -> page userBlogs := []info.UserBlog{} db.ListByQ(db.UserBlogs, bson.M{}, &userBlogs) for _, userBlog := range userBlogs { blogService.AddOrUpdateSingle(userBlog.UserId.Hex(), "", "About Me", userBlog.AboutMe) } // 2. 默认主题, 给admin用户 themeService.UpgradeThemeBeta2() // 3. UrlTitles // 3.1 note notes := []info.Note{} db.ListByQ(db.Notes, bson.M{}, ¬es) for _, note := range notes { data := bson.M{} noteId := note.NoteId.Hex() // PublicTime, RecommendTime = UpdatedTime if note.IsBlog && note.PublicTime.Year() < 100 { data["PublicTime"] = note.UpdatedTime data["RecommendTime"] = note.UpdatedTime Log("Time " + noteId) } data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note") db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, data) Log(noteId) } // 3.2 Log("notebook") notebooks := []info.Notebook{} db.ListByQ(db.Notebooks, bson.M{}, ¬ebooks) for _, notebook := range notebooks { notebookId := notebook.NotebookId.Hex() data := bson.M{} data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook") db.UpdateByIdAndUserIdMap2(db.Notebooks, notebook.NotebookId, notebook.UserId, data) Log(notebookId) } // 3.3 single /* singles := []info.BlogSingle{} db.ListByQ(db.BlogSingles, bson.M{}, &singles) for _, single := range singles { singleId := single.SingleId.Hex() blogService.UpdateSingleUrlTitle(single.UserId.Hex(), singleId, single.Title) Log(singleId) } */ // 删除索引 db.ShareNotes.DropIndex("UserId", "ToUserId", "NoteId") ok = true msg = "success" configService.UpdateGlobalStringConfig(userId, "UpgradeBetaToBeta2", "1") return }
func (this *UserService) ListUserInfosByUserIds(userIds []bson.ObjectId) []info.User { users := []info.User{} db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users) return users }
func (this *UserService) ListUserInfosByEmails(emails []string) []info.User { users := []info.User{} db.ListByQ(db.Users, bson.M{"Email": bson.M{"$in": emails}}, &users) return users }
// 查看note的分享信息 // 分享给了哪些用户和权限 // ShareNotes表 userId = me, noteId = ... // 还要查看该note的notebookId分享的信息 func (this *ShareService) ListNoteShareUserInfo(noteId, userId string) []info.ShareUserInfo { // 得到shareNote信息, 得到所有的ToUserId shareNotes := []info.ShareNote{} db.ListByQLimit(db.ShareNotes, bson.M{ "NoteId": bson.ObjectIdHex(noteId), "UserId": bson.ObjectIdHex(userId), "ToGroupId": bson.M{"$exists": false}, }, &shareNotes, 100) // Log("<<>>>>") // Log(len(shareNotes)) if len(shareNotes) == 0 { return nil } shareNotesMap := make(map[bson.ObjectId]info.ShareNote, len(shareNotes)) for _, each := range shareNotes { shareNotesMap[each.ToUserId] = each } toUserIds := make([]bson.ObjectId, len(shareNotes)) for i, eachShareNote := range shareNotes { toUserIds[i] = eachShareNote.ToUserId } note := noteService.GetNote(noteId, userId) if note.NoteId == "" { return nil } // 查看其notebook的shareNotebooks信息 shareNotebooks := []info.ShareNotebook{} db.ListByQ(db.ShareNotebooks, bson.M{"NotebookId": note.NotebookId, "UserId": bson.ObjectIdHex(userId), "ToUserId": bson.M{"$in": toUserIds}}, &shareNotebooks) shareNotebooksMap := make(map[bson.ObjectId]info.ShareNotebook, len(shareNotebooks)) for _, each := range shareNotebooks { shareNotebooksMap[each.ToUserId] = each } // 得到用户信息 userInfos := userService.ListUserInfosByUserIds(toUserIds) if len(userInfos) == 0 { return nil } shareUserInfos := make([]info.ShareUserInfo, len(userInfos)) for i, userInfo := range userInfos { _, hasNotebook := shareNotebooksMap[userInfo.UserId] shareUserInfos[i] = info.ShareUserInfo{ToUserId: userInfo.UserId, Email: userInfo.Email, Perm: shareNotesMap[userInfo.UserId].Perm, NotebookHasShared: hasNotebook, } } return shareUserInfos }