Esempio n. 1
0
        public PaginatedGroupRecommendation GetPaginatedGroupRecommendations(string currUserId, string[] followingUserIds, int[] followingGroups, PaginatedRequest parameters)
        {
            PaginatedGroupRecommendation ret = new PaginatedGroupRecommendation();

            string[] currUserFollowingIds = currentUserFollowings.Where(p => p.Value == FollowState.Confirmed).Select(p => p.Key).ToArray();

            int[] reccIds = _context.SetChild <UserGroup>()
                            .AsNoTracking()
                            .Where(p => currUserFollowingIds.Contains(p.UserId) && p.GroupFollowState == GroupFollowState.Followed && !userGroupFollowIds.Contains(p.GroupId))
                            .OrderByDescending(p => p.DateUtcFollowed)
                            .Skip((parameters.PageIndex - 1) * parameters.PageSize)
                            .Take(parameters.PageSize)
                            .Select(f => f.GroupId)
                            .ToArray();

            ret.Entities = _context.Set <Group>().AsNoTracking().Where(p => reccIds.Contains(p.Id)).Select(p => new InterestCard()
            {
                CoverImage             = p.CoverImage.ThumbPath,
                Id                     = p.Id,
                IsCurrentUserFollowing = userGroupFollowIds.Any(s => s == p.Id),
                Name                   = p.Name,
                AlphaColor             = p.ColorAlpha,
                ProfileImage           = p.ProfileImage.SmallPath,
                UrlKey                 = p.UrlKey
            }).ToPaginatedList(parameters.PageIndex, parameters.PageSize, int.MaxValue);
            // Get Total Reputation
            foreach (var group in ret.Entities)
            {
                // Get From Cache
                int?followerCount = _groupCacheService.GetFollowingUserCount(group.Id);
                // If exist in cache
                if (followerCount.HasValue)
                {
                    group.FollowerCount = followerCount.Value;
                }
                // If not exist in cache get from database and set cache
                else
                {
                    int count = _context.SetChild <UserGroup>().AsNoTracking().Where(p => p.GroupId == group.Id && p.GroupFollowState == GroupFollowState.Followed).Count();
                    group.FollowerCount = count;
                    _groupCacheService.SetFollowingUserCount(group.Id, count, 60);
                }
            }

            // return as paginated
            return(ret);
        }
Esempio n. 2
0
        public ICollection <InterestCard> GetPostGroups(int postId, string currUserId)
        {
            bool isLoggedIn = !string.IsNullOrEmpty(currUserId);

            int[] gp = _context.SetChild <GroupPost>().AsNoTracking().Where(p => p.PostId == postId).Select(p => p.GroupId).ToArray();
            IQueryable <InterestCard> ret = _context.Set <Group>().AsNoTracking()
                                            .Select(p => new InterestCard()
            {
                AlphaColor   = p.ColorAlpha,
                Id           = p.Id,
                Name         = p.Name,
                UrlKey       = p.UrlKey,
                ProfileImage = p.ProfileImage.SmallPath
            })
                                            .Where(p => gp.Contains(p.Id));

            foreach (var item in ret)
            {
                item.FollowerCount          = _groupCacheService.GetFollowingUserCount(item.Id) ?? _groupDataService.GetGroupFollowerCount(item.Id, 20);
                item.IsCurrentUserFollowing = !isLoggedIn ? false : userGroupFollowIds.Contains(item.Id);
            }
            return(ret.ToList());
        }
