public List <RawImage> ProfilesImages(int profileId, int skip, int take) { string resultsKey = $"{profileId}profileImages"; if (skip == 0) { int relationshipTier = friendRepo.RelationshipTier(currentProfile.profile.ProfileId, profileId); // Prep list. List <int?> imageIds = new List <int?>(); // If the requested segment does not start past the end of the list. if (skip < imageRepo.CountByProfileId(profileId)) { // Loop though requested segment of profile's images. foreach (Models.Image i in imageRepo.RangeByProfileId(profileId, skip, take)) { if (i.PrivacyLevel <= relationshipTier) { imageIds.Add(i.ImageId); } } sessionResults.AddResults(resultsKey, imageIds); } } List <RawImage> images = new List <RawImage>(); foreach (int imageId in sessionResults.GetResultsSegment(resultsKey, skip, take)) { images.Add(Util.GetRawImage(imageRepo.ById(imageId), 1)); } return(images); }
public List <CommentModel> SearchComments(int postId, int skip, int take, [FromBody] StringModel searchText) { if (searchText.str == "NULL") { return(null); } Post post = postRepo.ById(postId); if (post.PrivacyLevel > friendRepo.RelationshipTier(currentProfile.id, post.ProfileId)) { return(null); } string resultsKey = $"{postId}commentSearch"; if (skip == 0) { // Prep list for matches. Each index contains a key value pair of <CommentId, searchPoints>. List <KeyValuePair <int, int> > matches = new List <KeyValuePair <int, int> >(); // Split search terms into array of search terms. string[] searchTerms = searchText.str.Split(' '); // Define how many points an exact match is worth. int exactMatchWorth = 3; // Loop through all profiles in the database. foreach (Comment c in commentRepo.ByPostId(postId)) { // Define points variable and start it at 0. int points = 0; string[] contentTerms = c.Content.Split(' '); foreach (string contentTerm in contentTerms) { string lcContentTerm = contentTerm.ToLower(); // Loop through search terms. foreach (string searchTerm in searchTerms) { // Convert search term to lowercase. string lcSearchTerm = searchTerm.ToLower(); // If the terms are an exact match, add an exact match worth of points. if (lcSearchTerm == lcContentTerm) { points += exactMatchWorth; } // Else if the terms are a partial match, add 1 point. else if (lcSearchTerm.Contains(lcContentTerm) || lcContentTerm.Contains(lcSearchTerm)) { points++; } } } // If the comment earned any points, add its id to the list of matches. if (points > 0) { matches.Add(new KeyValuePair <int, int>(c.CommentId, points)); } } // Sort match results by points. matches.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value)); // Prep list for preped results. List <int?> commentIds = new List <int?>(); // Loop through matches. foreach (KeyValuePair <int, int> match in matches) { // Add preped result to results. commentIds.Add(match.Key); } sessionResults.AddResults(resultsKey, commentIds); } List <CommentModel> commentModels = new List <CommentModel>(); foreach (int commentId in sessionResults.GetResultsSegment(resultsKey, skip, take)) { commentModels.Add(GetCommentModel(commentId)); } // Return search results to user. return(commentModels); }
public List <ProfileModel> GetFriends(int profileId, string type, int skip, int take, [FromBody] StringModel search) { if (profileId != 0) { Profile profile = profileRepo.ById(profileId); if (profile.ProfileFriendsPrivacyLevel > friendRepo.RelationshipTier(currentProfile.id, profileId)) { return(null); } } List <int?> profileIds = new List <int?>(); List <ProfileModel> results = new List <ProfileModel>(); if (type == "profileModal") { string resultsKey = "profileModalFriends"; if (skip == 0) { sessionResults.AddResults(resultsKey, ProfileFriends(profileId)); } foreach (int sessionProfileId in sessionResults.GetResultsSegment(resultsKey, skip, take)) { results.Add(GetProfileModel(sessionProfileId)); } } else if (type == "friendDropdown") { string resultsKey = "friendDropdown"; if (skip == 0) { // If ID is provided and the user has access, return friends by ProfileID. if (profileId != 0 && profileRepo.ById(profileId).ProfileFriendsPrivacyLevel <= friendRepo.RelationshipTier(currentProfile.profile.ProfileId, profileId)) { sessionResults.AddResults(resultsKey, ProfileFriends(profileId)); } // If search string is provided, return profile search results. else if (search.str != "NULL") { sessionResults.AddResults(resultsKey, Search(search.str)); } // If no ID or search string was provided, return the current user's friend requests. else { sessionResults.AddResults(resultsKey, FriendRequests()); } } foreach (int sessionProfileId in sessionResults.GetResultsSegment(resultsKey, skip, take)) { results.Add(GetProfileModel(sessionProfileId)); } } return(results); }
public List <PostModel> ProfilePosts(int profileId, int skip, int take, string feedFilter, string feedType) { string resultsKey = "profilePosts"; // If a new feed is starting and the user has access permission. if (skip == 0) { IEnumerable <Post> posts = new List <Post>(); switch (feedType) { case "commentedPosts": posts = postRepo.Posts.Where(p => p.ProfileId == profileId && ( commentRepo.HasCommented(p.PostId, currentProfile.id) || commentRepo.ByPostId(p.PostId).Any(c => likeRepo.HasLiked(2, c.CommentId, currentProfile.id)) )); break; case "likedPosts": posts = postRepo.Posts.Where(p => p.ProfileId == profileId && likeRepo.HasLiked(1, p.PostId, currentProfile.id)); break; case "mainPosts": posts = postRepo.Posts.Where(p => p.ProfileId == profileId); break; } switch (feedFilter) { case "recent": posts = posts.OrderByDescending(p => p.DateTime); break; case "likes": posts = posts .OrderByDescending(p => p.DateTime) .OrderByDescending(p => likeRepo.CountByContentId(1, p.PostId)); break; case "comments": posts = posts .OrderByDescending(p => p.DateTime) .OrderByDescending(p => commentRepo.CountByPostId(p.PostId)); break; } // Sorting by HasCommentActivity must happen after sorting by DateTime. if (feedType == "CommentedPosts") { posts = posts .OrderByDescending(p => commentRepo.HasCommented(p.PostId, currentProfile.id)); } // Store list of post ids. List <int?> postIds = new List <int?>(); foreach (Post p in posts) { postIds.Add(p.PostId); } sessionResults.AddResults(resultsKey, postIds); } List <PostModel> postModels = new List <PostModel>(); foreach (int postId in sessionResults.GetResultsSegment(resultsKey, skip, take)) { postModels.Add(GetPostModel(postId)); } return(postModels); }