Ejemplo n.º 1
0
        public async Task <IActionResult> SendMessage([FromBody] SendMessageModel model)
        {
            var currentUser = await GetCurrentUserAsync();

            if (currentUser is null ||
                currentUser is default(User))
            {
                return(Unauthorized("No authorized user found.\nPlease log in by using your credentials."));
            }

            if (model is null ||
                model is default(SendMessageModel))
            {
                return(BadRequest(nameof(model)));
            }

            if (string.IsNullOrEmpty(model.ReceiverUserName))
            {
                return(BadRequest("ReceiverUserName cannot be null."));
            }

            if (string.IsNullOrWhiteSpace(model.Content))
            {
                return(BadRequest("You cannot send and empty message to another user.\nPlease add some message."));
            }

            if (string.Equals(model.ReceiverUserName, currentUser.Username))
            {
                return(BadRequest("You cannot send a message to yourself.\nPlease change the username to send a message to another user."));
            }

            // Check receiver user is exist.
            // Check receiver user is blocked the sender user.
            // In this situation we save the message coming from
            // blocked user but not showed up to the receiver user.
            var receiverUser = await _userService.GetUserByUsernameAsync(model.ReceiverUserName);

            if (receiverUser is null ||
                receiverUser is default(User))
            {
                return(NotFound("Receiver User not found. Please check the username or send a message to another user."));
            }

            try
            {
                var message = new Message()
                {
                    SenderUserId   = currentUser.Id,
                    ReceiverUserId = receiverUser.Id,
                    Content        = model.Content
                };
                await _messageService.InsertMessageAsync(message);

                // Check the receiver user is blocked the current user.
                var blockedUser = await _blockedUserService.CheckUserIsBlockedAsync(receiverUser.Id, currentUser.Id);

                if (blockedUser is null ||
                    blockedUser is default(BlockedUser))
                {
                    await _activityLogService.LogSendMessageActivityAsync(new ActivityLog()
                    {
                        UserId  = currentUser.Id,
                        Message = string.Format("{0} user send message to {1} user"
                                                , currentUser.Id, receiverUser.Id)
                    });

                    return(Ok("Message sent ✔"));
                }

                message.Blocked = true;
                await _messageService.UpdateMessageAsync(message);
            }
            catch (Exception ex)
            {
                await _logService.LogErrorAsync(new CreateLogModel()
                {
                    UserId    = currentUser.Id,
                    Title     = "SendMessage Error",
                    Message   = "Error happened in Message Controller, SendMessage function",
                    Exception = ex
                });

                return(Ok("Message not sent."));
            }

            await _activityLogService.LogInvalidSendMessageActivityAsync(new ActivityLog()
            {
                UserId  = currentUser.Id,
                Message = string.Format("{0} user send a blocked message to {1} user. This message will not be showed up to receiver"
                                        , currentUser.Id, receiverUser.Id)
            });

            // If blocked, we show a user friendly message to current user.
            return(Ok("Message received  ✔\nThis message will not be showed to the receiver. Because you are blocked by the receiver user."));
        }