func (server *Server) sendMessageToUser(sender *user.User, receiver string, messageToSend message.ReceiveMessage) { receiverRoom := server.roomList.GetUsersRoom(receiver) receiverUser := receiverRoom.GetUser(receiver) if receiverUser != nil { messageToBroadcast := message.SendMessage{messageToSend.Content, sender.Login, messageToSend.Time, sender.GravatarLink} messageToBroadcast.DetectAndAddEmoticonsInMessage() receiverSocket := *receiverUser.Socket receiverSocket.Emit("mp", messageToBroadcast.String()) } }
func (room *Room) GetMessages() []message.SendMessage { messages := db.Db.GetElementsFromBucket(db.MessageBucketPrefix + room.Name) var messagesToSend []message.SendMessage for _, m := range messages { var mess message.SendMessage err := json.Unmarshal([]byte(m), &mess) if err != nil { logger.Error("Désérialisation d'un message", err) } mess.DetectAndAddEmoticonsInMessage() messagesToSend = append(messagesToSend, mess) } return messagesToSend }
// messageReception Réception d'un message par un client func (server *Server) messageReception(user *user.User, receivedMessage string) { socket := *user.Socket receivedMessageObject := message.GetMessageObject(receivedMessage) if receivedMessageObject.IsMp() { messageParts := strings.Split(receivedMessageObject.Content, " ") receiver := messageParts[1] if receiver == user.Login { reply := "On ne s'envoie pas de message à soi-même" socket.Emit("command", "{\"Content\": \""+reply+"\"}") } else { receiverRoom := server.roomList.GetUsersRoom(receiver) if receiverRoom == nil || receiverRoom.Name != user.Room { reply := "L'utilisateur " + receiver + " n'est pas dans la salle !" socket.Emit("command", "{\"Content\": \""+reply+"\"}") } else { receivedMessageObject.Content = strings.Join(messageParts[2:], " ") if !receivedMessageObject.IsEmpty() { server.sendMessageToUser(user, receiver, receivedMessageObject) } } } } else if receivedMessageObject.IsCommand() { commandResult := server.executeCommand(receivedMessageObject.Content) socket.Emit("command", "{\"Content\": \""+commandResult+"\"}") } else if !receivedMessageObject.IsEmpty() { messageToBroadcast := message.SendMessage{receivedMessageObject.Content, user.Login, receivedMessageObject.Time, user.GravatarLink} server.saveMessageInDb(messageToBroadcast, user.Room) messageToBroadcast.DetectAndAddEmoticonsInMessage() socket.Emit("message", messageToBroadcast.String()) socket.BroadcastTo(user.Room, "message", messageToBroadcast.String()) } }