fmt all go file

This commit is contained in:
lealife
2015-11-13 17:58:41 +08:00
parent cba69444a8
commit 7809d00787
100 changed files with 3375 additions and 3331 deletions

View File

@ -1,12 +1,12 @@
package service
import (
"github.com/leanote/leanote/app/info"
"github.com/leanote/leanote/app/db"
"github.com/leanote/leanote/app/info"
. "github.com/leanote/leanote/app/lea"
"gopkg.in/mgo.v2/bson"
"time"
"strings"
"time"
)
type UserService struct {
@ -23,7 +23,7 @@ func (this *UserService) IncrUsn(userId string) int {
Log("inc Usn")
db.UpdateByQField(db.Users, query, "Usn", usn)
return usn
// return db.Update(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, bson.M{"$inc": bson.M{"ReadNum": 1}})
// return db.Update(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, bson.M{"$inc": bson.M{"ReadNum": 1}})
}
func (this *UserService) GetUsn(userId string) int {
@ -39,18 +39,18 @@ func (this *UserService) AddUser(user info.User) bool {
user.UserId = bson.NewObjectId()
}
user.CreatedTime = time.Now()
if user.Email != "" {
user.Email = strings.ToLower(user.Email)
// 发送验证邮箱
go func() {
emailService.RegisterSendActiveEmail(user, user.Email)
// 发送给我 life@leanote.com
emailService.SendEmail("life@leanote.com", "新增用户", "{header}用户名" + user.Email + "{footer}");
}();
emailService.SendEmail("life@leanote.com", "新增用户", "{header}用户名"+user.Email+"{footer}")
}()
}
return db.Insert(db.Users, user)
}
@ -94,11 +94,11 @@ func (this *UserService) GetUserInfoByAny(idEmailUsername string) info.User {
if IsObjectId(idEmailUsername) {
return this.GetUserInfo(idEmailUsername)
}
if strings.Contains(idEmailUsername, "@") {
return this.GetUserInfoByEmail(idEmailUsername)
}
// username
return this.GetUserInfoByUsername(idEmailUsername)
}
@ -111,7 +111,7 @@ func (this *UserService) setUserLogo(user *info.User) {
if user.Logo != "" && !strings.HasPrefix(user.Logo, "http") {
user.Logo = strings.Trim(user.Logo, "/")
user.Logo = configService.GetSiteUrl() + "/" + user.Logo
}
}
}
// 仅得到用户
@ -129,6 +129,7 @@ func (this *UserService) GetUserInfo(userId string) info.User {
this.setUserLogo(&user)
return user
}
// 得到用户信息 email
func (this *UserService) GetUserInfoByEmail(email string) info.User {
user := info.User{}
@ -137,6 +138,7 @@ func (this *UserService) GetUserInfoByEmail(email string) info.User {
this.setUserLogo(&user)
return user
}
// 得到用户信息 username
func (this *UserService) GetUserInfoByUsername(username string) info.User {
user := info.User{}
@ -163,11 +165,12 @@ func (this *UserService) ListUserInfosByEmails(emails []string) []info.User {
db.ListByQ(db.Users, bson.M{"Email": bson.M{"$in": emails}}, &users)
return users
}
// 用户信息即可
func (this *UserService) MapUserInfoByUserIds(userIds []bson.ObjectId) map[bson.ObjectId]info.User {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
userMap := make(map[bson.ObjectId]info.User, len(users))
for _, user := range users {
this.setUserLogo(&user)
@ -175,6 +178,7 @@ func (this *UserService) MapUserInfoByUserIds(userIds []bson.ObjectId) map[bson.
}
return userMap
}
// 用户信息和博客设置信息
func (this *UserService) MapUserInfoAndBlogInfosByUserIds(userIds []bson.ObjectId) map[bson.ObjectId]info.User {
return this.MapUserInfoByUserIds(userIds)
@ -184,33 +188,33 @@ func (this *UserService) MapUserInfoAndBlogInfosByUserIds(userIds []bson.ObjectI
func (this *UserService) MapUserAndBlogByUserIds(userIds []bson.ObjectId) map[string]info.UserAndBlog {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
userBlogs := []info.UserBlog{}
db.ListByQ(db.UserBlogs, bson.M{"_id": bson.M{"$in": userIds}}, &userBlogs)
userBlogMap := make(map[bson.ObjectId]info.UserBlog, len(userBlogs))
for _, user := range userBlogs {
userBlogMap[user.UserId] = user
}
userAndBlogMap := make(map[string]info.UserAndBlog, len(users))
for _, user := range users {
this.setUserLogo(&user)
userBlog, ok := userBlogMap[user.UserId]
if !ok {
continue
}
userAndBlogMap[user.UserId.Hex()] = info.UserAndBlog{
UserId: user.UserId,
Username: user.Username,
Email: user.Email,
Logo: user.Logo,
UserId: user.UserId,
Username: user.Username,
Email: user.Email,
Logo: user.Logo,
BlogTitle: userBlog.Title,
BlogLogo: userBlog.Logo,
BlogUrl: blogService.GetUserBlogUrl(&userBlog, user.Username),
BlogLogo: userBlog.Logo,
BlogUrl: blogService.GetUserBlogUrl(&userBlog, user.Username),
}
}
return userAndBlogMap
@ -220,11 +224,11 @@ func (this *UserService) MapUserAndBlogByUserIds(userIds []bson.ObjectId) map[st
func (this *UserService) GetUserAndBlogUrl(userId string) info.UserAndBlogUrl {
user := this.GetUserInfo(userId)
userBlog := blogService.GetUserBlog(userId)
blogUrls := blogService.GetBlogUrls(&userBlog, &user)
return info.UserAndBlogUrl{
User: user,
User: user,
BlogUrl: blogUrls.IndexUrl,
PostUrl: blogUrls.PostUrl,
}
@ -249,15 +253,15 @@ func (this *UserService) GetUserAndBlog(userId string) info.UserAndBlog {
// 通过ids得到users, 按id的顺序组织users
func (this *UserService) GetUserInfosOrderBySeq(userIds []bson.ObjectId) []info.User {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users);
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
usersMap := map[bson.ObjectId]info.User{}
for _, user := range users {
usersMap[user.UserId] = user
}
hasAppend := map[bson.ObjectId]bool{} // 为了防止userIds有重复的
users2 := []info.User{};
users2 := []info.User{}
for _, userId := range userIds {
if user, ok := usersMap[userId]; ok && !hasAppend[userId] {
hasAppend[userId] = true
@ -270,7 +274,7 @@ func (this *UserService) GetUserInfosOrderBySeq(userIds []bson.ObjectId) []info.
// 使用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)
@ -288,19 +292,19 @@ func (this *UserService) UpdateUsername(userId, username string) (bool, string)
}
usernameRaw := username // 原先的, 可能是同一个, 但有大小写
username = strings.ToLower(username)
// 先判断是否存在
userIdO := bson.ObjectIdHex(userId)
if db.Has(db.Users, bson.M{"Username": username, "_id": bson.M{"$ne": userIdO}}) {
return false, "usernameIsExisted"
}
ok := db.UpdateByQMap(db.Users, bson.M{"_id": userIdO}, bson.M{"Username": username, "UsernameRaw": usernameRaw})
return ok, ""
}
// 修改头像
func (this *UserService) UpdateAvatar(userId, avatarPath string) (bool) {
func (this *UserService) UpdateAvatar(userId, avatarPath string) bool {
userIdO := bson.ObjectIdHex(userId)
return db.UpdateByQField(db.Users, bson.M{"_id": userIdO}, "Logo", avatarPath)
}
@ -327,7 +331,7 @@ func (this *UserService) ResetPwd(adminUserId, userId, pwd string) (ok bool, msg
if configService.GetAdminUserId() != adminUserId {
return
}
passwd := GenPwd(pwd)
if passwd == "" {
return false, "GenerateHash error"
@ -337,24 +341,24 @@ func (this *UserService) ResetPwd(adminUserId, userId, pwd string) (ok bool, msg
}
// 修改主题
func (this *UserService) UpdateTheme(userId, theme string) (bool) {
func (this *UserService) UpdateTheme(userId, theme string) bool {
ok := db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Theme", theme)
return ok
}
// 帐户类型设置
func (this *UserService) UpdateAccount(userId, accountType string, accountStartTime, accountEndTime time.Time,
func (this *UserService) UpdateAccount(userId, accountType string, accountStartTime, accountEndTime time.Time,
maxImageNum, maxImageSize, maxAttachNum, maxAttachSize, maxPerAttachSize int) bool {
return db.UpdateByQI(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, info.UserAccount{
AccountType: accountType,
AccountStartTime: accountStartTime,
AccountEndTime: accountEndTime,
MaxImageNum: maxImageNum,
MaxImageSize: maxImageSize,
MaxAttachNum: maxAttachNum,
MaxAttachSize: maxAttachSize,
MaxPerAttachSize: maxPerAttachSize,
})
AccountType: accountType,
AccountStartTime: accountStartTime,
AccountEndTime: accountEndTime,
MaxImageNum: maxImageNum,
MaxImageSize: maxImageSize,
MaxAttachNum: maxAttachNum,
MaxAttachSize: maxAttachSize,
MaxPerAttachSize: maxPerAttachSize,
})
}
//---------------
@ -370,14 +374,14 @@ func (this *UserService) ActiveEmail(token string) (ok bool, msg, email string)
if userInfo.UserId == "" {
ok = false
msg = "不存在该用户"
return
return
}
// 修改之, 并将verified = true
ok = db.UpdateByQMap(db.Users, bson.M{"_id": userInfo.UserId}, bson.M{"Verified": true})
return
}
ok = false
msg = "该链接已过期"
return
@ -397,12 +401,12 @@ func (this *UserService) UpdateEmail(token string) (ok bool, msg, email string)
msg = "该邮箱已注册"
return
}
// 修改之, 并将verified = true
ok = db.UpdateByQMap(db.Users, bson.M{"_id": tokenInfo.UserId}, bson.M{"Email": email, "Verified": true})
return
}
ok = false
msg = "该链接已过期"
return
@ -417,13 +421,13 @@ func (this *UserService) ThirdAddUser(userId, email, pwd string) (ok bool, msg s
msg = "你已有帐户"
return
}
// 判断email是否存在
if this.IsExistsUser(email) {
msg = "该用户已存在"
return
}
ok = db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"Email": email, "Pwd": Md5(pwd)})
return
}
@ -432,12 +436,13 @@ func (this *UserService) ThirdAddUser(userId, email, pwd string) (ok bool, msg s
// 偏好设置
// 宽度
func (this *UserService)UpdateColumnWidth(userId string, notebookWidth, noteListWidth, mdEditorWidth int) bool {
return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)},
bson.M{"NotebookWidth": notebookWidth, "NoteListWidth": noteListWidth, "MdEditorWidth": mdEditorWidth})
func (this *UserService) UpdateColumnWidth(userId string, notebookWidth, noteListWidth, mdEditorWidth int) bool {
return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)},
bson.M{"NotebookWidth": notebookWidth, "NoteListWidth": noteListWidth, "MdEditorWidth": mdEditorWidth})
}
// 左侧是否隐藏
func (this *UserService)UpdateLeftIsMin(userId string, leftIsMin bool) bool {
func (this *UserService) UpdateLeftIsMin(userId string, leftIsMin bool) bool {
return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"LeftIsMin": leftIsMin})
}
@ -454,7 +459,7 @@ func (this *UserService) ListUsers(pageNumber, pageSize int, sortField string, i
}
query["$or"] = orQ
}
q := db.Users.Find(query);
q := db.Users.Find(query)
// 总记录数
count, _ := q.Count()
// 列表
@ -468,44 +473,44 @@ func (this *UserService) ListUsers(pageNumber, pageSize int, sortField string, i
func (this *UserService) GetAllUserByFilter(userFilterEmail, userFilterWhiteList, userFilterBlackList string, verified bool) []info.User {
query := bson.M{}
if verified {
query["Verified"] = true
}
orQ := []bson.M{}
if userFilterEmail != "" {
orQ = append(orQ, bson.M{"Email": bson.M{"$regex": bson.RegEx{".*?" + userFilterEmail + ".*", "i"}}},
orQ = append(orQ, bson.M{"Email": bson.M{"$regex": bson.RegEx{".*?" + userFilterEmail + ".*", "i"}}},
bson.M{"Username": bson.M{"$regex": bson.RegEx{".*?" + userFilterEmail + ".*", "i"}}},
)
}
if(userFilterWhiteList != "") {
if userFilterWhiteList != "" {
userFilterWhiteList = strings.Replace(userFilterWhiteList, "\r", "", -1)
emails := strings.Split(userFilterWhiteList, "\n");
emails := strings.Split(userFilterWhiteList, "\n")
orQ = append(orQ, bson.M{"Email": bson.M{"$in": emails}})
}
if len(orQ) > 0 {
query["$or"] = orQ
}
emailQ := bson.M{}
if(userFilterBlackList != "") {
if userFilterBlackList != "" {
userFilterWhiteList = strings.Replace(userFilterBlackList, "\r", "", -1)
bEmails := strings.Split(userFilterBlackList, "\n");
bEmails := strings.Split(userFilterBlackList, "\n")
emailQ["$nin"] = bEmails
query["Email"] = emailQ
}
LogJ(query)
users := []info.User{}
q := db.Users.Find(query);
q := db.Users.Find(query)
q.All(&users)
Log(len(users))
return users
}
// 统计
// 统计
func (this *UserService) CountUser() int {
return db.Count(db.Users, bson.M{})
}