Esempio n. 3
0
        public GroupIndexReturn GetGroupIndex(string urlKey, string currUserId, int pageIndex = 1, int pageSize = 8, string order = "publishDate")
        {
            bool isLoggedIn = !string.IsNullOrEmpty(currUserId);

            if (isLoggedIn)
            {
                GetUserLikes(currUserId);
            }
            else
            {
                currentUserFollowings = new Dictionary <string, FollowState>();
            }
            GroupIndexReturn ret = new GroupIndexReturn();

            ret.Group = _context.Set <Group>().AsNoTracking()
                        .Select(p => new GroupIndexDetail()
            {
                Description = p.Description,
                AlphaColor  = p.ColorAlpha,
                Id          = p.Id,
                Name        = p.Name,
                UrlKey      = p.UrlKey,
                CoverImage  = new BaseImageReturn()
                {
                    Dimension = p.CoverImage.ImageDimension,
                    Extension = p.CoverImage.FileExtension,
                    LazyUrl   = p.CoverImage.BlurLazyPath,
                    SmallUrl  = p.CoverImage.SmallPath,
                    ThumbUrl  = p.CoverImage.ThumbPath,
                    Url       = p.CoverImage.ResizedPath
                },
                ThumbnailImage = new BaseImageReturn()
                {
                    Dimension = p.ProfileImage.ImageDimension,
                    Extension = p.ProfileImage.FileExtension,
                    LazyUrl   = p.ProfileImage.BlurLazyPath,
                    SmallUrl  = p.ProfileImage.SmallPath,
                    ThumbUrl  = p.ProfileImage.ThumbPath,
                    Url       = p.ProfileImage.ResizedPath
                }
            })
                        .FirstOrDefault(p => p.UrlKey == urlKey);
            if (ret.Group == null)
            {
                return(ret);
            }
            ret.Group.FollowerCount = _groupCacheService.GetFollowingUserCount(ret.Group.Id) ?? GetGroupFollowerCount(ret.Group.Id, 10);
            ret.Group.FollowState   = !isLoggedIn ? GroupFollowState.Unfollowed: userFollowedInterest.Contains(ret.Group.Id) ?GroupFollowState.Followed :  GroupFollowState.Unfollowed;

            var postIdRels = _groupCacheService.GetPostRelationships(ret.Group.Id);

            if (postIdRels == null)
            {
                postIdRels = this.GetGroupPosts(ret.Group.Id);
                if (postIdRels.Count() > 0)
                {
                    _groupCacheService.SetPostRelationships(ret.Group.Id, postIdRels, 30);
                }
            }
            if (postIdRels.Count() == 0)
            {
                return(ret);
            }
            int[] paginatedPostIds = postIdRels.OrderByDescending(p => p.DateUtcAdded).Select(p => p.PostId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
            ret.Posts = new PaginatedPostCardReturn
            {
                Entities = _context.Set <Post>().AsNoTracking()
                           .Include(p => p.PostParts)
                           .ThenInclude(p => p.Image)
                           .OrderByDescending(f => f.DateUtcPublished)
                           .Select(p => new { Entity = p, p.UserInfo, p.PostParts, p.ReputationGains, p.Groups, ReviewCount = p.Reviews.Count() })
                           .Where(p => paginatedPostIds.Contains(p.Entity.Id))
                           .Select(p => new PostCardModel()
                {
                    DateUtcPublished = p.Entity.DateUtcPublished,
                    Content          = p.Entity.Content,
                    Title            = p.Entity.Title,
                    Id          = p.Entity.Id,
                    ReviewCount = p.ReviewCount,
                    PostParts   = p.Entity.PostParts.Select(f => new PostPartDisplay()
                    {
                        Description = f.Description,
                        Id          = f.Id,
                        Image       = new BaseImageReturn()
                        {
                            Dimension = f.Image.ImageDimension,
                            Extension = f.Image.FileExtension,
                            LazyUrl   = f.Image.BlurLazyPath,
                            SmallUrl  = f.Image.SmallPath,
                            ThumbUrl  = f.Image.ThumbPath,
                            Url       = f.Image.ResizedPath
                        },
                        PostId = f.PostId,
                        Title  = f.Title
                    }).ToList(),
                    AuthorInfo = new UserCardModel()
                    {
                        AppUserId    = p.UserInfo.AppUserId,
                        Name         = p.UserInfo.Name,
                        ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId),
                        Surname      = p.UserInfo.Surname,
                        Username     = p.UserInfo.UName,
                    }
                }).ToPaginatedList(1, 16, postIdRels.Count())
            };

            foreach (var post in ret.Posts.Entities)
            {
                post.Rating             = GetPostRating(post.Id, 2);
                post.IsCurrentUserLiked = isLoggedIn ? userPostLikesIds.Contains(post.Id) : false;
                post.FavouriteCount     = _postCacheService.GetPostLikesCount(post.Id)
                                          ?? this.GetPostLikeCount(post.Id, cacheTreshold: 20);
                if (currentUserFollowings.Any(p => p.Key == post.AuthorInfo.AppUserId))
                {
                    post.AuthorInfo.FollowState = currentUserFollowings[post.AuthorInfo.AppUserId];
                }
                else
                {
                    post.AuthorInfo.FollowState = FollowState.Unfollowed;
                }
            }

            return(ret);
        }
