Skip to content

Commit

Permalink
Add 'shared chat' tags (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder authored Sep 24, 2024
1 parent 69840c9 commit 98de288
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 10 deletions.
10 changes: 5 additions & 5 deletions MiniTwitch.Irc.Test/PrivmsgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Construct()
Assert.Equal(11148817, privmsg.Channel.Id);

// No source
Assert.Null(privmsg.Source);
Assert.Null(privmsg.SourceClient);
Assert.True(privmsg.ReplyWith(default!, default!).IsCompleted);

Assert.Equal(0, privmsg.Bits);
Expand Down Expand Up @@ -88,7 +88,7 @@ public void WithReply()
Assert.Equal(11148817, privmsg.Channel.Id);

// No source
Assert.Null(privmsg.Source);
Assert.Null(privmsg.SourceClient);
Assert.True(privmsg.ReplyWith(default!, default!).IsCompleted);

Assert.Equal(0, privmsg.Bits);
Expand Down Expand Up @@ -137,7 +137,7 @@ public void WithNestedReply()
Assert.Equal(11148817, privmsg.Channel.Id);

// No source
Assert.Null(privmsg.Source);
Assert.Null(privmsg.SourceClient);
Assert.True(privmsg.ReplyWith(default!, default!).IsCompleted);

Assert.Equal(0, privmsg.Bits);
Expand Down Expand Up @@ -185,7 +185,7 @@ public void WithBits()
Assert.Equal(11148817, privmsg.Channel.Id);

// No source
Assert.Null(privmsg.Source);
Assert.Null(privmsg.SourceClient);
Assert.True(privmsg.ReplyWith(default!, default!).IsCompleted);

Assert.Equal(10, privmsg.Bits);
Expand Down Expand Up @@ -233,7 +233,7 @@ public void WithCustomRewardId()
Assert.Equal(11148817, privmsg.Channel.Id);

// No source
Assert.Null(privmsg.Source);
Assert.Null(privmsg.SourceClient);
Assert.True(privmsg.ReplyWith(default!, default!).IsCompleted);

Assert.Equal(0, privmsg.Bits);
Expand Down
6 changes: 5 additions & 1 deletion MiniTwitch.Irc.Test/SpanSumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public class SpanSumTests
{ (int)Tags.MessageId, "message-id" },
{ (int)Tags.ThreadId, "thread-id" },
{ (int)Tags.CustomRewardId, "custom-reward-id" },
{ (int)Tags.AnimationId, "animation-id" }
{ (int)Tags.AnimationId, "animation-id" },
{ (int)Tags.SourceBadgeInfo, "source-badge-info" },
{ (int)Tags.SourceBadges, "source-badges" },
{ (int)Tags.SourceId, "source-id" },
{ (int)Tags.SourceRoomId, "source-room-id" },
};

