Skip to content

Commit

Permalink
Add usernotice tag for gifts 'msg-param-community-gift-id'
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder committed May 29, 2024
1 parent f1d6773 commit 08711d3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions MiniTwitch.Irc.Test/SpanSumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SpanSumTests
{ (int)Tags.MsgParamSubPlanName, "msg-param-sub-plan-name" },
{ (int)Tags.MsgParamStreakMonths, "msg-param-streak-months" },
{ (int)Tags.MsgParamMassGiftCount, "msg-param-mass-gift-count" },
{ (int)Tags.MsgParamCommunityGiftId, "msg-param-community-gift-id" },
{ (int)Tags.MsgParamCumulativeMonths, "msg-param-cumulative-months" },
{ (int)Tags.MsgParamRecipientUserName, "msg-param-recipient-user-name" },
{ (int)Tags.MsgParamShouldShareStreak, "msg-param-should-share-streak" },
Expand Down
4 changes: 4 additions & 0 deletions MiniTwitch.Irc/Interfaces/IGiftSubNotice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public interface IGiftSubNotice : IUsernotice
/// The tier of the gift subscription
/// </summary>
SubPlan SubPlan { get; }
/// <summary>
/// Id of the parent <see cref="IGiftSubNoticeIntro"/> gift message
/// </summary>
ulong CommunityGiftId { get; }
}
4 changes: 4 additions & 0 deletions MiniTwitch.Irc/Interfaces/IGiftSubNoticeIntro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public interface IGiftSubNoticeIntro : IUsernotice
/// The tier of the gift subscriptions
/// </summary>
SubPlan SubPlan { get; }
/// <summary>
/// Id of the gift message to link it with <see cref="IGiftSubNotice"/> messages
/// </summary>
ulong CommunityGiftId { get; }
}
1 change: 1 addition & 0 deletions MiniTwitch.Irc/Internal/Enums/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ internal enum Tags
MsgParamSubPlanName = 240890,
MsgParamStreakMonths = 251354,
MsgParamMassGiftCount = 267159,
MsgParamCommunityGiftId = 290376,
MsgParamCumulativeMonths = 298987,
MsgParamRecipientUserName = 312067,
MsgParamShouldShareStreak = 313048,
Expand Down
19 changes: 19 additions & 0 deletions MiniTwitch.Irc/Internal/Parsing/TagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static long GetLong(ReadOnlySpan<byte> span)
return span[0] == dash ? -1 * ParseLong(span[1..]) : ParseLong(span);
}

public static ulong GetULong(ReadOnlySpan<byte> span)
{
return ParseULong(span);
}

public static TEnum GetEnum<TEnum>(ReadOnlySpan<byte> span, bool useTry = true)
where TEnum : struct
{
Expand Down Expand Up @@ -118,4 +123,18 @@ private static long ParseLong(ReadOnlySpan<byte> span)

return result;
}

private static ulong ParseULong(ReadOnlySpan<byte> span)
{
const byte numBase = (byte)'0';

ulong result = 0;
foreach (byte b in span)
{
result *= 10L;
result += (ulong)b - numBase;
}

return result;
}
}
9 changes: 9 additions & 0 deletions MiniTwitch.Irc/Models/Usernotice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace MiniTwitch.Irc.Models;
public int ViewerCount { get; init; }
/// <inheritdoc/>
public bool ShouldShareStreak { get; init; }
/// <inheritdoc/>
public ulong CommunityGiftId { get; init; }

/// <inheritdoc/>
public long TmiSentTs { get; init; } = default;
Expand Down Expand Up @@ -116,6 +118,7 @@ internal Usernotice(ref IrcMessage message)
int totalGiftCount = 0;
int viewerCount = 0;
bool shouldShareStreak = false;
ulong communityGiftId = 0;

string charityName = string.Empty;
int donationAmount = 0;
Expand Down Expand Up @@ -276,6 +279,11 @@ internal Usernotice(ref IrcMessage message)
giftCount = TagHelper.GetInt(tagValue);
break;

//msg-param-community-gift-id
case (int)Tags.MsgParamCommunityGiftId:
communityGiftId = TagHelper.GetULong(tagValue);
break;

//msg-param-cumulative-months
case (int)Tags.MsgParamCumulativeMonths:
cumulativeMonths = TagHelper.GetInt(tagValue);
Expand Down Expand Up @@ -373,6 +381,7 @@ internal Usernotice(ref IrcMessage message)
this.CharityName = charityName;
this.DonationAmount = actualDonationAmount;
this.DonationCurrency = donationCurrency;
this.CommunityGiftId = communityGiftId;
}

/// <summary>
Expand Down

0 comments on commit 08711d3

Please sign in to comment.