// 主题所属节点 func (t *Topic) Node(db *mgo.Database) *Node { c := db.C(NODES) node := Node{} c.Find(bson.M{"_id": t.NodeId}).One(&node) return &node }
// 主题 func (c *Comment) Topic(db *mgo.Database) *Topic { // 内容 var topic Topic c_ := db.C(CONTENTS) c_.Find(bson.M{"_id": c.ContentId, "content.type": TypeTopic}).One(&topic) return &topic }
// 主题所属类型 func (a *Article) Category(db *mgo.Database) *ArticleCategory { c := db.C(ARTICLE_CATEGORIES) category := ArticleCategory{} c.Find(bson.M{"_id": a.CategoryId}).One(&category) return &category }
// 评论人 func (c *Comment) Creater(db *mgo.Database) *User { c_ := db.C(USERS) user := User{} c_.Find(bson.M{"_id": c.CreatedBy}).One(&user) return &user }
// IsEmailUsed returns true if the e-mail has been used. func IsEmailUsed(db *mgo.Database, email string) (bool, error) { if len(email) == 0 { return false, nil } c := db.C(USERS) count, err := c.Find(bson.M{"email": strings.ToLower(email)}).Count() return count > 0, err }
// IsUserExist checks if given user name exist, // the user name should be noncased unique. func IsUserExist(db *mgo.Database, name string) (bool, error) { if len(name) == 0 { return false, nil } c := db.C(USERS) count, err := c.Find(bson.M{"lowername": strings.ToLower(name)}).Count() return count > 0, err }
// 用户的最近10个回复 func (u *User) LatestReplies(db *mgo.Database) *[]Comment { c := db.C(COMMENTS) var replies []Comment c.Find(bson.M{"createdby": u.Id_, "type": TypeTopic}).Sort("-createdat").Limit(10).All(&replies) return &replies }
// 用户发表的最近10个主题 func (u *User) LatestTopics(db *mgo.Database) *[]Topic { c := db.C(CONTENTS) var topics []Topic c.Find(bson.M{"content.createdby": u.Id_, "content.type": TypeTopic}).Sort("-content.createdat").Limit(10).All(&topics) return &topics }
func (c *Content) Comments(db *mgo.Database) *[]Comment { c_ := db.C(COMMENTS) var comments []Comment c_.Find(bson.M{"contentid": c.Id_}).All(&comments) return &comments }
func GetUserByEmail(database *mgo.Database, email string) (user *User) { err := database.C("users").Find(bson.M{"e": email}).One(&user) if err != nil { glog.Warningf("Can't get user by email: %v", err) } return }
// 是否有权删除评论,只允许管理员删除 func (c *Comment) CanDelete(username string, db *mgo.Database) bool { var user User c_ := db.C(USERS) err := c_.Find(bson.M{"username": username}).One(&user) if err != nil { return false } return user.IsSuperuser }
func GetOauth2ById(db *mgo.Database, id bson.ObjectId) (oa *Oauth2, err error) { oa = new(Oauth2) c := db.C(OAUTH_2) err = c.FindId(id).One(oa) if err == mgo.ErrNotFound { return nil, ErrOauth2RecordNotExist } return }
// GetUsers returns given number of user objects with offset. func GetUsers(db *mgo.Database, num, offset int) ([]*User, error) { users := make([]*User, 0, num) c := db.C(USERS) err := c.Find(bson.M{"isactive", true}).Skip(offset).Limit(num).All(&users) if err == mgo.ErrNotFound { return nil, ErrUserNotExist } else if err != nil { return nil, err } return users, err }
func (c *Content) Updater(db *mgo.Database) *User { if c.UpdatedBy == "" { return nil } c_ := db.C(USERS) user := User{} c_.Find(bson.M{"_id": bson.ObjectIdHex(c.UpdatedBy)}).One(&user) return &user }
func (ct *CollectTopic) Topic(db *mgo.Database) *Topic { c := db.C(CONTENTS) var topic Topic err := c.Find(bson.M{"_id": bson.ObjectIdHex(ct.TopicId), "content.type": TypeTopic}).One(&topic) if err != nil { panic(err) return nil } return &topic }
func GetAuths(db *mgo.Database) ([]*LoginSource, error) { var auths = make([]*LoginSource, 0, 5) c := db.C(LOGIN_SOURCE) err := c.Find(nil).All(&auths) if err == mgo.ErrNotFound { return nil, ErrAuthenticationNotExist } else if err != nil { return nil, err } return auths, err }
func GetLoginSourceById(db *mgo.Database, id int64) (*LoginSource, error) { source := new(LoginSource) c := db.C(LOGIN_SOURCE) err := c.Find(bson.M{"id", id}).One(source) if err == mgo.ErrNotFound { return nil, ErrAuthenticationNotExist } else if err != nil { return nil, err } return source, nil }
func Login(c *mgo.Database, email string, password string) (user *models.User, err error) { err = c.C("users").Find(bson.M{"e": email}).One(&user) if err != nil { return } err = bcrypt.CompareHashAndPassword(user.Password, []byte(password)) if err != nil { user = nil } return }
// GetUserById returns the user object by given ID if exists. func GetUserById(db *mgo.Database, id bson.ObjectId) (*User, error) { u := new(User) c := db.C(USERS) err := c.FindId(id).One(u) if err == mgo.ErrNotFound { return nil, ErrUserNotExist } else if err != nil { return nil, err } return u, nil }
// Login validates and returns a user object if they exist in the database. func Login(d *mgo.Database, username, password string) (u *User, err error) { err = d.C("users").Find(bson.M{"email": username}).One(&u) if err != nil { return } err = bcrypt.CompareHashAndPassword(u.Password, []byte(password)) if err != nil { u = nil } return }
func DelLoginSource(db *mgo.Database, source *LoginSource) error { c := db.C(USERS) cnt, err := c.Find(bson.M{"loginsource": source.Id}).Count() if err != nil { return err } if cnt > 0 { return ErrAuthenticationUserUsed } c := db.C(LOGIN_SOURCE) err = c.RemoveId(source.Id_) return err }
func migrateToSuperGroup(db *mgo.Database, fromChatID int64, toChatID int64) { var chat chatData _, err := db.C("chats").FindId(fromChatID).Apply(mgo.Change{ Update: bson.M{"$set": bson.M{"migratedtochatid": toChatID}, "$unset": bson.M{"hooks": "", "membersids": ""}}, }, &chat) if err != nil { log.WithError(err).Error("migrateToSuperGroup remove") } if chat.ID != 0 { chat.ID = toChatID chat.Type = "supergroup" err := db.C("chats").Insert(chat) if err != nil { log.WithError(err).Error("migrateToSuperGroup insert") } } err = db.C("users").Update(bson.M{"hooks.chats": fromChatID}, bson.M{"$addToSet": bson.M{"hooks.$.chats": toChatID}}) if err != nil { log.WithError(err).Error("migrateToSuperGroup add new hook chats") } err = db.C("users").Update(bson.M{"hooks.chats": toChatID}, bson.M{"$pull": bson.M{"hooks.$.chats": fromChatID}}) if err != nil { log.WithError(err).Error("migrateToSuperGroup remove outdated hook chats") } }
// 是否有权编辑主题 func (c *Content) CanEdit(username string, db *mgo.Database) bool { var user User c_ := db.C(USERS) err := c_.Find(bson.M{"username": username}).One(&user) if err != nil { return false } if user.IsSuperuser { return true } return c.CreatedBy == user.Id_ }
// GetUserByName returns the user object by given name if exists. func GetUserByName(db *mgo.Database, name string) (*User, error) { if len(name) == 0 { return nil, ErrUserNotExist } u := new(User) c := db.C(USERS) err := c.Find(bson.M{"lowername": strings.ToLower(name)}).One(u) if err == mgo.ErrNotFound { return nil, ErrUserNotExist } else if err != nil { return nil, err } return u, nil }
func GetOauth2(db *mgo.Database, identity string) (oa *Oauth2, err error) { oa = new(Oauth2) c := db.C(OAUTH_2) err = c.Find(bson.M{"identity", identity}).One(oa) if err == mgo.ErrNotFound { return nil, ErrOauth2RecordNotExist } else if err != nil { return } else if oa.Uid == -1 { return oa, ErrOauth2NotAssociated } oa.User, err = GetUserById(oa.Uid) return oa, err }
// 只能收藏未收藏过的主题 func (c *Content) CanCollect(username string, db *mgo.Database) bool { var user User c_ := db.C(USERS) err := c_.Find(bson.M{"username": username}).One(&user) if err != nil { return false } has := false for _, v := range user.TopicsCollected { if v.TopicId == c.Id_.Hex() { has = true } } return !has }
// Ensure database indexes are respected for given mongo database func Index(db *mgo.Database) { if err := db.C("users").EnsureIndex(mgo.Index{ Key: []string{"email"}, Unique: true, }); err != nil { panic(err) } if err := db.C("accounts").EnsureIndex(mgo.Index{ Key: []string{"uid"}, Unique: true, }); err != nil { panic(err) } }
// GetOauthByUserId returns list of oauthes that are releated to given user. func GetOauthByUserId(db *mgo.Database, uid bson.ObjectId) ([]*Oauth2, error) { socials := make([]*Oauth2, 0, 5) c := db.C(OAUTH_2) err := c.Find(bson.M{"uid": uid}).All(socials) if err == mgo.ErrNotFound { return nil, ErrOauth2RecordNotExist } else if err != nil { return socials, err } for _, social := range socials { social.HasRecentActivity = social.Updated.Add(7 * 24 * time.Hour).After(time.Now()) } return socials, err }
// 主题的最近的一个回复 func (t *Topic) LatestReplier(db *mgo.Database) *User { if t.LatestReplierId == "" { return nil } c := db.C(USERS) user := User{} err := c.Find(bson.M{"_id": bson.ObjectIdHex(t.LatestReplierId)}).One(&user) if err != nil { return nil } return &user }
// CreateUser creates record of a new user. func CreateUser(db *mgo.Database, u *User) error { if !IsLegalName(u.Name) { return ErrUserNameIllegal } isExist, err := IsUserExist(u.Name) if err != nil { return err } else if isExist { return ErrUserAlreadyExist } isExist, err = IsEmailUsed(u.Email) if err != nil { return err } else if isExist { return ErrEmailAlreadyUsed } u.Id_ = bson.NewObjectId() u.LowerName = strings.ToLower(u.Name) u.Avatar = base.EncodeMd5(u.Email) u.AvatarEmail = u.Email u.Rands = GetUserSalt() u.Salt = GetUserSalt() u.EncodePasswd() lock.Lock() userIndex++ u.Index = userIndex lock.Unlock() if u.Index == 1 { u.IsAdmin = true u.IsActive = true } c := db.C(USERS) return c.Insert(u) }