Skip to content
This repository was archived by the owner on Jan 28, 2019. It is now read-only.

Commit 756c5ff

Browse files
authored
Merge pull request #3 from SlackKit/threads
Support for sending threaded messages
2 parents 21fdc0d + 062939e commit 756c5ff

File tree

5 files changed

+107
-47
lines changed

5 files changed

+107
-47
lines changed

.swiftlint.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
disabled_rules:
2+
- identifier_name
3+
- function_parameter_count
4+
line_length: 140
5+
excluded: # paths to ignore during linting. Takes precedence over `included`.
6+
- Carthage
7+
- Pods

SKRTMAPI.xcodeproj/project.pbxproj

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
2684F1791E95AA6900536DCC /* Frameworks */,
195195
2684F17A1E95AA6900536DCC /* Headers */,
196196
2684F17B1E95AA6900536DCC /* Resources */,
197+
26A2E6291EEB18D2005C25AC /* SwiftLint */,
197198
);
198199
buildRules = (
199200
);
@@ -212,6 +213,7 @@
212213
2684F1DE1E95ABD400536DCC /* Frameworks */,
213214
2684F1DF1E95ABD400536DCC /* Headers */,
214215
2684F1E01E95ABD400536DCC /* Resources */,
216+
2668B5101EEB39A20082DE33 /* SwiftLint */,
215217
);
216218
buildRules = (
217219
);
@@ -230,6 +232,7 @@
230232
2684F2021E95ABD600536DCC /* Frameworks */,
231233
2684F2031E95ABD600536DCC /* Headers */,
232234
2684F2041E95ABD600536DCC /* Resources */,
235+
2668B5111EEB39B30082DE33 /* SwiftLint */,
233236
);
234237
buildRules = (
235238
);
@@ -304,6 +307,51 @@
304307
};
305308
/* End PBXResourcesBuildPhase section */
306309

310+
/* Begin PBXShellScriptBuildPhase section */
311+
2668B5101EEB39A20082DE33 /* SwiftLint */ = {
312+
isa = PBXShellScriptBuildPhase;
313+
buildActionMask = 2147483647;
314+
files = (
315+
);
316+
inputPaths = (
317+
);
318+
name = SwiftLint;
319+
outputPaths = (
320+
);
321+
runOnlyForDeploymentPostprocessing = 0;
322+
shellPath = /bin/sh;
323+
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
324+
};
325+
2668B5111EEB39B30082DE33 /* SwiftLint */ = {
326+
isa = PBXShellScriptBuildPhase;
327+
buildActionMask = 2147483647;
328+
files = (
329+
);
330+
inputPaths = (
331+
);
332+
name = SwiftLint;
333+
outputPaths = (
334+
);
335+
runOnlyForDeploymentPostprocessing = 0;
336+
shellPath = /bin/sh;
337+
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
338+
};
339+
26A2E6291EEB18D2005C25AC /* SwiftLint */ = {
340+
isa = PBXShellScriptBuildPhase;
341+
buildActionMask = 2147483647;
342+
files = (
343+
);
344+
inputPaths = (
345+
);
346+
name = SwiftLint;
347+
outputPaths = (
348+
);
349+
runOnlyForDeploymentPostprocessing = 0;
350+
shellPath = /bin/sh;
351+
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
352+
};
353+
/* End PBXShellScriptBuildPhase section */
354+
307355
/* Begin PBXSourcesBuildPhase section */
308356
2684F1781E95AA6900536DCC /* Sources */ = {
309357
isa = PBXSourcesBuildPhase;

Sources/SKRTMAPI/Conformers/StarscreamRTM.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,47 @@ import SKCore
2727
import Starscream
2828

2929
public class StarscreamRTM: RTMWebSocket, WebSocketDelegate {
30-
31-
public var delegate: RTMDelegate?
30+
public weak var delegate: RTMDelegate?
3231
private var webSocket: WebSocket?
33-
32+
3433
public required init() {}
35-
36-
//MARK: - RTM
34+
35+
// MARK: - RTM
3736
public func connect(url: URL) {
3837
self.webSocket = WebSocket(url: url)
3938
self.webSocket?.delegate = self
4039
self.webSocket?.connect()
4140
}
42-
41+
4342
public func disconnect() {
4443
webSocket?.disconnect()
4544
}
46-
45+
4746
public func sendMessage(_ message: String) throws {
4847
guard webSocket != nil else {
4948
throw SlackError.rtmConnectionError
5049
}
5150
webSocket?.write(string: message)
5251
}
53-
52+
5453
public func ping() {
5554
webSocket?.write(ping: Data())
5655
}
57-
56+
5857
// MARK: - WebSocketDelegate
5958
public func websocketDidConnect(socket: WebSocket) {
6059
delegate?.didConnect()
6160
}
62-
61+
6362
public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
6463
webSocket = nil
6564
delegate?.disconnected()
6665
}
67-
66+
6867
public func websocketDidReceiveMessage(socket: WebSocket, text: String) {
6968
delegate?.receivedMessage(text)
7069
}
71-
70+
7271
public func websocketDidReceiveData(socket: WebSocket, data: Data) {}
7372
}
7473
#endif

