diff --git a/app/service/GroupService.go b/app/service/GroupService.go index 89c370c..8d72478 100644 --- a/app/service/GroupService.go +++ b/app/service/GroupService.go @@ -61,6 +61,32 @@ func (this *GroupService) GetGroups(userId string) ([]info.Group) { db.ListByQ(db.Groups, bson.M{"UserId": bson.ObjectIdHex(userId)}, &groups) return groups } + + +// 获取包含此用户的组对象数组 +func (this *GroupService) GetGroupsContainOf(userId string) []info.Group { + groupIds := this.GetGroupIdsContainOf(userId) + groups := []info.Group{} + db.ListByQ(db.Groups, bson.M{"_id": bson.M{"$in": groupIds}}, &groups) + return groups +} + +// 获取包含此用户的组Id数组 +func (this *GroupService) GetGroupIdsContainOf(userId string) []bson.ObjectId { + 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 +} + // 得到分组, shareService用 func (this *GroupService) GetGroup(userId, groupId string) (info.Group) { // 得到分组s @@ -131,3 +157,8 @@ func (this *GroupService) DeleteUser(ownUserId, groupId, userId string) (ok bool } return db.Delete(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(userId)}), "" } + +// 判断组中是否包含指定用户 +func (this *GroupService) IsExistsGroupUser(userId, groupId string) (ok bool) { + return db.Has(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId), "GroupId": bson.ObjectIdHex(groupId)}) +} \ No newline at end of file diff --git a/app/service/ShareService.go b/app/service/ShareService.go index d1c05bb..15552ee 100644 --- a/app/service/ShareService.go +++ b/app/service/ShareService.go @@ -666,8 +666,7 @@ func (this *ShareService) HasReadNotePerm(noteId, userId string) bool { // 得到笔记分享给的groups func (this *ShareService) GetNoteShareGroups(noteId, userId string) []info.ShareNote { - // 得到分组s - groups := groupService.GetGroups(userId) + groups := groupService.GetGroupsContainOf(userId) // 得到有分享的分组 shares := []info.ShareNote{} @@ -694,9 +693,7 @@ func (this *ShareService) GetNoteShareGroups(noteId, userId string) []info.Share // 共享笔记给分组 func (this *ShareService) AddShareNoteGroup(userId, noteId, groupId string, perm int) (bool) { - // 得到group, 是否是我的group - group := groupService.GetGroup(userId, groupId) - if group.GroupId == "" { + if !groupService.IsExistsGroupUser(userId, groupId) { return false } @@ -724,8 +721,7 @@ func (this *ShareService) DeleteShareNoteGroup(userId, noteId, groupId string) b // 得到笔记本分享给的groups func (this *ShareService) GetNotebookShareGroups(notebookId, userId string) []info.ShareNotebook { - // 得到分组s - groups := groupService.GetGroups(userId) + groups := groupService.GetGroupsContainOf(userId) // 得到有分享的分组 shares := []info.ShareNotebook{} @@ -752,9 +748,7 @@ func (this *ShareService) GetNotebookShareGroups(notebookId, userId string) []in } // 共享笔记给分组 func (this *ShareService) AddShareNotebookGroup(userId, notebookId, groupId string, perm int) (bool) { - // 得到group, 是否是我的group - group := groupService.GetGroup(userId, groupId) - if group.GroupId == "" { + if !groupService.IsExistsGroupUser(userId, groupId) { return false }