♻️ 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)
|
private async Task<SnFediverseActor?> GetOrFetchActorAsync(string actorUri)
|
||||||
{
|
{
|
||||||
var actor = await db.FediverseActors
|
var actor = await db.FediverseActors
|
||||||
|
.Include(a => a.Instance)
|
||||||
.FirstOrDefaultAsync(a => a.Uri == actorUri);
|
.FirstOrDefaultAsync(a => a.Uri == actorUri);
|
||||||
|
|
||||||
if (actor != null)
|
if (actor != null)
|
||||||
@@ -376,16 +377,6 @@ public class ActivityPubDeliveryService(
|
|||||||
|
|
||||||
try
|
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 domain = new Uri(actorUri).Host;
|
||||||
var instance = await db.FediverseInstances
|
var instance = await db.FediverseInstances
|
||||||
.FirstOrDefaultAsync(i => i.Domain == domain);
|
.FirstOrDefaultAsync(i => i.Domain == domain);
|
||||||
@@ -399,26 +390,23 @@ public class ActivityPubDeliveryService(
|
|||||||
};
|
};
|
||||||
db.FediverseInstances.Add(instance);
|
db.FediverseInstances.Add(instance);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
await discoveryService.FetchInstanceMetadataAsync(instance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
actor = new SnFediverseActor
|
actor = new SnFediverseActor
|
||||||
{
|
{
|
||||||
Uri = actorUri,
|
Uri = actorUri,
|
||||||
Username = ExtractUsername(actorUri),
|
Username = ExtractUsername(actorUri),
|
||||||
DisplayName = actorData.GetValueOrDefault("name")?.ToString(),
|
InstanceId = instance.Id,
|
||||||
Bio = actorData.GetValueOrDefault("summary")?.ToString(),
|
LastFetchedAt = NodaTime.SystemClock.Instance.GetCurrentInstant()
|
||||||
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
|
|
||||||
};
|
};
|
||||||
|
|
||||||
db.FediverseActors.Add(actor);
|
db.FediverseActors.Add(actor);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
await discoveryService.FetchActorDataAsync(actor);
|
||||||
|
await discoveryService.FetchInstanceMetadataAsync(instance);
|
||||||
|
|
||||||
|
actor.Instance = instance;
|
||||||
return actor;
|
return actor;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -69,7 +69,9 @@ public class MisskeyMetaResponse
|
|||||||
[JsonPropertyName("repositoryUrl")] public string? RepositoryUrl { get; init; }
|
[JsonPropertyName("repositoryUrl")] public string? RepositoryUrl { get; init; }
|
||||||
[JsonPropertyName("privacyPolicyUrl")] public string? PrivacyPolicyUrl { get; init; }
|
[JsonPropertyName("privacyPolicyUrl")] public string? PrivacyPolicyUrl { get; init; }
|
||||||
[JsonPropertyName("tosUrl")] public string? TosUrl { 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(
|
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
|
try
|
||||||
{
|
{
|
||||||
@@ -535,7 +537,8 @@ public partial class ActivityPubDiscoveryService(
|
|||||||
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, actor.Uri);
|
var request = new HttpRequestMessage(HttpMethod.Get, actor.Uri);
|
||||||
request.Headers.Accept.Add(
|
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);
|
var response = await HttpClient.SendAsync(request);
|
||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ public class WebFingerController(
|
|||||||
{
|
{
|
||||||
Rel = "http://webfinger.net/rel/profile-page",
|
Rel = "http://webfinger.net/rel/profile-page",
|
||||||
Type = "text/html",
|
Type = "text/html",
|
||||||
Href = $"https://{Domain}/users/{username}"
|
Href = $"https://{Domain}/publishers/{username}"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user