From 1ff1321d501230d0edb9101d0842341c1e8243bd Mon Sep 17 00:00:00 2001 From: Mahdi Bahrami Date: Mon, 3 Oct 2022 09:20:28 +0330 Subject: [PATCH] added pinned-chat info to PrivateMessage --- .../IncomingMessage/PrivateMessage.swift | 38 +++++++++++++-- .../PrivateMessageTests.swift | 47 ++++++++++++++++++- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Sources/TwitchIRC/IncomingMessage/PrivateMessage.swift b/Sources/TwitchIRC/IncomingMessage/PrivateMessage.swift index fb83287..6e64a8a 100644 --- a/Sources/TwitchIRC/IncomingMessage/PrivateMessage.swift +++ b/Sources/TwitchIRC/IncomingMessage/PrivateMessage.swift @@ -14,9 +14,7 @@ public struct PrivateMessage: MessageWithBadges { /// Replied user's Twitch identifier. public var userId = String() - public init() { } - - init( + internal init( displayName: String, userLogin: String, message: String, @@ -29,6 +27,29 @@ public struct PrivateMessage: MessageWithBadges { self.id = id self.userId = userId } + + public init() { } + } + + public struct PinnedChat { + public var amount = UInt() + public var canonicalAmount = UInt() + public var currency = String() + public var exponent = UInt() + + internal init( + amount: UInt, + canonicalAmount: UInt, + currency: String, + exponent: UInt + ) { + self.amount = amount + self.canonicalAmount = canonicalAmount + self.currency = currency + self.exponent = exponent + } + + public init() { } } /// Channel's name with no uppercased/Han characters. @@ -76,6 +97,8 @@ public struct PrivateMessage: MessageWithBadges { public var userId = String() /// Info about the replied message, if any. public var replyParent = ReplyParent() + /// Info of paid pinned messages. + public var pinnedChat = PinnedChat() /// Contains info about unused info and parsing problems. public var parsingLeftOvers = ParsingLeftOvers() @@ -138,9 +161,15 @@ public struct PrivateMessage: MessageWithBadges { id: parser.string(for: "reply-parent-msg-id"), userId: parser.string(for: "reply-parent-user-id") ) + self.pinnedChat = .init( + amount: parser.uint(for: "pinned-chat-paid-amount"), + canonicalAmount: parser.uint(for: "pinned-chat-paid-canonical-amount"), + currency: parser.string(for: "pinned-chat-paid-currency"), + exponent: parser.uint(for: "pinned-chat-paid-exponent") + ) let deprecatedKeys = ["turbo", "mod", "vip", "subscriber", "user-type"] - let occasionalKeys = [["crowd-chant-parent-msg-id"], ["bits"], ["emote-only"], ["msg-id"], ["custom-reward-id"], ["client-nonce"], ["flags"], ["first-msg"], ["reply-parent-display-name", "reply-parent-user-login", "reply-parent-msg-body", "reply-parent-msg-id", "reply-parent-user-id"]] + let occasionalKeys = [["crowd-chant-parent-msg-id"], ["bits"], ["emote-only"], ["msg-id"], ["custom-reward-id"], ["client-nonce"], ["flags"], ["first-msg"], ["reply-parent-display-name", "reply-parent-user-login", "reply-parent-msg-body", "reply-parent-msg-id", "reply-parent-user-id"], ["pinned-chat-paid-amount", "pinned-chat-paid-canonical-amount", "pinned-chat-paid-currency", "pinned-chat-paid-exponent"]] self.parsingLeftOvers = parser.getLeftOvers( excludedUnusedKeys: deprecatedKeys, @@ -153,4 +182,5 @@ public struct PrivateMessage: MessageWithBadges { #if swift(>=5.5) extension PrivateMessage: Sendable { } extension PrivateMessage.ReplyParent: Sendable { } +extension PrivateMessage.PinnedChat: Sendable { } #endif diff --git a/Tests/TwitchIRCTests/IncomingMessageTests/PrivateMessageTests.swift b/Tests/TwitchIRCTests/IncomingMessageTests/PrivateMessageTests.swift index db55c33..7fb4bfc 100644 --- a/Tests/TwitchIRCTests/IncomingMessageTests/PrivateMessageTests.swift +++ b/Tests/TwitchIRCTests/IncomingMessageTests/PrivateMessageTests.swift @@ -214,7 +214,7 @@ final class PrivateMessageTests: XCTestCase { XCTAssertTrue(msg.parsingLeftOvers.unusedPairs.isEmpty) } - /// Tests where `returningChatter`. + /// Tests `returningChatter`. func testParsedValues7() throws { let string = "@badge-info=;badges=vip/1;color=#0000FF;display-name=flexvegapro3;emotes=;first-msg=0;flags=;id=9c3e3024-a4b4-493b-8668-96597be002ac;mod=0;returning-chatter=0;room-id=550217406;subscriber=0;tmi-sent-ts=1655439139118;turbo=0;user-id=705215697;user-type= :flexvegapro3!flexvegapro3@flexvegapro3.tmi.twitch.tv PRIVMSG #novastark_7 :jajaja xD" @@ -244,6 +244,43 @@ final class PrivateMessageTests: XCTestCase { XCTAssertTrue(msg.replyParent == .init()) XCTAssertTrue(msg.parsingLeftOvers.isEmpty, "Non-empty parsing left-overs: \(msg.parsingLeftOvers)") } + + /// Tests `pinnedChat`. + func testParsedValues8() throws { + let string = "@badge-info=subscriber/3;badges=vip/1,subscriber/3,sub-gifter/300;color=#FFFF00;display-name=2Taqz;emotes=;first-msg=0;flags=;id=cd943357-38bf-4bc9-b524-c0da490dcc36;mod=0;pinned-chat-paid-amount=1200;pinned-chat-paid-canonical-amount=10;pinned-chat-paid-currency=EUR;pinned-chat-paid-exponent=2;returning-chatter=0;room-id=117855516;subscriber=1;tmi-sent-ts=1664561279533;turbo=0;user-id=249610052;user-type=;vip=1 :2taqz!2taqz@2taqz.tmi.twitch.tv PRIVMSG #domino :und wieder 12€ verbrennen" + + let msg: PrivateMessage = try TestUtils.parseAndUnwrap(string: string) + + XCTAssertEqual(msg.channel, "domino") + XCTAssertEqual(msg.message, "und wieder 12€ verbrennen") + XCTAssertEqual(msg.badgeInfo, ["subscriber/3"]) + XCTAssertEqual(msg.badges, ["vip/1", "subscriber/3", "sub-gifter/300"]) + XCTAssertEqual(msg.bits, "") + XCTAssertEqual(msg.color, "#FFFF00") + XCTAssertEqual(msg.displayName, "2Taqz") + XCTAssertEqual(msg.userLogin, "2taqz") + XCTAssertEqual(msg.emotes, "") + XCTAssertEqual(msg.emoteOnly, false) + XCTAssertEqual(msg.flags, []) + XCTAssertEqual(msg.firstMessage, false) + XCTAssertEqual(msg.returningChatter, false) + XCTAssertEqual(msg.messageId, "") + XCTAssertEqual(msg.id, "cd943357-38bf-4bc9-b524-c0da490dcc36") + XCTAssertEqual(msg.crowdChantParentMessageId, "") + XCTAssertEqual(msg.customRewardId, "") + XCTAssertEqual(msg.roomId, "117855516") + XCTAssertEqual(msg.tmiSentTs, 1664561279533) + XCTAssertEqual(msg.clientNonce, "") + XCTAssertEqual(msg.userId, "249610052") + XCTAssertTrue(msg.replyParent == .init()) + XCTAssertTrue(msg.pinnedChat == .init( + amount: 1200, + canonicalAmount: 10, + currency: "EUR", + exponent: 2 + )) + XCTAssertTrue(msg.parsingLeftOvers.isEmpty, "Non-empty parsing left-overs: \(msg.parsingLeftOvers)") + } } // MARK: - Emote Equatable @@ -264,3 +301,11 @@ private func == (lhs: PrivateMessage.ReplyParent, rhs: PrivateMessage.ReplyParen && lhs.id == rhs.id && lhs.userId == rhs.userId } + +// MARK: - PrivateMessage.PinnedChat Equatable (basically) +private func == (lhs: PrivateMessage.PinnedChat, rhs: PrivateMessage.PinnedChat) -> Bool { + lhs.amount == rhs.amount + && lhs.amount == rhs.amount + && lhs.currency == rhs.currency + && lhs.canonicalAmount == rhs.canonicalAmount +}