From 84b0da50932bb2471d52274b5c2af14073da2b7f Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Mon, 8 Mar 2021 19:27:08 -0500 Subject: [PATCH] better logging --- src/BirdsiteLive.Domain/ActivityPubService.cs | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/BirdsiteLive.Domain/ActivityPubService.cs b/src/BirdsiteLive.Domain/ActivityPubService.cs index 53c656c..fa3bf70 100644 --- a/src/BirdsiteLive.Domain/ActivityPubService.cs +++ b/src/BirdsiteLive.Domain/ActivityPubService.cs @@ -9,6 +9,7 @@ using BirdsiteLive.ActivityPub; using BirdsiteLive.ActivityPub.Converters; using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Settings; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Org.BouncyCastle.Bcpg; @@ -27,13 +28,15 @@ namespace BirdsiteLive.Domain private readonly InstanceSettings _instanceSettings; private readonly IHttpClientFactory _httpClientFactory; private readonly ICryptoService _cryptoService; + private readonly ILogger _logger; #region Ctor - public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings, IHttpClientFactory httpClientFactory) + public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings, IHttpClientFactory httpClientFactory, ILogger logger) { _cryptoService = cryptoService; _instanceSettings = instanceSettings; _httpClientFactory = httpClientFactory; + _logger = logger; } #endregion @@ -48,26 +51,34 @@ namespace BirdsiteLive.Domain public async Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox) { - var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); - var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId); - - var now = DateTime.UtcNow; - var nowString = now.ToString("s") + "Z"; - - var noteActivity = new ActivityCreateNote() + try { - context = "https://www.w3.org/ns/activitystreams", - id = $"{noteUri}/activity", - type = "Create", - actor = actor, - published = nowString, + var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); + var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId); - to = note.to, - cc = note.cc, - apObject = note - }; + var now = DateTime.UtcNow; + var nowString = now.ToString("s") + "Z"; - await PostDataAsync(noteActivity, targetHost, actor, targetInbox); + var noteActivity = new ActivityCreateNote() + { + context = "https://www.w3.org/ns/activitystreams", + id = $"{noteUri}/activity", + type = "Create", + actor = actor, + published = nowString, + + to = note.to, + cc = note.cc, + apObject = note + }; + + await PostDataAsync(noteActivity, targetHost, actor, targetInbox); + } + catch (Exception e) + { + _logger.LogError(e, "Error sending {Username} post ({NoteId}) to {Host}{Inbox}", username, noteId, targetHost, targetInbox); + throw; + } } public async Task PostDataAsync(T data, string targetHost, string actorUrl, string inbox = null)