refactoring

This commit is contained in:
Nicolas Constant 2021-02-14 01:51:54 -05:00
parent 537270cceb
commit af092d942d
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
4 changed files with 104 additions and 47 deletions

View file

@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub;
using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain;
namespace BirdsiteLive.Moderation.Actions
{
public interface IRejectAllFollowingsAction
{
Task ProcessAsync(Follower follower);
}
public class RejectAllFollowingsAction : IRejectAllFollowingsAction
{
private readonly ITwitterUserDal _twitterUserDal;
private readonly IUserService _userService;
private readonly InstanceSettings _instanceSettings;
#region Ctor
public RejectAllFollowingsAction(ITwitterUserDal twitterUserDal, IUserService userService, InstanceSettings instanceSettings)
{
_twitterUserDal = twitterUserDal;
_userService = userService;
_instanceSettings = instanceSettings;
}
#endregion
public async Task ProcessAsync(Follower follower)
{
foreach (var following in follower.Followings)
{
try
{
var f = await _twitterUserDal.GetTwitterUserAsync(following);
var activityFollowing = new ActivityFollow
{
type = "Follow",
actor = follower.ActorId,
apObject = UrlFactory.GetActorUrl(_instanceSettings.Domain, f.Acct)
};
await _userService.SendRejectFollowAsync(activityFollowing, follower.Host);
}
catch (Exception) { }
}
}
}
}

View file

@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub;
using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain;
namespace BirdsiteLive.Moderation.Actions
{
public interface IRejectFollowingAction
{
Task ProcessAsync(Follower follower, SyncTwitterUser twitterUser);
}
public class RejectFollowingAction : IRejectFollowingAction
{
private readonly IUserService _userService;
private readonly InstanceSettings _instanceSettings;
#region Ctor
public RejectFollowingAction(IUserService userService, InstanceSettings instanceSettings)
{
_userService = userService;
_instanceSettings = instanceSettings;
}
#endregion
public async Task ProcessAsync(Follower follower, SyncTwitterUser twitterUser)
{
try
{
var activityFollowing = new ActivityFollow
{
type = "Follow",
actor = follower.ActorId,
apObject = UrlFactory.GetActorUrl(_instanceSettings.Domain, twitterUser.Acct)
};
await _userService.SendRejectFollowAsync(activityFollowing, follower.Host);
}
catch (Exception) { }
}
}
}

View file

@ -19,23 +19,21 @@ namespace BirdsiteLive.Moderation.Actions
{ {
private readonly IFollowersDal _followersDal; private readonly IFollowersDal _followersDal;
private readonly ITwitterUserDal _twitterUserDal; private readonly ITwitterUserDal _twitterUserDal;
private readonly IUserService _userService; private readonly IRejectAllFollowingsAction _rejectAllFollowingsAction;
private readonly InstanceSettings _instanceSettings;
#region Ctor #region Ctor
public RemoveFollowerAction(IFollowersDal followersDal, ITwitterUserDal twitterUserDal, IUserService userService, InstanceSettings instanceSettings) public RemoveFollowerAction(IFollowersDal followersDal, ITwitterUserDal twitterUserDal, IRejectAllFollowingsAction rejectAllFollowingsAction)
{ {
_followersDal = followersDal; _followersDal = followersDal;
_twitterUserDal = twitterUserDal; _twitterUserDal = twitterUserDal;
_userService = userService; _rejectAllFollowingsAction = rejectAllFollowingsAction;
_instanceSettings = instanceSettings;
} }
#endregion #endregion
public async Task ProcessAsync(Follower follower) public async Task ProcessAsync(Follower follower)
{ {
// Perform undo following to user instance // Perform undo following to user instance
await RejectAllFollowingsAsync(follower); await _rejectAllFollowingsAction.ProcessAsync(follower);
// Remove twitter users if no more followers // Remove twitter users if no more followers
var followings = follower.Followings; var followings = follower.Followings;
@ -49,25 +47,5 @@ namespace BirdsiteLive.Moderation.Actions
// Remove follower from DB // Remove follower from DB
await _followersDal.DeleteFollowerAsync(follower.Id); await _followersDal.DeleteFollowerAsync(follower.Id);
} }
private async Task RejectAllFollowingsAsync(Follower follower)
{
foreach (var following in follower.Followings)
{
try
{
var f = await _twitterUserDal.GetTwitterUserAsync(following);
var activityFollowing = new ActivityFollow
{
type = "Follow",
actor = follower.ActorId,
apObject = UrlFactory.GetActorUrl(_instanceSettings.Domain, f.Acct)
};
await _userService.SendRejectFollowAsync(activityFollowing, follower.Host);
}
catch (Exception) { }
}
}
} }
} }

View file

@ -19,16 +19,14 @@ namespace BirdsiteLive.Moderation.Actions
{ {
private readonly IFollowersDal _followersDal; private readonly IFollowersDal _followersDal;
private readonly ITwitterUserDal _twitterUserDal; private readonly ITwitterUserDal _twitterUserDal;
private readonly IUserService _userService; private readonly IRejectFollowingAction _rejectFollowingAction;
private readonly InstanceSettings _instanceSettings;
#region Ctor #region Ctor
public RemoveTwitterAccountAction(IFollowersDal followersDal, ITwitterUserDal twitterUserDal, InstanceSettings instanceSettings, IUserService userService) public RemoveTwitterAccountAction(IFollowersDal followersDal, ITwitterUserDal twitterUserDal, IRejectFollowingAction rejectFollowingAction)
{ {
_followersDal = followersDal; _followersDal = followersDal;
_twitterUserDal = twitterUserDal; _twitterUserDal = twitterUserDal;
_instanceSettings = instanceSettings; _rejectFollowingAction = rejectFollowingAction;
_userService = userService;
} }
#endregion #endregion
@ -42,7 +40,7 @@ namespace BirdsiteLive.Moderation.Actions
foreach (var follower in followers) foreach (var follower in followers)
{ {
// Perform undo following to user instance // Perform undo following to user instance
await RejectFollowingAsync(follower, twitterUser); await _rejectFollowingAction.ProcessAsync(follower, twitterUser);
// Remove following from DB // Remove following from DB
if (follower.Followings.Contains(twitterUserId)) if (follower.Followings.Contains(twitterUserId))
@ -60,20 +58,5 @@ namespace BirdsiteLive.Moderation.Actions
// Remove twitter user // Remove twitter user
await _twitterUserDal.DeleteTwitterUserAsync(twitterUser.Acct); await _twitterUserDal.DeleteTwitterUserAsync(twitterUser.Acct);
} }
private async Task RejectFollowingAsync(Follower follower, SyncTwitterUser twitterUser)
{
try
{
var activityFollowing = new ActivityFollow
{
type = "Follow",
actor = follower.ActorId,
apObject = UrlFactory.GetActorUrl(_instanceSettings.Domain, twitterUser.Acct)
};
await _userService.SendRejectFollowAsync(activityFollowing, follower.Host);
}
catch (Exception) { }
}
} }
} }