Ejemplo n.º 1
0
        public async Task <int> SendSecureMessage(string encryptedMessage, string userId, int conversationId)
        {
            var whoSent = await userService.GetUserById(userProvider.GetUserId(Context));

            await userService.MakeUserOnline(whoSent.Id, Context.ConnectionId);

            try
            {
                await chatsService.ValidateDialog(whoSent.Id, userId);

                var user = await userService.GetUserById(userId);

                var created = await messagesService.AddEncryptedMessage(encryptedMessage, conversationId, whoSent.Id);

                var toSend = new Message
                {
                    Id = created.MessageID,
                    EncryptedPayload = created.EncryptedPayload,
                    TimeReceived     = created.TimeReceived.ToUTCString(),
                    State            = MessageState.Delivered,
                    //this is really needed, because if name/lastname of sender will change,
                    //it won't be reflected in encrypted payload.
                    User = whoSent.ToAppUserDto()
                };

                if (user.IsOnline)
                {
                    await SendMessageToUser(toSend, whoSent.Id, user.Connections.ToConnectionIds(), conversationId, true);
                }

                return(toSend.Id);
            }
            catch (Exception ex)
            {
                if (ex is NullReferenceException)
                {
                    await SendError(Context.ConnectionId, "Wrong user id was provided.");
                }
                else
                {
                    await SendError(Context.ConnectionId, ex.Message);
                }

                logger.LogError(ex.Message);
                return(0);
            }
        }