Example #1
0
// 主题所属节点
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
}
Example #2
0
// 主题
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
}
Example #3
0
// 主题所属类型
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
}
Example #4
0
// 评论人
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
}
Example #5
0
// 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
}
Example #6
0
// 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
}
Example #7
0
// 用户的最近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
}
Example #8
0
// 用户发表的最近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
}
Example #9
0
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
}
Example #10
0
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
}
Example #11
0
// 是否有权删除评论,只允许管理员删除
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
}
Example #12
0
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
}
Example #13
0
// 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
}
Example #14
0
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
}
Example #15
0
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

}
Example #16
0
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
}
Example #17
0
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
}
Example #18
0
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
}
Example #19
0
// 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
}
Example #20
0
// 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
}
Example #21
0
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
}
Example #22
0
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")
	}
}
Example #23
0
// 是否有权编辑主题
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_
}
Example #24
0
// 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
}
Example #25
0
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
}
Example #26
0
// 只能收藏未收藏过的主题
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
}
Example #27
0
// 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)
	}
}
Example #28
0
// 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
}
Example #29
0
// 主题的最近的一个回复
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
}
Example #30
0
// 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)
}