♻️ Improve the code in activitypub and webfinger
This commit is contained in:
@@ -369,6 +369,7 @@ public class ActivityPubDeliveryService(
|
||||
private async Task<SnFediverseActor?> GetOrFetchActorAsync(string actorUri)
|
||||
{
|
||||
var actor = await db.FediverseActors
|
||||
.Include(a => a.Instance)
|
||||
.FirstOrDefaultAsync(a => a.Uri == actorUri);
|
||||
|
||||
if (actor != null)
|
||||
@@ -376,16 +377,6 @@ public class ActivityPubDeliveryService(
|
||||
|
||||
try
|
||||
{
|
||||
var response = await HttpClient.GetAsync(actorUri);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return null;
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var actorData = JsonSerializer.Deserialize<Dictionary<string, object>>(json);
|
||||
|
||||
if (actorData == null)
|
||||
return null;
|
||||
|
||||
var domain = new Uri(actorUri).Host;
|
||||
var instance = await db.FediverseInstances
|
||||
.FirstOrDefaultAsync(i => i.Domain == domain);
|
||||
@@ -399,26 +390,23 @@ public class ActivityPubDeliveryService(
|
||||
};
|
||||
db.FediverseInstances.Add(instance);
|
||||
await db.SaveChangesAsync();
|
||||
await discoveryService.FetchInstanceMetadataAsync(instance);
|
||||
}
|
||||
|
||||
actor = new SnFediverseActor
|
||||
{
|
||||
Uri = actorUri,
|
||||
Username = ExtractUsername(actorUri),
|
||||
DisplayName = actorData.GetValueOrDefault("name")?.ToString(),
|
||||
Bio = actorData.GetValueOrDefault("summary")?.ToString(),
|
||||
InboxUri = actorData.GetValueOrDefault("inbox")?.ToString(),
|
||||
OutboxUri = actorData.GetValueOrDefault("outbox")?.ToString(),
|
||||
FollowersUri = actorData.GetValueOrDefault("followers")?.ToString(),
|
||||
FollowingUri = actorData.GetValueOrDefault("following")?.ToString(),
|
||||
AvatarUrl = actorData.GetValueOrDefault("icon")?.ToString(),
|
||||
InstanceId = instance.Id
|
||||
InstanceId = instance.Id,
|
||||
LastFetchedAt = NodaTime.SystemClock.Instance.GetCurrentInstant()
|
||||
};
|
||||
|
||||
db.FediverseActors.Add(actor);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await discoveryService.FetchActorDataAsync(actor);
|
||||
await discoveryService.FetchInstanceMetadataAsync(instance);
|
||||
|
||||
actor.Instance = instance;
|
||||
return actor;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -69,7 +69,9 @@ public class MisskeyMetaResponse
|
||||
[JsonPropertyName("repositoryUrl")] public string? RepositoryUrl { get; init; }
|
||||
[JsonPropertyName("privacyPolicyUrl")] public string? PrivacyPolicyUrl { get; init; }
|
||||
[JsonPropertyName("tosUrl")] public string? TosUrl { get; init; }
|
||||
[JsonPropertyName("maxNoteTextLength")] public int? MaxNoteTextLength { get; init; }
|
||||
|
||||
[JsonPropertyName("maxNoteTextLength")]
|
||||
public int? MaxNoteTextLength { get; init; }
|
||||
}
|
||||
|
||||
public partial class ActivityPubDiscoveryService(
|
||||
@@ -527,7 +529,7 @@ public partial class ActivityPubDiscoveryService(
|
||||
}
|
||||
}
|
||||
|
||||
private async Task FetchActorDataAsync(SnFediverseActor actor)
|
||||
public async Task FetchActorDataAsync(SnFediverseActor actor)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -535,7 +537,8 @@ public partial class ActivityPubDiscoveryService(
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, actor.Uri);
|
||||
request.Headers.Accept.Add(
|
||||
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/activity+json"));
|
||||
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/activity+json")
|
||||
);
|
||||
|
||||
var response = await HttpClient.SendAsync(request);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
|
||||
@@ -58,7 +58,7 @@ public class WebFingerController(
|
||||
{
|
||||
Rel = "http://webfinger.net/rel/profile-page",
|
||||
Type = "text/html",
|
||||
Href = $"https://{Domain}/users/{username}"
|
||||
Href = $"https://{Domain}/publishers/{username}"
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user