Sources/SKRTMAPI/Conformers/ZewoRTM.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ import SKCore
2828
import WebSocketClient
2929

3030
public class ZewoRTM: RTMWebSocket {
31-
32-
public var delegate: RTMDelegate?
31+
public weak var delegate: RTMDelegate?
3332
var webSocket: WebSocket?
3433
let queue = DispatchQueue(label: "com.launchsoft.slackkit")
35-
34+
3635
public required init() {}
37-
38-
//MARK: - RTM
36+
37+
// MARK: - RTM
3938
public func connect(url: URL) {
4039
queue.async {
4140
do {
@@ -48,11 +47,11 @@ public class ZewoRTM: RTMWebSocket {
4847
}
4948
}
5049
}
51-
50+
5251
public func disconnect() {
5352
try? webSocket?.close()
5453
}
55-
54+
5655
public func sendMessage(_ message: String) throws {
5756
guard webSocket != nil else {
5857
throw SlackError.rtmConnectionError
@@ -63,17 +62,17 @@ public class ZewoRTM: RTMWebSocket {
6362
throw error
6463
}
6564
}
66-
65+
6766
// MARK: - WebSocket
6867
private func setupSocket(_ webSocket: WebSocket) {
6968
webSocket.onText { (message) in
7069
self.delegate?.receivedMessage(message)
7170
}
72-
webSocket.onClose { (code: CloseCode?, reason: String?) in
71+
webSocket.onClose { _, _ in
7372
self.delegate?.disconnected()
7473
}
75-
webSocket.onPing { (data) in try webSocket.pong() }
76-
webSocket.onPong { (data) in try webSocket.ping() }
74+
webSocket.onPing { _ in try webSocket.pong() }
75+
webSocket.onPong { _ in try webSocket.ping() }
7776
self.webSocket = webSocket
7877
}
7978
}

Sources/SKRTMAPI/SKRTMAPI.swift

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public protocol RTMAdapter: class {
4141
func notificationForEvent(_ event: Event, type: EventType, instance: SKRTMAPI)
4242
}
4343

44-
public protocol RTMDelegate {
44+
public protocol RTMDelegate: class {
4545
func didConnect()
4646
func disconnected()
4747
func receivedMessage(_ message: String)
4848
}
4949

5050
public final class SKRTMAPI: RTMDelegate {
51-
51+
5252
public var rtm: RTMWebSocket
5353
public var adapter: RTMAdapter?
5454
public var token = "xoxp-SLACK_AUTH_TOKEN"
@@ -57,7 +57,7 @@ public final class SKRTMAPI: RTMDelegate {
5757

5858
var ping: Double?
5959
var pong: Double?
60-
60+
6161
public init(withAPIToken token: String, options: RTMOptions = RTMOptions(), rtm: RTMWebSocket? = nil) {
6262
self.token = token
6363
self.options = options
@@ -72,9 +72,14 @@ public final class SKRTMAPI: RTMDelegate {
7272
}
7373
self.rtm.delegate = self
7474
}
75-
75+
7676
public func connect() {
77-
WebAPI.rtmStart(token: token, simpleLatest: options.simpleLatest, noUnreads: options.noUnreads, mpimAware: options.mpimAware, success: {(response) in
77+
WebAPI.rtmStart(
78+
token: token,
79+
simpleLatest: options.simpleLatest,
80+
noUnreads: options.noUnreads,
81+
mpimAware: options.mpimAware,
82+
success: {(response) in
7883
guard let socketURL = response["url"] as? String, let url = URL(string: socketURL) else {
7984
return
8085
}
@@ -84,42 +89,44 @@ public final class SKRTMAPI: RTMDelegate {
8489
print(error)
8590
})
8691
}
87-
92+
8893
public func disconnect() {
8994
rtm.disconnect()
9095
}
91-
92-
public func sendMessage(_ message: String, channelID: String) throws {
96+
97+
public func sendMessage(_ message: String, channelID: String, threadTs: String? = nil) throws {
9398
guard connected else {
9499
throw SlackError.rtmConnectionError
95100
}
96101
do {
97-
let string = try format(message: message, channel: channelID)
102+
let string = try format(message: message, channel: channelID, threadTs: threadTs)
98103
try rtm.sendMessage(string)
99104
} catch let error {
100105
throw error
101106
}
102107
}
103-
104-
private func format(message: String, channel: String) throws -> String {
105-
let json: [String: Any] = [
108+
109+
private func format(message: String, channel: String, threadTs: String?) throws -> String {
110+
let json: [String: Any?] = [
106111
"id": Date().slackTimestamp,
107112
"type": "message",
108113
"channel": channel,
109-
"text": message.slackFormatEscaping
114+
"text": message.slackFormatEscaping,
115+
"thread_ts": threadTs
110116
]
111117
guard
112-
let data = try? JSONSerialization.data(withJSONObject: json, options: []),
118+
let data = try? JSONSerialization.data(withJSONObject: filterNilParameters(json), options: []),
113119
let str = String(data: data, encoding: String.Encoding.utf8)
114120
else {
115121
throw SlackError.clientJSONError
116122
}
117123
return str
118124
}
119-
120-
//MARK: - RTM Ping
125+
126+
// MARK: - RTM Ping
121127
private func pingRTMServer() {
122-
let delay = DispatchTime.now() + Double(UInt64(options.pingInterval * Double(UInt64.nanosecondsPerSecond))) / Double(UInt64.nanosecondsPerSecond)
128+
let pingInterval = Double(UInt64(options.pingInterval * Double(UInt64.nanosecondsPerSecond))) / Double(UInt64.nanosecondsPerSecond)
129+
let delay = DispatchTime.now() + pingInterval
123130
DispatchQueue.main.asyncAfter(deadline: delay) {
124131
guard self.connected && self.isConnectionTimedOut else {
125132
self.disconnect()
@@ -146,7 +153,7 @@ public final class SKRTMAPI: RTMDelegate {
146153
try rtm.sendMessage(string)
147154
}
148155
}
149-
156+
150157
var isConnectionTimedOut: Bool {
151158
if let pong = pong, let ping = ping {
152159
if pong - ping < options.timeout {
@@ -158,30 +165,30 @@ public final class SKRTMAPI: RTMDelegate {
158165
return true
159166
}
160167
}
161-
162-
//MARK: RTMDelegate
168+
169+
// MARK: RTMDelegate
163170
public func didConnect() {
164171
connected = true
165172
pingRTMServer()
166173
}
167-
174+
168175
public func disconnected() {
169176
connected = false
170177
if options.reconnect {
171178
connect()
172179
}
173180
}
174-
181+
175182
public func receivedMessage(_ message: String) {
176183
guard let data = message.data(using: String.Encoding.utf8) else {
177184
return
178185
}
179186

180-
if let json = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)) as? [String: Any] {
187+
if let json = (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String: Any] {
181188
dispatch(json)
182189
}
183190
}
184-
191+
185192
internal func dispatch(_ anEvent: [String: Any]) {
186193
let event = Event(anEvent)
187194
let type = event.type ?? .unknown

0 commit comments

Comments
 (0)