Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting Labels / Fixes Chat #135

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/FishyFlip/ATProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>

using FishyFlip.Lexicon.App.Bsky.Actor;
using FishyFlip.Lexicon.Com.Atproto.Identity;

namespace FishyFlip;
Expand Down Expand Up @@ -229,6 +230,34 @@ public async Task<string> GenerateOAuth2AuthenticationUrlAsync(string clientId,
}
}

/// <summary>
/// Calls GetPreferencesAsync to fetch the user's preferences.
/// Then gets the labels from the user, and creates a list of LabelParameters.
/// That are then added to the ATProtocol Options.
/// </summary>
/// <returns>List of LabelParameter.</returns>
public async Task<Result<List<LabelParameter>?>> SetDefaultLabelsAsync()
{
var (preferences, error) = await this.Actor.GetPreferencesAsync();
if (error is not null)
{
return error;
}

var labels = preferences?.Preferences?.FirstOrDefault(x => x.Type == "app.bsky.actor.defs#labelersPref") as LabelersPref;
if (labels?.Labelers is null)
{
return new List<LabelParameter>();
}

foreach (var label in labels.Labelers)
{
this.options.LabelParameters.Add(new LabelParameter(label.Did!));
}

return this.options.LabelParameters.ToList();
}

/// <summary>
/// Resolves an ATHandle to a host address.
/// </summary>
Expand Down
12 changes: 11 additions & 1 deletion src/FishyFlip/ATProtocolBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ATProtocolBuilder WithServiceEndpointUponLogin(bool serviceEndpointUponLo
}

/// <summary>
/// Enable auto renewing sessions.
/// Enable auto-renewing sessions.
/// </summary>
/// <param name="autoRenewSession">Auto Renew Session.</param>
/// <returns><see cref="ATProtocolBuilder"/>.</returns>
Expand All @@ -111,6 +111,16 @@ public ATProtocolBuilder EnableAutoRenewSession(bool autoRenewSession)
return this;
}

/// <summary>
/// Include the Bluesky Moderation service label by default.
/// </summary>
/// <returns><see cref="ATProtocolBuilder"/>.</returns>
public ATProtocolBuilder EnableBlueskyModerationService()
{
this.atProtocolOptions.LabelParameters.Add(LabelParameter.BlueskyModeration);
return this;
}

/// <summary>
/// Adds a logger.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/FishyFlip/ATProtocolOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public ATProtocolOptions()
/// </summary>
public JsonSerializerOptions JsonSerializerOptions { get; internal set; }

/// <summary>
/// Gets the label parameters.
/// </summary>
public HashSet<LabelParameter> LabelParameters { get; internal set; } = new HashSet<LabelParameter>();

/// <summary>
/// Gets a value indicating whether to switch to the service endpoint upon login, if available.
/// If it's not available, the original instance URL will be used.
Expand All @@ -79,6 +84,11 @@ public ATProtocolOptions()
/// </summary>
internal SourceGenerationContext SourceGenerationContext { get; }

/// <summary>
/// Gets the label definitions header.
/// </summary>
internal string LabelDefinitionsHeader => string.Join(", ", this.LabelParameters.Select(p => p.ToString()));

/// <summary>
/// Generates an HttpClient based on the options.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/FishyFlip/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public static class Constants
internal const string BlueskyApiClient = "FishyFlip";
internal const string ContentMediaType = "application/json";
internal const string AcceptedMediaType = "application/json";
internal const string AtProtoAcceptLabelers = "atproto-accept-labelers";
internal const string AtProtoContentLabelers = "atproto-content-labelers";
internal const string AtProtoProxy = "atproto-proxy";
internal const string BlueskyChatProxy = "did:web:api.bsky.chat#bsky_chat";
internal const string BlueskyModerationServiceDid = "did:plc:ar7c4by46qjdydhdevvrndac";

internal const string RedactParameter = "redact";

public static class Urls
{
Expand Down
24 changes: 18 additions & 6 deletions src/FishyFlip/Lexicon/App/Bsky/Actor/ActorEndpoints.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public static class ActorEndpoints
public static Task<Result<FishyFlip.Lexicon.App.Bsky.Actor.GetPreferencesOutput?>> GetPreferencesAsync (this FishyFlip.ATProtocol atp, CancellationToken cancellationToken = default)
{
var endpointUrl = GetPreferences.ToString();
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.GetPreferencesOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorGetPreferencesOutput!, cancellationToken);
var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoAcceptLabelers, atp.Options.LabelDefinitionsHeader);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.GetPreferencesOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorGetPreferencesOutput!, cancellationToken, headers);
}


Expand All @@ -55,8 +57,10 @@ public static class ActorEndpoints
List<string> queryStrings = new();
queryStrings.Add("actor=" + actor);

var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoAcceptLabelers, atp.Options.LabelDefinitionsHeader);
endpointUrl += string.Join("&", queryStrings);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.ProfileViewDetailed>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorProfileViewDetailed!, cancellationToken);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.ProfileViewDetailed>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorProfileViewDetailed!, cancellationToken, headers);
}


Expand All @@ -74,8 +78,10 @@ public static class ActorEndpoints
List<string> queryStrings = new();
queryStrings.Add(string.Join("&", actors.Select(n => "actors=" + n)));

var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoAcceptLabelers, atp.Options.LabelDefinitionsHeader);
endpointUrl += string.Join("&", queryStrings);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.GetProfilesOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorGetProfilesOutput!, cancellationToken);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.GetProfilesOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorGetProfilesOutput!, cancellationToken, headers);
}


Expand All @@ -102,8 +108,10 @@ public static class ActorEndpoints
queryStrings.Add("cursor=" + cursor);
}

var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoAcceptLabelers, atp.Options.LabelDefinitionsHeader);
endpointUrl += string.Join("&", queryStrings);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.GetSuggestionsOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorGetSuggestionsOutput!, cancellationToken);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.GetSuggestionsOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorGetSuggestionsOutput!, cancellationToken, headers);
}


Expand Down Expand Up @@ -166,8 +174,10 @@ public static class ActorEndpoints
queryStrings.Add("cursor=" + cursor);
}

var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoAcceptLabelers, atp.Options.LabelDefinitionsHeader);
endpointUrl += string.Join("&", queryStrings);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.SearchActorsOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorSearchActorsOutput!, cancellationToken);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.SearchActorsOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorSearchActorsOutput!, cancellationToken, headers);
}


Expand All @@ -194,8 +204,10 @@ public static class ActorEndpoints
queryStrings.Add("limit=" + limit);
}

var headers = new Dictionary<string, string>();
headers.Add(Constants.AtProtoAcceptLabelers, atp.Options.LabelDefinitionsHeader);
endpointUrl += string.Join("&", queryStrings);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.SearchActorsTypeaheadOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorSearchActorsTypeaheadOutput!, cancellationToken);
return atp.Get<FishyFlip.Lexicon.App.Bsky.Actor.SearchActorsTypeaheadOutput>(endpointUrl, atp.Options.SourceGenerationContext.AppBskyActorSearchActorsTypeaheadOutput!, cancellationToken, headers);
}

}
Expand Down
Loading
Loading