// 得到用户信息 userId
func (this *UserService) GetUserInfo(userId string) info.User {
	user := info.User{}
	db.Get(db.Users, userId, &user)
	// Logo路径问题, 有些有http: 有些没有
	this.setUserLogo(&user)
	return user
}
// delete attach
func (this *AttachService) DeleteAttach(attachId, userId string) (bool, string) {
	attach := info.Attach{}
	db.Get(db.Attachs, attachId, &attach)

	if attach.AttachId != "" {
		// 判断是否有权限为笔记添加附件
		if !shareService.HasUpdateNotePerm(attach.NoteId.Hex(), userId) {
			return false, "No Perm"
		}

		if db.Delete(db.Attachs, bson.M{"_id": bson.ObjectIdHex(attachId)}) {
			this.updateNoteAttachNum(attach.NoteId, -1)
			attach.Path = strings.TrimLeft(attach.Path, "/")
			//modified by JacobXie
			var err error
			if qiniuService.IsUseQiniu() {
				err = qiniuService.DeleteOnQiniu(attach.Path)
			} else {
				err = os.Remove(revel.BasePath + "/" + attach.Path)
			}
			if err == nil {
				// userService.UpdateAttachSize(note.UserId.Hex(), -attach.Size)
				// 修改note Usn
				noteService.IncrNoteUsn(attach.NoteId.Hex(), userId)

				return true, "delete file success"
			}
			return false, "delete file error"
		}
		return false, "db error"
	}
	return false, "no such item"
}
// 获取文件路径
// 要判断是否具有权限
// userId是否具有attach的访问权限
func (this *AttachService) GetAttach(attachId, userId string) (attach info.Attach) {
	if attachId == "" {
		return
	}

	attach = info.Attach{}
	db.Get(db.Attachs, attachId, &attach)
	path := attach.Path
	if path == "" {
		return
	}

	note := noteService.GetNoteById(attach.NoteId.Hex())

	// 判断权限

	// 笔记是否是公开的
	if note.IsBlog {
		return
	}

	// 笔记是否是我的
	if note.UserId.Hex() == userId {
		return
	}

	// 我是否有权限查看或协作
	if shareService.HasReadNotePerm(attach.NoteId.Hex(), userId) {
		return
	}

	attach = info.Attach{}
	return
}
// 点赞/取消赞
func (this *BlogService) LikeComment(commentId, userId string) (ok bool, isILike bool, num int) {
	ok = false
	isILike = false
	num = 0
	comment := info.BlogComment{}

	db.Get(db.BlogComments, commentId, &comment)

	var n int
	if comment.LikeUserIds != nil && len(comment.LikeUserIds) > 0 && InArray(comment.LikeUserIds, userId) {
		n = -1
		// 从点赞名单删除
		db.Update(db.BlogComments, bson.M{"_id": bson.ObjectIdHex(commentId)},
			bson.M{"$pull": bson.M{"LikeUserIds": userId}})
		isILike = false
	} else {
		n = 1
		// 添加之
		db.Update(db.BlogComments, bson.M{"_id": bson.ObjectIdHex(commentId)},
			bson.M{"$push": bson.M{"LikeUserIds": userId}})
		isILike = true
	}

	if comment.LikeUserIds == nil {
		num = 0
	} else {
		num = len(comment.LikeUserIds) + n
	}

	ok = db.Update(db.BlogComments, bson.M{"_id": bson.ObjectIdHex(commentId)},
		bson.M{"$set": bson.M{"LikeNum": num}})

	return
}
func (this *NotebookService) GetNotebookByUserIdAndUrlTitle(userId, notebookIdOrUrlTitle string) info.Notebook {
	notebook := info.Notebook{}
	if IsObjectId(notebookIdOrUrlTitle) {
		db.Get(db.Notebooks, notebookIdOrUrlTitle, &notebook)
	} else {
		db.GetByQ(db.Notebooks, bson.M{"UserId": bson.ObjectIdHex(userId), "UrlTitle": encodeValue(notebookIdOrUrlTitle)}, &notebook)
	}
	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
}
// 得到共享的笔记内容
// 并返回笔记的权限!!!
func (this *ShareService) GetShareNoteContent(noteId, myUserId, sharedUserId string) (noteContent info.NoteContent) {
	noteContent = info.NoteContent{}
	// 是否单独共享了该notebook
	// 或者, 其notebook共享了我
	//	Log(this.HasSharedNote(noteId, myUserId))
	//	Log(this.HasSharedNotebook(noteId, myUserId, sharedUserId))
	Log(this.HasReadPerm(sharedUserId, myUserId, noteId))
	if this.HasReadPerm(sharedUserId, myUserId, noteId) {
		//	if this.HasSharedNote(noteId, myUserId) || this.HasSharedNotebook(noteId, myUserId, sharedUserId) {
		db.Get(db.NoteContents, noteId, &noteContent)
	} else {
	}
	return
}
// 评论
// 在noteId博客下userId 给toUserId评论content
// commentId可为空(针对某条评论评论)
func (this *BlogService) Comment(noteId, toCommentId, userId, content string) (bool, info.BlogComment) {
	var comment info.BlogComment
	if content == "" {
		return false, comment
	}

	note := noteService.GetNoteById(noteId)
	if !note.IsBlog {
		return false, comment
	}

	comment = info.BlogComment{CommentId: bson.NewObjectId(),
		NoteId:      bson.ObjectIdHex(noteId),
		UserId:      bson.ObjectIdHex(userId),
		Content:     content,
		CreatedTime: time.Now(),
	}
	var comment2 = info.BlogComment{}
	if toCommentId != "" {
		comment2 = info.BlogComment{}
		db.Get(db.BlogComments, toCommentId, &comment2)
		if comment2.CommentId != "" {
			comment.ToCommentId = comment2.CommentId
			comment.ToUserId = comment2.UserId
		}
	} else {
		// comment.ToUserId = note.UserId
	}
	ok := db.Insert(db.BlogComments, comment)
	if ok {
		// 评论+1
		db.Update(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, bson.M{"$inc": bson.M{"CommentNum": 1}})
	}

	if userId != note.UserId.Hex() || toCommentId != "" {
		go func() {
			this.sendEmail(note, comment2, userId, content)
		}()
	}

	return ok, comment
}
// 作者(或管理员)可以删除所有评论
// 自己可以删除评论
func (this *BlogService) DeleteComment(noteId, commentId, userId string) bool {
	note := noteService.GetNoteById(noteId)
	if !note.IsBlog {
		return false
	}

	comment := info.BlogComment{}
	db.Get(db.BlogComments, commentId, &comment)

	if comment.CommentId == "" {
		return false
	}

	if userId == configService.GetAdminUserId() || note.UserId.Hex() == userId || comment.UserId.Hex() == userId {
		if db.Delete(db.BlogComments, bson.M{"_id": bson.ObjectIdHex(commentId)}) {
			// 评论-1
			db.Update(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, bson.M{"$inc": bson.M{"CommentNum": -1}})
			return true
		}
	}

	return false
}
// 仅得到用户
func (this *UserService) GetUser(userId string) info.User {
	user := info.User{}
	db.Get(db.Users, userId, &user)
	return user
}
func (this *NotebookService) GetNotebookById(notebookId string) info.Notebook {
	notebook := info.Notebook{}
	db.Get(db.Notebooks, notebookId, &notebook)
	return notebook
}
// 获取文件路径
// 要判断是否具有权限
// userId是否具有fileId的访问权限
func (this *FileService) GetFile(userId, fileId string) string {
	if fileId == "" {
		return ""
	}

	file := info.File{}
	db.Get(db.Files, fileId, &file)
	path := file.Path
	if path == "" {
		return ""
	}

	// 1. 判断权限

	// 是否是我的文件
	if userId != "" && file.UserId.Hex() == userId {
		return path
	}

	// 得到使用过该fileId的所有笔记NoteId
	// 这些笔记是否有public的, 若有则ok
	// 这些笔记(笔记本)是否有共享给我的, 若有则ok

	noteIds := noteImageService.GetNoteIds(fileId)
	if noteIds != nil && len(noteIds) > 0 {
		// 这些笔记是否有public的
		if db.Has(db.Notes, bson.M{"_id": bson.M{"$in": noteIds}, "IsBlog": true}) {
			return path
		}

		// 2014/12/28 修复, 如果是分享给用户组, 那就不行, 这里可以实现
		for _, noteId := range noteIds {
			note := noteService.GetNoteById(noteId.Hex())
			if shareService.HasReadPerm(note.UserId.Hex(), userId, noteId.Hex()) {
				return path
			}
		}
		/*
			// 若有共享给我的笔记?
			// 对该笔记可读?
			if db.Has(db.ShareNotes, bson.M{"ToUserId": bson.ObjectIdHex(userId), "NoteId": bson.M{"$in": noteIds}}) {
				return path
			}

			// 笔记本是否共享给我?
			// 通过笔记得到笔记本
			notes := []info.Note{}
			db.ListByQWithFields(db.Notes, bson.M{"_id": bson.M{"$in": noteIds}}, []string{"NotebookId"}, &notes)
			if notes != nil && len(notes) > 0 {
				notebookIds := make([]bson.ObjectId, len(notes))
				for i := 0; i < len(notes); i++ {
					notebookIds[i] = notes[i].NotebookId
				}

				if db.Has(db.ShareNotebooks, bson.M{"ToUserId": bson.ObjectIdHex(userId), "NotebookId": bson.M{"$in": notebookIds}}) {
					return path
				}
			}
		*/
	}

	// 可能是刚复制到owner上, 但内容又没有保存, 所以没有note->imageId的映射, 此时看是否有fromFileId
	if file.FromFileId != "" {
		fromFile := info.File{}
		db.Get2(db.Files, file.FromFileId, &fromFile)
		if fromFile.UserId.Hex() == userId {
			return fromFile.Path
		}
	}

	return ""
}
func (this *BlogService) GetSingle(singleId string) info.BlogSingle {
	page := info.BlogSingle{}
	db.Get(db.BlogSingles, singleId, &page)
	return page
}
func (this *BlogService) GetUserBlog(userId string) info.UserBlog {
	userBlog := info.UserBlog{}
	db.Get(db.UserBlogs, userId, &userBlog)
	this.fixUserBlog(&userBlog)
	return userBlog
}