-
Notifications
You must be signed in to change notification settings - Fork 5
feat(helix): support pinned message endpoints #217
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| using System.Collections; | ||
| using MiniTwitch.Helix.Enums; | ||
| using MiniTwitch.Helix.Internal.Json; | ||
| using MiniTwitch.Helix.Responses; | ||
|
|
||
| namespace MiniTwitch.Helix.Test.DataSources; | ||
|
|
||
| public class PinnedMessageDataSource : IEnumerable<object[]> | ||
| { | ||
| private static readonly PinnedChatMessages.Pin Base = new( | ||
| MessageId: "a42a84b2-7ad7-4ac1-95bb-0843d70e005a", | ||
| BroadcasterId: "46673989", | ||
| SenderUserId: "77481472", | ||
| SenderUserLogin: "jpsauce", | ||
| SenderUserDisplayName: "JPSauce", | ||
| PinnedByUserId: "82674227", | ||
| PinnedByUserLogin: "jammehge", | ||
| PinnedByUserDisplayName: "JAMMEHGE", | ||
| Message: new PinnedChatMessages.Message( | ||
| Text: "fix it! now!!!", | ||
| Fragments: [ | ||
| new PinnedChatMessages.MessageFragment( | ||
| Type: MessageFragmentType.Text, | ||
| Text: "fix it! now!!!", | ||
| Cheermote: null, | ||
| Emote: null, | ||
| Mention: null | ||
| ) | ||
| ] | ||
| ), | ||
| StartsAt: new DateTimeOffset(2026, 05, 16, 23, 10, 16, TimeSpan.Zero), | ||
| EndsAt: new DateTimeOffset(2026, 01, 02, 03, 04, 55, TimeSpan.Zero), | ||
| UpdatedAt: new DateTimeOffset(2026, 01, 02, 03, 04, 55, TimeSpan.Zero) | ||
| ); | ||
|
|
||
| private static string JsonFromModel(PinnedChatMessages.Pin pin) => | ||
| $$""" | ||
| { | ||
| "data": [ | ||
| { | ||
| "broadcaster_id": "{{pin.BroadcasterId}}", | ||
| "ends_at": {{(pin.EndsAt is {} ea ? $"\"{ea:yyyy-MM-ddTHH:mm:sszzz}\"" : "null")}}, | ||
| "message": { | ||
| "fragments": [ | ||
| {{string.Join(", ", pin.Message.Fragments.Select(BuildFragment))}} | ||
| ], | ||
| "text": "{{pin.Message.Text}}" | ||
| }, | ||
| "message_id": "{{pin.MessageId}}", | ||
| "pinned_by_user_id": "{{pin.PinnedByUserId}}", | ||
| "pinned_by_user_login": "{{pin.PinnedByUserLogin}}", | ||
| "pinned_by_user_name": "{{pin.PinnedByUserDisplayName}}", | ||
| "sender_user_id": "{{pin.SenderUserId}}", | ||
| "sender_user_login": "{{pin.SenderUserLogin}}", | ||
| "sender_user_name": "{{pin.SenderUserDisplayName}}", | ||
| "starts_at": "{{pin.StartsAt:yyyy-MM-ddTHH:mm:sszzz}}", | ||
| "updated_at": "{{pin.UpdatedAt:yyyy-MM-ddTHH:mm:sszzz}}" | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
|
|
||
| private static string BuildFragment(PinnedChatMessages.MessageFragment fragment) => | ||
| $$""" | ||
| { | ||
| "cheermote": {{(fragment.Cheermote is { } c ? $"{{\"prefix\": \"{c.Prefix}\", \"bits\": {c.Bits}, \"tier\": {c.Tier}}}" : "null")}}, | ||
| "emote": {{(fragment.Emote is { } e ? $"{{\"id\": \"{e.Id}\", \"emote_set_id\": \"{e.EmoteSetId}\", \"owner_id\": \"{e.OwnerId}\", \"format\": [{string.Join(", ", e.Format.Select(f => $"\"{f}\""))}]}}" : "null")}}, | ||
| "mention": {{(fragment.Mention is { } m ? $"{{\"user_id\": \"{m.UserId}\", \"user_login\": \"{m.UserLogin}\", \"user_name\": \"{m.UserDisplayName}\"}}" : "null")}}, | ||
| "text": "{{fragment.Text}}", | ||
| "type": "{{SnakeCase.Instance.ConvertToCase(fragment.Type.ToString())}}" | ||
| } | ||
| """; | ||
|
|
||
| private static IEnumerable<PinnedChatMessages.Pin> Cases => | ||
| [ | ||
| Base with { EndsAt = null }, | ||
| Base with | ||
| { | ||
| Message = Base.Message with | ||
| { | ||
| Fragments = | ||
| [ | ||
| Base.Message.Fragments[0] with | ||
| { | ||
| Type = MessageFragmentType.Cheermote, | ||
| Cheermote = new PinnedChatMessages.CheermoteFragment("prefix_1", 1, 2), | ||
| Emote = null, | ||
| Mention = null | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| Base with | ||
| { | ||
| Message = Base.Message with | ||
| { | ||
| Fragments = | ||
| [ | ||
| Base.Message.Fragments[0] with | ||
| { | ||
| Type = MessageFragmentType.Emote, | ||
| Cheermote = null, | ||
| Emote = new PinnedChatMessages.EmoteFragment("emoteId_1", "emoteSetId_1", "ownerId_1", ["format_1"]), | ||
| Mention = null | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| Base with | ||
| { | ||
| Message = Base.Message with | ||
| { | ||
| Fragments = | ||
| [ | ||
| Base.Message.Fragments[0] with | ||
| { | ||
| Type = MessageFragmentType.Mention, | ||
| Cheermote = null, | ||
| Emote = null, | ||
| Mention = new PinnedChatMessages.MentionFragment("userId_1", "userLogin_1", "userDisplayName_1") | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| public IEnumerator<object[]> GetEnumerator() => Cases | ||
| .Select(@case => new object[] | ||
| { | ||
| JsonFromModel(@case), | ||
| new PinnedChatMessages { Data = [@case] } | ||
| }) | ||
| .GetEnumerator(); | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace MiniTwitch.Helix.Enums; | ||
|
|
||
| public enum MessageFragmentType | ||
| { | ||
| Text, | ||
| Emote, | ||
| Cheermote, | ||
| Mention | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| using System.Text.Json.Serialization; | ||
| using MiniTwitch.Helix.Enums; | ||
| using MiniTwitch.Helix.Internal.Json; | ||
| using MiniTwitch.Helix.Models; | ||
|
|
||
| namespace MiniTwitch.Helix.Responses; | ||
|
|
||
| public class PinnedChatMessages : BaseResponse<PinnedChatMessages.Pin> | ||
| { | ||
| public record Pin( | ||
| [property: JsonPropertyName("message_id")] | ||
| string MessageId, | ||
| [property: JsonPropertyName("broadcaster_id")] | ||
| string BroadcasterId, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. user ids are treated as int64 across all of minitwitch, update user id props to be |
||
| [property: JsonPropertyName("sender_user_id")] | ||
| string SenderUserId, | ||
| [property: JsonPropertyName("sender_user_login")] | ||
| string SenderUserLogin, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| [property: JsonPropertyName("sender_user_name")] | ||
| string SenderUserDisplayName, | ||
| [property: JsonPropertyName("pinned_by_user_id")] | ||
| string PinnedByUserId, | ||
| [property: JsonPropertyName("pinned_by_user_login")] | ||
| string PinnedByUserLogin, | ||
| [property: JsonPropertyName("pinned_by_user_name")] | ||
| string PinnedByUserDisplayName, | ||
| [property: JsonPropertyName("message")] | ||
| Message Message, | ||
| [property: JsonPropertyName("starts_at")] | ||
| DateTimeOffset StartsAt, | ||
| [property: JsonPropertyName("ends_at")] | ||
| DateTimeOffset? EndsAt, | ||
| [property: JsonPropertyName("updated_at")] | ||
| DateTimeOffset UpdatedAt | ||
| ); | ||
|
|
||
| public record Message( | ||
| [property: JsonPropertyName("text")] | ||
| string Text, | ||
| [property: JsonPropertyName("fragments")] | ||
| IReadOnlyList<MessageFragment> Fragments | ||
| ); | ||
|
|
||
| public record MessageFragment( | ||
| [property: JsonPropertyName("type")] | ||
| [property: JsonConverter(typeof(EnumConverter<MessageFragmentType>))] | ||
| MessageFragmentType Type, | ||
| [property: JsonPropertyName("text")] | ||
| string Text, | ||
| [property: JsonPropertyName("cheermote")] | ||
| CheermoteFragment? Cheermote, | ||
| [property: JsonPropertyName("emote")] | ||
| EmoteFragment? Emote, | ||
| [property: JsonPropertyName("mention")] | ||
| MentionFragment? Mention | ||
| ); | ||
|
|
||
| public record CheermoteFragment( | ||
| [property: JsonPropertyName("prefix")] | ||
| string Prefix, | ||
| [property: JsonPropertyName("bits")] | ||
| int Bits, | ||
| [property: JsonPropertyName("tier")] | ||
| int Tier | ||
| ); | ||
|
|
||
| public record EmoteFragment( | ||
| [property: JsonPropertyName("id")] | ||
| string Id, | ||
| [property: JsonPropertyName("emote_set_id")] | ||
| string EmoteSetId, | ||
| [property: JsonPropertyName("owner_id")] | ||
| string OwnerId, | ||
| [property: JsonPropertyName("format")] | ||
| IReadOnlyList<string> Format | ||
| ); | ||
|
|
||
| public record MentionFragment( | ||
| [property: JsonPropertyName("user_id")] | ||
| string UserId, | ||
| [property: JsonPropertyName("user_login")] | ||
| string UserLogin, | ||
| [property: JsonPropertyName("user_name")] | ||
| string UserDisplayName | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The deserializer has
PropertyNamingPolicy = new SnakeCaseNamingPolicy(), you can get rid of most of these.