[Fact]
Expand Down
3 changes: 3 additions & 0 deletions MiniTwitch.Irc/Interfaces/IAnnouncementNotice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniTwitch.Irc.Enums;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;
/// <summary>
Expand All @@ -25,4 +26,6 @@ public interface IAnnouncementNotice : IUsernotice
/// <para><see cref="string.Empty"/> if there are none</para>
/// </summary>
string Flags { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
3 changes: 3 additions & 0 deletions MiniTwitch.Irc/Interfaces/ICharityDonation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniTwitch.Irc.Enums;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;

Expand All @@ -23,4 +24,6 @@ public interface ICharityDonation : IUnixTimestamped, IUsernotice
/// The message emitted in chat when the event occurs
/// </summary>
string SystemMessage { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
3 changes: 3 additions & 0 deletions MiniTwitch.Irc/Interfaces/IGiftSubNotice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniTwitch.Irc.Enums;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;

Expand Down Expand Up @@ -42,4 +43,6 @@ public interface IGiftSubNotice : IUsernotice
/// Id of the parent <see cref="IGiftSubNoticeIntro"/> gift message
/// </summary>
ulong CommunityGiftId { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
3 changes: 3 additions & 0 deletions MiniTwitch.Irc/Interfaces/IGiftSubNoticeIntro.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniTwitch.Irc.Enums;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;

Expand Down Expand Up @@ -28,4 +29,6 @@ public interface IGiftSubNoticeIntro : IUsernotice
/// Id of the gift message to link it with <see cref="IGiftSubNotice"/> messages
/// </summary>
ulong CommunityGiftId { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
6 changes: 5 additions & 1 deletion MiniTwitch.Irc/Interfaces/IPaidUpgradeNotice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace MiniTwitch.Irc.Interfaces;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;

/// <summary>
/// Contains information about a user that continued their gifted subscription
Expand All @@ -20,4 +22,6 @@ public interface IPaidUpgradeNotice : IUsernotice
/// <para>Note: Value is <see cref="string.Empty"/> if the previous subscription's gifter was anonymous</para>
/// </summary>
string GifterDisplayName { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
3 changes: 3 additions & 0 deletions MiniTwitch.Irc/Interfaces/IPrimeUpgradeNotice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniTwitch.Irc.Enums;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;

Expand All @@ -16,4 +17,6 @@ public interface IPrimeUpgradeNotice : IUsernotice
/// The tier of the new subscription
/// </summary>
SubPlan SubPlan { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
2 changes: 2 additions & 0 deletions MiniTwitch.Irc/Interfaces/IRaidNotice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public interface IRaidNotice : IUsernotice
/// The amount of viewers joining from the raid
/// </summary>
int ViewerCount { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
3 changes: 3 additions & 0 deletions MiniTwitch.Irc/Interfaces/ISubscriberNotice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniTwitch.Irc.Enums;
using MiniTwitch.Irc.Models;

namespace MiniTwitch.Irc.Interfaces;

Expand Down Expand Up @@ -52,4 +53,6 @@ public interface ISubNotice : IUsernotice
/// <para>Note 2: May be <see cref="string.Empty"/> even for resubscriptions</para>
/// </summary>
string Message { get; }
/// <inheritdoc cref="Usernotice.Source"/>
MessageSource Source { get; }
}
8 changes: 8 additions & 0 deletions MiniTwitch.Irc/Internal/Enums/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ internal enum Tags
MessageId = 108019,
ThreadId = 102312,
CustomRewardId = 159093,
//privmsg: source-badge-info
SourceBadgeInfo = 192510,
//privmsg: source-badges
SourceBadges = 151340,
//privmsg: source-id
SourceId = 104305,
//privmsg: source-room-id
SourceRoomId = 160655,
}
23 changes: 23 additions & 0 deletions MiniTwitch.Irc/Models/MessageSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace MiniTwitch.Irc.Models;

/// <summary>
/// Source information about messages in shared chats
/// </summary>
public sealed class MessageSource
{
/// <summary>
/// Whether source information is available.
/// <para>When <see langword="false"/>, indicates that the channel does not have shared chat enabled</para>
/// </summary>
public bool HasSource => this.ChannelId != 0;
/// <inheritdoc cref="MessageAuthor.BadgeInfo"/>
public string BadgeInfo { get; init; } = string.Empty;
/// <inheritdoc cref="MessageAuthor.Badges"/>
public string Badges { get; init; } = string.Empty;
/// <inheritdoc cref="Privmsg.Id"/>
public string MessageId { get; init; } = string.Empty;
/// <summary>
/// ID of the channel where this message originates from
/// </summary>
public long ChannelId { get; init; } = 0;
}
46 changes: 43 additions & 3 deletions MiniTwitch.Irc/Models/Privmsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,22 @@ namespace MiniTwitch.Irc.Models;
/// Whether the emotes in the message are gigantified
/// </summary>
public bool IsGigantifiedEmoteMessage { get; init; }
/// <summary>
/// Source information about the message.
/// <para>Only populated if <see cref="MessageSource.HasSource"/> is <see langword="true"/></para>
/// </summary>
public MessageSource Source { get; init; }

/// <inheritdoc/>
public long TmiSentTs { get; init; }
/// <inheritdoc/>
public DateTimeOffset SentTimestamp => DateTimeOffset.FromUnixTimeMilliseconds(this.TmiSentTs);

internal IrcClient? Source { get; init; }
internal IrcClient? SourceClient { get; init; }

internal Privmsg(ref IrcMessage message, IrcClient? source = null)
{
this.Source = source;
this.SourceClient = source;

// MessageAuthor
string badges = string.Empty;
Expand Down Expand Up @@ -132,10 +137,18 @@ internal Privmsg(ref IrcMessage message, IrcClient? source = null)
bool firstMsg = false;
bool returningChatter = false;
string customRewardId = string.Empty;

// bit stuff
bool gigantified = false;
bool animated = false;
string animation = string.Empty;

// MessageSource
string sourceBadgeInfo = string.Empty;
string sourceBadges = string.Empty;
string sourceId = string.Empty;
long sourceRoomId = 0;

using IrcTags tags = message.ParseTags();
foreach (IrcTag tag in tags)
{
Expand Down Expand Up @@ -297,6 +310,26 @@ internal Privmsg(ref IrcMessage message, IrcClient? source = null)
case (int)Tags.CustomRewardId:
customRewardId = TagHelper.GetString(tagValue);
break;

//source-badge-info
case (int)Tags.SourceBadgeInfo:
sourceBadgeInfo = TagHelper.GetString(tagValue, intern: true, unescape: true);
break;

//source-badges
case (int)Tags.SourceBadges:
sourceBadges = TagHelper.GetString(tagValue, intern: true);
break;

//source-id
case (int)Tags.SourceId:
sourceId = TagHelper.GetString(tagValue);
break;

//source-room-id
case (int)Tags.SourceRoomId:
sourceRoomId = TagHelper.GetLong(tagValue);
break;
}
}

Expand Down Expand Up @@ -344,6 +377,13 @@ internal Privmsg(ref IrcMessage message, IrcClient? source = null)
IsAnimated = animated,
AnimationId = animation
};
this.Source = new MessageSource()
{
BadgeInfo = sourceBadgeInfo,
Badges = sourceBadges,
ChannelId = sourceRoomId,
MessageId = sourceId,
};
}

/// <summary>
Expand All @@ -354,7 +394,7 @@ internal Privmsg(ref IrcMessage message, IrcClient? source = null)
/// <param name="replyInThread">Prefer replying to the target message in the same thread instead of creating a new one</param>
/// <param name="cancellationToken">A cancellation token to stop further execution of asynchronous actions</param>
public ValueTask ReplyWith(string reply, bool action = false, bool replyInThread = false, CancellationToken cancellationToken = default) =>
this.Source?.ReplyTo(this, reply, action, replyInThread, cancellationToken) ?? ValueTask.CompletedTask;
this.SourceClient?.ReplyTo(this, reply, action, replyInThread, cancellationToken) ?? ValueTask.CompletedTask;

/// <summary>
/// Construct a message from a string. Useful for testing
Expand Down
38 changes: 38 additions & 0 deletions MiniTwitch.Irc/Models/Usernotice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ namespace MiniTwitch.Irc.Models;
public bool ShouldShareStreak { get; init; }
/// <inheritdoc/>
public ulong CommunityGiftId { get; init; }
/// <summary>
/// Source information about the message.
/// <para>Only populated if <see cref="MessageSource.HasSource"/> is <see langword="true"/></para>
/// </summary>
public MessageSource Source { get; init; }

/// <inheritdoc/>
public long TmiSentTs { get; init; } = default;
Expand Down Expand Up @@ -126,6 +131,12 @@ internal Usernotice(ref IrcMessage message)
double actualDonationAmount = 0;
CurrencyCode donationCurrency = CurrencyCode.None;

// MessageSource
string sourceBadgeInfo = string.Empty;
string sourceBadges = string.Empty;
string sourceId = string.Empty;
long sourceRoomId = 0;

using IrcTags tags = message.ParseTags();
foreach (IrcTag tag in tags)
{
Expand Down Expand Up @@ -323,6 +334,26 @@ internal Usernotice(ref IrcMessage message)
case (int)Tags.MsgParamDonationCurrency:
donationCurrency = TagHelper.GetEnum<CurrencyCode>(tagValue);
break;

//source-badge-info
case (int)Tags.SourceBadgeInfo:
sourceBadgeInfo = TagHelper.GetString(tagValue, intern: true, unescape: true);
break;

//source-badges
case (int)Tags.SourceBadges:
sourceBadges = TagHelper.GetString(tagValue, intern: true);
break;

//source-id
case (int)Tags.SourceId:
sourceId = TagHelper.GetString(tagValue);
break;

//source-room-id
case (int)Tags.SourceRoomId:
sourceRoomId = TagHelper.GetLong(tagValue);
break;
}
}

Expand Down Expand Up @@ -382,6 +413,13 @@ internal Usernotice(ref IrcMessage message)
this.DonationAmount = actualDonationAmount;
this.DonationCurrency = donationCurrency;
this.CommunityGiftId = communityGiftId;
this.Source = new MessageSource()
{
BadgeInfo = sourceBadgeInfo,
Badges = sourceBadges,
ChannelId = sourceRoomId,
MessageId = sourceId,
};
}

/// <summary>
Expand Down

0 comments on commit 98de288

Please sign in to comment.