2014-11-12 17:32:03 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/leanote/leanote/app/db"
|
2015-11-13 17:58:41 +08:00
|
|
|
"github.com/leanote/leanote/app/info"
|
|
|
|
// . "github.com/leanote/leanote/app/lea"
|
2014-11-12 17:32:03 +08:00
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
"time"
|
2015-11-13 17:58:41 +08:00
|
|
|
// "strings"
|
2014-11-12 17:32:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 用户组, 用户组用户管理
|
|
|
|
|
|
|
|
type GroupService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加分组
|
|
|
|
func (this *GroupService) AddGroup(userId, title string) (bool, info.Group) {
|
2015-11-13 17:58:41 +08:00
|
|
|
group := info.Group{
|
|
|
|
GroupId: bson.NewObjectId(),
|
|
|
|
UserId: bson.ObjectIdHex(userId),
|
|
|
|
Title: title,
|
2014-11-12 17:32:03 +08:00
|
|
|
CreatedTime: time.Now(),
|
|
|
|
}
|
|
|
|
return db.Insert(db.Groups, group), group
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 删除分组
|
|
|
|
// 判断是否有好友
|
|
|
|
func (this *GroupService) DeleteGroup(userId, groupId string) (ok bool, msg string) {
|
2014-12-09 23:17:36 +08:00
|
|
|
/*
|
2015-11-13 17:58:41 +08:00
|
|
|
if db.Has(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId)}) {
|
|
|
|
return false, "groupHasUsers"
|
|
|
|
}
|
2014-12-09 23:17:36 +08:00
|
|
|
*/
|
2015-06-15 18:01:48 +08:00
|
|
|
if !this.isMyGroup(userId, groupId) {
|
|
|
|
return false, "notMyGroup"
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
// 删除分组后, 需要删除所有用户分享到该组的笔记本, 笔记
|
2015-11-13 17:58:41 +08:00
|
|
|
|
|
|
|
shareService.DeleteAllShareNotebookGroup(groupId)
|
|
|
|
shareService.DeleteAllShareNoteGroup(groupId)
|
|
|
|
|
2014-12-09 23:17:36 +08:00
|
|
|
db.DeleteAll(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId)})
|
2014-11-12 17:32:03 +08:00
|
|
|
return db.DeleteByIdAndUserId(db.Groups, groupId, userId), ""
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// TODO 删除分组后, 在shareNote, shareNotebook中也要删除
|
|
|
|
}
|
2015-06-15 18:01:48 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 修改group标题
|
|
|
|
func (this *GroupService) UpdateGroupTitle(userId, groupId, title string) (ok bool) {
|
|
|
|
return db.UpdateByIdAndUserIdField(db.Groups, groupId, userId, "Title", title)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 得到用户的所有分组(包括下的所有用户)
|
2015-11-13 17:58:41 +08:00
|
|
|
func (this *GroupService) GetGroupsAndUsers(userId string) []info.Group {
|
|
|
|
/*
|
|
|
|
// 得到我的分组
|
|
|
|
groups := []info.Group{}
|
|
|
|
db.ListByQ(db.Groups, bson.M{"UserId": bson.ObjectIdHex(userId)}, &groups)
|
|
|
|
*/
|
2015-06-15 18:01:48 +08:00
|
|
|
// 我的分组, 及我所属的分组
|
2015-11-13 17:58:41 +08:00
|
|
|
groups := this.GetGroupsContainOf(userId)
|
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 得到其下的用户
|
|
|
|
for i, group := range groups {
|
|
|
|
group.Users = this.GetUsers(group.GroupId.Hex())
|
|
|
|
groups[i] = group
|
|
|
|
}
|
|
|
|
return groups
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 仅仅得到所有分组
|
2015-11-13 17:58:41 +08:00
|
|
|
func (this *GroupService) GetGroups(userId string) []info.Group {
|
2014-11-12 17:32:03 +08:00
|
|
|
// 得到分组s
|
|
|
|
groups := []info.Group{}
|
|
|
|
db.ListByQ(db.Groups, bson.M{"UserId": bson.ObjectIdHex(userId)}, &groups)
|
|
|
|
return groups
|
|
|
|
}
|
2015-03-30 20:19:48 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
// 得到我的和我所属组的ids
|
2015-11-13 17:58:41 +08:00
|
|
|
func (this *GroupService) GetMineAndBelongToGroupIds(userId string) []bson.ObjectId {
|
2015-06-15 18:01:48 +08:00
|
|
|
// 所属组
|
|
|
|
groupIds := this.GetBelongToGroupIds(userId)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
m := map[bson.ObjectId]bool{}
|
|
|
|
for _, groupId := range groupIds {
|
|
|
|
m[groupId] = true
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
// 我的组
|
|
|
|
myGroups := this.GetGroups(userId)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
for _, group := range myGroups {
|
|
|
|
if !m[group.GroupId] {
|
|
|
|
groupIds = append(groupIds, group.GroupId)
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
return groupIds
|
|
|
|
}
|
|
|
|
|
2015-03-30 20:19:48 +08:00
|
|
|
// 获取包含此用户的组对象数组
|
2015-05-21 20:49:58 +08:00
|
|
|
// 获取该用户所属组, 和我的组
|
2015-03-30 20:19:48 +08:00
|
|
|
func (this *GroupService) GetGroupsContainOf(userId string) []info.Group {
|
2015-05-21 20:49:58 +08:00
|
|
|
// 我的组
|
|
|
|
myGroups := this.GetGroups(userId)
|
|
|
|
myGroupMap := map[bson.ObjectId]bool{}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-05-21 20:49:58 +08:00
|
|
|
for _, group := range myGroups {
|
|
|
|
myGroupMap[group.GroupId] = true
|
2015-03-30 20:19:48 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-05-21 20:49:58 +08:00
|
|
|
// 所属组
|
|
|
|
groupIds := this.GetBelongToGroupIds(userId)
|
2015-03-30 20:19:48 +08:00
|
|
|
|
2015-05-21 20:49:58 +08:00
|
|
|
groups := []info.Group{}
|
|
|
|
db.ListByQ(db.Groups, bson.M{"_id": bson.M{"$in": groupIds}}, &groups)
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-05-21 20:49:58 +08:00
|
|
|
for _, group := range groups {
|
|
|
|
if !myGroupMap[group.GroupId] {
|
|
|
|
myGroups = append(myGroups, group)
|
|
|
|
}
|
2015-03-30 20:19:48 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-05-21 20:49:58 +08:00
|
|
|
return myGroups
|
2015-03-30 20:19:48 +08:00
|
|
|
}
|
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 得到分组, shareService用
|
2015-11-13 17:58:41 +08:00
|
|
|
func (this *GroupService) GetGroup(userId, groupId string) info.Group {
|
2014-11-12 17:32:03 +08:00
|
|
|
// 得到分组s
|
|
|
|
group := info.Group{}
|
|
|
|
db.GetByIdAndUserId(db.Groups, groupId, userId, &group)
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
// 得到某分组下的用户
|
2015-11-13 17:58:41 +08:00
|
|
|
func (this *GroupService) GetUsers(groupId string) []info.User {
|
2014-11-12 17:32:03 +08:00
|
|
|
// 得到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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 得到我所属的所有分组ids
|
2015-11-13 17:58:41 +08:00
|
|
|
func (this *GroupService) GetBelongToGroupIds(userId string) []bson.ObjectId {
|
2014-11-12 17:32:03 +08:00
|
|
|
// 得到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) isMyGroup(ownUserId, groupId string) (ok bool) {
|
|
|
|
return db.Has(db.Groups, bson.M{"_id": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(ownUserId)})
|
2015-06-15 18:01:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 判断组中是否包含指定用户
|
|
|
|
func (this *GroupService) IsExistsGroupUser(userId, groupId string) (ok bool) {
|
|
|
|
// 如果我拥有这个组, 那也行
|
|
|
|
if this.isMyGroup(userId, groupId) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return db.Has(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId), "GroupId": bson.ObjectIdHex(groupId)})
|
|
|
|
}
|
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 为group添加用户
|
|
|
|
// 用户是否已存在?
|
|
|
|
func (this *GroupService) AddUser(ownUserId, groupId, userId string) (ok bool, msg string) {
|
|
|
|
// groupId是否是ownUserId的?
|
2015-06-15 18:01:48 +08:00
|
|
|
/*
|
2015-11-13 17:58:41 +08:00
|
|
|
if !this.IsExistsGroupUser(ownUserId, groupId) {
|
2015-11-28 14:28:51 +08:00
|
|
|
return false, "forbiddenNotMyGroup"
|
2015-11-13 17:58:41 +08:00
|
|
|
}
|
2015-06-15 18:01:48 +08:00
|
|
|
*/
|
2014-11-12 17:32:03 +08:00
|
|
|
if !this.isMyGroup(ownUserId, groupId) {
|
2015-11-28 14:28:51 +08:00
|
|
|
return false, "forbiddenNotMyGroup"
|
2014-11-12 17:32:03 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 是否已存在
|
|
|
|
if db.Has(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(userId)}) {
|
2015-11-28 14:28:51 +08:00
|
|
|
return false, "userExistsInGroup"
|
2014-11-12 17:32:03 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
return db.Insert(db.GroupUsers, info.GroupUser{
|
|
|
|
GroupUserId: bson.NewObjectId(),
|
2015-11-13 17:58:41 +08:00
|
|
|
GroupId: bson.ObjectIdHex(groupId),
|
|
|
|
UserId: bson.ObjectIdHex(userId),
|
2014-11-12 17:32:03 +08:00
|
|
|
CreatedTime: time.Now(),
|
|
|
|
}), ""
|
|
|
|
}
|
2015-06-15 18:01:48 +08:00
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
// 删除用户
|
|
|
|
func (this *GroupService) DeleteUser(ownUserId, groupId, userId string) (ok bool, msg string) {
|
|
|
|
// groupId是否是ownUserId的?
|
2015-06-15 18:01:48 +08:00
|
|
|
/*
|
2015-11-13 17:58:41 +08:00
|
|
|
if !this.IsExistsGroupUser(ownUserId, groupId) {
|
2015-11-28 14:28:51 +08:00
|
|
|
return false, "forbiddenNotMyGroup"
|
2015-11-13 17:58:41 +08:00
|
|
|
}
|
2015-06-15 18:01:48 +08:00
|
|
|
*/
|
2014-11-12 17:32:03 +08:00
|
|
|
if !this.isMyGroup(ownUserId, groupId) {
|
2015-11-28 14:28:51 +08:00
|
|
|
return false, "forbiddenNotMyGroup"
|
2014-11-12 17:32:03 +08:00
|
|
|
}
|
2015-11-13 17:58:41 +08:00
|
|
|
|
2015-06-15 18:01:48 +08:00
|
|
|
// 删除该用户分享到本组的笔记本, 笔记
|
2015-11-13 17:58:41 +08:00
|
|
|
shareService.DeleteShareNotebookGroupWhenDeleteGroupUser(userId, groupId)
|
|
|
|
shareService.DeleteShareNoteGroupWhenDeleteGroupUser(userId, groupId)
|
|
|
|
|
2014-11-12 17:32:03 +08:00
|
|
|
return db.Delete(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(userId)}), ""
|
|
|
|
}
|