added migration calls

This commit is contained in:
Nicolas Constant
2022-12-28 17:52:05 -05:00
parent f7ca9fd86d
commit f45e9ed9f7
5 changed files with 136 additions and 9 deletions

View File

@@ -5,5 +5,7 @@ namespace BirdsiteLive.Common.Regexes
public class UrlRegexes public class UrlRegexes
{ {
public static readonly Regex Url = new Regex(@"(.?)(((http|ftp|https):\/\/)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)"); public static readonly Regex Url = new Regex(@"(.?)(((http|ftp|https):\/\/)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");
public static readonly Regex Domain = new Regex(@"^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)+$");
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using BirdsiteLive.Twitter; using BirdsiteLive.Twitter;
using System.Security.Cryptography; using System.Security.Cryptography;
@@ -11,25 +12,31 @@ using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.Common.Settings; using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Domain.Enum; using BirdsiteLive.Domain.Enum;
using System.Net.Http;
using BirdsiteLive.Common.Regexes;
namespace BirdsiteLive.Domain namespace BirdsiteLive.Domain
{ {
public class MigrationService public class MigrationService
{ {
private readonly InstanceSettings _instanceSettings; private readonly InstanceSettings _instanceSettings;
private readonly ITheFedInfoService _theFedInfoService;
private readonly ITwitterTweetsService _twitterTweetsService; private readonly ITwitterTweetsService _twitterTweetsService;
private readonly IActivityPubService _activityPubService; private readonly IActivityPubService _activityPubService;
private readonly ITwitterUserDal _twitterUserDal; private readonly ITwitterUserDal _twitterUserDal;
private readonly IFollowersDal _followersDal; private readonly IFollowersDal _followersDal;
private readonly IHttpClientFactory _httpClientFactory;
#region Ctor #region Ctor
public MigrationService(ITwitterTweetsService twitterTweetsService, IActivityPubService activityPubService, ITwitterUserDal twitterUserDal, IFollowersDal followersDal, InstanceSettings instanceSettings) public MigrationService(ITwitterTweetsService twitterTweetsService, IActivityPubService activityPubService, ITwitterUserDal twitterUserDal, IFollowersDal followersDal, InstanceSettings instanceSettings, ITheFedInfoService theFedInfoService, IHttpClientFactory httpClientFactory)
{ {
_twitterTweetsService = twitterTweetsService; _twitterTweetsService = twitterTweetsService;
_activityPubService = activityPubService; _activityPubService = activityPubService;
_twitterUserDal = twitterUserDal; _twitterUserDal = twitterUserDal;
_followersDal = followersDal; _followersDal = followersDal;
_instanceSettings = instanceSettings; _instanceSettings = instanceSettings;
_theFedInfoService = theFedInfoService;
_httpClientFactory = httpClientFactory;
} }
#endregion #endregion
@@ -245,14 +252,58 @@ namespace BirdsiteLive.Domain
}); });
} }
public async Task TriggerRemoteMigrationAsync(string id, string tweetid, string handle) public async Task TriggerRemoteMigrationAsync(string id, string tweetIdStg, string handle)
{ {
//TODO var url = $"https://{{0}}/migration/move/{{1}}/{{2}}/{handle}";
await ProcessRemoteMigrationAsync(id, tweetIdStg, url);
} }
public async Task TriggerRemoteDeleteAsync(string id, string tweetid) public async Task TriggerRemoteDeleteAsync(string id, string tweetIdStg)
{ {
//TODO var url = $"https://{{0}}/migration/delete/{{1}}/{{2}}";
await ProcessRemoteMigrationAsync(id, tweetIdStg, url);
}
private async Task ProcessRemoteMigrationAsync(string id, string tweetIdStg, string urlPattern)
{
try
{
var instances = await RetrieveCompatibleBslInstancesAsync();
var tweetId = ExtractedTweetId(tweetIdStg);
foreach (var instance in instances)
{
try
{
var host = instance.Host;
if(!UrlRegexes.Domain.IsMatch(host)) continue;
var url = string.Format(urlPattern, host, id, tweetId);
var client = _httpClientFactory.CreateClient();
var result = await client.PostAsync(url, null);
result.EnsureSuccessStatusCode();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private async Task<List<BslInstanceInfo>> RetrieveCompatibleBslInstancesAsync()
{
var instances = await _theFedInfoService.GetBslInstanceListAsync();
var filteredInstances = instances
.Where(x => x.Version >= new Version(0, 21, 0))
.ToList();
return filteredInstances;
} }
private byte[] GetHash(string inputString) private byte[] GetHash(string inputString)

View File

@@ -104,7 +104,7 @@ namespace BirdsiteLive.Controllers
try try
{ {
await _migrationService.MigrateAccountAsync(fediverseUserValidation, id); await _migrationService.MigrateAccountAsync(fediverseUserValidation, id);
await _migrationService.TriggerRemoteMigrationAsync(id, tweetid, handle); _migrationService.TriggerRemoteMigrationAsync(id, tweetid, handle);
data.MigrationSuccess = true; data.MigrationSuccess = true;
} }
catch (Exception e) catch (Exception e)
@@ -157,7 +157,7 @@ namespace BirdsiteLive.Controllers
try try
{ {
await _migrationService.DeleteAccountAsync(id); await _migrationService.DeleteAccountAsync(id);
await _migrationService.TriggerRemoteDeleteAsync(id, tweetid); _migrationService.TriggerRemoteDeleteAsync(id, tweetid);
data.MigrationSuccess = true; data.MigrationSuccess = true;
} }
catch (Exception e) catch (Exception e)

View File

@@ -0,0 +1,72 @@
using BirdsiteLive.Common.Regexes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BirdsiteLive.Common.Tests
{
[TestClass]
public class UrlRegexesTests
{
[TestMethod]
public void Url_Test()
{
var input = "https://misskey.tdl/users/8hwf6zy2k1#main-key";
Assert.IsTrue(UrlRegexes.Url.IsMatch(input));
}
[TestMethod]
public void Url_Not_Test()
{
var input = "misskey.tdl/users/8hwf6zy2k1#main-key";
Assert.IsFalse(UrlRegexes.Url.IsMatch(input));
}
[TestMethod]
public void Domain_Test()
{
var input = "misskey-data_sq.tdl";
Assert.IsTrue(UrlRegexes.Domain.IsMatch(input));
}
[TestMethod]
public void Domain_Numbers_Test()
{
var input = "miss45654QAzedqskey-data_sq.tdl";
Assert.IsTrue(UrlRegexes.Domain.IsMatch(input));
}
[TestMethod]
public void Domain_Subdomain_Test()
{
var input = "s.sub.dqdq-_Dz9sd.tdl";
Assert.IsTrue(UrlRegexes.Domain.IsMatch(input));
}
[TestMethod]
public void Domain_Not_Test()
{
var input = "mis$s45654QAzedqskey-data_sq.tdl";
Assert.IsFalse(UrlRegexes.Domain.IsMatch(input));
}
[TestMethod]
public void Domain_Slash_Test()
{
var input = "miss45654QAz/edqskey-data_sq.tdl";
Assert.IsFalse(UrlRegexes.Domain.IsMatch(input));
}
[TestMethod]
public void Domain_NotSub_Test()
{
var input = ".mis$s45654QAzedqskey-data_sq.tdl";
Assert.IsFalse(UrlRegexes.Domain.IsMatch(input));
}
[TestMethod]
public void Domain_NotExt_Test()
{
var input = ".mis$s45654QAzedqskey-data_sq.tdl";
Assert.IsFalse(UrlRegexes.Domain.IsMatch(input));
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.Common.Regexes;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using Moq;
@@ -27,6 +28,7 @@ namespace BirdsiteLive.Domain.Tests
foreach (var instanceInfo in bslInstanceList) foreach (var instanceInfo in bslInstanceList)
{ {
Assert.IsFalse(string.IsNullOrWhiteSpace(instanceInfo.Host)); Assert.IsFalse(string.IsNullOrWhiteSpace(instanceInfo.Host));
Assert.IsTrue(UrlRegexes.Domain.IsMatch(instanceInfo.Host));
Assert.IsTrue(instanceInfo.Version > new Version(0, 1, 0)); Assert.IsTrue(instanceInfo.Version > new Version(0, 1, 0));
} }
} }