func DeleteUserInfoByUid(uid uint64) error { c := common.MongoCollection(USER_DB, USER_INFO_TABLE) if err := c.Remove(bson.M{"uid": uid}); err != nil { return errors.As(err, uid) } return nil }
func (msg *UserMsgItem) SafeMapTimeoutCall(key interface{}) { // 超时未删除则保存到db c := common.MongoCollection(MSG_DB, MSG_USER_MSG_TABLE) if err := c.Insert(msg); err != nil { syslog.Info(err, *msg) } }
func SaveUserMsgToDB(userMsg *UserMsgItem) { // 超时未删除则保存到db c := common.MongoCollection(MSG_DB, MSG_USER_MSG_TABLE) if err := c.Insert(userMsg); err != nil { syslog.Info(err, *userMsg) } }
func SetUserInfo(sel, set map[string]interface{}) error { c := common.MongoCollection(USER_DB, USER_INFO_TABLE) if err := c.Update(bson.M(sel), bson.M{"$set": bson.M(set)}); err != nil { return errors.As(err, sel, set) } return nil }
func SetTeamMember(sel, set map[string]interface{}) error { c := common.MongoCollection(TEAM_DB, TEAM_MEMBER_TABLE) if err := c.Update(bson.M(sel), bson.M{"$set": bson.M(set)}); err != nil { return errors.As(err, sel, set) } return nil }
func deleteFile(filelist []string) error { c := common.MongoCollection(FILE_DB, FILE_QINIU_FILE_TABLE) _, err := c.RemoveAll(bson.M{"filename": bson.M{"$in": filelist}}) if err != nil && err != mgo.ErrNotFound { return errors.As(err, filelist) } return nil }
func GetUserCount() (uint64, error) { c := common.MongoCollection(USER_DB, USER_INFO_TABLE) count, err := c.Find(nil).Count() if err != nil { return 0, errors.As(err) } return uint64(count), nil }
func GetTeamList(sel map[string]interface{}, start, count int) ([]TeamInfo, error) { c := common.MongoCollection(TEAM_DB, TEAM_INFO_TABLE) team_list := []TeamInfo{} err := c.Find(bson.M(sel)).Skip(start).Limit(count).All(&team_list) return team_list, errors.As(err, sel) }
func DeleteTeam(teamid uint64) error { c := common.MongoCollection(TEAM_DB, TEAM_INFO_TABLE) err := c.Remove(bson.M{"teamid": teamid}) if err != mgo.ErrNotFound { return errors.As(err, teamid) } return nil }
func UserMsgInit() error { c := common.MongoCollection(MSG_DB, MSG_USER_MSG_TABLE) index := mgo.Index{ Key: []string{"touid", "msgid"}, } return errors.As(c.EnsureIndex(index)) }
func DeleteTeamMember(teamid, uid uint64) error { c := common.MongoCollection(TEAM_DB, TEAM_MEMBER_TABLE) err := c.Remove(bson.M{"teamid": teamid, "uid": uid}) if err != mgo.ErrNotFound { return errors.As(err, uid, teamid) } return nil }
func DeleteFriend(uid, fid uint64) error { c := common.MongoCollection(USER_DB, USER_FRIEND_TABLE) err := c.Remove(bson.M{"fuid": fid, "uid": uid}) if err != mgo.ErrNotFound { return errors.As(err, fid) } return nil }
func GetUserInfo(sel map[string]interface{}) (*UserInfo, error) { c := common.MongoCollection(USER_DB, USER_INFO_TABLE) user := &UserInfo{} if err := c.Find(bson.M(sel)).One(user); err != nil { return nil, errors.As(err, sel) } return user, nil }
func addFile(file *File) error { c := common.MongoCollection(FILE_DB, FILE_QINIU_FILE_TABLE) sel := bson.M{"filename": file.Filename} set := bson.M{ "$set": bson.M{"expiredtime": file.ExpiredTime}, "$setOnInsert": bson.M{"createtime": file.CreateTime}, } _, err := c.Upsert(sel, set) return errors.As(err, *file) }
func FriendInit() error { c := common.MongoCollection(USER_DB, USER_FRIEND_TABLE) index := mgo.Index{ Key: []string{"uid", "fuid"}, Unique: true, DropDups: true, } return errors.As(c.EnsureIndex(index)) }
func TeamMemberInit() error { c := common.MongoCollection(TEAM_DB, TEAM_MEMBER_TABLE) index := mgo.Index{ Key: []string{"teamid", "uid"}, Unique: true, DropDups: true, } return errors.As(c.EnsureIndex(index)) }
func AddFriend(friend *Friend) error { //sel := map[string]interface{}{"uid":friend.Uid, "fuid":friend.FUid} //set := map[string]interface{}{"type":friend.Type} if err := DeleteFriend(friend.Uid, friend.FUid); err != nil { return errors.As(err, *friend) } c := common.MongoCollection(USER_DB, USER_FRIEND_TABLE) friend.CreateDT = time.Now().Format(protocol.DATETIME_FMT) friend.UpdateDT = time.Now().Format(protocol.DATETIME_FMT) return errors.As(c.Insert(friend), *friend) }
func GetNextUserId() (uint64, error) { c := common.MongoCollection(USER_DB, USER_INFO_TABLE) userInfo := &UserInfo{} if err := c.Find(nil).Sort("-uid").One(&userInfo); err != nil { if err == mgo.ErrNotFound { return uint64(MIN_USER_ID), nil } return 0, errors.As(err) } return userInfo.Uid + 1, nil }
func GetTeam(teamid uint64) *TeamInfo { c := common.MongoCollection(TEAM_DB, TEAM_INFO_TABLE) team := &TeamInfo{} iter := c.Find(bson.M{"teamid": teamid}).Iter() defer iter.Close() if iter.Next(team) { return team } return nil }
func GetTeamMember(teamid, uid uint64) *TeamMember { c := common.MongoCollection(TEAM_DB, TEAM_MEMBER_TABLE) tm := &TeamMember{} iter := c.Find(bson.M{"teamid": teamid, "uid": uid}).Iter() defer iter.Close() if iter.Next(tm) { return tm } return nil }
func GetFriendList(uid uint64, typ int) ([]uint64, error) { c := common.MongoCollection(USER_DB, USER_FRIEND_TABLE) friend := &Friend{} uids := []uint64{} iter := c.Find(bson.M{"uid": uid, "type": typ}).Iter() defer iter.Close() for iter.Next(friend) { uids = append(uids, friend.FUid) } return uids, nil }
func GetFriend(uid, fid uint64) *Friend { c := common.MongoCollection(USER_DB, USER_FRIEND_TABLE) friend := &Friend{} iter := c.Find(bson.M{"fuid": fid, "uid": uid}).Iter() defer iter.Close() if iter.Next(friend) { return friend } return nil }
func GetSysTeamList() ([]TeamInfo, error) { c := common.MongoCollection(TEAM_DB, TEAM_INFO_TABLE) teamList := []TeamInfo{} team := &TeamInfo{} iter := c.Find(bson.M{"teamtype": 1}).Iter() defer iter.Close() for iter.Next(team) { teamList = append(teamList, *team) } return teamList, nil }
func (this *FileMgr) RemoveExpiredFile() error { c := common.MongoCollection(FILE_DB, FILE_QINIU_FILE_TABLE) expiredTime := time.Now().Unix() iter := c.Find(bson.M{"expiredtime": bson.M{"$lt": expiredTime}}).Iter() defer iter.Close() file := &File{} keys := []string{} for iter.Next(file) { keys = append(keys, file.Filename) } QiniuDeleteFiles(keys) return nil }
func UserInfoInit() error { c := common.MongoCollection(USER_DB, USER_INFO_TABLE) index := mgo.Index{ Key: []string{"uid"}, Unique: true, DropDups: true, Background: true, } if err := c.EnsureIndex(index); err != nil { return errors.As(err, index) } return nil }
func GetTeamMemberList(teamid uint64) ([]uint64, error) { c := common.MongoCollection(TEAM_DB, TEAM_MEMBER_TABLE) tm := &TeamMember{} uids := []uint64{} iter := c.Find(bson.M{"teamid": teamid}).Iter() defer iter.Close() for iter.Next(tm) { uids = append(uids, tm.Uid) } return uids, nil }
func CreateTeam(team *TeamInfo) error { team_mutex_lock.Lock() defer team_mutex_lock.Unlock() c := common.MongoCollection(TEAM_DB, TEAM_INFO_TABLE) if team.TeamId == 0 { teamId, err := GetNextTeamId(team.Uid) if err != nil { return errors.As(err, *team) } team.TeamId = teamId } team.CreateDate = time.Now().Format(DATETIME_FMT) return errors.As(c.Insert(team), *team) }
//User System func SaveUserInfo(user *UserInfo) error { user_mutex_lock.Lock() defer user_mutex_lock.Unlock() c := common.MongoCollection(USER_DB, USER_INFO_TABLE) if user.Uid == 0 { uid, err := GetNextUserId() if err != nil { return errors.As(err, *user) } user.Uid = uid } if err := c.Insert(user); err != nil { return errors.As(err, *user) } return nil }
func DelUserMsg(uid, msgid uint64) error { // 在缓存就不操作数据库 fmt.Println() key := msgKey(uid, msgid) if msg := g_userMsgMgr.msgMap.Get(key); msg != nil { g_userMsgMgr.msgMap.Delete(key) return nil } // 不在缓存就删除数据库 g_userMsgMgr.msgMap.Delete(msgKey(uid, msgid)) c := common.MongoCollection(MSG_DB, MSG_USER_MSG_TABLE) err := c.Remove(bson.M{"msgid": msgid, "touid": uid}) if err != mgo.ErrNotFound { return errors.As(err, uid, msgid) } return nil }
func QiniuInit() error { c := common.MongoCollection(FILE_DB, FILE_QINIU_FILE_TABLE) index := mgo.Index{ Key: []string{"filename"}, Unique: true, DropDups: true, } g_file_mgr = &FileMgr{ fileCh: make(chan *File, 100), expiredTimer: time.Tick(time.Hour), waitGroup: sync.NewWaitGroup(), rsClient: rs.New(&digest.Mac{AccessKey: g_access_key, SecretKey: []byte(g_secret_key)}), } go g_file_mgr.run() return errors.As(c.EnsureIndex(index)) }