Example #1
0
func SendMsgController(conn interface{}, msg *protocol.WMessage) {
	collectionUser := common.DBMongo.C("user")
	collection := common.DBMongo.C("message")

	//好友之间消息的转发

	//判断两者是不是好友
	n, err := collectionUser.Find(bson.M{"user_name": msg.UserInfo.GetUsername(), "friends.name": msg.SendMsg.GetReceiver()}).Count()
	if err != nil {
		common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 2)
		Logger.Error("send msg:", err)
		return
	}

	if n != 0 {
		nowTime := time.Now()
		log.Println("time:", nowTime)
		time := nowTime.Unix()
		msg.SendMsg.MsgTime = &time
		buffer, err := proto.Marshal(msg)
		if err != nil {
			common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 2)
			Logger.Warn("protobuf marshal failed: ", err)
			return
		}
		newbuffer := append(common.IntToBytes(len(buffer)), buffer...)
		if OnlineCheck(msg.SendMsg.GetReceiver()) {
			switch (UserMap[msg.SendMsg.GetReceiver()]).(type) {
			case net.Conn:
				_, err = (UserMap[msg.SendMsg.GetReceiver()]).(net.Conn).Write(newbuffer)
				common.CheckErr(err)
			case websocket.Conn:
				_, err = (UserMap[msg.SendMsg.GetReceiver()]).(*websocket.Conn).Write(newbuffer)
				common.CheckErr(err)
			}
		}
		common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 0)
		var message models.Message
		message.Id = bson.NewObjectId()
		message.Sender = msg.UserInfo.GetUsername()
		message.MsgType = int32(msg.SendMsg.GetType())
		message.Receiver = msg.SendMsg.GetReceiver()
		message.Msg = msg.SendMsg.GetMsg()
		message.MsgTime = nowTime

		err = collection.Insert(&message)
		if err != nil {
			Logger.Error("insert message: ", err)
			log.Println("message: ", err)
		}

	} else {
		common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 1)
	}
}
Example #2
0
func GroupSystemMsg(msg *protocol.WMessage, group *models.Group, num int) {
	collection := common.DBMongo.C("group")

	//获取所有在这个组的成员,并发送系统提示消息
	for _, v := range group.Members {
		var message models.Message

		//把系统消息加入到数据库
		message.Id = bson.NewObjectId()
		message.GroupId = msg.Group.GetId()
		message.Receiver = v.Name
		message.MsgType = 0

		//遍历到此用户时,发送回执,不发送下面的消息
		if v.Name == msg.UserInfo.GetUsername() && num != 2 {
			common.CommonResultReturn(UserMap[msg.UserInfo.GetUsername()], "groupExitResult", msg.GetMsgTypeId(), 0)
			continue
		}
		//如果num = 0,证明是管理员解散群
		if num == 0 {
			message.Msg = msg.UserInfo.GetUsername() + "管理员解散该群"
		} else if num == 1 {
			message.Msg = msg.UserInfo.GetUsername() + "退出该群"

		} else if num == 2 {
			//如果num=2,管理员踢人通知
			if v.Name == msg.UserInfo.GetUsername() {
				common.CommonResultReturn(UserMap[msg.UserInfo.GetUsername()], "groupKickResult", msg.GetMsgTypeId(), 0)
				continue
			}
			message.Msg = msg.Friends[0].GetUsername() + "被管理员请出群"
		}

		nowTime := time.Now()
		message.MsgTime = nowTime
		if err := collection.Insert(&message); err != nil {
			Logger.Error("insert group message: ", err)
			continue
		}

		//如果此成员在线,给其发送系统消息
		if OnlineCheck(v.Name) {
			sendMsg := &protocol.SendMessage{}
			sendMsg.Receiver = &v.Name
			typeSystem := protocol.Mode_SYSTEM
			sendMsg.Type = &typeSystem
			sendMsg.Msg = &message.Msg
			msgTime := nowTime.Unix()
			sendMsg.MsgTime = &msgTime

			if num == 2 {
				returnMsg := &protocol.WMessage{
					MsgType: proto.String("groupKickResult"),
					//MsgTypeId: proto.Int32(13),
					StataCode: proto.Int32(0),
					Group: &protocol.Groups{
						Id: proto.String(msg.Group.GetId()),
					},

					SendMsg: sendMsg,
				}

				buffer, err := proto.Marshal(returnMsg)
				if err != nil {
					Logger.Warn("protobuf marshal failed: ", err)
					return
				}
				newbuffer := append(common.IntToBytes(len(buffer)), buffer...)
				switch UserMap[v.Name].(type) {
				case net.Conn:
					_, err = (UserMap[v.Name]).(net.Conn).Write(newbuffer)
					common.CheckErr(err)
				case websocket.Conn:
					_, err = (UserMap[v.Name]).(*websocket.Conn).Write(newbuffer)
					common.CheckErr(err)
				}
			} else {

				returnMsg := &protocol.WMessage{
					MsgType: proto.String("groupExitResult"),
					//MsgTypeId: proto.Int32(13),
					StataCode: proto.Int32(0),
					Group: &protocol.Groups{
						Id: proto.String(msg.Group.GetId()),
					},

					SendMsg: sendMsg,
				}

				buffer, err := proto.Marshal(returnMsg)
				if err != nil {
					Logger.Warn("protobuf marshal failed: ", err)
					return
				}
				newbuffer := append(common.IntToBytes(len(buffer)), buffer...)
				switch UserMap[v.Name].(type) {
				case net.Conn:
					_, err = (UserMap[v.Name]).(net.Conn).Write(newbuffer)
					common.CheckErr(err)
				case websocket.Conn:
					_, err = (UserMap[v.Name]).(*websocket.Conn).Write(newbuffer)
					common.CheckErr(err)
				}
			}
		}
	}
}
Example #3
0
func GroupMsgController(conn interface{}, msg *protocol.WMessage) {
	collection := common.DBMongo.C("message")
	collectionGroup := common.DBMongo.C("group")
	var groupMsg models.Message
	var group models.Group
	nowTime := time.Now()

	//判断此成员是否在此群组中
	n, err := collectionGroup.Find(bson.M{"_id": msg.Group.GetId(), "members.name": msg.UserInfo.GetUsername()}).Count()
	if err != nil {
		common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 2)
		Logger.Error("count :", err)
		return
	}
	if n == 0 {
		//此用户不在此群组中
		common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 1)
		return
	}

	//保存聊天记录到数据库
	groupMsg.Id = bson.NewObjectId()
	groupMsg.Sender = msg.UserInfo.GetUsername()
	groupMsg.GroupId = msg.Group.GetId()
	groupMsg.MsgType = int32(msg.SendMsg.GetType())
	groupMsg.Msg = msg.SendMsg.GetMsg()
	groupMsg.MsgTime = nowTime

	err = collection.Insert(&groupMsg)
	if err != nil {
		Logger.Error("insert group msg:", err)
	}

	//转发群组信息给群内所有人
	//获取群组员
	err = collectionGroup.Find(bson.M{"_id": msg.Group.GetId()}).Select(bson.M{"members": true}).All(&group)
	if err != nil {
		Logger.Error("select group member: ", err)
		common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 3)
		return
	}
	common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 0)
	//发送人
	user := &protocol.User{}
	user.Username = &groupMsg.Sender

	for _, v := range group.Members {
		//把发送消息的用户排除
		if v.Name == msg.UserInfo.GetUsername() {
			continue
		}
		//把群聊信息保存到数据库
		var message models.Message
		message.Id = bson.NewObjectId()
		message.GroupId = msg.Group.GetId()
		message.Receiver = v.Name
		message.MsgType = int32(msg.SendMsg.GetType())
		message.MsgTime = nowTime
		if collection.Insert(&message) != nil {
			Logger.Error("insert group message:", err)
		}

		//如果此成员在线,给其发送消息
		if OnlineCheck(v.Name) {
			sendMsg := &protocol.SendMessage{}

			sendMsg.Receiver = &v.Name
			sendMsg.Type = msg.SendMsg.Type
			sendMsg.Msg = &groupMsg.Msg
			msgTime := nowTime.Unix()
			sendMsg.MsgTime = &msgTime

			returnMsg := &protocol.WMessage{
				MsgType: proto.String("groupMsgResult"),
				//MsgTypeId: proto.Int32(16),
				StataCode: proto.Int32(0),
				SendMsg:   sendMsg,
				Group: &protocol.Groups{
					Id: proto.String(msg.Group.GetId()),
				},
				UserInfo: user,
			}

			buffer, err := proto.Marshal(returnMsg)
			if err != nil {
				Logger.Warn("protobuf marshal failed: ", err)
				continue
			}
			newbuffer := append(common.IntToBytes(len(buffer)), buffer...)
			switch UserMap[v.Name].(type) {
			case net.Conn:
				_, err = (UserMap[v.Name]).(net.Conn).Write(newbuffer)
				common.CheckErr(err)
			case websocket.Conn:
				_, err = (UserMap[v.Name]).(*websocket.Conn).Write(newbuffer)
				common.CheckErr(err)
			}
		}
	}
}
Example #4
0
//发送好友请求
func SendFirendRequestController(conn interface{}, msg *protocol.WMessage) {
	collection := common.DBMongo.C("user")
	nowTime := time.Now()

	//判断需要添加的好友是否存在
	exist, err := collection.Find(bson.M{"user_name": msg.AddFriend.GetReceiver()}).Count()
	if err != nil {

		common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3)
		Logger.Error("get friend relation:", err)
		return
	}
	if exist != 0 {

		//验证是否已经是其好友
		n, err := collection.Find(bson.M{"user_name": msg.AddFriend.GetSender(), "friends.name": msg.AddFriend.GetReceiver(), "friends.mode": 3}).Count()
		if err != nil {
			common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3)
			Logger.Error("get friend relation:", err)
			return
		}

		//已经是好友关系
		if n != 0 {
			common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 2)
			return
		}

		//把添加好友信息保存到message表
		if int32(msg.AddFriend.GetModes()) == 2 {
			var saveMsg models.Message
			saveMsg.Id = bson.NewObjectId()
			saveMsg.Sender = msg.AddFriend.GetSender()
			saveMsg.Receiver = msg.AddFriend.GetReceiver()
			saveMsg.MsgType = int32(msg.AddFriend.GetModes())
			saveMsg.Msg = msg.UserInfo.GetUsername() + "请求添加您为好友"
			saveMsg.MsgTime = nowTime

			err = collection.Insert(&saveMsg)
			if err != nil {
				common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3)
				Logger.Error("add friend request insert database error:", err)
				return
			}

			//把添加好友的信息发给被添加人
			if OnlineCheck(msg.AddFriend.GetReceiver()) {
				num := nowTime.Unix()
				msg.SendMsg.MsgTime = &num
				buffer, err := proto.Marshal(msg)
				if err != nil {
					common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3)
					Logger.Warn("protobuf marshal failed: ", err)
					fmt.Println("failed: %s\n", err)
					//Log.SetLogger("file", `{"filename": "savelog.log"}`)
					return
				}
				length := len(buffer)
				newbuffer := append(common.IntToBytes(length), buffer...)
				switch (UserMap[msg.AddFriend.GetReceiver()]).(type) {
				case net.Conn:
					_, err = (UserMap[msg.AddFriend.GetReceiver()]).(net.Conn).Write(newbuffer)
					common.CheckErr(err)
				case websocket.Conn:
					_, err = (UserMap[msg.AddFriend.GetReceiver()]).(*websocket.Conn).Write(newbuffer)
					common.CheckErr(err)
				}
			}
			common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 0)
		} else {
			//发送的数据不正确
			common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 4)
		}

	} else {
		//被添加人不存在
		common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 1)
	}
}