From f2c98f9d8605b61a22ead644d91973e7c9f92170 Mon Sep 17 00:00:00 2001 From: Peter Zignego Date: Tue, 5 Mar 2019 23:10:59 -0500 Subject: [PATCH 1/4] Add support for member_left_channel and member_joined_channel events --- SKClient/Sources/Client.swift | 28 +++++++++ SKCore/Sources/Event.swift | 10 ++- SlackKit.xcodeproj/project.pbxproj | 12 ++++ .../Resources/member_joined_channel.json | 8 +++ .../Resources/member_left_channel.json | 7 +++ SlackKitTests/SKClient/SKClientTests.swift | 63 +++++++++++++++++++ SlackKitTests/SKCore/SKCoreTests.swift | 28 ++++----- 7 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 SlackKitTests/Resources/member_joined_channel.json create mode 100644 SlackKitTests/Resources/member_left_channel.json create mode 100644 SlackKitTests/SKClient/SKClientTests.swift diff --git a/SKClient/Sources/Client.swift b/SKClient/Sources/Client.swift index 9ced15a..4d81a7e 100755 --- a/SKClient/Sources/Client.swift +++ b/SKClient/Sources/Client.swift @@ -115,6 +115,10 @@ open class Client { manualPresenceChange(event) case .prefChange: changePreference(event) + case .memberJoinedChannel: + memberJoinedChannel(event) + case .memberLeftChannel: + memberLeftChannel(event) case .userChange: userChange(event) case .teamJoin: @@ -360,6 +364,30 @@ extension Client { func channelHistoryChanged(_ event: Event) { } + + func memberJoinedChannel(_ event: Event) { + guard + let channel = event.channel?.id, + let member = event.user?.id + else { + return + } + + channels[channel]?.members?.append(member) + } + + func memberLeftChannel(_ event: Event) { + guard + let channel = event.channel?.id, + let member = event.user?.id + else { + return + } + + if let index = channels[channel]?.members?.index(of: member) { + channels[channel]?.members?.remove(at: index) + } + } } // MARK: - Do Not Disturb diff --git a/SKCore/Sources/Event.swift b/SKCore/Sources/Event.swift index 17c52f0..0c26999 100755 --- a/SKCore/Sources/Event.swift +++ b/SKCore/Sources/Event.swift @@ -64,6 +64,8 @@ public enum EventType: String { case pinRemoved = "pin_removed" case pong = "pong" case presenceChange = "presence_change" + case memberJoinedChannel = "member_joined_channel" + case memberLeftChannel = "member_left_channel" case manualPresenceChange = "manual_presence_change" case prefChange = "pref_change" case userChange = "user_change" @@ -151,6 +153,7 @@ public class Event { public let edited: Edited? public let bot: Bot? public let channel: Channel? + public let channelType: String? public let comment: Comment? public let user: User? public let files: [File] @@ -158,10 +161,12 @@ public class Event { public let nestedMessage: Message? public let itemUser: String? public let item: Item? + public let teamID: String? public let dndStatus: DoNotDisturbStatus? public let subteam: UserGroup? public let subteamID: String? - public var profile: CustomProfile? + public let profile: CustomProfile? + public let inviterID: String? //swiftlint:disable function_body_length public init(_ event: [String: Any]) { @@ -198,6 +203,9 @@ public class Event { nestedMessage = Message(dictionary: event["message"] as? [String: Any]) profile = CustomProfile(profile: event["profile"] as? [String: Any]) files = (event["files"] as? [Any])?.compactMap { File(file: $0 as? [String: Any]) } ?? [] + channelType = event["channel_type"] as? String + teamID = event["team"] as? String + inviterID = event["inviter"] as? String // Comment, Channel, and User can come across as Strings or Dictionaries if let commentDictionary = event["comment"] as? [String: Any] { diff --git a/SlackKit.xcodeproj/project.pbxproj b/SlackKit.xcodeproj/project.pbxproj index f352338..7fbaebe 100644 --- a/SlackKit.xcodeproj/project.pbxproj +++ b/SlackKit.xcodeproj/project.pbxproj @@ -19,6 +19,9 @@ 2601B6D62223038A00F197AB /* user.json in Resources */ = {isa = PBXBuildFile; fileRef = 26D4E6242220731800A67B67 /* user.json */; }; 2601B6D72223038A00F197AB /* usergroup.json in Resources */ = {isa = PBXBuildFile; fileRef = 26D4E6252220731800A67B67 /* usergroup.json */; }; 2601B6E2222371A100F197AB /* SKCoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26D4E6362220733F00A67B67 /* SKCoreTests.swift */; }; + 2601B70C222F6CFD00F197AB /* SKClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2601B70B222F6CFD00F197AB /* SKClientTests.swift */; }; + 2601B70F222F766D00F197AB /* member_joined_channel.json in Resources */ = {isa = PBXBuildFile; fileRef = 2601B70D222F766D00F197AB /* member_joined_channel.json */; }; + 2601B710222F766D00F197AB /* member_left_channel.json in Resources */ = {isa = PBXBuildFile; fileRef = 2601B70E222F766D00F197AB /* member_left_channel.json */; }; 263B102421FE33A000AF9EF9 /* UserGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263B100921FE33A000AF9EF9 /* UserGroup.swift */; }; 263B102521FE33A000AF9EF9 /* Scope.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263B100A21FE33A000AF9EF9 /* Scope.swift */; }; 263B102621FE33A000AF9EF9 /* AttachmentField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263B100B21FE33A000AF9EF9 /* AttachmentField.swift */; }; @@ -191,6 +194,9 @@ /* Begin PBXFileReference section */ 2601B6B72220813600F197AB /* SlackKitTests.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = SlackKitTests.xcconfig; sourceTree = ""; }; 2601B6C1222085AB00F197AB /* SlackKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SlackKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 2601B70B222F6CFD00F197AB /* SKClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKClientTests.swift; sourceTree = ""; }; + 2601B70D222F766D00F197AB /* member_joined_channel.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = member_joined_channel.json; sourceTree = ""; }; + 2601B70E222F766D00F197AB /* member_left_channel.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = member_left_channel.json; sourceTree = ""; }; 263B0F9B21FE235100AF9EF9 /* SKCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SKCore.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 263B0FAA21FE23A000AF9EF9 /* SKClient.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SKClient.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 263B0FB721FE23B000AF9EF9 /* SKWebAPI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SKWebAPI.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -824,6 +830,8 @@ 26D4E6202220731700A67B67 /* rtm.start.json */, 26D4E6242220731800A67B67 /* user.json */, 26D4E6252220731800A67B67 /* usergroup.json */, + 2601B70D222F766D00F197AB /* member_joined_channel.json */, + 2601B70E222F766D00F197AB /* member_left_channel.json */, ); path = Resources; sourceTree = ""; @@ -847,6 +855,7 @@ 26D4E61B222072C600A67B67 /* SKClient */ = { isa = PBXGroup; children = ( + 2601B70B222F6CFD00F197AB /* SKClientTests.swift */, ); path = SKClient; sourceTree = ""; @@ -1168,7 +1177,9 @@ buildActionMask = 2147483647; files = ( 2601B6CD2223038A00F197AB /* channel.json in Resources */, + 2601B710222F766D00F197AB /* member_left_channel.json in Resources */, 2601B6CE2223038A00F197AB /* conversation.json in Resources */, + 2601B70F222F766D00F197AB /* member_joined_channel.json in Resources */, 2601B6CF2223038A00F197AB /* events.json in Resources */, 2601B6D02223038A00F197AB /* file.json in Resources */, 2601B6D12223038A00F197AB /* group.json in Resources */, @@ -1230,6 +1241,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 2601B70C222F6CFD00F197AB /* SKClientTests.swift in Sources */, 2601B6E2222371A100F197AB /* SKCoreTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SlackKitTests/Resources/member_joined_channel.json b/SlackKitTests/Resources/member_joined_channel.json new file mode 100644 index 0000000..4459370 --- /dev/null +++ b/SlackKitTests/Resources/member_joined_channel.json @@ -0,0 +1,8 @@ +{ + "type": "member_joined_channel", + "user": "U0CJ1TWKX", + "channel": "C0CHZA86Q", + "channel_type": "C", + "team": "T0CHZBU59", + "inviter": "U123456789" +} diff --git a/SlackKitTests/Resources/member_left_channel.json b/SlackKitTests/Resources/member_left_channel.json new file mode 100644 index 0000000..5fe8f63 --- /dev/null +++ b/SlackKitTests/Resources/member_left_channel.json @@ -0,0 +1,7 @@ +{ + "type": "member_left_channel", + "user": "U0CJ5PC7L", + "channel": "C0CJ25PDM", + "channel_type": "C", + "team": "T0CHZBU59" +} diff --git a/SlackKitTests/SKClient/SKClientTests.swift b/SlackKitTests/SKClient/SKClientTests.swift new file mode 100644 index 0000000..6074523 --- /dev/null +++ b/SlackKitTests/SKClient/SKClientTests.swift @@ -0,0 +1,63 @@ +// +// SKClientTests.swift +// SlackKitTests +// +// Created by Peter Zignego on 3/5/19. +// Copyright © 2019 Peter Zignego. All rights reserved. +// + +import XCTest +@testable import SKClient + +final class SKClientTests: XCTestCase { + + static var rootPath: String { + #if Xcode + return Bundle(for: self).resourcePath! + #else + return "SlackKitTests/Resources" + #endif + } + + struct JSONData { + static let rtm_start = try! Data(contentsOf: URL(fileURLWithPath: "\(rootPath)/rtm.start.json")) + static let member_joined_channel = try! Data(contentsOf: URL(fileURLWithPath: "\(rootPath)/member_joined_channel.json")) + static let member_left_channel = try! Data(contentsOf: URL(fileURLWithPath: "\(rootPath)/member_left_channel.json")) + } + + static var allTests = [ + ("testMemberJoinedChannel", testMemberJoinedChannel), + ("testMemberLeftChannel", testMemberLeftChannel) + ] + + var client: Client! + + override func setUp() { + client = Client() + client.initialSetup(JSON: try! JSONSerialization.jsonObject(with: JSONData.rtm_start, options: []) as! [String: Any]) + } + + func testMemberJoinedChannel() { + let channelId = "C0CHZA86Q" + let userId = "U0CJ1TWKX" + let json = try! JSONSerialization.jsonObject(with: JSONData.member_joined_channel, options: []) as! [String: Any] + client.memberJoinedChannel(Event(json)) + if let contains = client.channels[channelId]?.members?.contains(userId) { + XCTAssertTrue(contains) + } else { + XCTFail() + } + } + + func testMemberLeftChannel() { + let channelId = "C0CJ25PDM" + let userId = "U0CJ5PC7L" + let json = try! JSONSerialization.jsonObject(with: JSONData.member_left_channel, options: []) as! [String: Any] + client.memberLeftChannel(Event(json)) + if let contains = client.channels[channelId]?.members?.contains(userId) { + XCTAssertFalse(contains) + } else { + XCTFail() + } + } +} diff --git a/SlackKitTests/SKCore/SKCoreTests.swift b/SlackKitTests/SKCore/SKCoreTests.swift index 2cf921f..0caa1ff 100755 --- a/SlackKitTests/SKCore/SKCoreTests.swift +++ b/SlackKitTests/SKCore/SKCoreTests.swift @@ -46,6 +46,19 @@ final class SKCoreTests: XCTestCase { static let events = try! Data(contentsOf: URL(fileURLWithPath: "\(rootPath)/events.json")) } + static var allTests = [ + ("testChannel", testChannel), + ("testConversation", testConversation), + ("testFile", testFile), + ("testGroup", testGroup), + ("testIm", testIm), + ("TestMpim", testMpim), + ("testUser", testUser), + ("testUserGroup", testUserGroup), + ("testEvents", testEvents) + ] + + func testEvents() { let eventsKeys = [ // Bot Event "bot_added","bot_changed", @@ -73,22 +86,9 @@ final class SKCoreTests: XCTestCase { "user_change", "user_typing" ] - static var allTests = [ - ("testChannel", testChannel), - ("testConversation", testConversation), - ("testFile", testFile), - ("testGroup", testGroup), - ("testIm", testIm), - ("TestMpim", testMpim), - ("testUser", testUser), - ("testUserGroup", testUserGroup), - ("testEvents",testEvents) - ] - - func testEvents() { let data = JSONData.events let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] - _ = self.eventsKeys.map { json[$0] as! [String: Any] }.map { Event($0) } + _ = eventsKeys.map { json[$0] as! [String: Any] }.map { Event($0) } } func testChannel() { From 03561eab3605ccd3a343d35f8ae703de510d9c08 Mon Sep 17 00:00:00 2001 From: Peter Zignego Date: Tue, 5 Mar 2019 23:11:25 -0500 Subject: [PATCH 2/4] Generate test files on CI --- LinuxMain.swift | 8 -------- SlackKitTests/XCTestManifests.swift | 23 ----------------------- azure-pipelines.yml | 1 + 3 files changed, 1 insertion(+), 31 deletions(-) delete mode 100644 LinuxMain.swift delete mode 100755 SlackKitTests/XCTestManifests.swift diff --git a/LinuxMain.swift b/LinuxMain.swift deleted file mode 100644 index 4803c3d..0000000 --- a/LinuxMain.swift +++ /dev/null @@ -1,8 +0,0 @@ -import XCTest - -import SlackKitTests - -var tests = [XCTestCaseEntry]() -tests += SlackKitTests.__allTests() - -XCTMain(tests) diff --git a/SlackKitTests/XCTestManifests.swift b/SlackKitTests/XCTestManifests.swift deleted file mode 100755 index 02f5a56..0000000 --- a/SlackKitTests/XCTestManifests.swift +++ /dev/null @@ -1,23 +0,0 @@ -import XCTest - -extension SKCoreTests { - static let __allTests = [ - ("testChannel", testChannel), - ("testConversation", testConversation), - ("testEvents", testEvents), - ("testFile", testFile), - ("testGroup", testGroup), - ("testIm", testIm), - ("testMpim", testMpim), - ("testUser", testUser), - ("testUserGroup", testUserGroup), - ] -} - -#if !os(macOS) -public func __allTests() -> [XCTestCaseEntry] { - return [ - testCase(SKCoreTests.__allTests), - ] -} -#endif diff --git a/azure-pipelines.yml b/azure-pipelines.yml index b4f7fd0..8b1ec48 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -81,4 +81,5 @@ jobs: # Swift build $(Build.SourcesDirectory)/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin/swift build # Swift test + $(Build.SourcesDirectory)/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin/swift test --generate-linuxmain $(Build.SourcesDirectory)/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin/swift test \ No newline at end of file From 1ad14f016a0228731c8874c2cf92323e5e27dee2 Mon Sep 17 00:00:00 2001 From: Peter Zignego Date: Tue, 5 Mar 2019 23:11:37 -0500 Subject: [PATCH 3/4] Remove old files --- Podfile | 28 ------ .../xcschemes/Robot or Not Bot.xcscheme | 91 ------------------- 2 files changed, 119 deletions(-) delete mode 100644 Podfile delete mode 100644 SlackKit.xcodeproj/xcshareddata/xcschemes/Robot or Not Bot.xcscheme diff --git a/Podfile b/Podfile deleted file mode 100644 index ee6a15a..0000000 --- a/Podfile +++ /dev/null @@ -1,28 +0,0 @@ -use_frameworks! - -target 'SlackKit macOS' do - platform :osx, '10.11' - pod 'SKCore', '~> 4.1.0' - pod 'SKClient', '~> 4.1.0' - pod 'SKWebAPI', '~> 4.1.0' - pod 'SKRTMAPI', '~> 4.1.0' - pod 'SKServer', '~> 4.1.0' -end - -target 'SlackKit iOS' do - platform :ios, '9.0' - pod 'SKCore', '~> 4.1.0' - pod 'SKClient', '~> 4.1.0' - pod 'SKWebAPI', '~> 4.1.0' - pod 'SKRTMAPI', '~> 4.1.0' - pod 'SKServer', '~> 4.1.0' -end - -target 'SlackKit tvOS' do - platform :tvos, '9.0' - pod 'SKCore', '~> 4.1.0' - pod 'SKClient', '~> 4.1.0' - pod 'SKWebAPI', '~> 4.1.0' - pod 'SKRTMAPI', '~> 4.1.0' - pod 'SKServer', '~> 4.1.0' -end \ No newline at end of file diff --git a/SlackKit.xcodeproj/xcshareddata/xcschemes/Robot or Not Bot.xcscheme b/SlackKit.xcodeproj/xcshareddata/xcschemes/Robot or Not Bot.xcscheme deleted file mode 100644 index c9abdd9..0000000 --- a/SlackKit.xcodeproj/xcshareddata/xcschemes/Robot or Not Bot.xcscheme +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 245d70912516d0ba7eed8f1c340bdbe6ebed8d40 Mon Sep 17 00:00:00 2001 From: Peter Zignego Date: Tue, 5 Mar 2019 23:17:24 -0500 Subject: [PATCH 4/4] Guess not --- LinuxMain.swift | 8 ++++++++ SlackKit.xcodeproj/project.pbxproj | 2 -- SlackKitTests/XCTestManifests.swift | 31 +++++++++++++++++++++++++++++ azure-pipelines.yml | 1 - 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 LinuxMain.swift create mode 100644 SlackKitTests/XCTestManifests.swift diff --git a/LinuxMain.swift b/LinuxMain.swift new file mode 100644 index 0000000..4803c3d --- /dev/null +++ b/LinuxMain.swift @@ -0,0 +1,8 @@ +import XCTest + +import SlackKitTests + +var tests = [XCTestCaseEntry]() +tests += SlackKitTests.__allTests() + +XCTMain(tests) diff --git a/SlackKit.xcodeproj/project.pbxproj b/SlackKit.xcodeproj/project.pbxproj index 7fbaebe..40af15c 100644 --- a/SlackKit.xcodeproj/project.pbxproj +++ b/SlackKit.xcodeproj/project.pbxproj @@ -311,7 +311,6 @@ 26D4E6292220731800A67B67 /* rtm.connect.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = rtm.connect.json; sourceTree = ""; }; 26D4E62A2220731800A67B67 /* file.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = file.json; sourceTree = ""; }; 26D4E6362220733F00A67B67 /* SKCoreTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SKCoreTests.swift; sourceTree = ""; }; - 26D4E6372220733F00A67B67 /* XCTestManifests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestManifests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -803,7 +802,6 @@ 26D4E6112220719D00A67B67 /* SlackKitTests */ = { isa = PBXGroup; children = ( - 26D4E6372220733F00A67B67 /* XCTestManifests.swift */, 26D4E61A222072BC00A67B67 /* SlackKit */, 26D4E61B222072C600A67B67 /* SKClient */, 26D4E61C222072D100A67B67 /* SKCore */, diff --git a/SlackKitTests/XCTestManifests.swift b/SlackKitTests/XCTestManifests.swift new file mode 100644 index 0000000..75eb945 --- /dev/null +++ b/SlackKitTests/XCTestManifests.swift @@ -0,0 +1,31 @@ +import XCTest + +extension SKClientTests { + static let __allTests = [ + ("testMemberJoinedChannel", testMemberJoinedChannel), + ("testMemberLeftChannel", testMemberLeftChannel), + ] +} + +extension SKCoreTests { + static let __allTests = [ + ("testChannel", testChannel), + ("testConversation", testConversation), + ("testEvents", testEvents), + ("testFile", testFile), + ("testGroup", testGroup), + ("testIm", testIm), + ("testMpim", testMpim), + ("testUser", testUser), + ("testUserGroup", testUserGroup), + ] +} + +#if !os(macOS) +public func __allTests() -> [XCTestCaseEntry] { + return [ + testCase(SKClientTests.__allTests), + testCase(SKCoreTests.__allTests), + ] +} +#endif diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 8b1ec48..b4f7fd0 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -81,5 +81,4 @@ jobs: # Swift build $(Build.SourcesDirectory)/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin/swift build # Swift test - $(Build.SourcesDirectory)/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin/swift test --generate-linuxmain $(Build.SourcesDirectory)/swift-4.2.1-RELEASE-ubuntu16.04/usr/bin/swift test \ No newline at end of file