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

extracted tag parsing logic, changes for NET6/NET7 #238

Merged
merged 9 commits into from
Aug 4, 2023
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
54 changes: 14 additions & 40 deletions TwitchLib.Client.Models/Announcement.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Collections.Generic;
using System.Drawing;
using TwitchLib.Client.Enums;
using TwitchLib.Client.Models.Extensions.NetCore;
using TwitchLib.Client.Enums;
using TwitchLib.Client.Models.Interfaces;
using TwitchLib.Client.Models.Internal;

namespace TwitchLib.Client.Models
{
/// <summary>Class representing Announcement in a Twitch channel.</summary>
public class Announcement
public class Announcement : IHexColorProperty
{
/// <summary>Property representing announcement message id</summary>
public string Id { get; }
Expand Down Expand Up @@ -55,7 +53,7 @@ public class Announcement
public UserType UserType { get; }

/// <summary>Property representing the tmi-sent-ts value.</summary>
public string TmiSentTs { get; }
public DateTimeOffset TmiSent { get; }

/// <summary>Property representing emote set of announcement.</summary>
public string EmoteSet { get; }
Expand All @@ -69,11 +67,8 @@ public class Announcement
/// <summary>Property representing the color value of the announcement.</summary>
public string MsgParamColor { get; }

/// <summary>Property representing the colorhex of the announcer.</summary>
public string ColorHex { get; }

/// <summary>Property representing HEX color as a System.Drawing.Color object.</summary>
public Color Color { get; }
/// <inheritdoc/>
public string HexColor { get; }

/// <summary>Property representing the message of the announcement.</summary>
public string Message { get; }
Expand All @@ -85,15 +80,13 @@ public Announcement(IrcMessage ircMessage)
{
RawIrc = ircMessage.ToString();
Message = ircMessage.Message;

foreach (var tag in ircMessage.Tags.Keys)
foreach (var tag in ircMessage.Tags)
{
var tagValue = ircMessage.Tags[tag];

switch (tag)
var tagValue = tag.Value;
switch (tag.Key)
{
case Tags.Badges:
Badges = Common.Helpers.ParseBadges(tagValue);
Badges = TagHelper.ToBadges(tagValue);
foreach (var badge in Badges)
{
switch (badge.Key)
Expand Down Expand Up @@ -123,12 +116,10 @@ public Announcement(IrcMessage ircMessage)
}
break;
case Tags.BadgeInfo:
BadgeInfo = Common.Helpers.ParseBadges(tagValue);
BadgeInfo = TagHelper.ToBadges(tagValue);
break;
case Tags.Color:
ColorHex = tagValue;
if (!string.IsNullOrEmpty(ColorHex))
Color = ColorTranslator.FromHtml(ColorHex);
HexColor = tagValue;
break;
case Tags.MsgParamColor:
MsgParamColor = tagValue;
Expand All @@ -153,30 +144,13 @@ public Announcement(IrcMessage ircMessage)
SystemMessageParsed = tagValue.Replace("\\s", " ");
break;
case Tags.TmiSentTs:
TmiSentTs = tagValue;
TmiSent = TagHelper.ToDateTimeOffsetFromUnixMs(tagValue);
break;
case Tags.UserId:
UserId = tagValue;
break;
case Tags.UserType:
switch (tagValue)
{
case "mod":
UserType = UserType.Moderator;
break;
case "global_mod":
UserType = UserType.GlobalModerator;
break;
case "admin":
UserType = UserType.Admin;
break;
case "staff":
UserType = UserType.Staff;
break;
default:
UserType = UserType.Viewer;
break;
}
UserType = TagHelper.ToUserType(tag.Value);
break;
}
}
Expand Down
7 changes: 2 additions & 5 deletions TwitchLib.Client.Models/Builders/ChatMessageBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

using TwitchLib.Client.Enums;
using TwitchLib.Client.Enums;

namespace TwitchLib.Client.Models.Builders
{
Expand Down Expand Up @@ -161,8 +159,7 @@ public ChatMessage Build()
_twitchLibMessage.UserId,
_twitchLibMessage.Username,
_twitchLibMessage.DisplayName,
_twitchLibMessage.ColorHex,
_twitchLibMessage.Color,
_twitchLibMessage.HexColor,
_twitchLibMessage.EmoteSet,
_message,
_twitchLibMessage.UserType,
Expand Down
12 changes: 5 additions & 7 deletions TwitchLib.Client.Models/Builders/GiftedSubscriptionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

using TwitchLib.Client.Enums;
using TwitchLib.Client.Enums;

namespace TwitchLib.Client.Models.Builders
{
Expand All @@ -27,7 +25,7 @@ public sealed class GiftedSubscriptionBuilder : IBuilder<GiftedSubscription>, IF
private string _roomId;
private string _systemMsg;
private string _systemMsgParsed;
private string _tmiSentTs;
private DateTimeOffset _tmiSent;
private string _userId;
private UserType _userType;

Expand Down Expand Up @@ -161,9 +159,9 @@ public GiftedSubscriptionBuilder WithSystemMsgParsed(string systemMsgParsed)
return this;
}

public GiftedSubscriptionBuilder WithTmiSentTs(string tmiSentTs)
public GiftedSubscriptionBuilder WithTmiSent(DateTimeOffset tmiSent)
{
_tmiSentTs = tmiSentTs;
_tmiSent = tmiSent;
return this;
}

Expand Down Expand Up @@ -207,7 +205,7 @@ public GiftedSubscription Build()
_isSubscriber,
_systemMsg,
_systemMsgParsed,
_tmiSentTs,
_tmiSent,
_isTurbo,
_userType,
_userId);
Expand Down
11 changes: 5 additions & 6 deletions TwitchLib.Client.Models/Builders/RaidNotificationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;

using System.Drawing;
using TwitchLib.Client.Enums;

namespace TwitchLib.Client.Models.Builders
Expand All @@ -23,7 +22,7 @@ public sealed class RaidNotificationBuilder : IBuilder<RaidNotification>, IFromI
private string _roomId;
private string _systemMsg;
private string _systemMsgParsed;
private string _tmiSentTs;
private DateTimeOffset _tmiSent;
private string _userId;
private UserType _userType;

Expand Down Expand Up @@ -129,9 +128,9 @@ public RaidNotificationBuilder WithSystemMsgParsed(string systemMsgParsed)
return this;
}

public RaidNotificationBuilder WithTmiSentTs(string tmiSentTs)
public RaidNotificationBuilder WithTmiSent(DateTimeOffset tmiSent)
{
_tmiSentTs = tmiSentTs;
_tmiSent = tmiSent;
return this;
}

Expand Down Expand Up @@ -170,7 +169,7 @@ public RaidNotification Build()
_isSubscriber,
_systemMsg,
_systemMsgParsed,
_tmiSentTs,
_tmiSent,
_isTurbo,
_userType,
_userId);
Expand Down
5 changes: 2 additions & 3 deletions TwitchLib.Client.Models/Builders/ReSubscriberBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public override SubscriberBase Build()
return new ReSubscriber(
Badges,
BadgeInfo,
ColorHex,
Color,
HexColor,
DisplayName,
EmoteSet,
Id,
Expand All @@ -47,7 +46,7 @@ public override SubscriberBase Build()
IsTurbo,
IsSubscriber,
IsPartner,
TmiSentTs,
TmiSent,
UserType,
RawIrc,
Channel,
Expand Down
12 changes: 5 additions & 7 deletions TwitchLib.Client.Models/Builders/SentMessageBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System.Collections.Generic;

using TwitchLib.Client.Enums;
using TwitchLib.Client.Enums;

namespace TwitchLib.Client.Models.Builders
{
public sealed class SentMessageBuilder : IBuilder<SentMessage>
{
private readonly List<KeyValuePair<string, string>> _badges = new List<KeyValuePair<string, string>>();
private string _channel;
private string _colorHex;
private string _color;
private string _displayName;
private string _emoteSet;
private bool _isModerator;
Expand All @@ -32,9 +30,9 @@ public SentMessageBuilder WithChannel(string channel)
return this;
}

public SentMessageBuilder WithColorHex(string colorHex)
public SentMessageBuilder WithColor(string color)
{
_colorHex = colorHex;
_color = color;
return this;
}

Expand Down Expand Up @@ -89,7 +87,7 @@ public SentMessage Build()
return new SentMessage(
_badges,
_channel,
_colorHex,
_color,
_displayName,
_emoteSet,
_isModerator,
Expand Down
29 changes: 9 additions & 20 deletions TwitchLib.Client.Models/Builders/SubscriberBaseBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Drawing;

using System.Drawing;
using TwitchLib.Client.Enums;

namespace TwitchLib.Client.Models.Builders
Expand All @@ -11,9 +9,7 @@ public class SubscriberBaseBuilder : IBuilder<SubscriberBase>

public List<KeyValuePair<string, string>> BadgeInfo { get; } = new List<KeyValuePair<string, string>>();

protected string ColorHex { get; set; }

protected Color Color { get; set; }
protected string HexColor { get; set; }

protected string DisplayName { get; set; }

Expand Down Expand Up @@ -45,7 +41,7 @@ public class SubscriberBaseBuilder : IBuilder<SubscriberBase>

protected string ParsedSystemMessage { get; set; }

protected string TmiSentTs { get; set; }
protected DateTimeOffset TmiSent { get; set; }

protected string UserId { get; set; }

Expand Down Expand Up @@ -84,15 +80,9 @@ public SubscriberBaseBuilder WithBadgeInfos(params KeyValuePair<string, string>[
return this;
}

public SubscriberBaseBuilder WithColorHex(string colorHex)
{
ColorHex = colorHex;
return this;
}

public SubscriberBaseBuilder WithColor(Color color)
public SubscriberBaseBuilder WithColor(string color)
{
Color = color;
HexColor = color;
return this;
}

Expand Down Expand Up @@ -186,9 +176,9 @@ public SubscriberBaseBuilder WithParsedSystemMessage(string parsedSystemMessage)
return this;
}

public SubscriberBaseBuilder WithTmiSentTs(string tmiSentTs)
public SubscriberBaseBuilder WithTmiSent(DateTimeOffset tmiSent)
{
TmiSentTs = tmiSentTs;
TmiSent = tmiSent;
return this;
}

Expand Down Expand Up @@ -245,8 +235,7 @@ public virtual SubscriberBase Build()
return new SubscriberBase(
Badges,
BadgeInfo,
ColorHex,
Color,
HexColor,
DisplayName,
EmoteSet,
Id,
Expand All @@ -266,7 +255,7 @@ public virtual SubscriberBase Build()
IsTurbo,
IsSubscriber,
IsPartner,
TmiSentTs,
TmiSent,
UserType,
RawIrc,
Channel,
Expand Down
5 changes: 2 additions & 3 deletions TwitchLib.Client.Models/Builders/SubscriberBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public override SubscriberBase Build()
return new Subscriber(
Badges,
BadgeInfo,
ColorHex,
Color,
HexColor,
DisplayName,
EmoteSet,
Id,
Expand All @@ -47,7 +46,7 @@ public override SubscriberBase Build()
IsTurbo,
IsSubscriber,
IsPartner,
TmiSentTs,
TmiSent,
UserType,
RawIrc,
Channel);
Expand Down
15 changes: 3 additions & 12 deletions TwitchLib.Client.Models/Builders/TwitchLibMessageBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Drawing;

using TwitchLib.Client.Enums;
using TwitchLib.Client.Enums;

namespace TwitchLib.Client.Models.Builders
{
Expand All @@ -17,15 +14,9 @@ public TwitchLibMessageBuilder WithBadges(List<KeyValuePair<string, string>> bad
return this;
}

public TwitchLibMessageBuilder WithColorHex(string colorHex)
{
ColorHex = colorHex;
return this;
}

public TwitchLibMessageBuilder WithColorHex(Color color)
public TwitchLibMessageBuilder WithColorHex(string color)
{
Color = color;
HexColor = color;
return this;
}

Expand Down
Loading
Loading