Beispiel #1
0
        public FullProfileModel GetFullProfile(int profileId)
        {
            int?blockerId = friendRepo.BlockerProfileId(currentProfile.id, profileId);

            if (blockerId != null && (blockerId == currentProfile.id || blockerId == profileId))
            {
                return(null);
            }

            // Get profile by ProfileID.
            Profile profile = profileRepo.ById(profileId);

            // Get profile picture image by ImageID.
            Image image = imageRepo.ById(profile.ProfilePicture);

            // Prep profile.
            FullProfileModel fullProfileModel = new FullProfileModel
            {
                // Details from profile.
                ProfileId = profile.ProfileId,
                FirstName = profile.FirstName,
                LastName  = profile.LastName,

                // Get data for relationship button.
                RelationToUser = friendRepo.RelationToUser(currentProfile.id, profileId)
            };

            int relationshipTier = friendRepo.RelationshipTier(currentProfile.id, profileId);

            // If bio privacy level does not exceed relationship level
            if (profile.ProfileBioPrivacyLevel <= relationshipTier)
            {
                fullProfileModel.Bio = profile.Bio;
            }
            else
            {
                fullProfileModel.Bio = "";
            }

            // If current user has access to profile picture and the image is not null.
            if (profile.ProfilePicturePrivacyLevel <= relationshipTier && image.ImageId != 0)
            {
                fullProfileModel.ProfilePicture = Util.GetRawImage(image, 2);
            }
            else
            {
                fullProfileModel.ProfilePicture = Util.GetRawImage(new Models.Image(), 2);
            }

            return(fullProfileModel);
        }
