예제 #1
0
        private void HandleDirectMessage(DirectMessageDto directMessageDto)
        {
            if (!AuthService.IsLoggedIn(_user))
            {
                Communication.SendError(_webSocket, "You must be logged in to send direct message");
                Log.Warning("Not logged in user tried to send direct message");
                return;
            }

            var receiver      = AuthService.GetUserFromUsername(directMessageDto.Receiver);
            var directMessage = new DirectMessage
            {
                CreatedAt = DateTimeOffset.Now,
                Sender    = _user.UserId,
                Receiver  = receiver.UserId,
                Text      = directMessageDto.Text
            };

            // Tries to save the direct message in the database
            if (MessageService.SaveDirectMessage(directMessage))
            {
                // Check if the user is currently connected to the server
                if (_connectedClient.ContainsKey(receiver))
                {
                    MessageService.SendDirectMessage(
                        directMessage,
                        _connectedClient[receiver]
                        );
                }

                Communication.SendSuccess(_webSocket);
            }
        }
        public static void SendDirectMessage(DirectMessage directMessage, WebSocket receiverWebsocket)
        {
            var receiver = AuthService.GetUserFromId(directMessage.Receiver);
            var sender   = AuthService.GetUserFromId(directMessage.Sender);
            var message  = new DirectMessageDto
            {
                Sender   = sender.Username,
                Receiver = receiver.Username,
                Text     = directMessage.Text
            };

            Communication.SendResponse(receiverWebsocket, message);
        }