// 得到notebook的share信息
// TODO 这里必须要分页, 最多取100个用户; 限制
func (this *ShareService) ListNotebookShareUserInfo(notebookId, userId string) []info.ShareUserInfo {
	// notebook的shareNotebooks信息
	shareNotebooks := []info.ShareNotebook{}

	db.ListByQLimit(db.ShareNotebooks,
		bson.M{
			"NotebookId": bson.ObjectIdHex(notebookId),
			"UserId":     bson.ObjectIdHex(userId),
			"ToGroupId":  bson.M{"$exists": false},
		},
		&shareNotebooks, 100)

	if len(shareNotebooks) == 0 {
		return nil
	}

	// 得到用户信息
	toUserIds := make([]bson.ObjectId, len(shareNotebooks))
	for i, each := range shareNotebooks {
		toUserIds[i] = each.ToUserId
	}
	userInfos := userService.ListUserInfosByUserIds(toUserIds)

	if len(userInfos) == 0 {
		return nil
	}

	shareNotebooksMap := make(map[bson.ObjectId]info.ShareNotebook, len(shareNotebooks))
	for _, each := range shareNotebooks {
		shareNotebooksMap[each.ToUserId] = each
	}

	shareUserInfos := make([]info.ShareUserInfo, len(userInfos))
	for i, userInfo := range userInfos {
		shareUserInfos[i] = info.ShareUserInfo{ToUserId: userInfo.UserId,
			Email: userInfo.Email,
			Perm:  shareNotebooksMap[userInfo.UserId].Perm,
		}
	}

	return shareUserInfos
}
// 查看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
}