From 0e9938b712e3eb6cb4358933baf7099e53c6c2b7 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Wed, 9 Feb 2022 01:15:48 -0500 Subject: [PATCH] added user is gone exception --- src/BirdsiteLive.Domain/ActivityPubService.cs | 7 +++- .../Exceptions/UserIsGoneException.cs | 8 +++++ .../Controllers/UsersController.cs | 34 ++++++++++++------- 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/BirdsiteLive.Domain/Exceptions/UserIsGoneException.cs diff --git a/src/BirdsiteLive.Domain/ActivityPubService.cs b/src/BirdsiteLive.Domain/ActivityPubService.cs index 37fe0b5..c242a99 100644 --- a/src/BirdsiteLive.Domain/ActivityPubService.cs +++ b/src/BirdsiteLive.Domain/ActivityPubService.cs @@ -11,7 +11,6 @@ using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Settings; using Microsoft.Extensions.Logging; using Newtonsoft.Json; -using Org.BouncyCastle.Bcpg; namespace BirdsiteLive.Domain { @@ -45,6 +44,12 @@ namespace BirdsiteLive.Domain var httpClient = _httpClientFactory.CreateClient(); httpClient.DefaultRequestHeaders.Add("Accept", "application/activity+json"); var result = await httpClient.GetAsync(objectId); + + if (result.StatusCode == HttpStatusCode.Gone) + throw new UserIsGoneException(); + + result.EnsureSuccessStatusCode(); + var content = await result.Content.ReadAsStringAsync(); var actor = JsonConvert.DeserializeObject(content); diff --git a/src/BirdsiteLive.Domain/Exceptions/UserIsGoneException.cs b/src/BirdsiteLive.Domain/Exceptions/UserIsGoneException.cs new file mode 100644 index 0000000..96476fe --- /dev/null +++ b/src/BirdsiteLive.Domain/Exceptions/UserIsGoneException.cs @@ -0,0 +1,8 @@ +using System; + +namespace BirdsiteLive.Domain +{ + public class UserIsGoneException : Exception + { + } +} \ No newline at end of file diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index f59ae21..de63af1 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -183,33 +183,41 @@ namespace BirdsiteLive.Controllers switch (activity?.type) { case "Follow": - { - var succeeded = await _userService.FollowRequestedAsync(signature, r.Method, r.Path, - r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), activity as ActivityFollow, body); - if (succeeded) return Accepted(); - else return Unauthorized(); - } + { + var succeeded = await _userService.FollowRequestedAsync(signature, r.Method, r.Path, + r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), + activity as ActivityFollow, body); + if (succeeded) return Accepted(); + else return Unauthorized(); + } case "Undo": if (activity is ActivityUndoFollow) { var succeeded = await _userService.UndoFollowRequestedAsync(signature, r.Method, r.Path, - r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), activity as ActivityUndoFollow, body); + r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), + activity as ActivityUndoFollow, body); if (succeeded) return Accepted(); else return Unauthorized(); } + return Accepted(); case "Delete": - { - var succeeded = await _userService.DeleteRequestedAsync(signature, r.Method, r.Path, - r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), activity as ActivityDelete, body); - if (succeeded) return Accepted(); - else return Unauthorized(); - } + { + var succeeded = await _userService.DeleteRequestedAsync(signature, r.Method, r.Path, + r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers), + activity as ActivityDelete, body); + if (succeeded) return Accepted(); + else return Unauthorized(); + } default: return Accepted(); } } } + catch (UserIsGoneException) + { + return Accepted(); + } catch (UserNotFoundException) { return NotFound();