Пример #1
0
        public async Task <SendMessagePayload> SendMessageAsync(
            SendMessageInput input,
            FieldNode field,
            [GlobalState] string currentUserEmail,
            PersonByEmailDataLoader personByEmail,
            [Service] IMessageRepository messageRepository,
            [Service] IEventDispatcher eventDispatcher,
            CancellationToken cancellationToken)
        {
            IReadOnlyList <Person> participants =
                await personByEmail.LoadAsync(
                    cancellationToken, currentUserEmail, input.RecipientEmail)
                .ConfigureAwait(false);

            if (participants[1] is null)
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetCode("UNKNOWN_RECIPIENT")
                          .SetMessage("The given recipient id is invalid.")
                          .AddLocation(field.Arguments[0])
                          .Build());
            }

            Person sender    = participants[0];
            Person recipient = participants[1];

            var message = new Message(
                sender.Id,
                recipient.Id,
                input.Text);

            await messageRepository.AddMessageAsync(
                message, cancellationToken)
            .ConfigureAwait(false);

            await eventDispatcher.SendAsync(
                recipient.Email, message, cancellationToken)
            .ConfigureAwait(false);

            return(new SendMessagePayload(
                       sender,
                       recipient,
                       message,
                       input.ClientMutationId));
        }
Пример #2
0
        public async Task <SendMessagePayload> SendMessageAsync(
            SendMessageInput input,
            FieldNode field,
            [GlobalState] string currentUserEmail,
            PersonByEmailDataLoader personByEmail,
            [Service] ChatDbContext dbContext,
            [Service] ITopicEventSender eventSender,
            CancellationToken cancellationToken)
        {
            IReadOnlyList <Person> participants =
                await personByEmail.LoadAsync(
                    cancellationToken, currentUserEmail, input.RecipientEmail);

            if (participants[1] is null)
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetCode("UNKNOWN_RECIPIENT")
                          .SetMessage("The given recipient id is invalid.")
                          .AddLocation(field.Arguments[0])
                          .Build());
            }

            Person sender    = participants[0];
            Person recipient = participants[1];

            var message = new Message
            {
                SenderId    = sender.Id,
                RecipientId = recipient.Id,
                Text        = input.Text,
                Sent        = DateTime.UtcNow
            };

            dbContext.Messages.Add(message);
            await dbContext.SaveChangesAsync(cancellationToken);

            await eventSender.SendAsync(recipient.Email, message, cancellationToken);

            return(new SendMessagePayload(
                       sender,
                       recipient,
                       message,
                       input.ClientMutationId));
        }