// 使用email(username), 得到用户信息 func (this *UserService) GetUserInfoByName(emailOrUsername string) info.User { emailOrUsername = strings.ToLower(emailOrUsername) user := info.User{} if strings.Contains(emailOrUsername, "@") { db.GetByQ(db.Users, bson.M{"Email": emailOrUsername}, &user) } else { db.GetByQ(db.Users, bson.M{"Username": emailOrUsername}, &user) } this.setUserLogo(&user) return user }
// 添加或更新标签, 先查下是否存在, 不存在则添加, 存在则更新 // 都要统计下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 }
// 得到用户信息 email func (this *UserService) GetUserInfoByEmail(email string) info.User { user := info.User{} db.GetByQ(db.Users, bson.M{"Email": email}, &user) // Logo路径问题, 有些有http: 有些没有 this.setUserLogo(&user) return user }
// 验证token, 是否存在, 过时? func (this *TokenService) VerifyToken(token string, tokenType int) (ok bool, msg string, tokenInfo info.Token) { overHours = this.GetOverHours(tokenType) ok = false if token == "" { msg = "不存在" return } db.GetByQ(db.Tokens, bson.M{"Token": token}, &tokenInfo) if tokenInfo.UserId == "" { msg = "不存在" return } // 验证是否过时 now := time.Now() duration := now.Sub(tokenInfo.CreatedTime) if duration.Hours() > overHours { msg = "过期" return } ok = true return }
// 得到用户信息 username func (this *UserService) GetUserInfoByUsername(username string) info.User { user := info.User{} username = strings.ToLower(username) db.GetByQ(db.Users, bson.M{"Username": username}, &user) // Logo路径问题, 有些有http: 有些没有 this.setUserLogo(&user) return user }
// 通过id或urlTitle得到博客 func (this *BlogService) GetBlogByIdAndUrlTitle(userId string, noteIdOrUrlTitle string) (blog info.BlogItem) { if IsObjectId(noteIdOrUrlTitle) { return this.GetBlog(noteIdOrUrlTitle) } note := info.Note{} db.GetByQ(db.Notes, bson.M{"UserId": bson.ObjectIdHex(userId), "UrlTitle": encodeValue(noteIdOrUrlTitle), "IsBlog": true, "IsTrash": false}, ¬e) return this.GetBlogItem(note) }
func (this *NotebookService) GetNotebookByUserIdAndUrlTitle(userId, notebookIdOrUrlTitle string) info.Notebook { notebook := info.Notebook{} if IsObjectId(notebookIdOrUrlTitle) { db.Get(db.Notebooks, notebookIdOrUrlTitle, ¬ebook) } else { db.GetByQ(db.Notebooks, bson.M{"UserId": bson.ObjectIdHex(userId), "UrlTitle": encodeValue(notebookIdOrUrlTitle)}, ¬ebook) } return notebook }
func (this *BlogService) GetSingleByUserIdAndUrlTitle(userId, singleIdOrUrlTitle string) info.BlogSingle { page := info.BlogSingle{} if IsObjectId(singleIdOrUrlTitle) { db.Get(db.BlogSingles, singleIdOrUrlTitle, &page) } else { db.GetByQ(db.BlogSingles, bson.M{"UserId": bson.ObjectIdHex(userId), "UrlTitle": encodeValue(singleIdOrUrlTitle)}, &page) } return page }
// 得到共享笔记本下的notes func (this *ShareService) ListShareNotesByNotebookId(notebookId, myUserId, sharedUserId string, page, pageSize int, sortField string, isAsc bool) []info.ShareNoteWithPerm { // 1 首先判断是否真的sharedUserId 共享了 notebookId 给 myUserId q := this.getOrQ(myUserId) q["NotebookId"] = bson.ObjectIdHex(notebookId) q["UserId"] = bson.ObjectIdHex(sharedUserId) shareNotebook := info.ShareNotebook{} db.GetByQ(db.ShareNotebooks, q, &shareNotebook) if shareNotebook.NotebookId == "" { return nil } perm := shareNotebook.Perm // 2 得到该notebook下分页显示所有的notes _, notes := noteService.ListUserNotes(sharedUserId, notebookId, false, page, pageSize, sortField, isAsc, false) // 3 添加权限信息 // 3.1 如果该notebook自己有其它权限信息, 比如1, 那么覆盖notebook的权限信息 noteIds := make([]bson.ObjectId, len(notes)) for i, note := range notes { noteIds[i] = note.NoteId } // 笔记的权限 shareNotes := []info.ShareNote{} delete(q, "NotebookId") q["NoteId"] = bson.M{"$in": noteIds} db.ShareNotes.Find(q).Sort("-ToUserId").All(&shareNotes) // 给个的权限>给组织的权限 notePerms := map[bson.ObjectId]int{} for _, each := range shareNotes { if _, ok := notePerms[each.NoteId]; !ok { notePerms[each.NoteId] = each.Perm } } Log("笔记权限") LogJ(notePerms) // 3.2 组合 notesWithPerm := make([]info.ShareNoteWithPerm, len(notes)) for i, each := range notes { thisPerm := perm if selfPerm, ok := notePerms[each.NoteId]; ok { thisPerm = selfPerm } notesWithPerm[i] = info.ShareNoteWithPerm{each, thisPerm} } return notesWithPerm }
// 得到主题信息, 为了给博客用 func (this *ThemeService) GetThemeInfo(themeId, style string) map[string]interface{} { q := bson.M{} if themeId == "" { if style == "" { style = defaultStyle } q["Style"] = style q["IsDefault"] = true } else { q["_id"] = bson.ObjectIdHex(themeId) } theme := info.Theme{} db.GetByQ(db.Themes, q, &theme) return theme.Info }
// 复制图片 func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, string) { // 是否已经复制过了 file2 := info.File{} db.GetByQ(db.Files, bson.M{"UserId": bson.ObjectIdHex(toUserId), "FromFileId": bson.ObjectIdHex(fileId)}, &file2) if file2.FileId != "" { return true, file2.FileId.Hex() } // 复制之 file := info.File{} db.GetByIdAndUserId(db.Files, fileId, userId, &file) if file.FileId == "" || file.UserId.Hex() != userId { return false, "" } _, ext := SplitFilename(file.Name) newFilename := NewGuid() + ext dir := "files/" + toUserId + "/images" filePath := dir + "/" + newFilename err := os.MkdirAll(dir, 0755) if err != nil { return false, "" } _, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath) if err != nil { Log(err) return false, "" } fileInfo := info.File{Name: newFilename, Title: file.Title, Path: filePath, Size: file.Size, FromFileId: file.FileId} id := bson.NewObjectId() fileInfo.FileId = id fileId = id.Hex() Ok, _ := this.AddImage(fileInfo, "", toUserId, false) if Ok { return Ok, id.Hex() } return false, "" }
// 删除标签, 供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 }
func (this *ThemeService) GetThemeById(themeId string) info.Theme { theme := info.Theme{} db.GetByQ(db.Themes, bson.M{"_id": bson.ObjectIdHex(themeId)}, &theme) return theme }
func (this *BlogService) GetUserBlogByDomain(domain string) info.UserBlog { blogUser := info.UserBlog{} db.GetByQ(db.UserBlogs, bson.M{"Domain": domain}, &blogUser) this.fixUserBlog(&blogUser) return blogUser }
// 得到blog, blogService用 // 不要传userId, 因为是公开的 func (this *NoteService) GetBlogNote(noteId string) (note info.Note) { note = info.Note{} db.GetByQ(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId), "IsBlog": true, "IsTrash": false}, ¬e) return }
// fileService调用 // 不能是已经删除了的, life bug, 客户端删除后, 竟然还能在web上打开 func (this *NoteService) GetNoteById(noteId string) (note info.Note) { note = info.Note{} db.GetByQ(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId), "IsDeleted": false}, ¬e) return }
func (this *UserService) GetUserInfoByThirdUserId(thirdUserId string) info.User { user := info.User{} db.GetByQ(db.Users, bson.M{"ThirdUserId": thirdUserId}, &user) this.setUserLogo(&user) return user }
// 通过email得到userId func (this *UserService) GetUserId(email string) string { email = strings.ToLower(email) user := info.User{} db.GetByQ(db.Users, bson.M{"Email": email}, &user) return user.UserId.Hex() }