2022-11-02 05:10:46 +01:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-11-02 04:38:54 +01:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2022-11-02 05:10:46 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2022-11-02 04:38:54 +01:00
|
|
|
|
using Npgsql.TypeHandlers;
|
2022-11-02 05:10:46 +01:00
|
|
|
|
using BirdsiteLive.Domain;
|
2022-12-14 04:55:22 +01:00
|
|
|
|
using BirdsiteLive.Domain.Enum;
|
2022-12-25 06:09:38 +01:00
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
2022-12-28 21:00:14 +01:00
|
|
|
|
using BirdsiteLive.Models;
|
2022-12-29 00:29:57 +01:00
|
|
|
|
using System.Reflection.Metadata;
|
2022-11-02 04:38:54 +01:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class MigrationController : Controller
|
|
|
|
|
{
|
2022-11-02 05:10:46 +01:00
|
|
|
|
private readonly MigrationService _migrationService;
|
2022-12-25 06:09:38 +01:00
|
|
|
|
private readonly ITwitterUserDal _twitterUserDal;
|
|
|
|
|
|
2022-11-02 05:10:46 +01:00
|
|
|
|
#region Ctor
|
2022-12-25 06:09:38 +01:00
|
|
|
|
public MigrationController(MigrationService migrationService, ITwitterUserDal twitterUserDal)
|
2022-11-02 05:10:46 +01:00
|
|
|
|
{
|
|
|
|
|
_migrationService = migrationService;
|
2022-12-25 06:09:38 +01:00
|
|
|
|
_twitterUserDal = twitterUserDal;
|
2022-11-02 05:10:46 +01:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-11-02 04:38:54 +01:00
|
|
|
|
[HttpGet]
|
2022-12-14 04:55:22 +01:00
|
|
|
|
[Route("/migration/move/{id}")]
|
|
|
|
|
public IActionResult IndexMove(string id)
|
2022-11-02 04:38:54 +01:00
|
|
|
|
{
|
2022-11-02 05:10:46 +01:00
|
|
|
|
var migrationCode = _migrationService.GetMigrationCode(id);
|
2022-11-02 04:38:54 +01:00
|
|
|
|
var data = new MigrationData()
|
|
|
|
|
{
|
|
|
|
|
Acct = id,
|
|
|
|
|
MigrationCode = migrationCode
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-14 04:55:22 +01:00
|
|
|
|
return View("Index", data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/migration/delete/{id}")]
|
|
|
|
|
public IActionResult IndexDelete(string id)
|
|
|
|
|
{
|
|
|
|
|
var migrationCode = _migrationService.GetDeletionCode(id);
|
|
|
|
|
var data = new MigrationData()
|
|
|
|
|
{
|
|
|
|
|
Acct = id,
|
|
|
|
|
MigrationCode = migrationCode
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View("Delete", data);
|
2022-11-02 04:38:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2022-12-14 04:55:22 +01:00
|
|
|
|
[Route("/migration/move/{id}")]
|
|
|
|
|
public async Task<IActionResult> MigrateMove(string id, string tweetid, string handle)
|
2022-11-02 04:38:54 +01:00
|
|
|
|
{
|
2022-11-02 05:10:46 +01:00
|
|
|
|
var migrationCode = _migrationService.GetMigrationCode(id);
|
2022-11-02 04:38:54 +01:00
|
|
|
|
var data = new MigrationData()
|
|
|
|
|
{
|
|
|
|
|
Acct = id,
|
|
|
|
|
MigrationCode = migrationCode,
|
|
|
|
|
|
|
|
|
|
IsAcctProvided = !string.IsNullOrWhiteSpace(handle),
|
|
|
|
|
IsTweetProvided = !string.IsNullOrWhiteSpace(tweetid),
|
|
|
|
|
|
|
|
|
|
TweetId = tweetid,
|
|
|
|
|
FediverseAccount = handle
|
|
|
|
|
};
|
2022-11-04 07:07:50 +01:00
|
|
|
|
ValidatedFediverseUser fediverseUserValidation = null;
|
|
|
|
|
|
2022-12-25 06:09:38 +01:00
|
|
|
|
//Verify can be migrated
|
|
|
|
|
var twitterAccount = await _twitterUserDal.GetTwitterUserAsync(id);
|
2022-12-26 00:15:54 +01:00
|
|
|
|
if (twitterAccount != null && twitterAccount.Deleted)
|
2022-12-25 06:09:38 +01:00
|
|
|
|
{
|
|
|
|
|
data.ErrorMessage = "This account has been deleted, it can't be migrated";
|
|
|
|
|
return View("Index", data);
|
|
|
|
|
}
|
2022-12-29 00:21:10 +01:00
|
|
|
|
if (twitterAccount != null &&
|
|
|
|
|
(!string.IsNullOrWhiteSpace(twitterAccount.MovedTo)
|
2022-12-26 00:15:54 +01:00
|
|
|
|
|| !string.IsNullOrWhiteSpace(twitterAccount.MovedToAcct)))
|
2022-12-25 06:09:38 +01:00
|
|
|
|
{
|
|
|
|
|
data.ErrorMessage = "This account has been moved already, it can't be migrated again";
|
|
|
|
|
return View("Index", data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start migration
|
2022-11-02 05:10:46 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-11-04 07:07:50 +01:00
|
|
|
|
fediverseUserValidation = await _migrationService.ValidateFediverseAcctAsync(handle);
|
2022-12-14 04:55:22 +01:00
|
|
|
|
var isTweetValid = _migrationService.ValidateTweet(id, tweetid, MigrationTypeEnum.Migration);
|
2022-11-02 04:38:54 +01:00
|
|
|
|
|
2022-11-04 07:07:50 +01:00
|
|
|
|
data.IsAcctValid = fediverseUserValidation.IsValid;
|
2022-11-02 05:10:46 +01:00
|
|
|
|
data.IsTweetValid = isTweetValid;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
data.ErrorMessage = e.Message;
|
|
|
|
|
}
|
2022-12-14 06:17:00 +01:00
|
|
|
|
|
2022-11-04 07:07:50 +01:00
|
|
|
|
if (data.IsAcctValid && data.IsTweetValid && fediverseUserValidation != null)
|
2022-11-02 05:10:46 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-21 00:47:21 +01:00
|
|
|
|
await _migrationService.MigrateAccountAsync(fediverseUserValidation, id);
|
2022-12-28 23:52:05 +01:00
|
|
|
|
_migrationService.TriggerRemoteMigrationAsync(id, tweetid, handle);
|
2022-11-02 05:10:46 +01:00
|
|
|
|
data.MigrationSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
data.ErrorMessage = e.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View("Index", data);
|
2022-11-02 04:38:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 05:10:46 +01:00
|
|
|
|
[HttpPost]
|
2022-12-14 04:55:22 +01:00
|
|
|
|
[Route("/migration/delete/{id}")]
|
|
|
|
|
public async Task<IActionResult> MigrateDelete(string id, string tweetid)
|
|
|
|
|
{
|
2022-12-26 00:15:54 +01:00
|
|
|
|
var deletionCode = _migrationService.GetDeletionCode(id);
|
2022-12-14 04:55:22 +01:00
|
|
|
|
|
|
|
|
|
var data = new MigrationData()
|
|
|
|
|
{
|
|
|
|
|
Acct = id,
|
2022-12-26 00:15:54 +01:00
|
|
|
|
MigrationCode = deletionCode,
|
2022-12-14 04:55:22 +01:00
|
|
|
|
|
|
|
|
|
IsTweetProvided = !string.IsNullOrWhiteSpace(tweetid),
|
|
|
|
|
|
|
|
|
|
TweetId = tweetid
|
|
|
|
|
};
|
2022-12-29 00:21:10 +01:00
|
|
|
|
|
2022-12-25 06:09:38 +01:00
|
|
|
|
//Verify can be deleted
|
|
|
|
|
var twitterAccount = await _twitterUserDal.GetTwitterUserAsync(id);
|
2022-12-26 00:15:54 +01:00
|
|
|
|
if (twitterAccount != null && twitterAccount.Deleted)
|
2022-12-25 06:09:38 +01:00
|
|
|
|
{
|
|
|
|
|
data.ErrorMessage = "This account has been deleted, it can't be deleted again";
|
2022-12-26 00:15:54 +01:00
|
|
|
|
return View("Delete", data);
|
2022-12-25 06:09:38 +01:00
|
|
|
|
}
|
2022-12-14 04:55:22 +01:00
|
|
|
|
|
2022-12-25 06:09:38 +01:00
|
|
|
|
// Start deletion
|
2022-12-14 04:55:22 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-26 00:15:54 +01:00
|
|
|
|
var isTweetValid = _migrationService.ValidateTweet(id, tweetid, MigrationTypeEnum.Deletion);
|
2022-12-14 04:55:22 +01:00
|
|
|
|
data.IsTweetValid = isTweetValid;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
data.ErrorMessage = e.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.IsTweetValid)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-21 00:47:21 +01:00
|
|
|
|
await _migrationService.DeleteAccountAsync(id);
|
2022-12-28 23:52:05 +01:00
|
|
|
|
_migrationService.TriggerRemoteDeleteAsync(id, tweetid);
|
2022-12-14 04:55:22 +01:00
|
|
|
|
data.MigrationSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
data.ErrorMessage = e.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 00:15:54 +01:00
|
|
|
|
return View("Delete", data);
|
2022-12-14 04:55:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("/migration/move/{id}/{tweetid}/{handle}")]
|
|
|
|
|
public async Task<IActionResult> RemoteMigrateMove(string id, string tweetid, string handle)
|
2022-11-02 04:38:54 +01:00
|
|
|
|
{
|
2022-12-29 00:29:57 +01:00
|
|
|
|
//Check inputs
|
|
|
|
|
if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(tweetid) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(handle))
|
|
|
|
|
return StatusCode(422);
|
|
|
|
|
|
2022-12-25 06:09:38 +01:00
|
|
|
|
//Verify can be migrated
|
|
|
|
|
var twitterAccount = await _twitterUserDal.GetTwitterUserAsync(id);
|
2022-12-29 00:21:10 +01:00
|
|
|
|
if (twitterAccount != null && (twitterAccount.Deleted
|
2022-12-25 06:09:38 +01:00
|
|
|
|
|| !string.IsNullOrWhiteSpace(twitterAccount.MovedTo)
|
2022-12-29 00:21:10 +01:00
|
|
|
|
|| !string.IsNullOrWhiteSpace(twitterAccount.MovedToAcct)))
|
2022-12-25 06:09:38 +01:00
|
|
|
|
return Ok();
|
|
|
|
|
|
|
|
|
|
// Start migration
|
2022-11-04 07:07:50 +01:00
|
|
|
|
var fediverseUserValidation = await _migrationService.ValidateFediverseAcctAsync(handle);
|
2022-12-14 06:17:00 +01:00
|
|
|
|
var isTweetValid = _migrationService.ValidateTweet(id, tweetid, MigrationTypeEnum.Migration);
|
2022-11-02 05:10:46 +01:00
|
|
|
|
|
2022-11-04 07:07:50 +01:00
|
|
|
|
if (fediverseUserValidation.IsValid && isTweetValid)
|
2022-11-02 05:10:46 +01:00
|
|
|
|
{
|
2022-12-21 00:47:21 +01:00
|
|
|
|
await _migrationService.MigrateAccountAsync(fediverseUserValidation, id);
|
2022-11-02 05:10:46 +01:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 06:17:00 +01:00
|
|
|
|
return StatusCode(400);
|
2022-11-02 04:38:54 +01:00
|
|
|
|
}
|
2022-12-14 04:55:22 +01:00
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2022-12-25 06:10:37 +01:00
|
|
|
|
[Route("/migration/delete/{id}/{tweetid}")]
|
2022-12-14 06:17:00 +01:00
|
|
|
|
public async Task<IActionResult> RemoteMigrateDelete(string id, string tweetid)
|
2022-12-14 04:55:22 +01:00
|
|
|
|
{
|
2022-12-29 00:29:57 +01:00
|
|
|
|
//Check inputs
|
|
|
|
|
if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(tweetid))
|
|
|
|
|
return StatusCode(422);
|
|
|
|
|
|
2022-12-25 06:09:38 +01:00
|
|
|
|
//Verify can be deleted
|
|
|
|
|
var twitterAccount = await _twitterUserDal.GetTwitterUserAsync(id);
|
2022-12-29 00:21:10 +01:00
|
|
|
|
if (twitterAccount != null && twitterAccount.Deleted) return Ok();
|
2022-12-25 06:09:38 +01:00
|
|
|
|
|
|
|
|
|
// Start deletion
|
2022-12-14 06:17:00 +01:00
|
|
|
|
var isTweetValid = _migrationService.ValidateTweet(id, tweetid, MigrationTypeEnum.Deletion);
|
|
|
|
|
|
|
|
|
|
if (isTweetValid)
|
|
|
|
|
{
|
2022-12-21 00:47:21 +01:00
|
|
|
|
await _migrationService.DeleteAccountAsync(id);
|
2022-12-14 06:17:00 +01:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return StatusCode(400);
|
2022-12-14 04:55:22 +01:00
|
|
|
|
}
|
2022-11-02 04:38:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|