public async void CreateData(UITextField tf_ChatMessage, UIViewController ViewController)
        {
            string Message = tf_ChatMessage.Text;

            if (Message.Trim() != "")
            {
                tf_ChatMessage.ResignFirstResponder();
                tf_ChatMessage.Text = "";

                var chatMessage = new ChatMessage();
                chatMessage.Message = Message;
                chatMessage.Isme    = true;
                chatMessage.Type    = 0;

                bool IsSuccess = chatMessageService.CreateChatMessage(chatMessage);
                if (IsSuccess)
                {
                    chatMessageList.Insert(0, chatMessage);
                    UpdateTableViewRow();
                }
                else
                {
                    SharedService.ShowErrorDialog("新增資料失敗", ViewController);
                    return;
                }

                AllRequestResult result = null;
                await Task.Run(async() =>
                {
                    result = await chatMessageService.CreateMessage(chatMessage.Message);
                });

                if (result.IsSuccess)
                {
                    string ResMessage = (string)result.Result;
                    ResMessage = ResMessage.Replace("\\n", "\n");
                    var ResChatMessage = new ChatMessage();
                    ResChatMessage.Message = ResMessage;
                    ResChatMessage.Isme    = false;
                    ResChatMessage.Type    = 0;

                    IsSuccess = chatMessageService.CreateChatMessage(ResChatMessage);

                    if (IsSuccess)
                    {
                        chatMessageList.Insert(0, ResChatMessage);
                        UpdateTableViewRow();
                    }
                    else
                    {
                        SharedService.ShowErrorDialog("新增資料失敗", ViewController);
                        return;
                    }
                }
                else
                {
                    SharedService.WebExceptionHandler((WebException)result.Result, ViewController);
                }
            }
        }
        public async Task <IActionResult> AddMessage(ChatMessage chatMessage, int toTeamChatId)
        {
            var newMessage = await chatMessageService.CreateMessage(chatMessage, userService.GetUserId(), toTeamChatId);

            if (newMessage != null)
            {
                return(Ok(newMessage));
            }

            return(BadRequest("Message error not sent !!"));
        }
Пример #3
0
 public IActionResult CreateMessages([FromBody] ChatMessageViewModel viewModel)
 {
     try
     {
         _chatMessageService.CreateMessage(viewModel);
         return(Ok());
     }
     catch (CustomException ex)
     {
         return(StatusCode(ex.StatusCode, ex.Message));
     }
     catch (Exception ex)
     {
         WriteExceptionInfo(ex);
         return(StatusCode(500));
     }
 }
Пример #4
0
        public async Task SendToTeam(string message, int teamId)
        {
            try
            {
                var Chat = await teamChatService.GetTeamChatOfTeam(teamId);

                if (Chat != null)
                {
                    await Groups.AddToGroupAsync(Context.ConnectionId, Chat.ChatName);

                    ChatMessage chatMessage = new ChatMessage()
                    {
                        Content = message
                    };


                    var newMessage = await chatMessageService.CreateMessage(chatMessage, userService.GetUserId(), Chat.Id); //save message in database

                    if (newMessage != null)
                    {
                        MessageViewModel messageViewModel = new MessageViewModel()
                        {
                            Content   = newMessage.Content,
                            Timestamp = newMessage.Timestamp,
                            FromUser  = newMessage.CreatorId,
                            ToChat    = newMessage.ChatId
                        };

                        await Clients.Group(Chat.ChatName).SendAsync("newMessage", messageViewModel);   //get Content,Timestamp , FromUser Details , ToChatDetails
                    }
                }
            }
            catch (Exception)
            {
                await Clients.Caller.SendAsync("onError", "Message not send! Message should be 1-500 characters.");  //on person who send message only
            }
        }
Пример #5
0
        public async Task ProcessIncomingMessage(ChatMessage message)
        {
            if (message == null)
            {
                throw new UnprocessableEntityException("Cannot process null message");
            }

            // check if message from same source with same SourceMessageId already in database
            if (await chatMessageService.MessageExists(message))
            {
                logger.LogInformation($"{message.Source} message id {message.SourceMessageId} already in database, skipping");
                return;
            }

            // - check for author
            if (message.Author == null)
            {
                if (message.AuthorId == 0)
                {
                    throw new UnprocessableEntityException("Author not defined");
                }

                message.Author = await authorService.GetAuthors().SingleOrDefaultAsync(x => x.Id == message.AuthorId);
            }

            if (message.Author == null)
            {
                logger.LogWarning($"Message without author... skipping");
                return;
            }

            if (!authorService.IsValid(message.Author))
            {
                logger.LogInformation($"invalid author, skipping: Source='{message.Author.Source}', SourceAuthorId='{message.Author.SourceAuthorId}', Name='{message.Author.Name}'");
                return;
            }

            var author = await authorService.EnsureAuthor(message.AuthorId, message.Author);

            // Housekeeping: syncs author's name changes
            if (author.Name != message.Author.Name)
            {
                author.Name = message.Author.Name;
            }

            // - check infractions
            if (chatRulesService.HasInfractions(message))
            {
                // apply infraction penalty
                author.Score -= 1;
            }
            else
            {
                author.Score += 1;
            }

            await authorService.Update(author);

            // save message
            if (message.AuthorId == 0)
            {
                message.AuthorId = author.Id;
            }
            await chatMessageService.CreateMessage(message);

            await SendToChannel("All", message);

            if (await botService.IsBotCommand(message))
            {
                await ProcessIncomingBotMessage(message);

                return;
            }

            // send message to view channels
            foreach (var channel in await chatRulesService.AllowedChannels(message))
            {
                await SendToChannel(channel, message);
            }

            //if (message.AuthorId > 0)
            //{
            //    message.Author = null;
            //}
        }