using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using xCommons.Extensions; using xDataService.Extensions; using xExceptions.Constants; using xIdentityModels.Constants; using xIdentityModels.Dtos; using xIdentityModels.Navigations; using xModels.Dtos; namespace xIds.Providers { public partial class XIdentityManager { // #region Friendship Actions ... // #region Actions ... /// /// Send a Follow Request for userSelectByParam to /// destUserSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollowing public async Task Follow( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // XFriendshipFollowing following = null; XFriendshipFollower follower = null; var isRequested = IsFollowRequested(xUser, xDestUser); // // Check Request Happens Or not ... if (!isRequested) { // following = new XFriendshipFollowing { UserId = xUser.Id, DestId = xDestUser.Id, State = XFriendshipState.Pending }; // follower = new XFriendshipFollower { UserId = xDestUser.Id, DestId = xUser.Id, State = XFriendshipState.Pending }; // xUser.Followings.Add(following); xDestUser.Followers.Add(follower); } else { // // if Request Done Before Must Check State ... following = xUser.Followings.FirstOrDefault(f => f.DestId == xDestUser.Id); follower = xDestUser.Followers.FirstOrDefault(f => f.DestId == xUser.Id); if (following.IsNull() || follower.IsNull()) { XException.NotFound.Throw(); } // switch (following.State) { case XFriendshipState.Accepted: case XFriendshipState.Blocked: throw XException.NotAllowed.ToException(); case XFriendshipState.Rejected: following.State = XFriendshipState.Pending; follower.State = XFriendshipState.Pending; break; } } // var canContinue = !following.IsNull() && !follower.IsNull(); if (!canContinue) { XException.ActionFailed.Throw(); } // await UpdateUserAsync(xUser); await UpdateUserAsync(xDestUser); // var result = following; // return result; } /// /// Cancel Following /// /// caller user identifier /// dest user identifier /// a boolean value public async Task Cancel( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // XFriendshipFollowing following = null; XFriendshipFollower follower = null; var isRequested = IsFollowRequested(xUser, xDestUser); // // Check Request Happens Or not ... if (!isRequested) { return false; } // // if Request Done Before Must Check State ... following = xUser.Followings.FirstOrDefault(f => f.DestId == xDestUser.Id); follower = xDestUser.Followers.FirstOrDefault(f => f.DestId == xUser.Id); if (following.IsNull() || follower.IsNull()) { XException.NotFound.Throw(); } // switch (following.State) { case XFriendshipState.Accepted: case XFriendshipState.Blocked: throw XException.NotAllowed.ToException(); case XFriendshipState.Rejected: case XFriendshipState.Pending: DbContext.Followers.Remove(follower); DbContext.Followings.Remove(following); break; } // var canContinue = !following.IsNull() && !follower.IsNull(); if (!canContinue) { XException.ActionFailed.Throw(); } // await DbContext.SaveChangesAsync(); // return true; } /// /// userSelectByParam remove friendship by it's Follower /// destUserSelectByParam /// /// caller user identifier /// dest user identifier /// public async Task UnFollowFollower( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); ValidateIsFollowers(xUser, xDestUser); // var follower = xUser.Followers .FirstOrDefault(f => f.DestId == xDestUser.Id && f.State == XFriendshipState.Accepted); var following = xDestUser.Followings .FirstOrDefault(f => f.DestId == xUser.Id && f.State == XFriendshipState.Accepted); if (follower.IsNull() || following.IsNull()) { XException.NotFound.Throw(); } // DbContext.Followers.Remove(follower); DbContext.Followings.Remove(following); // await DbContext.SaveChangesAsync(); } /// /// userSelectByParam remove friendship by it's Followings /// destUserSelectByParam /// /// caller user identifier /// dest user identifier /// public async Task UnFollowFollowing( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); ValidateIsFollowings(xUser, xDestUser); // var following = xUser.Followings .FirstOrDefault(f => f.DestId == xDestUser.Id && f.State == XFriendshipState.Accepted); var follower = xDestUser.Followers .FirstOrDefault(f => f.DestId == xUser.Id && f.State == XFriendshipState.Accepted); if (follower.IsNull() || following.IsNull()) { XException.NotAllowed.Throw(); } // DbContext.Followers.Remove(follower); DbContext.Followings.Remove(following); // await DbContext.SaveChangesAsync(); } /// /// userSelectByParam Block it's follower destUserSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollowing public async Task Block( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // // Validate XDestUser is Accepted Follower of XUser ... ValidateIsFollowers(xUser, xDestUser); // // Retrieve FriendShip Models ... var following = xDestUser.Followings .FirstOrDefault(f => f.DestId == xUser.Id && ( f.State == XFriendshipState.Accepted ) ); var follower = xUser.Followers .FirstOrDefault(f => f.DestId == xDestUser.Id && ( f.State == XFriendshipState.Accepted ) ); if (following.IsNull() || follower.IsNull()) { XException.NotAllowed.Throw(); } // follower.State = XFriendshipState.Blocked; following.State = XFriendshipState.Blocked; // DbContext.Followers.Update(follower); DbContext.Followings.Update(following); // await DbContext.SaveChangesAsync(); // var result = following; // return result; } /// /// userSelectByParam UnBlock it's follower destUserSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollowing public async Task UnBlock( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // // Validate XDestUser is Accepted Follower of XUser ... ValidateIsBlock(xUser, xDestUser); // // Retrieve FriendShip Models ... var following = xDestUser.Followings .FirstOrDefault(f => f.DestId == xUser.Id && ( f.State == XFriendshipState.Blocked ) ); var follower = xUser.Followers .FirstOrDefault(f => f.DestId == xDestUser.Id && ( f.State == XFriendshipState.Blocked ) ); if ( following.IsNull() || follower.IsNull() ) { XException.NotAllowed.Throw(); } // follower.State = XFriendshipState.Accepted; following.State = XFriendshipState.Accepted; // DbContext.Followers.Update(follower); DbContext.Followings.Update(following); // await DbContext.SaveChangesAsync(); // var result = following; // return result; } #endregion // #region Request Handlers ... /// /// userSelectByParam Accept Follow Request of /// destUserSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollower public async Task AcceptRequest( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // // Validate Request Sent and in Pending State ... ValidateFollowingRequestForAccept(xUser, xDestUser); // // Retrieve FriendShip Models ... var following = xDestUser.Followings .FirstOrDefault(f => f.DestId == xUser.Id && ( f.State == XFriendshipState.Pending || f.State == XFriendshipState.Rejected ) ); var follower = xUser.Followers .FirstOrDefault(f => f.DestId == xDestUser.Id && ( f.State == XFriendshipState.Pending || f.State == XFriendshipState.Rejected ) ); if (following.IsNull() || follower.IsNull()) { XException.NotAllowed.Throw(); } // follower.State = XFriendshipState.Accepted; following.State = XFriendshipState.Accepted; // await UpdateUserAsync(xUser); await UpdateUserAsync(xDestUser); // var result = follower; // return result; } /// /// Reject a Follow Request of destUserSelectByParam /// with userSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollower public async Task RejectRequest( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // // Validate Request Sent and in Pending State ... ValidateFollowingRequest(xUser, xDestUser); // // Retrieve Pending Specified User Request Friendship models ... var following = xDestUser.Followings .FirstOrDefault(f => f.DestId == xUser.Id && f.State == XFriendshipState.Pending); var follower = xUser.Followers .FirstOrDefault(f => f.DestId == xDestUser.Id && f.State == XFriendshipState.Pending); if (following.IsNull() || follower.IsNull()) { XException.NotFound.Throw(); } // follower.State = XFriendshipState.Rejected; following.State = XFriendshipState.Rejected; // DbContext.Followers.Update(follower); DbContext.Followings.Update(following); // await DbContext.SaveChangesAsync(); // var result = follower; // return result; } #endregion // #region Getters ... /// /// determines destUserSelectByParam is in followers of /// userSelectByParam or not /// /// caller user identifier /// dest user identifier /// check user is banned or not, default is true /// check a user can log in in system or not, default is true /// a boolean value public async Task IsFollower( string userSelectByParam, string destUserSelectByParam, bool checkIsBanned = true, bool checkCanLoginPolicies = true ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // var result = IsFollowRequested(xDestUser, xUser, XFriendshipState.Accepted); // return result; } /// /// retrieve Friendship state of destUserSelectByParam /// in Followers of userSelectByParam /// /// caller user identifier /// dest user identifier /// check user is banned or not, default is true /// check a user can log in in system or not, default is true /// a member of XFriendshipState public async Task GetFollowerState( string userSelectByParam, string destUserSelectByParam, bool checkIsBanned = true, bool checkCanLoginPolicies = true ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // var isFollow = IsFollowRequested(xDestUser, xUser); if (!isFollow) { return XFriendshipState.None; } // var follower = xUser.Followers.FirstOrDefault(f => f.DestId == xDestUser.Id); if (follower.IsNull()) { XException.NotFound.Throw(); } // return follower.State; } /// /// get follower friendship model of destUserSelectByParam /// in followers of userSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollower public async Task GetFollower( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // var isFollow = IsFollowRequested(xDestUser, xUser); if (!isFollow) { return null; } // var follower = xUser.Followers .FirstOrDefault(f => f.DestId == xDestUser.Id); if (follower.IsNull()) { XException.NotFound.Throw(); } // var result = follower; // return result; } /// /// Retrieve Accepted Followers list of userSelectByParam /// /// caller user identifier /// a collection of XFriendshipFollower instances public async Task> GetFollowers( string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // var result = xUser.Followers .Where(f => f.State == XFriendshipState.Accepted); // return result; } /// /// Retrieve all Followers list of userSelectByParam /// /// caller user identifier /// a collection of XFriendshipFollower instances public async Task> GetAllFollowers( string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // var result = xUser.Followers; // return result; } /// /// Read Specified User's Follower's List based on Query Model ... /// /// /// /// public async Task> QueryFollowers( XQuery query, string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotNull(query); ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: false, containDetails: false, ignoreDisabledUser: false, checkCanLoginPolicies: false ); // // Prepare Query ... // if (query.PageSize < DataConfiguration .PagingConfiguration .MinAvailablePageSize) { // query.PageSize = DataConfiguration .PagingConfiguration .DefaultPageSize; } // if (query.PageSize > DataConfiguration .PagingConfiguration .MaxAvailablePageSize) { // query.PageSize = DataConfiguration .PagingConfiguration .MaxAvailablePageSize; } // // Prepare Enumerator ... Expression> whereClause = x => x.UserId == xUser.Id; var enumerator = DbContext.Followers.AsAsyncEnumerable(); var resultItems = new List(); await foreach (var entity in enumerator) { // var isApproved = whereClause.Compile()(entity); if (isApproved) { // // Convert Approved Entity to Dto ... // then Validate it and Add it to Results ... var dto = await ToDto(entity); if (!dto.IsNull()) { resultItems.Add(dto); } } } // // Enumerable ... var result = resultItems.AsEnumerable(); // // Count Total Items ... var totalItemsCount = result.Count(); // // Apply Filter ... if (!query.Filter.IsNullOrEmpty()) { result = result.ApplyFilter(query.Filter); } // // Count Filtered Items ... var totalFilteredItemsCount = result.Count(); // // Apply Paging ... if (totalItemsCount > 0 && totalFilteredItemsCount > 0) { // // Apply Sorting ... if (!query.SortBy.IsNullOrEmpty()) { // // Since Sorting Based on Date is Very Important ... // we have to do Sorting at first Time then // try to apply Paging ... result = result .ApplySorting( query.SortBy, query.IsAscending) .ToList(); } // // Apply Paging ... result = result .ApplyPaging( query.Page, query.PageSize) .ToList(); } // // Calculating Page Count ... int pagesCount = totalItemsCount / query.PageSize; if (totalItemsCount % query.PageSize > 0) { pagesCount++; } // // Generate Result Object ... var queryResult = new XQueryResult { Page = query.Page, TotalPages = pagesCount, PageSize = query.PageSize, TotalItems = totalItemsCount, Items = result.AsEnumerable(), TotalFilteredItems = totalFilteredItemsCount }; // return queryResult; } /// /// determines destUserSelectByParam is in followings of /// userSelectByParam or not /// /// caller user identifier /// dest user identifier /// check user is banned or not, default is true /// check a user can log in in system or not, default is true /// a boolean value public async Task IsFollowing( string userSelectByParam, string destUserSelectByParam, bool checkIsBanned = true, bool checkCanLoginPolicies = true ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // var result = IsFollowRequested(xUser, xDestUser, XFriendshipState.Accepted); // return result; } /// /// retrieve Frindship State of destUserSelectByParam /// in Followings of userSelectByParam /// /// caller user identifier /// dest user identifier /// check user is banned or not, default is true /// check a user can log in in system or not, default is true /// a member of XFriendshipState public async Task GetFollowingState( string userSelectByParam, string destUserSelectByParam, bool checkIsBanned = true, bool checkCanLoginPolicies = true ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // var isFollow = IsFollowRequested(xUser, xDestUser); if (!isFollow) { return XFriendshipState.None; } // var following = xUser.Followings.FirstOrDefault(f => f.DestId == xDestUser.Id); if (following.IsNull()) { XException.NotFound.Throw(); } // return following.State; } /// /// get following friendship model of destUserSelectByParam /// in followings of userSelectByParam /// /// caller user identifier /// dest user identifier /// an instance of XFriendshipFollowing public async Task GetFollowing( string userSelectByParam, string destUserSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // // Validate Not Same Source and Dest Users ... ValidateNotSameUsers(xUser, xDestUser); // var isFollow = IsFollowRequested(xUser, xDestUser); if (!isFollow) { return null; } // var following = xUser.Followings.FirstOrDefault(f => f.DestId == xDestUser.Id); if (following.IsNull()) { XException.NotFound.Throw(); } // var result = following; // return result; } /// /// Retrieve accepted Followings List of userSelectByParam /// /// caller user identifier /// a collection of XFriendshipFollowing instances public async Task> GetFollowings( string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // var result = xUser.Followings .Where(f => f.State == XFriendshipState.Accepted); // return result; } /// /// Retrieve all Followings List of userSelectByParam /// /// caller user identifier /// a collection of XFriendshipFollowing instances public async Task> GetAllFollowings(string userSelectByParam) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: true, containDetails: true, ignoreDisabledUser: false, checkCanLoginPolicies: true ); // var result = xUser.Followings; // return result; } /// /// Read Specified User's Following's List based on Query Model ... /// /// /// /// public async Task> QueryFollowings( XQuery query, string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotNull(query); ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve User ... var xUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, checkIsBanned: false, containDetails: false, ignoreDisabledUser: false, checkCanLoginPolicies: false ); // // Prepare Query ... // if (query.PageSize < DataConfiguration .PagingConfiguration .MinAvailablePageSize) { // query.PageSize = DataConfiguration .PagingConfiguration .DefaultPageSize; } // if (query.PageSize > DataConfiguration .PagingConfiguration .MaxAvailablePageSize) { // query.PageSize = DataConfiguration .PagingConfiguration .MaxAvailablePageSize; } // // Prepare Enumerator ... Expression> whereClause = x => x.UserId == xUser.Id; var enumerator = DbContext.Followings.AsAsyncEnumerable(); var resultItems = new List(); await foreach (var entity in enumerator) { // var isApproved = whereClause.Compile()(entity); if (isApproved) { // // Convert Approved Entity to Dto ... // then Validate it and Add it to Results ... var dto = await ToDto(entity); if (!dto.IsNull()) { resultItems.Add(dto); } } } // // Enumerable ... var result = resultItems.AsEnumerable(); // // Count Total Items ... var totalItemsCount = result.Count(); // // Apply Filter ... if (!query.Filter.IsNullOrEmpty()) { result = result.ApplyFilter(query.Filter); } // // Count Filtered Items ... var totalFilteredItemsCount = result.Count(); // // Apply Paging ... if (totalItemsCount > 0 && totalFilteredItemsCount > 0) { // // Apply Sorting ... if (!query.SortBy.IsNullOrEmpty()) { // // Since Sorting Based on Date is Very Important ... // we have to do Sorting at first Time then // try to apply Paging ... result = result .ApplySorting( query.SortBy, query.IsAscending) .ToList(); } // // Apply Paging ... result = result .ApplyPaging( query.Page, query.PageSize) .ToList(); } // // Calculating Page Count ... int pagesCount = totalItemsCount / query.PageSize; if (totalItemsCount % query.PageSize > 0) { pagesCount++; } // // Generate Result Object ... var queryResult = new XQueryResult { Page = query.Page, TotalPages = pagesCount, PageSize = query.PageSize, TotalItems = totalItemsCount, Items = result.AsEnumerable(), TotalFilteredItems = totalFilteredItemsCount }; // return queryResult; } #endregion // #region Others ... /// /// Retrieve List Of All Followings User Id's /// of userSelectByParam /// /// caller user identifier /// a collection of user identifiers public async Task> GetFollowingList( string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve followings List ... var followings = await GetFollowings(userSelectByParam); // var result = followings.Select(f => f.DestId); // return result.ToList(); } /// /// Retrieve List Of All Followers User Id's /// of userSelectByParam /// /// caller user identifier /// a collection of user identifiers public async Task> GetFollowersList( string userSelectByParam ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate and Retrieve followings List ... var followers = await GetFollowers(userSelectByParam); // var result = followers.Select(f => f.DestId); // return result.ToList(); } /// /// retrieve Friendship Info Model for Specific User /// /// caller user identifier /// dest user identifier /// check a user can log in in system or not, default is true /// check user is banned or not, default is true /// an instance of XFriendshipInfoDto public async Task GetFriendshipInfo( string userSelectByParam, string destUserSelectByParam, bool checkCanLoginPolicies = true, bool checkIsBanned = true ) { // // Validate Args ... ValidationProvider.NotEmpty( userSelectByParam, destUserSelectByParam ); // // Validate and Retrieve Dest User ... var xSourceUser = await ValidateUserExistsAndRetrieve( userSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // // Validate and Retrieve Dest User ... var xDestUser = await ValidateUserExistsAndRetrieve( destUserSelectByParam, forceAdmin: false, containDetails: true, ignoreDisabledUser: false, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // var isSame = xSourceUser.Id == xDestUser.Id; var isFollower = false; var isFollowing = false; var followerState = XFriendshipState.None; var followingState = XFriendshipState.None; // if (!isSame) { // isFollower = await IsFollower( userSelectByParam, destUserSelectByParam, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // isFollowing = await IsFollowing( userSelectByParam, destUserSelectByParam, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // followerState = await GetFollowerState( userSelectByParam, destUserSelectByParam, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); // followingState = await GetFollowingState( userSelectByParam, destUserSelectByParam, checkIsBanned: checkIsBanned, checkCanLoginPolicies: checkCanLoginPolicies ); } // var result = new XFriendshipInfoDto { UserId = xDestUser.Id, IsFollower = isFollower, IsFollowing = isFollowing, FollowerState = followerState, FollowingState = followingState, Followers = xDestUser.Followers .Count(f => f.State == XFriendshipState.Accepted), Followings = xDestUser.Followings .Count(f => f.State == XFriendshipState.Accepted) }; // return result; } #endregion #endregion } }