Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #150 from pvzig/member-leave-join-events
Browse files Browse the repository at this point in the history
Add support for member_joined_channel and member_left_channel events
  • Loading branch information
pvzig committed Mar 6, 2019
2 parents bde39be + 245d709 commit c2f7632
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 136 deletions.
28 changes: 0 additions & 28 deletions Podfile

This file was deleted.

28 changes: 28 additions & 0 deletions SKClient/Sources/Client.swift
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion SKCore/Sources/Event.swift
Expand Up @@ -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"
Expand Down Expand Up @@ -151,17 +153,20 @@ 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]
public let message: Message?
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]) {
Expand Down Expand Up @@ -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] {
Expand Down
14 changes: 12 additions & 2 deletions SlackKit.xcodeproj/project.pbxproj
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -191,6 +194,9 @@
/* Begin PBXFileReference section */
2601B6B72220813600F197AB /* SlackKitTests.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = SlackKitTests.xcconfig; sourceTree = "<group>"; };
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 = "<group>"; };
2601B70D222F766D00F197AB /* member_joined_channel.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = member_joined_channel.json; sourceTree = "<group>"; };
2601B70E222F766D00F197AB /* member_left_channel.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = member_left_channel.json; sourceTree = "<group>"; };
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; };
Expand Down Expand Up @@ -305,7 +311,6 @@
26D4E6292220731800A67B67 /* rtm.connect.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = rtm.connect.json; sourceTree = "<group>"; };
26D4E62A2220731800A67B67 /* file.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = file.json; sourceTree = "<group>"; };
26D4E6362220733F00A67B67 /* SKCoreTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SKCoreTests.swift; sourceTree = "<group>"; };
26D4E6372220733F00A67B67 /* XCTestManifests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestManifests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -797,7 +802,6 @@
26D4E6112220719D00A67B67 /* SlackKitTests */ = {
isa = PBXGroup;
children = (
26D4E6372220733F00A67B67 /* XCTestManifests.swift */,
26D4E61A222072BC00A67B67 /* SlackKit */,
26D4E61B222072C600A67B67 /* SKClient */,
26D4E61C222072D100A67B67 /* SKCore */,
Expand All @@ -824,6 +828,8 @@
26D4E6202220731700A67B67 /* rtm.start.json */,
26D4E6242220731800A67B67 /* user.json */,
26D4E6252220731800A67B67 /* usergroup.json */,
2601B70D222F766D00F197AB /* member_joined_channel.json */,
2601B70E222F766D00F197AB /* member_left_channel.json */,
);
path = Resources;
sourceTree = "<group>";
Expand All @@ -847,6 +853,7 @@
26D4E61B222072C600A67B67 /* SKClient */ = {
isa = PBXGroup;
children = (
2601B70B222F6CFD00F197AB /* SKClientTests.swift */,
);
path = SKClient;
sourceTree = "<group>";
Expand Down Expand Up @@ -1168,7 +1175,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 */,
Expand Down Expand Up @@ -1230,6 +1239,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2601B70C222F6CFD00F197AB /* SKClientTests.swift in Sources */,
2601B6E2222371A100F197AB /* SKCoreTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

This file was deleted.

8 changes: 8 additions & 0 deletions 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"
}
7 changes: 7 additions & 0 deletions SlackKitTests/Resources/member_left_channel.json
@@ -0,0 +1,7 @@
{
"type": "member_left_channel",
"user": "U0CJ5PC7L",
"channel": "C0CJ25PDM",
"channel_type": "C",
"team": "T0CHZBU59"
}
63 changes: 63 additions & 0 deletions 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()
}
}
}

0 comments on commit c2f7632

Please sign in to comment.