Пример #1
0
        public async Task<ActionResult> Message(int threadId, string messageText)
        {
            var userIdCurrent = User.Identity.GetUserId();

            var messageParticipants = await _messageParticipantService.Query(x => x.MessageThreadID == threadId).SelectAsync();
            if (!messageParticipants.Any(x => x.UserID == userIdCurrent))
                return RedirectToAction("Messages");

            // Add message
            var message = new Message()
            {
                MessageThreadID = threadId,
                UserFrom = userIdCurrent,
                Body = messageText,
                ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added,
                Created = DateTime.Now,
                LastUpdated = DateTime.Now,
            };

            _messageService.Insert(message);

            await _unitOfWorkAsync.SaveChangesAsync();

            // Add message readstate
            foreach (var messageParticipant in messageParticipants.Where(x => x.UserID != userIdCurrent))
            {
                _messageReadStateService.Insert(new MessageReadState()
                {
                    MessageID = message.ID,
                    UserID = messageParticipant.UserID,
                    ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
                });
            }

            await _unitOfWorkAsync.SaveChangesAsync();

            TempData[TempDataKeys.UserMessage] = "[[[Message is sent.]]]";

            return RedirectToAction("Message", new { threadId = threadId });
        }
Пример #2
0
        /// <summary>
        /// Send Message
        /// </summary>
        /// <param name="userFrom"></param>
        /// <param name="userTo"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static async Task SendMessage(MessageSendModel messageModel)
        {
            var unitOfWork = ContainerManager.GetConfiguredContainer().Resolve<IUnitOfWorkAsync>();

            MessageThread messageThread;

            // Use message subject to find the message thread if no listing id is provided
            if (!messageModel.ListingID.HasValue)
            {
                var messageThreadQuery = await MessageThreadService
                    .Query(x =>
                        x.Subject.Equals(messageModel.Subject, StringComparison.InvariantCultureIgnoreCase) &&
                        x.MessageParticipants.Any(y => y.UserID == messageModel.UserFrom) &&
                        x.MessageParticipants.Any(y => y.UserID == messageModel.UserTo))
                    .SelectAsync();

                messageThread = messageThreadQuery.FirstOrDefault();
            }
            else
            {
                // Otherwise, use listing ID
                var messageThreadQuery = await MessageThreadService
                    .Query(x => x.ListingID == messageModel.ListingID)
                    .SelectAsync();

                messageThread = messageThreadQuery.FirstOrDefault();
            }

            // Create message thread if there is not one yet.
            if (messageThread == null)
            {
                messageThread = new MessageThread()
                {
                    Subject = messageModel.Subject,
                    ListingID = messageModel.ListingID,
                    Created = DateTime.Now,
                    LastUpdated = DateTime.Now,
                    ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
                };

                MessageThreadService.Insert(messageThread);

                await UnitOfWorkAsync.SaveChangesAsync();

                // Add message participants
                MessageParticipantService.Insert(new MessageParticipant()
                {
                    UserID = messageModel.UserFrom,
                    MessageThreadID = messageThread.ID,
                    ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
                });

                MessageParticipantService.Insert(new MessageParticipant()
                {
                    UserID = messageModel.UserTo,
                    MessageThreadID = messageThread.ID,
                    ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
                });
            }

            // Insert mail message to db
            var message = new Message()
            {
                UserFrom = messageModel.UserFrom,
                Body = messageModel.Body,
                MessageThreadID = messageThread.ID,
                Created = DateTime.Now,
                LastUpdated = DateTime.Now,
                ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
            };

            MessageService.Insert(message);

            await UnitOfWorkAsync.SaveChangesAsync();

            // Add read state of messages
            MessageReadStateService.Insert(new MessageReadState()
            {
                MessageID = message.ID,
                UserID = messageModel.UserFrom,
                ReadDate = DateTime.Now,
                ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
            });

            MessageReadStateService.Insert(new MessageReadState()
            {
                MessageID = message.ID,
                UserID = messageModel.UserTo,
                ReadDate = null,
                ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added
            });

            await UnitOfWorkAsync.SaveChangesAsync();
        }