public async Task <MessagesRes> GetMessaages(MessagesReq req, DatabaseContext context)
        {
            var currentUserData = this._currentUserService.GetCurrentUserData();

            var res = new MessagesRes
            {
                Messages = await context.ChatMessages
                           .Where(m => m.ConnectionId == req.ConnectionId)
                           .Select(m => new Message
                {
                    Id             = m.Id,
                    Direction      = m.Direction,
                    Body           = m.Body,
                    SendTime       = m.SendTime,
                    ReceiveTime    = m.ReceiveTime,
                    ConnectionId   = m.Connection.Id,
                    ConnectionType = m.Connection.ConnectionType,
                    CompanyId      = m.Connection.Vacancy.Event.Company.Id,
                    CompanyName    = m.Connection.Vacancy.Event.Company.Name,
                    ExpertId       = m.Connection.ExpertProfile.Id,
                    ExpertName     = m.Connection.ExpertProfile.User.DisplayName,
                }).ToArrayAsync()
            };

            if (!res.Messages.All(m => m.CompanyId == currentUserData.CompanyId || m.ExpertId == currentUserData.ExpertProfileId))
            {
                throw new NotYourMessagesException();
            }

            return(res);
        }
        public async Task <MessagesRes> GetNewIncomingMessages(DatabaseContext context)
        {
            var currentUserId = this._currentUserService.GetCurrentUserData().Id;

            var res = new MessagesRes
            {
                Messages = await context.ChatMessages
                           .Where(m =>
                                  m.Connection.ConnectionStatus == ConnectionStatuses.Connected &&
                                  (
                                      m.Direction == ChatMessageDirections.VacancyToExpert && m.Connection.ExpertProfile.UserId == currentUserId && m.ReceiveTime == null ||
                                      m.Direction == ChatMessageDirections.ExpertToCompany && m.Connection.Vacancy.Event.Company.UserId == currentUserId && m.ReceiveTime == null
                                  )
                                  )
                           .Select(m => new Message
                {
                    Id             = m.Id,
                    Body           = m.Body,
                    SendTime       = m.SendTime,
                    ReceiveTime    = m.ReceiveTime,
                    ConnectionId   = m.Connection.Id,
                    ConnectionType = m.Connection.ConnectionType,
                    CompanyId      = m.Connection.Vacancy.Event.Company.Id,
                    CompanyName    = m.Connection.Vacancy.Event.Company.Name,
                    ExpertId       = m.Connection.ExpertProfile.Id,
                    ExpertName     = m.Connection.ExpertProfile.User.DisplayName,
                }).ToArrayAsync()
            };

            return(res);
        }
        public async Task MarkConnectionAsViewed(long connectionId, DatabaseContext context)
        {
            var currentUserId = this._currentUserService.GetCurrentUserData().Id;

            var connection = await context.Connections
                             .Include(c => c.ExpertProfile.User)
                             .Include(c => c.Vacancy.Event.Company)
                             .Include(c => c.ChatMessages)
                             .SingleAsync(c => c.Id == connectionId);

            var oppositeUserId = this.GetOppositeUserId(connection, currentUserId);

            var messages = connection.ChatMessages
                           .Where(m => m.ReceiveTime == null && (
                                      (m.Connection.ExpertProfile.UserId == currentUserId && m.Direction == ChatMessageDirections.VacancyToExpert) ||
                                      (m.Connection.Vacancy.Event.Company.UserId == currentUserId && m.Direction == ChatMessageDirections.ExpertToCompany)
                                      )).ToArray();

            foreach (var message in messages)
            {
                message.ReceiveTime = DateTimeOffset.Now;
            }

            if (this.MessagesDelivered != null)
            {
                var res = new MessagesRes
                {
                    Messages = messages.Select(m => new Message
                    {
                        Id             = m.Id,
                        Direction      = m.Direction,
                        Body           = m.Body,
                        SendTime       = m.SendTime,
                        ReceiveTime    = m.ReceiveTime,
                        ConnectionId   = m.Connection.Id,
                        ConnectionType = m.Connection.ConnectionType,
                        CompanyId      = m.Connection.Vacancy.Event.Company.Id,
                        CompanyName    = m.Connection.Vacancy.Event.Company.Name,
                        ExpertId       = m.Connection.ExpertProfile.Id,
                        ExpertName     = m.Connection.ExpertProfile.User.DisplayName,
                    }).ToArray()
                };

                await this.MessagesDelivered(res, this.GetOppositeUserId(connection, currentUserId), currentUserId);
            }
        }
Beispiel #4
0
 private async Task OnMessagesDelivered(MessagesRes res, string senderId, string receiverId)
 {
     await Clients.Users(new[] { senderId, receiverId }).SendAsync("MessagesDelivered", res);
 }