1680 lines
54 KiB
C#
1680 lines
54 KiB
C#
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 ...
|
|
/// <summary>
|
|
/// Send a Follow Request for userSelectByParam to
|
|
/// destUserSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollowing</see></returns>
|
|
public async Task<XFriendshipFollowing> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cancel Following
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>a boolean value</returns>
|
|
public async Task<bool> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// userSelectByParam remove friendship by it's Follower
|
|
/// destUserSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns></returns>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// userSelectByParam remove friendship by it's Followings
|
|
/// destUserSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns></returns>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// userSelectByParam Block it's follower destUserSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollowing</see></returns>
|
|
public async Task<XFriendshipFollowing> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// userSelectByParam UnBlock it's follower destUserSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollowing</see></returns>
|
|
public async Task<XFriendshipFollowing> 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 ...
|
|
/// <summary>
|
|
/// userSelectByParam Accept Follow Request of
|
|
/// destUserSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollower</see></returns>
|
|
public async Task<XFriendshipFollower> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reject a Follow Request of destUserSelectByParam
|
|
/// with userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollower</see></returns>
|
|
public async Task<XFriendshipFollower> 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 ...
|
|
/// <summary>
|
|
/// determines destUserSelectByParam is in followers of
|
|
/// userSelectByParam or not
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <param name="checkIsBanned">check user is banned or not, default is true</param>
|
|
/// <param name="checkCanLoginPolicies">check a user can log in in system or not, default is true</param>
|
|
/// <returns>a boolean value</returns>
|
|
public async Task<bool> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Friendship state of destUserSelectByParam
|
|
/// in Followers of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <param name="checkIsBanned">check user is banned or not, default is true</param>
|
|
/// <param name="checkCanLoginPolicies">check a user can log in in system or not, default is true</param>
|
|
/// <returns>a member of <see>XFriendshipState</see></returns>
|
|
public async Task<XFriendshipState> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// get follower friendship model of destUserSelectByParam
|
|
/// in followers of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollower</see></returns>
|
|
public async Task<XFriendshipFollower> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Accepted Followers list of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <returns>a collection of <see>XFriendshipFollower</see> instances</returns>
|
|
public async Task<IEnumerable<XFriendshipFollower>> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Followers list of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <returns>a collection of <see>XFriendshipFollower</see> instances</returns>
|
|
public async Task<IEnumerable<XFriendshipFollower>> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read Specified User's Follower's List based on Query Model ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="userSelectByParam"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XFriendDto>> 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<Func<XFriendshipFollower, bool>> whereClause = x => x.UserId == xUser.Id;
|
|
var enumerator = DbContext.Followers.AsAsyncEnumerable();
|
|
var resultItems = new List<XFriendDto>();
|
|
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<XFriendDto>
|
|
{
|
|
Page = query.Page,
|
|
TotalPages = pagesCount,
|
|
PageSize = query.PageSize,
|
|
TotalItems = totalItemsCount,
|
|
Items = result.AsEnumerable(),
|
|
TotalFilteredItems = totalFilteredItemsCount
|
|
};
|
|
|
|
//
|
|
return queryResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// determines destUserSelectByParam is in followings of
|
|
/// userSelectByParam or not
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <param name="checkIsBanned">check user is banned or not, default is true</param>
|
|
/// <param name="checkCanLoginPolicies">check a user can log in in system or not, default is true</param>
|
|
/// <returns>a boolean value</returns>
|
|
public async Task<bool> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Frindship State of destUserSelectByParam
|
|
/// in Followings of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <param name="checkIsBanned">check user is banned or not, default is true</param>
|
|
/// <param name="checkCanLoginPolicies">check a user can log in in system or not, default is true</param>
|
|
/// <returns>a member of <see>XFriendshipState</see></returns>
|
|
public async Task<XFriendshipState> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// get following friendship model of destUserSelectByParam
|
|
/// in followings of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <returns>an instance of <see>XFriendshipFollowing</see></returns>
|
|
public async Task<XFriendshipFollowing> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve accepted Followings List of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <returns>a collection of <see>XFriendshipFollowing</see> instances</returns>
|
|
public async Task<IEnumerable<XFriendshipFollowing>> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Followings List of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <returns>a collection of <see>XFriendshipFollowing</see> instances</returns>
|
|
public async Task<IEnumerable<XFriendshipFollowing>> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read Specified User's Following's List based on Query Model ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="userSelectByParam"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XFriendDto>> 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<Func<XFriendshipFollowing, bool>> whereClause = x => x.UserId == xUser.Id;
|
|
var enumerator = DbContext.Followings.AsAsyncEnumerable();
|
|
var resultItems = new List<XFriendDto>();
|
|
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<XFriendDto>
|
|
{
|
|
Page = query.Page,
|
|
TotalPages = pagesCount,
|
|
PageSize = query.PageSize,
|
|
TotalItems = totalItemsCount,
|
|
Items = result.AsEnumerable(),
|
|
TotalFilteredItems = totalFilteredItemsCount
|
|
};
|
|
|
|
//
|
|
return queryResult;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Others ...
|
|
/// <summary>
|
|
/// Retrieve List Of All Followings User Id's
|
|
/// of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <returns>a collection of user identifiers</returns>
|
|
public async Task<ICollection<string>> 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve List Of All Followers User Id's
|
|
/// of userSelectByParam
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <returns>a collection of user identifiers</returns>
|
|
public async Task<ICollection<string>> 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Friendship Info Model for Specific User
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">caller user identifier</param>
|
|
/// <param name="destUserSelectByParam">dest user identifier</param>
|
|
/// <param name="checkCanLoginPolicies">check a user can log in in system or not, default is true</param>
|
|
/// <param name="checkIsBanned">check user is banned or not, default is true</param>
|
|
/// <returns>an instance of <see>XFriendshipInfoDto</see></returns>
|
|
public async Task<XFriendshipInfoDto> 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
|
|
}
|
|
} |