Beispiel #2
0
        [AllowAnonymous] // This needs to allow anonymous because the user accessing this page won't have an account yet.
        public async Task <IActionResult> Create(CreateModel model)
        {
            // Make controller base class do security check on itself, and validate account creation fields.
            if (ModelState.IsValid && ValidateCreateModel(model))
            {
                IdentityUser user = new IdentityUser // Satisfy both UserName and Email field of IdentityUser with the provided email.
                {
                    UserName = model.Email,
                    Email    = model.Email
                };

                // Attempt to create account and get hold onto result.
                IdentityResult result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded) // Account creation succesful.
                {
                    // Retrieve the user that was just created.
                    IdentityUser id = await userManager.FindByEmailAsync(model.Email);

                    // Create Profile instance with primary key ID of IdentityUser for password, provided name, and default values.
                    // The user's Profile is retrieved using the Id of their IdentityUser.
                    Profile profile = new Profile
                    {
                        Password       = id.Id,                               // The user will not enter this number when logging in, this will get passed behind the scenes.
                        FirstName      = FirstLetterToUpper(model.FirstName), // provideded
                        LastName       = FirstLetterToUpper(model.LastName),  // provideded
                        Bio            = "",                                  // default
                        ProfilePicture = 0,                                   // default
                        DateTime       = DateTime.UtcNow                      // default
                    };

                    // Add profile to the database.
                    profileRepo.SaveProfile(profile);

                    // Sign them in with the profile that was just added to the database.
                    currentProfile.SetProfile(profileRepo.ById(profile.ProfileId));

                    // Return home page.
                    return(RedirectToAction("Index", "Home"));
                }
                else // Account creation failed.
                {
                    // Transfer errors from account creation results to controller model state so they can be displayed on error page.
                    foreach (IdentityError error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }

            // If controller model state or credentials were invalid, send back provided account details and or account creation result errors.
            return(View(model));
        }
Beispiel #3
0
        // prep comment to be sent to client
        public CommentModel GetCommentModel(int commentId)
        {
            Comment comment = commentRepo.ById(commentId); // get comment by CommentId

            if (comment == null)
            {
                return(null);                                      // if no comment was found, return null
            }
            Profile profile = profileRepo.ById(comment.ProfileId); // get handle on owner of comment

            DateTime?likeDateTime = new DateTime();
            Like     like         = likeRepo.ByTypeAndProfileId(2, commentId, currentProfile.id);

            if (like != null)
            {
                likeDateTime = like.DateTime.ToLocalTime();
            }

            LikeModel likes = new LikeModel                                       // attach info for likes
            {
                ContentId   = commentId,                                          // link like data to parent comment by CommentId
                ContentType = 2,
                Count       = likeRepo.CountByContentId(2, commentId),            // set like count by CommentId
                HasLiked    = likeRepo.HasLiked(2, commentId, currentProfile.id), // determine if user has like and assign value
                DateTime    = likeDateTime
            };

            Image    profilePicture         = imageRepo.ById(profile.ProfilePicture);
            string   relationToUser         = friendRepo.RelationToUser(currentProfile.id, profile.ProfileId);
            int      relationshipTier       = friendRepo.RelationshipTier(currentProfile.id, profile.ProfileId);
            DateTime?relationChangeDateTime = friendRepo.RelationshipChangeDatetime(currentProfile.id, profile.ProfileId);
            int?     blockerProfileId       = friendRepo.BlockerProfileId(currentProfile.id, profile.ProfileId);

            ProfileModel profileModel = Util.GetProfileModel(
                profile, profilePicture, relationToUser, relationshipTier, relationChangeDateTime, blockerProfileId);

            CommentModel commentModel = new CommentModel // fill with data from comment and likeModel
            {
                CommentId = comment.CommentId,
                Content   = comment.Content,
                HasSeen   = comment.HasSeen,

                // attach prepped ProfileModel XXX shouldn't need to enter all this data about the user
                Profile = profileModel,

                DateTime = comment.DateTime.ToLocalTime(),
                Likes    = likes,
                PostId   = comment.PostId
            };

            return(commentModel);
        }
Beispiel #4
0
        /*
         *  Returns home page to user.
         */
        public IActionResult Index()
        {
            // If someone is logged in, bring them home.
            if (currentProfile.profile == null)
            {
                return(RedirectToAction("Login", "Account"));
            }

            currentProfile.SetProfile(profileRepo.ById(currentProfile.profile.ProfileId)); // Refreshes current profile

            // Returns home page to user with their ProfileID attached.
            return(View(currentProfile.id));
        }
Beispiel #5
0
        public void DeleteImage(int imageId)
        {
            // Get a handle on the image record by ImageID provided.
            Models.Image image = imageRepo.ById(imageId);

            // Validate image ownership.
            if (image.ProfileId == currentProfile.id)
            {
                // Burrow into the nested dependencies and delete them on the way out.
                // Pattern: (1)prep list, (2)fill list, (3)loop list, (4)repeat pattern on dependencies, (5)delete record.

                List <Post> posts = new List <Post>(); // (1)Prep list.
                foreach (Post p in postRepo.Posts.Where(p => p.ImageId == imageId))
                {
                    posts.Add(p);
                }                         // (2)Fill list.
                foreach (Post p in posts) // (3)Loop list.
                {
                    // (4)Repeat pattern on dependencies.
                    List <Comment> comments = new List <Comment>();
                    foreach (Comment c in commentRepo.ByPostId(imageId))
                    {
                        comments.Add(c);
                    }
                    foreach (Comment c in comments)
                    {
                        List <Like> commentLikes = new List <Like>();
                        foreach (Like l in likeRepo.ByTypeAndId(2, c.CommentId))
                        {
                            commentLikes.Add(l);
                        }
                        foreach (Like l in commentLikes)
                        {
                            likeRepo.DeleteLike(l);
                        }

                        commentRepo.DeleteComment(c);
                    }

                    List <Like> postLikes = new List <Like>();
                    foreach (Like l in likeRepo.ByTypeAndId(1, p.PostId))
                    {
                        postLikes.Add(l);
                    }
                    foreach (Like l in postLikes)
                    {
                        likeRepo.DeleteLike(l);
                    }

                    // (5)Delete record.
                    postRepo.DeletePost(p);
                }

                // If the user is using the image to be deleted as a profile picture, give them their default profile picture.
                if (currentProfile.profile.ProfilePicture == imageId)
                {
                    // Get profile by current user's ProfileID.
                    // (By doing this, instead of using currentUser.profile, it gaurentees that this is the latest version of the profile.)
                    Profile profile = profileRepo.ById(currentProfile.id);
                    profile.ProfilePicture = 0;         // Give default profile picture ImageID.
                    profileRepo.SaveProfile(profile);   // Commit profile changes.
                    currentProfile.SetProfile(profile); // Update CurrentProfile in session.
                }

                // Remove fullsize and thumbnail images from file system.
                DeleteFromFileSystem(image.Name, env.WebRootPath);

                // Delete image record from database.
                imageRepo.DeleteImage(image);
            }
        }
Beispiel #6
0
        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);
        }
        //-----------------------------------------UTIL---------------------------------------------//

        /*
         *   Preps a post to be sent back to the user.
         */
        public PostModel GetPostModel(int postId)
        {
            // Get handle on post by PostID.
            Post post = postRepo.ById(postId);

            // Get handle on post owner by ProfileID
            Profile profile = profileRepo.ById(post.ProfileId);

            int relationshipTier = friendRepo.RelationshipTier(currentProfile.id, profile.ProfileId);

            if (post.PrivacyLevel <= relationshipTier)
            {
                string   relationToUser         = friendRepo.RelationToUser(currentProfile.id, profile.ProfileId);
                DateTime?relationChangeDatetime = friendRepo.RelationshipChangeDatetime(currentProfile.id, postId);
                int?     blockerProfileId       = friendRepo.BlockerProfileId(currentProfile.id, postId);

                // Get handle on profile picture of owner of post.
                Image profilePicture = imageRepo.ById(profile.ProfilePicture);

                ProfileModel profileModel = Util.GetProfileModel(
                    profile, profilePicture, relationToUser, relationshipTier, relationChangeDatetime, blockerProfileId);

                RawImage postImage = Util.GetRawImage(imageRepo.ById(post.ImageId), 2);

                int  count    = likeRepo.CountByContentId(1, postId);
                bool hasLiked = likeRepo.HasLiked(1, postId, currentProfile.id);

                Like     like     = likeRepo.ByTypeAndProfileId(1, postId, currentProfile.id);
                DateTime dateTime = new DateTime();
                if (like != null)
                {
                    dateTime = like.DateTime.ToLocalTime();
                }

                LikeModel likes = new LikeModel
                {
                    ContentId   = postId,
                    ContentType = 1,
                    Count       = count,
                    HasLiked    = hasLiked,
                    DateTime    = dateTime
                };

                // Prep post.
                PostModel postModel = new PostModel
                {
                    // Details from post record.
                    PostId   = post.PostId,
                    Caption  = post.Caption,
                    DateTime = post.DateTime.ToLocalTime(),
                    Profile  = profileModel,
                    Likes    = likes,
                    Image    = postImage
                };

                // If the post does not have an image, set the image field to null.
                if (post.ImageId == 0)
                {
                    postModel.Image = null;
                }

                // Else attach prepped image.
                else
                {
                    postModel.Image = Util.GetRawImage(imageRepo.ById(post.ImageId), 2, true);
                }

                // Return prepped post to caller.
                return(postModel);
            }
            return(null);
        }