-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cab3a05
commit b30889c
Showing
14 changed files
with
354 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// <copyright file="BlueskyChat.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip; | ||
|
||
/// <summary> | ||
/// Bluesky Chat. | ||
/// </summary> | ||
public sealed class BlueskyChat | ||
{ | ||
private ATProtocol proto; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BlueskyChat"/> class. | ||
/// </summary> | ||
/// <param name="proto"><see cref="ATProtocol"/>.</param> | ||
internal BlueskyChat(ATProtocol proto) | ||
{ | ||
this.proto = proto; | ||
} | ||
|
||
private ATProtocolOptions Options => this.proto.Options; | ||
|
||
private HttpClient Client => this.proto.Client; | ||
|
||
/// <summary> | ||
/// Retrieves the messages of a conversation asynchronously. | ||
/// </summary> | ||
/// <param name="convoId">The unique identifier of the conversation.</param> | ||
/// <param name="cursor">The cursor for pagination in the conversation.</param> | ||
/// <param name="limit">Limit of conversations to get, defaults to 60.</param> | ||
/// <param name="cancellationToken">An optional token to cancel the asynchronous operation.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Result{ConversationMessages?}"/> that encapsulates the result of the operation.</returns> | ||
public Task<Result<ConversationMessages?>> GetConversationMessagesAsync(string convoId, string? cursor = default, int limit = 60, CancellationToken cancellationToken = default) | ||
{ | ||
var url = $"{Constants.Urls.Bluesky.Chat.Convo.GetMessages}?convoId={convoId}"; | ||
|
||
if (!string.IsNullOrWhiteSpace(cursor)) | ||
{ | ||
url += $"&cursor={cursor}"; | ||
} | ||
|
||
if (limit > 0) | ||
{ | ||
url += $"&limit={limit}"; | ||
} | ||
|
||
var headers = new Dictionary<string, string> | ||
{ | ||
{ Constants.ATProtoProxy.Proxy, Constants.ATProtoProxy.BskyChat }, | ||
}; | ||
|
||
return this.Client.Get<ConversationMessages>(url, this.Options.SourceGenerationContext.ConversationMessages, this.Options.JsonSerializerOptions, cancellationToken, this.Options.Logger, headers); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves a list of conversations asynchronously. | ||
/// </summary> | ||
/// <param name="cursor">An optional string that represents the cursor position in the list. Default is an empty string.</param> | ||
/// <param name="limit">Limit of conversations to get, defaults to 60.</param> | ||
/// <param name="cancellationToken">An optional token to cancel the asynchronous operation.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Result{ConversationList?}"/> that encapsulates the result of the operation.</returns> | ||
public Task<Result<ConversationList?>> GetConversationsAsync(string cursor = "", int limit = 60, CancellationToken cancellationToken = default) | ||
{ | ||
var url = $"{Constants.Urls.Bluesky.Chat.Convo.ListConvos}?limit={limit}"; | ||
|
||
if (!string.IsNullOrWhiteSpace(cursor)) | ||
{ | ||
url += $"&cursor={cursor}"; | ||
} | ||
|
||
var headers = new Dictionary<string, string> | ||
{ | ||
{ Constants.ATProtoProxy.Proxy, Constants.ATProtoProxy.BskyChat }, | ||
}; | ||
|
||
return this.Client.Get<ConversationList>(url, this.Options.SourceGenerationContext.ConversationList, this.Options.JsonSerializerOptions, cancellationToken, this.Options.Logger, headers); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves a conversation asynchronously. | ||
/// </summary> | ||
/// <param name="convoId">The unique identifier of the conversation.</param> | ||
/// <param name="cancellationToken">An optional token to cancel the asynchronous operation.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Result{Conversation?}"/> that encapsulates the result of the operation.</returns> | ||
public Task<Result<ConversationView?>> GetConversationAsync(string convoId, CancellationToken cancellationToken = default) | ||
{ | ||
var url = $"{Constants.Urls.Bluesky.Chat.Convo.GetConvo}?convoId={convoId}"; | ||
var headers = new Dictionary<string, string> | ||
{ | ||
{ Constants.ATProtoProxy.Proxy, Constants.ATProtoProxy.BskyChat }, | ||
}; | ||
|
||
return this.Client.Get<ConversationView>(url, this.Options.SourceGenerationContext.ConversationView, this.Options.JsonSerializerOptions, cancellationToken, this.Options.Logger, headers); | ||
} | ||
|
||
/// <summary> | ||
/// Sends a message asynchronously in a conversation. | ||
/// </summary> | ||
/// <param name="convoId">The unique identifier of the conversation.</param> | ||
/// <param name="message">The message to be sent.</param> | ||
/// <param name="cancellationToken">An optional token to cancel the asynchronous operation.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Result{MessageView}"/> that encapsulates the result of the operation.</returns> | ||
public Task<Result<MessageView>> SendMessageAsync(string convoId, string message, CancellationToken cancellationToken = default) | ||
{ | ||
var url = $"{Constants.Urls.Bluesky.Chat.Convo.SendMessage}"; | ||
var createMessage = new CreateMessage(convoId, new CreateMessageMessage(message)); | ||
var headers = new Dictionary<string, string> | ||
{ | ||
{ Constants.ATProtoProxy.Proxy, Constants.ATProtoProxy.BskyChat }, | ||
}; | ||
|
||
return this.Client.Post<CreateMessage, MessageView>(url, this.Options.SourceGenerationContext.CreateMessage, this.Options.SourceGenerationContext.MessageView, this.Options.JsonSerializerOptions, createMessage, cancellationToken, this.Options.Logger, headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// <copyright file="AllowIncomingPref.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// Adult Content Preference. | ||
/// </summary> | ||
public class AllowIncomingPref : ATRecord | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AllowIncomingPref"/> class. | ||
/// </summary> | ||
/// <param name="allowIncoming">Type of chat messages to allow.</param> | ||
/// <param name="type">ATRecord Type.</param> | ||
[JsonConstructor] | ||
public AllowIncomingPref(string allowIncoming, string? type = default) | ||
: base(type) | ||
{ | ||
this.AllowIncoming = allowIncoming; | ||
this.Type = type ?? Constants.DeclarationTypes.AllowIncomingPref; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value the type of chat messages to allow. | ||
/// </summary> | ||
public string AllowIncoming { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// <copyright file="ChatSender.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// Chat Sender. | ||
/// </summary> | ||
/// <param name="Did">ATDid.</param> | ||
public record ChatSender(ATDid Did); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// <copyright file="Conversation.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// Bluesky Conversation. | ||
/// </summary> | ||
public record Conversation(string Id, string Rev, int UnreadCount, bool Muted); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// <copyright file="ConversationList.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// Represents a list of conversations. | ||
/// </summary> | ||
/// <param name="Convos">An array of Conversation objects.</param> | ||
/// <param name="Cursor">An optional string that represents the cursor position in the list. Default is an empty string.</param> | ||
public record ConversationList(Conversation[] Convos, string Cursor = ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// <copyright file="ConversationMessages.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// Represents a collection of messages in a conversation. | ||
/// </summary> | ||
/// <param name="Messages">An array of <see cref="MessageView"/> instances representing the messages in the conversation.</param> | ||
/// <param name="Cursor">A string representing the cursor for pagination in the conversation.</param> | ||
public record ConversationMessages(MessageView[] Messages, string Cursor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// <copyright file="ConversationView.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// The Conversation View. | ||
/// </summary> | ||
/// <param name="Convo">The conversation.</param> | ||
public record ConversationView(Conversation Convo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// <copyright file="CreateMessage.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models.Internal; | ||
|
||
/// <summary> | ||
/// Create Message. | ||
/// </summary> | ||
/// <param name="ConvoId">Conversation Id.</param> | ||
/// <param name="Message">Message.</param> | ||
internal record CreateMessage(string ConvoId, CreateMessageMessage Message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// <copyright file="CreateMessageMessage.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models.Internal; | ||
|
||
/// <summary> | ||
/// Create Message, with the message. | ||
/// </summary> | ||
/// <param name="Text">Text of message.</param> | ||
internal record CreateMessageMessage(string Text); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// <copyright file="MessageView.cs" company="Drastic Actions"> | ||
// Copyright (c) Drastic Actions. All rights reserved. | ||
// </copyright> | ||
|
||
namespace FishyFlip.Models; | ||
|
||
/// <summary> | ||
/// Represents a view of a message in a chat conversation. | ||
/// </summary> | ||
public class MessageView : ATRecord | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MessageView"/> class. | ||
/// </summary> | ||
/// <param name="id">The unique identifier of the message.</param> | ||
/// <param name="rev">The revision of the message.</param> | ||
/// <param name="sender">The sender of the message.</param> | ||
/// <param name="text">The text content of the message.</param> | ||
/// <param name="sentAt">The date and time when the message was sent.</param> | ||
/// <param name="type">The type of the message. If not provided, defaults to <see cref="Constants.ConversationTypes.MessageView"/>.</param> | ||
[JsonConstructor] | ||
public MessageView(string id, string rev, ChatSender sender, string text, DateTime sentAt, string? type = default) | ||
: base(type) | ||
{ | ||
this.Id = id; | ||
this.Rev = rev; | ||
this.Sender = sender; | ||
this.Text = text; | ||
this.SentAt = sentAt; | ||
this.Type = type ?? Constants.ConversationTypes.MessageView; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the unique identifier of the message. | ||
/// </summary> | ||
public string Id { get; } | ||
|
||
/// <summary> | ||
/// Gets the revision of the message. | ||
/// </summary> | ||
public string Rev { get; } | ||
|
||
/// <summary> | ||
/// Gets the sender of the message. | ||
/// </summary> | ||
public ChatSender Sender { get; } | ||
|
||
/// <summary> | ||
/// Gets the text content of the message. | ||
/// </summary> | ||
public string Text { get; } | ||
|
||
/// <summary> | ||
/// Gets the date and time when the message was sent. | ||
/// </summary> | ||
public DateTime SentAt { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.