func GetUserTeamMsgList(uid uint64) ([]*TextTeamMsg, error) { msgList := []*TextTeamMsg{} msgIdList, err := RedisGetUserTeamMsgList(uid) if err != nil { return msgList, errors.As(err, uid) } for _, msgId := range msgIdList { msgBuf, err := RedisGetTeamMsg(msgId) if err != nil { return msgList, errors.As(err, msgId) } _, jsonBody, _, err := common.UnpackageData(msgBuf) if err != nil { return msgList, errors.As(err, uid) } msg := new(TextTeamMsg) if err := json.Unmarshal(jsonBody, msg); err != nil { return msgList, errors.As(err, string(jsonBody)) } msg.CmdType = common.DU_PUSH_CMD_IM_TEAM_MSG msgList = append(msgList, msg) } return msgList, nil }
func RedisAddFriend(uid, fid uint64, typ int) error { userKey := "" if err := RedisDeleteFriend(uid, fid, typ); err != nil { return errors.As(err, uid, fid, typ) } if typ == 1 { userKey = fmt.Sprintf("%s%d", SET_WHITELIST, uid) } else { userKey = fmt.Sprintf("%s%d", SET_BLACKLIST, uid) // 加入黑名单 删除白名单 if err := RedisDeleteFriend(uid, fid, 1); err != nil { return errors.As(err, uid, fid, typ) } } val := fmt.Sprintf("%d", fid) redis.RedisSAdd(userKey, val) // 双向好友 if typ == 1 { key_2 := fmt.Sprintf("%s%d", SET_WHITELIST, fid) uid_2 := fmt.Sprintf("%d", uid) redis.RedisSAdd(key_2, uid_2) } return nil }
func ConsumerGO(topic, channel string, goCount uint, handle Handler) (*ConsumerT, error) { msgHandle := &ConsumerT{ consumers: []*nsq.Consumer{}, handle: handle, msgCh: make(chan *Message, MSG_CHAN_SIZE), waitGroup: sync_.NewWaitGroup(), } addrSlice := strings.Split(g_nsqdMgr.addrs, ",") for _, addr := range addrSlice { for i := 0; i < CONSUMER_COUNT_PER_ADDR; i++ { consumer, err := nsq.NewConsumer(topic, channel, nsq.NewConfig()) if err != nil { return nil, errors.As(err, topic, channel) } consumer.SetLogger(nil, nsq.LogLevelInfo) consumer.AddHandler(msgHandle) if err := consumer.ConnectToNSQD(addr); err != nil { return nil, errors.As(err, topic, channel, g_nsqdMgr.addrs) } msgHandle.consumers = append(msgHandle.consumers, consumer) } } g_nsqdMgr.consumers = msgHandle for i := 0; i < int(goCount); i++ { go msgHandle.work() } return msgHandle, nil }
func (location *LocationInfo) NewLocation() error { session := common.MongoGet() defer common.MongoPut(session) c := session.DB("du").C("location") index := mgo.Index{ Key: []string{"$2d:loc"}, Bits: 26, Background: true, } if err := c.EnsureIndex(index); err != nil { return errors.As(err, index) } newLoc := bson.M{ "uid": location.Uid, "loc": location.Loc, "sendtime": location.SendTime, } if err := c.Insert(newLoc); err != nil { return errors.As(err, newLoc) } return nil }
func init() { if err := TeamMemberInit(); err != nil { panic(errors.As(err).Error()) } if err := TeamInfoInit(); err != nil { panic(errors.As(err).Error()) } }
func init() { if err := FriendInit(); err != nil { panic(errors.As(err).Error()) } if err := UserInfoInit(); err != nil { panic(errors.As(err).Error()) } }
func SetUserState(us *UserState) error { key := fmt.Sprintf("user_state_%v", us.Uid) body, err := json.Marshal(us) if err != nil { return errors.As(err, *us) } if err := redis_.Set(key, string(body)); err != nil { return errors.As(err, string(body)) } return nil }
func SetUserStateOnline(uid uint64, online bool) error { us, err := GetUserState(uid) if err != nil { return errors.As(err, uid, online) } us.Online = online if err := SetUserState(us); err != nil { return errors.As(err, uid, online) } return nil }
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(common.DATETIME_FMT) friend.UpdateDT = time.Now().Format(common.DATETIME_FMT) return errors.As(c.Insert(friend), *friend) }
func RedisQueryTeam(tid uint64) (*TeamInfo, error) { userKey := fmt.Sprintf("%s%d", KEY_TEAM_INFO, tid) val, err := redis.RedisGet(userKey) if err != nil { return nil, errors.As(err, tid) } team := &TeamInfo{} if err := json.Unmarshal([]byte(val), team); err != nil { return nil, errors.As(err, string(val)) } return team, nil }
func GetUserState(uid uint64) (*UserState, error) { us := &UserState{Uid: uid} key := fmt.Sprintf("user_state_%v", us.Uid) val, err := redis_.Get(key) if err != nil { if errors.ERR_NO_DATA.Equal(err) { return us, nil } return us, errors.As(err, key) } if err := json.Unmarshal([]byte(val), us); err != nil { return us, errors.As(err, val) } return us, nil }
func SetUserDetailInfo(sel, set map[string]interface{}) error { c := common.MongoCollection("dudb", "user_detail_info") if err := c.Update(bson.M(sel), bson.M{"$set": bson.M(set)}); err != nil { return errors.As(err, sel, set) } return nil }
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 RedisQueryFriendList(uid uint64, typ int) ([]uint64, error) { userKey := "" if typ == 1 { userKey = fmt.Sprintf("%s%d", SET_WHITELIST, uid) } else { userKey = fmt.Sprintf("%s%d", SET_BLACKLIST, uid) } strUidList, err := redis.SMembers(userKey).Result() if err != nil { return []uint64{}, errors.As(err, uid, typ) } if strUidList == nil || len(strUidList) < 1 { return []uint64{}, nil } uidList := make([]uint64, len(strUidList)) for i, val := range strUidList { uidList[i], _ = strconv.ParseUint(val, 10, 64) } return uidList, nil }
func ExecRet(sqlStr string, args ...interface{}) (uint64, error) { sqlDB := Get() defer Put(sqlDB) ret, err := sqlDB.Exec(sqlStr, args...) if err != nil { return 0, errors.As(err, sqlStr, args) } id, err := ret.LastInsertId() if err != nil { return 0, errors.As(err, sqlStr, args) } return uint64(id), nil }
func RedisAddUserTeamMsg(uids []uint64, msgId uint64, score float64) error { for _, uid := range uids { if uid <= 100000 { continue } userKey := fmt.Sprintf("%s%d", SET_TEAMMSGID, uid) cnt, err := redis.ZCard(userKey).Result() if err != nil { cnt = 0 } if int(cnt) >= common.MAX_TEAM_MSG_PER { redis.ZRemRangeByRank(userKey, 0, 0).Result() } val := fmt.Sprintf("%d", msgId) _, err = redis.ZAdd(userKey, redis.Z{score, val}).Result() if err != nil { return errors.As(err, uids, msgId, score) } } return nil }
func DeleteUserDetailInfoByUid(uid uint64) error { c := common.MongoCollection("dudb", "user_detail_info") if err := c.Remove(bson.M{"uid": uid}); err != nil { return errors.As(err, uid) } return nil }
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 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) }
func ModifyUserPwdByUid(uid uint64, pwd string) error { sel := map[string]interface{}{"uid": uid} set := map[string]interface{}{"password": pwd} if err := SetUserInfo(sel, set); err != nil { return errors.As(err, uid, pwd) } return 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 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 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 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 ModifyUserPwdByPhoneNum(phonenum, pwd string) error { sel := map[string]interface{}{"phonenum": phonenum} set := map[string]interface{}{"password": pwd} if err := SetUserInfo(sel, set); err != nil { return errors.As(err, phonenum, pwd) } 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 RedisGetTeamMsg(msgId uint64) ([]byte, error) { userKey := fmt.Sprintf("%s%d", KEY_TEAMMSGBUF, msgId) val, err := redis.RedisGet(userKey) if err != nil { return nil, errors.As(err, msgId) } return []byte(val), 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 }
//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 DBGetUidByToken(token string) (uint64, error) { var uid uint64 = 0 sqlStr := fmt.Sprintf("select uid from t_devicetoken where token = '%d'", token) rows, err := common.MysqlQuery(sqlStr) if err != nil { return 0, errors.As(err) } defer rows.Close() for rows.Next() { if err := rows.Scan(&uid); err != nil { return uid, errors.As(err, token) } } return uid, nil }