protected async Task UpdateRelationshipToUser()

in code/Server/WebRole/Controllers/RelationshipsControllerBase.cs [89:173]


        protected async Task<IHttpActionResult> UpdateRelationshipToUser(
            string callerClassName,
            string callerMethodName,
            RelationshipOperation relationshipOperation,
            string actedOnUserHandle)
        {
            string actorUserHandle = this.UserHandle;
            DateTime currentTime = DateTime.UtcNow;

            if (actorUserHandle == actedOnUserHandle)
            {
                this.BadRequest(ResponseStrings.NotAllowed);
            }

            IUserProfileEntity userProfileEntity = await this.usersManager.ReadUserProfile(actedOnUserHandle, this.AppHandle);
            if (userProfileEntity == null)
            {
                this.NotFound(ResponseStrings.UserNotFound);
            }

            if (relationshipOperation == RelationshipOperation.FollowUser
                && userProfileEntity.Visibility == UserVisibilityStatus.Private)
            {
                relationshipOperation = RelationshipOperation.PendingUser;
            }

            string followerKeyUserHandle = actorUserHandle;
            string followingKeyUserHandle = actedOnUserHandle;

            if (relationshipOperation == RelationshipOperation.FollowUser
                || relationshipOperation == RelationshipOperation.PendingUser
                || relationshipOperation == RelationshipOperation.UnfollowUser)
            {
                followerKeyUserHandle = actedOnUserHandle;
                followingKeyUserHandle = actorUserHandle;
            }

            IUserRelationshipLookupEntity followerRelationshipLookupEntity
                = await this.relationshipsManager.ReadFollowerRelationship(followerKeyUserHandle, followingKeyUserHandle, this.AppHandle);
            if (followerRelationshipLookupEntity != null && followerRelationshipLookupEntity.LastUpdatedTime > currentTime)
            {
                return this.Conflict(ResponseStrings.NewerItemExists);
            }

            IUserRelationshipLookupEntity followingRelationshipLookupEntity
                = await this.relationshipsManager.ReadFollowingRelationshipToUser(followingKeyUserHandle, followerKeyUserHandle, this.AppHandle);
            if (followingRelationshipLookupEntity != null && followingRelationshipLookupEntity.LastUpdatedTime > currentTime)
            {
                return this.Conflict(ResponseStrings.NewerItemExists);
            }

            if (relationshipOperation == RelationshipOperation.AcceptUser)
            {
                if (followerRelationshipLookupEntity == null
                    || followerRelationshipLookupEntity.UserRelationshipStatus != UserRelationshipStatus.Pending)
                {
                    return this.Forbidden(ResponseStrings.NotAllowed);
                }
            }

            string relationshipHandle = null;
            if (relationshipOperation == RelationshipOperation.AcceptUser
                || relationshipOperation == RelationshipOperation.BlockUser
                || relationshipOperation == RelationshipOperation.FollowUser
                || relationshipOperation == RelationshipOperation.PendingUser)
            {
                relationshipHandle = this.handleGenerator.GenerateShortHandle();
            }

            await this.relationshipsManager.UpdateRelationshipToUser(
                ProcessType.Frontend,
                relationshipOperation,
                relationshipHandle,
                followerKeyUserHandle,
                followingKeyUserHandle,
                this.AppHandle,
                currentTime,
                followerRelationshipLookupEntity,
                followingRelationshipLookupEntity);

            string logEntry = $"ActedOnUserHandle = {actedOnUserHandle}, RelationshipOperation = {relationshipOperation.ToString()}, RelationshipHandle = {relationshipHandle}";
            logEntry += $", OldRelationshipStatus = {followingRelationshipLookupEntity?.UserRelationshipStatus.ToString()}, NewRelationshipStatus = {relationshipOperation.ToString()}";
            this.LogControllerEnd(this.log, callerClassName, callerMethodName, logEntry);
            return this.NoContent();
        }