Beispiel #1
0
func (h *Handler) SendMsg(head *common.PackageHead, jsonBody []byte, tail *common.PackageTail) ([]byte, error) {
	var req SendUserMsg
	if err := json.Unmarshal(jsonBody, &req); err != nil {
		return nil, errors.As(err, string(jsonBody))
	}

	var msg message.UserMsgItem
	msg.SendTime = uint32(time.Now().Unix())
	msg.MsgId = message.GetUserMsgID()
	msg.FromUid = tail.UID
	msg.ToUid = req.ToUid
	msg.Content = req.Content
	msg.Type = req.Type
	msg.ExtraData = req.ExtraData

	msgBody, err := json.Marshal(msg)
	if err != nil {
		return nil, errors.As(err, msg)
	}

	msgBuf := common.GetBuffer()
	defer common.PutBuffer(msgBuf)
	head.PkgLen = uint16(common.DATA_PACKAGE_HEAD_SIZE + len(jsonBody))
	if err := common.Package(msgBuf, head, msgBody, tail); err != nil {
		return nil, errors.As(err)
	}
	common.NsqPublish(common.MSG_CENTER_TOPIC, msgBuf.Bytes())
	message.SaveUserMsg(&msg)

	respStr := fmt.Sprintf(`{"msgid":%d,"sendtime":%d}`, msg.MsgId, msg.SendTime)
	return []byte(respStr), nil
}
Beispiel #2
0
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)
	g_redis_client.SAdd(userKey, val)

	// 双向好友
	if typ == 1 {
		key_2 := fmt.Sprintf("%s%d", SET_WHITELIST, fid)
		uid_2 := fmt.Sprintf("%d", uid)
		g_redis_client.SAdd(key_2, uid_2)
	}

	return nil
}
Beispiel #3
0
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)
}
Beispiel #4
0
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
}
Beispiel #5
0
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 := g_redis_client.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
}
Beispiel #6
0
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
}
Beispiel #7
0
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
}
Beispiel #8
0
func (c *Client) Response(msg *protocol.DataPackage) error {
	head := msg.Head
	tail := msg.Tail

	if (head.Command == protocol.CLIENT_CMD_USER_LOGIN || head.Command == protocol.CLIENT_CMD_USER_REGISTER) && tail.UID != 0 {
		c.Uid = tail.UID
		c.Stat = CLIENT_STATE_ONLINE
	}

	buf := protocol.GetBuffer()
	defer protocol.PutBuffer(buf)
	binary.Write(buf, binary.BigEndian, head)
	binary.Write(buf, binary.BigEndian, msg.BodyData())

	// write to Conn
	c.Conn.SetDeadline(time.Now().Add(NET_WRITE_DATELINE))
	if _, err := c.Conn.Write(buf.Bytes()); err != nil {
		if e, ok := err.(net.Error); ok && e.Timeout() {
			syslog.Info("write timeouat:", err, *c)
			return nil
		}
		syslog.Warn("write to conn fail :", err.Error(), msg.Tail)
		return errors.As(err)
	} else {
		syslog.Debug2("write to conn success:", head, string(msg.BodyData()), msg.Tail)
	}
	return nil
}
Beispiel #9
0
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)
}
Beispiel #10
0
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
}
Beispiel #11
0
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
}
Beispiel #12
0
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
}
Beispiel #13
0
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)
}
Beispiel #14
0
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
}
Beispiel #15
0
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
}
Beispiel #16
0
func (h *Handler) ReceivedUserMsg(head common.PackageHead, jsonBody []byte, tail *common.PackageTail) ([]byte, error) {
	var req RecvUserMsg
	if err := json.Unmarshal(jsonBody, &req); err != nil {
		return nil, errors.As(err, string(jsonBody))
	}

	message.DelUserMsg(tail.UID, req.MsgId)
	return nil, nil
}
Beispiel #17
0
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
}
Beispiel #18
0
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
}
Beispiel #19
0
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))
}
Beispiel #20
0
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
}
Beispiel #21
0
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
}
Beispiel #22
0
//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
}
Beispiel #23
0
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)
}
Beispiel #24
0
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))
}
Beispiel #25
0
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))
}
Beispiel #26
0
func (c *Client) Produce(msg *protocol.DataPackage) error {
	head := msg.Head
	buf := protocol.GetBuffer()
	defer protocol.PutBuffer(buf)
	if err := msg.Package(buf); err != nil {
		return errors.As(err)
	}

	if head.Command == protocol.CLIENT_CMD_SEND_USER_MSG || head.Command == protocol.CLIENT_CMD_RECV_USER_MSG {
		if err := common.NsqPublish(common.MSG_SERVER_TOPIC, buf.Bytes()); err != nil {
			return errors.As(err)
		}
		syslog.Debug("gateway --> msg_server publish message:", head, msg.Tail)
	} else if head.Command == protocol.CLIENT_CMD_USER_LOGIN || head.Command == protocol.CLIENT_CMD_USER_REGISTER {
		if err := common.NsqPublish(common.USER_CENTER_TOPIC, buf.Bytes()); err != nil {
			return errors.As(err)
		}
		syslog.Debug("gateway --> user_center publish message:", head, msg.Tail)
	}
	return nil
}
Beispiel #27
0
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
}
Beispiel #28
0
func RedisIsTeamMember(teamId, uid uint64) (bool, error) {
	userKey := fmt.Sprintf("%s%d", SET_TEAM_MEMBER, teamId)
	val := fmt.Sprintf("%d", uid)

	is, err := redis.SIsMember(userKey, val).Result()

	if err != nil {
		return false, errors.As(err, teamId, uid)
	}

	return is, nil
}
Beispiel #29
0
func RedisSetTeam(team *TeamInfo) error {
	userKey := fmt.Sprintf("%s%d", KEY_TEAM_INFO, team.TeamId)
	val, err := redis.RedisGet(userKey)
	if err != nil {
		return errors.As(err, *team)
	}

	var t TeamInfo
	err = json.Unmarshal([]byte(val), &t)
	if err != nil {
		return errors.As(err, *team)
	}

	if len(team.TeamName) > 1 {
		t.TeamName = team.TeamName
	}
	if len(team.CoreInfo) > 1 {
		t.CoreInfo = team.CoreInfo
	}

	if len(team.ExInfo) > 1 {
		t.ExInfo = team.ExInfo
	}

	b, err := json.Marshal(t)
	if err != nil {
		return errors.As(err, t)
	}
	userKey = fmt.Sprintf("%s%d", KEY_TEAM_INFO, team.TeamId)
	redis.RedisSet(userKey, string(b))

	//info version
	val = fmt.Sprintf("%d", time.Now().Unix())
	userKey = fmt.Sprintf("%s%d", KEY_TEAM_INFO_VER, team.TeamId)
	redis.RedisSet(userKey, val)

	return nil
}
Beispiel #30
0
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
}