public async Task <JsonResult> ProfileData(Guid id)
        {
            var authId = this.User.Identity.GetUserIdGuid();
            var user   = await _userService.QueryUser().SingleAsync(u => u.Id == id);

            if (authId != id)
            {
                await _userService.IncrementViewCount(id);
            }

            var userDto = new UserFullViewDto
            {
                AlbumPhotos = JsonConvert.DeserializeObject <List <string> >(user.AlbumPhotoUrls)
                              .Select(PhotoUrlService.GetPhotoDto).ToList(),
                Birthday     = user.Birthday,
                Description  = user.Description,
                Gender       = user.Gender,
                Interest     = user.Interest,
                Name         = user.Name,
                ProfilePhoto = PhotoUrlService.GetPhotoDto(user.ProfilePhotoUrl),
                UserId       = user.Id,
                Location     = user.Location,
                CanEdit      = authId.HasValue ? authId.Value == id : false,
                IsOnline     = _onlineUserService.GetOnlineUsers().Any(u => u.Id == user.Id)
            };

            return(Json(userDto, JsonRequestBehavior.AllowGet));
        }
        public async Task <JsonResult> UploadPhotoForAlbum()
        {
            var userId = this.User.Identity.GetUserIdGuid().Value;

            if (this.HttpContext.Request.Files == null ||
                this.HttpContext.Request.Files.Count == 0)
            {
                return(Json(new
                {
                    success = false,
                    errorMessage = "File not found."
                }));
            }

            var    photo   = this.HttpContext.Request.Files[0];
            string fileUrl = await this.SaveFile(userId, photo.FileName, photo.InputStream);

            await _userService.AddAlbumPhotoUrl(userId, fileUrl);

            _hubContext.Clients.All
            .GalleryPhotoUploaded(userId, PhotoUrlService.GetPhotoDto(fileUrl));

            return(Json(new
            {
                success = true
            }));
        }
        public async Task <JsonResult> UploadProfilePhoto()
        {
            var userId = this.User.Identity.GetUserIdGuid().Value;
            var user   = await _userService.QueryUser()
                         .SingleAsync(u => u.Id == userId);

            if (this.HttpContext.Request.Files == null ||
                this.HttpContext.Request.Files.Count == 0)
            {
                return(Json(new
                {
                    success = false,
                    errorMessage = "File not found."
                }));
            }

            var    photo   = this.HttpContext.Request.Files[0];
            string fileUrl = await this.SaveFile(userId, "profile_" + photo.FileName, photo.InputStream);

            if (user.ProfilePhotoUrl != null)
            {
                var result = await _fileStorageService.DeleteFile(user.ProfilePhotoUrl);
            }

            await _userService.SetProfilePhotoUrl(userId, fileUrl);

            _hubContext.Clients.All
            .ProfilePhotoChanged(userId, PhotoUrlService.GetPhotoDto(fileUrl));

            return(Json(new
            {
                success = true
            }));
        }
        public async Task <JsonResult> ConversationListData()
        {
            var authUserId        = this.User.Identity.GetUserIdGuid().Value;
            var userConversations = (await _userService
                                     .QueryUser()
                                     .Include(u => u.Conversations)
                                     .Include(u => u.Conversations.Select(c => c.Users))
                                     .SingleAsync(u => u.Id == authUserId))
                                    .Conversations
                                    .Where(c => !c.ConversationOptions.Single(opt => opt.UserId == authUserId).IsDeleted);

            List <ConversationPreviewDto> listDto = new List <ConversationPreviewDto>();

            foreach (var conv in userConversations)
            {
                var user    = conv.Users.First(u => u.Id != authUserId);
                var userDto = new UserViewDto
                {
                    Gender       = user.Gender,
                    Name         = user.Name,
                    ProfilePhoto = PhotoUrlService.GetPhotoDto(user.ProfilePhotoUrl),
                    UserId       = user.Id
                };

                var lastMessage = conv.Messages
                                  .OrderByDescending(m => m.SentOn)
                                  .Where(m => m.SentByUserId != authUserId)
                                  .FirstOrDefault();

                MessageDto lastMessageDto = lastMessage != null
                    ? new MessageDto
                {
                    SentByUserId = lastMessage.SentByUserId,
                    SentOn       = lastMessage.SentOn,
                    Text         = lastMessage.Text
                } : null;

                listDto.Add(new ConversationPreviewDto
                {
                    ConversationId = conv.Id,
                    Message        = lastMessageDto,
                    User           = userDto,
                    HasNewMessages = conv
                                     .ConversationOptions
                                     .Single(u => u.UserId == authUserId)
                                     .HasNewMessages
                });
            }

            return(Json(listDto, JsonRequestBehavior.AllowGet));
        }
        public async Task <JsonResult> Send(MessageViewModel model)
        {
            var userId = this.User.Identity.GetUserIdGuid().Value;
            await _conversationService.SendMessage(
                model.ConversationId,
                userId,
                model.SentOn,
                model.Text);

            var interlocutorUserIds = await _conversationService.QueryConversation()
                                      .Include(c => c.Users)
                                      .Where(c => c.Id == model.ConversationId)
                                      .SelectMany(c => c.Users)
                                      .Select(u => u.Id)
                                      .ToListAsync();

            foreach (var id in interlocutorUserIds
                     .Where(interlocId => interlocId != userId))
            {
                var userDto = await _userService.QueryUser()
                              .SingleAsync(u => u.Id == userId);

                _conversationHub
                .Clients
                .User(id.ToString())
                .MessageReceived(model.ConversationId,
                                 new UserViewDto
                {
                    Gender       = userDto.Gender,
                    Name         = userDto.Name,
                    ProfilePhoto = PhotoUrlService.GetPhotoDto(userDto.ProfilePhotoUrl),
                    UserId       = userDto.Id
                },
                                 model.Text,
                                 model.SentOn);

                _conversationHub
                .Clients
                .User(id.ToString())
                .HasNewMessages(id);
            }

            return(Json(new MessagePostedViewModel
            {
                UserId = userId,
                Text = model.Text,
                SentOn = model.SentOn
            }));
        }
        public async Task <JsonResult> UserTileListData(List <Guid> skipIds, int take, Gender gender)
        {
            Expression <Func <UserEntity, bool> > skipPredicate = user => true;

            if (skipIds != null)
            {
                skipPredicate = user => !skipIds.Contains(user.Id);
            }

            //Filter only if man or woman gender is specified
            Expression <Func <UserEntity, bool> > genderPredicate = user => true;

            if (gender != Gender.NotSpecified)
            {
                genderPredicate = user => user.Gender == gender;
            }

            var users = (await _userService.QueryUser()
                         .Where(skipPredicate)
                         .Where(genderPredicate)
                         .Where(u => !u.IsDeactivated)
                         .OrderBy(u => Guid.NewGuid())
                         .Take(take)
                         .Select(u => new
            {
                Gender = u.Gender,
                Name = u.Name,
                ProfilePhotoUrl = u.ProfilePhotoUrl,
                UserId = u.Id,
                Interest = u.Interest
            })
                         .ToListAsync())
                        .Select(u => new UserViewDto
            {
                Gender       = u.Gender,
                Name         = u.Name,
                ProfilePhoto = PhotoUrlService.GetPhotoDto(u.ProfilePhotoUrl),
                UserId       = u.UserId,
                Interest     = u.Interest
            });

            return(Json(users, JsonRequestBehavior.AllowGet));
        }
        public async Task <JsonResult> ConversationData(Guid id)
        {
            var userId = this.User.Identity.GetUserIdGuid().Value;

            var conversation = await _conversationService
                               .QueryConversation()
                               .Include(c => c.Messages)
                               .Include(c => c.Users)
                               .SingleAsync(c => c.Id == id);

            var interlocutorId = conversation.Users.First(u => u.Id != userId).Id;

            ConversationDto conversationDto = new ConversationDto
            {
                ConversationId = conversation.Id,
                Title          = conversation.Users.Single(u => u.Id != userId).Name,
                Messages       = conversation.Messages.OrderByDescending(m => m.SentOn)
                                 .Take(20)
                                 .Select(m => new MessageDto
                {
                    SentByUserId = m.SentByUserId,
                    SentOn       = m.SentOn,
                    Text         = m.Text,
                    IsSentByUser = m.SentByUserId == userId
                })
                                 .OrderBy(m => m.SentOn)
                                 .ToList(),
                Users = conversation.Users.Select(u => new UserViewDto
                {
                    Gender       = u.Gender,
                    Name         = u.Name,
                    ProfilePhoto = PhotoUrlService.GetPhotoDto(u.ProfilePhotoUrl),
                    UserId       = u.Id
                }).ToList(),
                IsInterlocutorOnline = _onlineUserService.GetOnlineUsers()
                                       .Any(u => u.Id == interlocutorId),
                InterlocutorId = interlocutorId
            };

            return(Json(conversationDto, JsonRequestBehavior.AllowGet));
        }
        public async Task <JsonResult> ProfileSummaryData()
        {
            var userId = this.User.Identity.GetUserIdGuid().Value;
            var user   = await _userService.QueryUser()
                         .Include(u => u.Conversations.Select(c => c.ConversationOptions))
                         .SingleAsync(u => u.Id == userId);

            if (user.IsDeactivated)
            {
                HttpContext.GetOwinContext().Authentication.SignOut();
                throw new ApplicationException("Account deactivated. Please contact support.");
            }

            var profileDto = new ProfileSummaryDto
            {
                Settings = new UserSettings
                {
                    ShowWelcome = user.ShowWelcome
                },
                User = new UserViewDto
                {
                    Gender       = user.Gender,
                    Name         = user.Name,
                    ProfilePhoto = PhotoUrlService.GetPhotoDto(user.ProfilePhotoUrl),
                    UserId       = user.Id,
                    Interest     = user.Interest,
                    Birthday     = user.Birthday
                },
                HasNewMessages = user.Conversations
                                 .SelectMany(c => c.ConversationOptions)
                                 .Where(opt => opt.UserId == userId)
                                 .Any(opt => opt.HasNewMessages)
            };

            return(Json(profileDto, JsonRequestBehavior.AllowGet));
        }