Esempio n. 1
0
        public async Task OnMessageReceived(IScenePeerClient client, string text, Action <ChatMessageDto, DestinationType> sender)
        {
            // Check if user is connected
            ChatUserInfoDto userInfo = null;

            _connectedUsers.TryGetValue(client, out userInfo);
            if (userInfo == null)
            {
                throw new ClientException($"User not found {client.Id} .");
            }

            // Store message in log
            ChatMessage messageLog = new ChatMessage
            {
                Channel  = client.SceneId,
                Date     = DateTime.UtcNow,
                Message  = text,
                Metadata = new JObject(),
                UserInfo = new UserInfo
                {
                    UserId = userInfo.UserId,
                    Data   = JObject.Parse(userInfo.Data)
                }
            };

            ChatMessageDto messageDto = new ChatMessageDto
            {
                Message   = text,
                TimeStamp = _env.Clock,
                UserInfo  = userInfo,
            };

            ChatMsgReceivedContext receiveCtx = new ChatMsgReceivedContext(messageLog, messageDto);

            receiveCtx.Destination = DestinationType.All;

            await _chatEventHandler?.RunEventHandler(eh =>
            {
                return(eh.OnMessageReceived(receiveCtx));
            }, ex =>
            {
                _log.Log(LogLevel.Error, _logCategory, "An error occured when the chat service check if the user is in jail.", ex);
                throw new ClientException($"An error occured when the chat service check if the user is in jail.");
            });

            _chatRepository.AddMessageLog(receiveCtx.Message);
            _messagesSource.OnNext(receiveCtx.Message);
            sender(receiveCtx.MessageDto, receiveCtx.Destination);
        }
Esempio n. 2
0
        public async Task <List <ChatMessageDto> > SeekHistory(string channel, string dateStart, string dateEnd)
        {
            List <ChatMessage>    messagesData = new List <ChatMessage>();
            List <ChatMessageDto> result       = new List <ChatMessageDto>();

            try
            {
                DateTime start = DateTime.ParseExact(dateStart, "ddMMyyyyHH:mm:ss", CultureInfo.InvariantCulture);
                DateTime end   = DateTime.ParseExact(dateEnd, "ddMMyyyyHH:mm:ss", CultureInfo.InvariantCulture);
                messagesData = await _chatRepository.SeekHistoryMessage(channel, start, end);
            }
            catch (ArgumentNullException argumentEx)
            {
                throw HttpHelper.HttpError(System.Net.HttpStatusCode.BadRequest, "Some argument are null");
            }
            catch (FormatException formatEx)
            {
                throw HttpHelper.HttpError(System.Net.HttpStatusCode.BadRequest, "Date format not supported");
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, _logCategory, "An error occured when seek history in chat repository", ex.Message);
                throw HttpHelper.HttpError(System.Net.HttpStatusCode.InternalServerError, "Server Internal error(s)");
            }

            if (result.Count == 0)
            {
                throw HttpHelper.HttpError(System.Net.HttpStatusCode.NoContent, $"No data found in request date range DateStart={dateStart}, DateEnd={dateEnd}");
            }
            else
            {
                foreach (ChatMessage msg in messagesData)
                {
                    var message = new ChatMessageDto
                    {
                        Message   = msg.Message,
                        TimeStamp = TimestampHelper.DateTimeToUnixTimeStamp(msg.Date),
                        UserInfo  = new ChatUserInfoDto
                        {
                            UserId = msg.UserInfo.UserId,
                            Data   = msg.UserInfo.Data.ToString(),
                        }
                    };
                    result.Add(message);
                }
                return(result);
            }
        }
Esempio n. 3
0
        public async Task LoadHistory(RequestContext <IScenePeerClient> ctx)
        {
            List <ChatMessageDto> result = new List <ChatMessageDto>();

            try
            {
                long startTimestamp = ctx.ReadObject <long>();
                long endTimestamp   = ctx.ReadObject <long>();

                DateTime start = TimestampHelper.UnixTimeStampSecondToDateTime(startTimestamp);
                DateTime end   = TimestampHelper.UnixTimeStampSecondToDateTime(endTimestamp);

                string channelName  = ctx.RemotePeer.SceneId;
                var    messagesData = await _chat.LoadHistory(channelName, start, end);

                foreach (ChatMessage msg in messagesData)
                {
                    var message = new ChatMessageDto {
                        Message   = msg.Message,
                        TimeStamp = TimestampHelper.DateTimeToUnixTimeStamp(msg.Date),
                        UserInfo  = new ChatUserInfoDto
                        {
                            UserId = msg.UserInfo.UserId,
                            Data   = msg.UserInfo.Data.ToString(),
                        }
                    };
                    result.Add(message);
                }
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, _logCategory, "Error occured when server try to load history", ex);
                throw new ClientException(ex.Message);
            }

            ctx.SendValue(result);
        }
Esempio n. 4
0
 internal ChatMsgReceivedContext(ChatMessage message, ChatMessageDto messageDto)
 {
     Message    = message;
     MessageDto = messageDto;
 }