2021-02-12 00:31:00 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2021-02-05 01:12:54 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-02-12 00:31:00 -05:00
|
|
|
|
using BirdsiteLive.ActivityPub;
|
|
|
|
|
using BirdsiteLive.ActivityPub.Converters;
|
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2021-02-05 01:12:54 -05:00
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
|
|
|
|
using BirdsiteLive.DAL.Models;
|
2021-02-12 00:31:00 -05:00
|
|
|
|
using BirdsiteLive.Domain;
|
2021-02-05 01:12:54 -05:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Moderation.Actions
|
|
|
|
|
{
|
|
|
|
|
public interface IRemoveTwitterAccountAction
|
|
|
|
|
{
|
|
|
|
|
Task ProcessAsync(SyncTwitterUser twitterUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RemoveTwitterAccountAction : IRemoveTwitterAccountAction
|
|
|
|
|
{
|
|
|
|
|
private readonly IFollowersDal _followersDal;
|
|
|
|
|
private readonly ITwitterUserDal _twitterUserDal;
|
2021-02-12 00:31:00 -05:00
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
private readonly InstanceSettings _instanceSettings;
|
2021-02-05 01:12:54 -05:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2021-02-12 00:31:00 -05:00
|
|
|
|
public RemoveTwitterAccountAction(IFollowersDal followersDal, ITwitterUserDal twitterUserDal, InstanceSettings instanceSettings, IUserService userService)
|
2021-02-05 01:12:54 -05:00
|
|
|
|
{
|
|
|
|
|
_followersDal = followersDal;
|
|
|
|
|
_twitterUserDal = twitterUserDal;
|
2021-02-12 00:31:00 -05:00
|
|
|
|
_instanceSettings = instanceSettings;
|
|
|
|
|
_userService = userService;
|
2021-02-05 01:12:54 -05:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public async Task ProcessAsync(SyncTwitterUser twitterUser)
|
|
|
|
|
{
|
|
|
|
|
// Check Followers
|
|
|
|
|
var twitterUserId = twitterUser.Id;
|
|
|
|
|
var followers = await _followersDal.GetFollowersAsync(twitterUserId);
|
|
|
|
|
|
|
|
|
|
// Remove all Followers
|
|
|
|
|
foreach (var follower in followers)
|
|
|
|
|
{
|
|
|
|
|
// Perform undo following to user instance
|
2021-02-12 00:31:00 -05:00
|
|
|
|
await RejectFollowingAsync(follower, twitterUser);
|
2021-02-05 01:12:54 -05:00
|
|
|
|
|
|
|
|
|
// Remove following from DB
|
|
|
|
|
if (follower.Followings.Contains(twitterUserId))
|
|
|
|
|
follower.Followings.Remove(twitterUserId);
|
|
|
|
|
|
|
|
|
|
if (follower.FollowingsSyncStatus.ContainsKey(twitterUserId))
|
|
|
|
|
follower.FollowingsSyncStatus.Remove(twitterUserId);
|
|
|
|
|
|
|
|
|
|
if (follower.Followings.Any())
|
|
|
|
|
await _followersDal.UpdateFollowerAsync(follower);
|
|
|
|
|
else
|
|
|
|
|
await _followersDal.DeleteFollowerAsync(follower.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove twitter user
|
|
|
|
|
await _twitterUserDal.DeleteTwitterUserAsync(twitterUser.Acct);
|
|
|
|
|
}
|
2021-02-12 00:31:00 -05:00
|
|
|
|
|
|
|
|
|
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) { }
|
|
|
|
|
}
|
2021-02-05 01:12:54 -05:00
|
|
|
|
}
|
|
|
|
|
}
|