Esempio n. 4
0
        public UserInterestsReturn GetUserInterests(string userName, int pageIndex, int pageSize, string currUserId)
        {
            UserInterestsReturn ret = new UserInterestsReturn();
            // Get User Id
            string userId = GetUserId(userName);

            // Get Following Interest From Cache
            int[] followedInterestIds = _userLikesCacheService.GetUserFollowingGroupsIds(userId);
            // If Cache is Null Get From Database OrderedBy Reputation and Set Cache
            if (followedInterestIds == null || followedInterestIds.Count() == 0)
            {
                followedInterestIds = _context.SetChild <UserGroup>().AsNoTracking().Where(p => p.UserId == userId && p.GroupFollowState == GroupFollowState.Followed)
                                      .OrderByDescending(p => p.UserReputationInGroup).Select(p => p.GroupId).ToArray();

                _userLikesCacheService.SetUserFollowingGroupsIds(userId, followedInterestIds, 60);
            }
            // Paginate Group Ids' so we don't need to paginate them in database query
            int[] paginatedFollowedInterestIds = followedInterestIds.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
            // Load CurrentUser Groups To Check If User Following These Groups
            int[] currentUserFollowingGroups = _userLikesCacheService.GetUserFollowingGroupsIds(currUserId);
            if (currentUserFollowingGroups == null || currentUserFollowingGroups.Count() == 0)
            {
                currentUserFollowingGroups = _context.SetChild <UserGroup>().AsNoTracking()
                                             .Where(p => p.UserId == currUserId && p.GroupFollowState == GroupFollowState.Followed)
                                             .Select(p => p.GroupId).ToArray();
                _userLikesCacheService.SetUserFollowingGroupsIds(currUserId, currentUserFollowingGroups, 60);
            }
            // Fetch  Groups from database

            List <InterestCard> groups = _context.Set <Group>().AsNoTracking().Where(p => paginatedFollowedInterestIds.Contains(p.Id)).Select(p => new InterestCard()
            {
                CoverImage             = p.CoverImage.ThumbPath,
                Id                     = p.Id,
                IsCurrentUserFollowing = currentUserFollowingGroups.Any(s => s == p.Id),
                Name                   = p.Name,
                AlphaColor             = p.ColorAlpha,
                ProfileImage           = p.ProfileImage.SmallPath,
                UrlKey                 = p.UrlKey
            }).ToList();

            // Foreach group Set Follower Count
            foreach (var group in groups)
            {
                // Get From Cache
                int?followerCount = _groupFollowCacheService.GetFollowingUserCount(group.Id);
                // If exist in cache
                if (followerCount.HasValue)
                {
                    group.FollowerCount = followerCount.Value;
                }
                // If not exist in cache get from database and set cache
                else
                {
                    int count = _context.SetChild <UserGroup>().AsNoTracking().Where(p => p.GroupId == group.Id && p.GroupFollowState == GroupFollowState.Followed).Count();
                    group.FollowerCount = count;
                    _groupFollowCacheService.SetFollowingUserCount(group.Id, count, 60);
                }
            }
            ret.Entities = groups.ToPaginatedList(pageIndex, pageSize, followedInterestIds.Count());
            return(ret);
        }