Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c58abcd089 | |||
| eccf73c592 | |||
| bed79c30b5 | |||
| 0d08f2a7c9 | |||
| 54af4e1a67 | |||
| 1fdb849858 | |||
| 44b39326b2 | |||
| 59e7d6c77c | |||
| d333e12077 | |||
| c63761cc2f | |||
| b00bf0b2a3 | |||
| b877fac99b | |||
| f50107f261 | |||
| 0274f5842e | |||
| a9708d001a | |||
| 465a3860ee | |||
| 9bf2961dc7 | |||
| d81f2ab221 | |||
| 337e3b7709 | |||
| f50efd5f7a | |||
| f80dcf4ae9 | |||
| 13b8b36c10 | |||
| 696da2722b | |||
| 461115f6d3 | |||
| 38aaedee2a | |||
| 98a3973ab0 | |||
| 846bc84035 | |||
| 283e3c0614 | |||
| 44568d407c | |||
| b8f20d7397 | |||
| 2770499879 | |||
| 38e64939f1 | |||
| 71f066a6f8 | |||
| 4e87bea2fa | |||
| 3d6516922f | |||
| 1b98af2b11 | |||
| 750604a801 | |||
| 47f5d040a2 | |||
| 3c34557617 | |||
| d60b8bf88a | |||
| f2f95b7a26 | |||
| c4b38dbc14 | |||
| 7fffbc8dba | |||
| ab39fda0f5 | |||
| 11e9f2e9ab | |||
| a93b4b7966 | |||
| b639fff78e | |||
| 6075577ec5 | |||
| 7a967b8f7d | |||
| 5f53f89693 | |||
| 30d701f6de | |||
| 82e200f4cc | |||
| 6cca5c3956 | |||
| 43f184a8ed | |||
| c5ce209e54 | |||
| fc336cd2ee | |||
| 7138cf3f4f |
@@ -18,6 +18,8 @@ DerivedData
|
||||
*.xcuserstate
|
||||
.build
|
||||
Packages/
|
||||
*.xcodeproj/
|
||||
*.DS_Store
|
||||
|
||||
# CocoaPods
|
||||
#
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
3.0.2
|
||||
@@ -0,0 +1,9 @@
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "echobot",
|
||||
targets: [],
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 0, minor: 0),
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
import Foundation
|
||||
import SlackKit
|
||||
|
||||
class Echobot: MessageEventsDelegate {
|
||||
|
||||
let client: SlackClient
|
||||
|
||||
init(token: String) {
|
||||
client = SlackClient(apiToken: token)
|
||||
client.messageEventsDelegate = self
|
||||
}
|
||||
|
||||
// MARK: MessageEventsDelegate
|
||||
func sent(_ message: Message, client: SlackClient) {}
|
||||
func changed(_ message: Message, client: SlackClient) {}
|
||||
func deleted(_ message: Message?, client: SlackClient) {}
|
||||
func received(_ message: Message, client: SlackClient) {
|
||||
listen(message: message)
|
||||
}
|
||||
|
||||
// MARK: Echobot Internal Logic
|
||||
private func listen(message: Message) {
|
||||
if let channel = message.channel, let text = message.text, let id = client.authenticatedUser?.id {
|
||||
if id != message.user && message.user != nil {
|
||||
client.webAPI.sendMessage(channel:channel, text: text, linkNames: true, success: {(response) in
|
||||
}, failure: { (error) in
|
||||
print("Echobot failed to reply due to error:\(error)")
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let echobot = Echobot(token: "xoxb-SLACK_API_TOKEN")
|
||||
echobot.client.connect()
|
||||
@@ -0,0 +1,8 @@
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "leaderboard",
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 0, minor: 0),
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,141 @@
|
||||
import Foundation
|
||||
import SlackKit
|
||||
|
||||
class Leaderboard: MessageEventsDelegate {
|
||||
|
||||
var leaderboard: [String: Int] = [String: Int]()
|
||||
let atSet = CharacterSet(charactersIn: "@")
|
||||
|
||||
let client: SlackClient
|
||||
|
||||
init(token: String) {
|
||||
client = SlackClient(apiToken: token)
|
||||
client.messageEventsDelegate = self
|
||||
}
|
||||
|
||||
enum Command: String {
|
||||
case leaderboard = "leaderboard"
|
||||
}
|
||||
|
||||
enum Trigger: String {
|
||||
case plusPlus = "++"
|
||||
case minusMinus = "--"
|
||||
}
|
||||
|
||||
// MARK: MessageEventsDelegate
|
||||
func sent(_ message: Message, client: SlackClient) {}
|
||||
func changed(_ message: Message, client: SlackClient) {}
|
||||
func deleted(_ message: Message?, client: SlackClient) {}
|
||||
func received(_ message: Message, client: SlackClient) {
|
||||
listen(message: message)
|
||||
}
|
||||
|
||||
// MARK: Leaderboard Internal Logic
|
||||
private func listen(message: Message) {
|
||||
if let id = client.authenticatedUser?.id, let text = message.text {
|
||||
if text.lowercased().contains(Command.leaderboard.rawValue) && text.contains(id) {
|
||||
handleCommand(command: .leaderboard, channel: message.channel)
|
||||
}
|
||||
}
|
||||
if message.text?.contains(Trigger.plusPlus.rawValue) == true {
|
||||
handleMessageWithTrigger(message: message, trigger: .plusPlus)
|
||||
}
|
||||
if message.text?.contains(Trigger.minusMinus.rawValue) == true {
|
||||
handleMessageWithTrigger(message: message, trigger: .minusMinus)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleMessageWithTrigger(message: Message, trigger: Trigger) {
|
||||
if let text = message.text, let start = text.range(of: "@")?.lowerBound, let end = text.range(of: trigger.rawValue)?.lowerBound {
|
||||
let string = String(text.characters[start...end].dropLast().dropFirst())
|
||||
let users = client.users.values.filter{$0.id == self.userID(string: string)}
|
||||
if users.count > 0 {
|
||||
let idString = userID(string: string)
|
||||
initalizationForValue(dictionary: &leaderboard, value: idString)
|
||||
scoringForValue(dictionary: &leaderboard, value: idString, trigger: trigger)
|
||||
} else {
|
||||
initalizationForValue(dictionary: &leaderboard, value: string)
|
||||
scoringForValue(dictionary: &leaderboard, value: string, trigger: trigger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func handleCommand(command: Command, channel:String?) {
|
||||
switch command {
|
||||
case .leaderboard:
|
||||
if let id = channel {
|
||||
client.webAPI.sendMessage(channel:id, text: "Leaderboard", linkNames: true, attachments: [constructLeaderboardAttachment()], success: {(response) in
|
||||
print(response)
|
||||
}, failure: { (error) in
|
||||
print("Leaderboard failed to post due to error:\(error)")
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func initalizationForValue( dictionary: inout [String: Int], value: String) {
|
||||
if dictionary[value] == nil {
|
||||
dictionary[value] = 0
|
||||
}
|
||||
}
|
||||
|
||||
private func scoringForValue( dictionary: inout [String: Int], value: String, trigger: Trigger) {
|
||||
switch trigger {
|
||||
case .plusPlus:
|
||||
dictionary[value]?+=1
|
||||
case .minusMinus:
|
||||
dictionary[value]?-=1
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Leaderboard Interface
|
||||
private func constructLeaderboardAttachment() -> Attachment? {
|
||||
let 💯 = AttachmentField(title: "💯", value: swapIDsForNames(string: topItems(dictionary: &leaderboard)), short: true)
|
||||
let 💩 = AttachmentField(title: "💩", value: swapIDsForNames(string: bottomItems(dictionary: &leaderboard)), short: true)
|
||||
return Attachment(fallback: "Leaderboard", title: "Leaderboard", colorHex: AttachmentColor.good.rawValue, text: "", fields: [💯, 💩])
|
||||
}
|
||||
|
||||
private func topItems(dictionary: inout [String: Int]) -> String {
|
||||
let sortedKeys = dictionary.keys.sorted(by: { (k1: String, k2: String) -> Bool in
|
||||
return dictionary[k1]! > dictionary[k2]!
|
||||
}).filter({ dictionary[$0]! > 0})
|
||||
let sortedValues = dictionary.values.sorted(by: {$0 > $1}).filter({$0 > 0})
|
||||
return leaderboardString(keys: sortedKeys, values: sortedValues)
|
||||
}
|
||||
|
||||
private func bottomItems( dictionary: inout [String: Int]) -> String {
|
||||
let sortedKeys = dictionary.keys.sorted(by: { (k1: String, k2: String) -> Bool in
|
||||
return dictionary[k1]! < dictionary[k2]!
|
||||
}).filter({ dictionary[$0]! < 0})
|
||||
let sortedValues = dictionary.values.sorted(by: {$0 < $1}).filter({$0 < 0})
|
||||
return leaderboardString(keys: sortedKeys, values: sortedValues)
|
||||
}
|
||||
|
||||
private func leaderboardString(keys: [String], values: [Int]) -> String {
|
||||
var returnValue = ""
|
||||
for i in 0..<values.count {
|
||||
returnValue += keys[i] + " (" + "\(values[i])" + ")\n"
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
// MARK: - Utilities
|
||||
private func swapIDsForNames(string: String) -> String {
|
||||
var returnString = string
|
||||
for key in client.users.keys {
|
||||
if let name = client.users[key]?.name {
|
||||
if returnString.contains(key) {
|
||||
returnString = returnString.replacingOccurrences(of: key, with: "@"+name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnString
|
||||
}
|
||||
|
||||
private func userID(string: String) -> String {
|
||||
return string.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
|
||||
}
|
||||
}
|
||||
|
||||
let leaderboard = Leaderboard(token: "xoxb-SLACK_API_TOKEN")
|
||||
leaderboard.client.connect()
|
||||
@@ -0,0 +1,9 @@
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "robot-or-not-bot",
|
||||
targets: [],
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 0, minor: 0),
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,117 @@
|
||||
import Foundation
|
||||
import SlackKit
|
||||
|
||||
class RobotOrNotBot: MessageEventsDelegate {
|
||||
|
||||
let verdicts: [String:Bool] = [
|
||||
"Mr. Roboto" : false,
|
||||
"Service Kiosks": false,
|
||||
"Darth Vader": false,
|
||||
"K-9": true,
|
||||
"Emotions": false,
|
||||
"Self-Driving Cars": false,
|
||||
"Telepresence Robots": false,
|
||||
"Roomba": true,
|
||||
"Assembly-Line Robot": false,
|
||||
"ASIMO": false,
|
||||
"KITT": false,
|
||||
"USS Enterprise": false,
|
||||
"Transformers": true,
|
||||
"Jaegers": false,
|
||||
"The Major": false,
|
||||
"Siri": false,
|
||||
"The Terminator": true,
|
||||
"Commander Data": false,
|
||||
"Marvin the Paranoid Android": true,
|
||||
"Pinocchio": false,
|
||||
"Droids": true,
|
||||
"Hitchbot": false,
|
||||
"Mars Rovers": false,
|
||||
"Space Probes": false,
|
||||
"Sasquatch": false,
|
||||
"Toaster": false,
|
||||
"Toaster Oven": false,
|
||||
"Cylons": false,
|
||||
"V'ger": true,
|
||||
"Ilia Robot": false,
|
||||
"The TARDIS": false,
|
||||
"Johnny 5": true,
|
||||
"Twiki": true,
|
||||
"Dr. Theopolis": false,
|
||||
"robots.txt": false,
|
||||
"Lobot": false,
|
||||
"Vicki": true,
|
||||
"GlaDOS": false,
|
||||
"Turrets": true,
|
||||
"Wheatley": true,
|
||||
"Herbie the Love Bug": false,
|
||||
"Iron Man": false,
|
||||
"Ultron": false,
|
||||
"The Vision": false,
|
||||
"Clockwork Droids": false,
|
||||
"Podcasts": false,
|
||||
"Cars": false,
|
||||
"Swimming Pool Cleaners": false,
|
||||
"Burritos": false,
|
||||
"Prince Robot IV": false,
|
||||
"Daleks": false,
|
||||
"Cybermen": false,
|
||||
"The Internet of Things": false,
|
||||
"Nanobots": true,
|
||||
"Two Intermeshed Gears": false,
|
||||
"Crow T. Robot": true,
|
||||
"Tom Servo": true,
|
||||
"Thomas and Friends": false,
|
||||
"Replicants": false,
|
||||
"Chatbots": false,
|
||||
"Agents": false,
|
||||
"Lego Simulated Worm Toy": true,
|
||||
"Ghosts": false,
|
||||
"Exos": true,
|
||||
"Rasputin": false,
|
||||
"Tamagotchi": false,
|
||||
"T-1000": true,
|
||||
"The Tin Woodman": false,
|
||||
"Mic N. The Robot": true,
|
||||
"Robot Or Not Bot": false
|
||||
]
|
||||
|
||||
let client: SlackClient
|
||||
|
||||
init(token: String) {
|
||||
client = SlackClient(apiToken: token)
|
||||
client.messageEventsDelegate = self
|
||||
}
|
||||
|
||||
// MARK: MessageEventsDelegate
|
||||
func received(_ message: Message, client: SlackClient) {
|
||||
if let id = client.authenticatedUser?.id {
|
||||
if message.text?.contains(id) == true {
|
||||
handleMessage(message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
func changed(_ message: Message, client: SlackClient) {}
|
||||
func deleted(_ message: Message?, client: SlackClient) {}
|
||||
func sent(_ message: Message, client: SlackClient) {}
|
||||
|
||||
private func handleMessage(message: Message) {
|
||||
if let text = message.text?.lowercased(), let channel = message.channel {
|
||||
for (robot, verdict) in verdicts {
|
||||
let lowerbot = robot.lowercased()
|
||||
if text.contains(lowerbot) {
|
||||
if verdict == true {
|
||||
client.webAPI.addReaction(name: "robot_face", channel: channel, timestamp: message.ts, success: nil, failure: nil)
|
||||
} else {
|
||||
client.webAPI.addReaction(name: "no_entry_sign", channel: channel, timestamp: message.ts, success: nil, failure: nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
client.webAPI.addReaction(name: "question", channel: channel, timestamp: message.ts, success: nil, failure: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let slackbot = RobotOrNotBot(token: "xoxb-SLACK_API_TOKEN")
|
||||
slackbot.client.connect()
|
||||
+4
-4
@@ -27,8 +27,8 @@ let package = Package(
|
||||
name: "SlackKit",
|
||||
targets: [],
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 5),
|
||||
.Package(url: "https://github.com/czechboy0/Jay.git", majorVersion: 0, minor: 5),
|
||||
.Package(url: "https://github.com/Zewo/WebSocket", majorVersion: 0, minor: 5),
|
||||
]
|
||||
.Package(url: "https://github.com/Zewo/WebSocketClient", majorVersion: 0, minor: 14),
|
||||
.Package(url: "https://github.com/Zewo/HTTPClient.git", majorVersion: 0, minor: 14)
|
||||
],
|
||||
exclude: ["Examples"]
|
||||
)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||

|
||||
##iOS/OS X Slack Client Library
|
||||
|
||||
   [](https://github.com/apple/swift-package-manager)
|
||||
##SlackKit: A Swift Slack Client Library
|
||||
###Description
|
||||
This is a Slack client library for iOS and OS X written in Swift. It's intended to expose all of the functionality of Slack's [Real Time Messaging API](https://api.slack.com/rtm) as well as the [web APIs](https://api.slack.com/web) that are accessible by [bot users](https://api.slack.com/bot-users).
|
||||
This is a Slack client library for Linux written in Swift. It's intended to expose all of the functionality of Slack's [Real Time Messaging API](https://api.slack.com/rtm) as well as the [web APIs](https://api.slack.com/web) that are accessible by [bot users](https://api.slack.com/bot-users).
|
||||
|
||||
###Installation
|
||||
|
||||
####Swift Package Manager
|
||||
Add SlackKit to your Package.swift
|
||||
|
||||
@@ -11,35 +14,37 @@ Add SlackKit to your Package.swift
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "MySlackApp",
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 1)
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 0, minor: 0)
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
Run `swift-build` on your application’s main directory.
|
||||
####Development
|
||||
To develop an application that uses SlackKit in Xcode, simply use SwiftPM:
|
||||
```
|
||||
swift package generate-xcodeproj
|
||||
```
|
||||
|
||||
####CocoaPods
|
||||
Add the pod to your podfile:
|
||||
```
|
||||
pod 'SlackKit'
|
||||
```
|
||||
and run
|
||||
```
|
||||
pod install
|
||||
```
|
||||
|
||||
To use the library in your project import it:
|
||||
```
|
||||
import SlackKit
|
||||
```
|
||||
|
||||
###Examples
|
||||
See the [examples folder](https://github.com/pvzig/SlackKit/tree/linux/Examples) for a few examples of how you can use SlackKit.
|
||||
|
||||
###Deployment
|
||||
Deploy your application to Heroku using [this buildpack](https://github.com/kylef/heroku-buildpack-swift). You can also deploy your application anywhere you can deploy a docker container. For more detailed instructions please see [this post](https://medium.com/@pvzig/building-slack-bots-in-swift-b99e243e444c).
|
||||
|
||||
###Usage
|
||||
To use SlackKit you'll need a bearer token which identifies a single user. You can generate a [full access token or create one using OAuth 2](https://api.slack.com/web).
|
||||
|
||||
Once you have a token, initialize a client instance using it:
|
||||
```swift
|
||||
let client = Client(apiToken: "YOUR_SLACK_API_TOKEN")
|
||||
let client = SlackClient(apiToken: "YOUR_SLACK_API_TOKEN")
|
||||
```
|
||||
|
||||
If you want to receive messages from the Slack RTM API, connect to it.
|
||||
@@ -47,11 +52,6 @@ If you want to receive messages from the Slack RTM API, connect to it.
|
||||
client.connect()
|
||||
```
|
||||
|
||||
You can also set options for a ping/pong interval, timeout interval, and automatic reconnection:
|
||||
```swift
|
||||
client.connect(pingInterval: 2, timeout: 10, reconnect: false)
|
||||
```
|
||||
|
||||
Once connected, the client will begin to consume any messages sent by the Slack RTM API.
|
||||
|
||||
####Web API Methods
|
||||
@@ -73,6 +73,7 @@ SlackKit currently supports the a subset of the Slack Web APIs that are availabl
|
||||
- files.comments.edit
|
||||
- files.comments.delete
|
||||
- files.delete
|
||||
- files.info
|
||||
- files.upload
|
||||
- groups.close
|
||||
- groups.history
|
||||
@@ -111,11 +112,10 @@ SlackKit currently supports the a subset of the Slack Web APIs that are availabl
|
||||
|
||||
They can be accessed through a Client object’s `webAPI` property:
|
||||
```swift
|
||||
client.webAPI.authenticationTest({
|
||||
(authenticated) -> Void in
|
||||
print(authenticated)
|
||||
}){(error) -> Void in
|
||||
print(error)
|
||||
client.webAPI.authenticationTest({(authenticated) in
|
||||
print(authenticated)
|
||||
}) {(error) in
|
||||
print(error)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -123,110 +123,98 @@ client.webAPI.authenticationTest({
|
||||
|
||||
To receive delegate callbacks for certain events, register an object as the delegate for those events:
|
||||
```swift
|
||||
client.slackEventsDelegate = self
|
||||
client.connectionEventsDelegate = self
|
||||
```
|
||||
|
||||
There are a number of delegates that you can set to receive callbacks for certain events.
|
||||
|
||||
#####SlackEventsDelegate
|
||||
##### ConnectionEventsDelegate
|
||||
```swift
|
||||
func clientConnected()
|
||||
func clientDisconnected()
|
||||
func preferenceChanged(preference: String, value: AnyObject)
|
||||
func userChanged(user: User)
|
||||
func presenceChanged(user: User?, presence: String?)
|
||||
func manualPresenceChanged(user: User?, presence: String?)
|
||||
func botEvent(bot: Bot)
|
||||
connected(_ client: Client)
|
||||
disconnected(_ client: Client)
|
||||
connectionFailed(_ client: Client, error: SlackError)
|
||||
```
|
||||
|
||||
#####MessageEventsDelegate
|
||||
##### MessageEventsDelegate
|
||||
```swift
|
||||
func messageSent(message: Message)
|
||||
func messageReceived(message: Message)
|
||||
func messageChanged(message: Message)
|
||||
func messageDeleted(message: Message?)
|
||||
sent(_ message: Message, client: Client)
|
||||
received(_ message: Message, client: Client)
|
||||
changed(_ message: Message, client: Client)
|
||||
deleted(_ message: Message?, client: Client)
|
||||
```
|
||||
|
||||
#####ChannelEventsDelegate
|
||||
##### ChannelEventsDelegate
|
||||
```swift
|
||||
func userTyping(channel: Channel?, user: User?)
|
||||
func channelMarked(channel: Channel, timestamp: String?)
|
||||
func channelCreated(channel: Channel)
|
||||
func channelDeleted(channel: Channel)
|
||||
func channelRenamed(channel: Channel)
|
||||
func channelArchived(channel: Channel)
|
||||
func channelHistoryChanged(channel: Channel)
|
||||
func channelJoined(channel: Channel)
|
||||
func channelLeft(channel: Channel)
|
||||
userTypingIn(_ channel: Channel, user: User, client: Client)
|
||||
marked(_ channel: Channel, timestamp: String, client: Client)
|
||||
created(_ channel: Channel, client: Client)
|
||||
deleted(_ channel: Channel, client: Client)
|
||||
renamed(_ channel: Channel, client: Client)
|
||||
archived(_ channel: Channel, client: Client)
|
||||
historyChanged(_ channel: Channel, client: Client)
|
||||
joined(_ channel: Channel, client: Client)
|
||||
left(_ channel: Channel, client: Client)
|
||||
```
|
||||
|
||||
#####DoNotDisturbEventsDelegate
|
||||
##### DoNotDisturbEventsDelegate
|
||||
```swift
|
||||
doNotDisturbUpdated(dndStatus: DoNotDisturbStatus)
|
||||
doNotDisturbUserUpdated(dndStatus: DoNotDisturbStatus, user: User?)
|
||||
updated(_ status: DoNotDisturbStatus, client: Client)
|
||||
userUpdated(_ status: DoNotDisturbStatus, user: User, client: Client)
|
||||
```
|
||||
|
||||
#####GroupEventsDelegate
|
||||
##### GroupEventsDelegate
|
||||
```swift
|
||||
func groupOpened(group: Channel)
|
||||
opened(_ group: Channel, client: Client)
|
||||
```
|
||||
|
||||
#####FileEventsDelegate
|
||||
##### FileEventsDelegate
|
||||
```swift
|
||||
func fileProcessed(file: File)
|
||||
func fileMadePrivate(file: File)
|
||||
func fileDeleted(file: File)
|
||||
func fileCommentAdded(file: File, comment: Comment)
|
||||
func fileCommentEdited(file: File, comment: Comment)
|
||||
func fileCommentDeleted(file: File, comment: Comment)
|
||||
processed(_ file: File, client: Client)
|
||||
madePrivate(_ file: File, client: Client)
|
||||
deleted(_ file: File, client: Client)
|
||||
commentAdded(_ file: File, comment: Comment, client: Client)
|
||||
commentEdited(_ file: File, comment: Comment, client: Client)
|
||||
commentDeleted(_ file: File, comment: Comment, client: Client)
|
||||
```
|
||||
|
||||
#####PinEventsDelegate
|
||||
##### PinEventsDelegate
|
||||
```swift
|
||||
func itemPinned(item: Item?, channel: Channel?)
|
||||
func itemUnpinned(item: Item?, channel: Channel?)
|
||||
pinned(_ item: Item, channel: Channel?, client: Client)
|
||||
unpinned(_ item: Item, channel: Channel?, client: Client)
|
||||
```
|
||||
|
||||
#####StarEventsDelegate
|
||||
##### StarEventsDelegate
|
||||
```swift
|
||||
func itemStarred(item: Item, star: Bool)
|
||||
starred(_ item: Item, starred: Bool, _ client: Client)
|
||||
```
|
||||
|
||||
#####ReactionEventsDelegate
|
||||
##### ReactionEventsDelegate
|
||||
```swift
|
||||
func reactionAdded(reaction: String?, item: Item?, itemUser: String?)
|
||||
func reactionRemoved(reaction: String?, item: Item?, itemUser: String?)
|
||||
added(_ reaction: String, item: Item, itemUser: String, client: Client)
|
||||
removed(_ reaction: String, item: Item, itemUser: String, client: Client)
|
||||
```
|
||||
|
||||
#####TeamEventsDelegate
|
||||
##### SlackEventsDelegate
|
||||
```swift
|
||||
func teamJoined(user: User)
|
||||
func teamPlanChanged(plan: String)
|
||||
func teamPreferencesChanged(preference: String, value: AnyObject)
|
||||
func teamNameChanged(name: String)
|
||||
func teamDomainChanged(domain: String)
|
||||
func teamEmailDomainChanged(domain: String)
|
||||
func teamEmojiChanged()
|
||||
preferenceChanged(_ preference: String, value: Any?, client: Client)
|
||||
userChanged(_ user: User, client: Client)
|
||||
presenceChanged(_ user: User, presence: String, client: Client)
|
||||
manualPresenceChanged(_ user: User, presence: String, client: Client)
|
||||
botEvent(_ bot: Bot, client: Client)
|
||||
```
|
||||
|
||||
#####SubteamEventsDelegate
|
||||
##### TeamEventsDelegate
|
||||
```swift
|
||||
func subteamEvent(userGroup: UserGroup)
|
||||
func subteamSelfAdded(subteamID: String)
|
||||
func subteamSelfRemoved(subteamID: String)
|
||||
userJoined(_ user: User, client: Client)
|
||||
planChanged(_ plan: String, client: Client)
|
||||
preferencesChanged(_ preference: String, value: Any?, client: Client)
|
||||
nameChanged(_ name: String, client: Client)
|
||||
domainChanged(_ domain: String, client: Client)
|
||||
emailDomainChanged(_ domain: String, client: Client)
|
||||
emojiChanged(_ client: Client)
|
||||
```
|
||||
|
||||
###Examples
|
||||
####Leaderboard
|
||||
Included in the OSX-Sample is an example application of a bot you might make using SlackKit. It’s a basic leaderboard scoring bot, in the spirit of [PlusPlus](https://plusplus.chat).
|
||||
|
||||
To configure it, enter your bot’s API token in `AppDelegate.swift` for the Leaderboard bot:
|
||||
|
||||
##### SubteamEventsDelegate
|
||||
```swift
|
||||
let learderboard = Leaderboard(token: "SLACK_AUTH_TOKEN")
|
||||
event(_ userGroup: UserGroup, client: Client)
|
||||
selfAdded(_ subteamID: String, client: Client)
|
||||
selfRemoved(_ subteamID: String, client: Client)
|
||||
```
|
||||
##### TeamProfileEventsDelegate
|
||||
```swift
|
||||
changed(_ profile: CustomProfile, client: Client)
|
||||
deleted(_ profile: CustomProfile, client: Client)
|
||||
reordered(_ profile: CustomProfile, client: Client)
|
||||
```
|
||||
|
||||
It adds a point for every `@thing++`, subtracts a point for every `@thing--`, and shows a leaderboard when asked `@botname leaderboard`.
|
||||
|
||||
###Get In Touch
|
||||
[@pvzig](https://twitter.com/pvzig)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:SlackRTMKit.xcodeproj">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
@@ -1,72 +0,0 @@
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "7C8CB4917A87F5E5383E6800524CC50437576509",
|
||||
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {
|
||||
|
||||
},
|
||||
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
|
||||
"7C8CB4917A87F5E5383E6800524CC50437576509" : 0,
|
||||
"15033DFF73D1DAA84A806DC9B6994613D8A2EB4F" : 0,
|
||||
"4B8317071C2E8191C0071D14B19E229FF434BD5B" : 0,
|
||||
"8951CEB15DDB2D711276960E67900B8AA34FF427" : 0,
|
||||
"10A2CB6C4392F0E306FF8D509BE5E88232276EA4" : 0,
|
||||
"269ADF55FAAC04D68303BE30B6BD693E717155DE" : 0,
|
||||
"997F6D72B6636BD16A82A9AFFBB55CCAE1A5596D" : 0,
|
||||
"704B817219E4CBC31AA92368EF54A4E2835A8C3D" : 0
|
||||
},
|
||||
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "06B58BC9-590E-416B-85DA-67AE0673C049",
|
||||
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
|
||||
"7C8CB4917A87F5E5383E6800524CC50437576509" : "SlackKit\/",
|
||||
"15033DFF73D1DAA84A806DC9B6994613D8A2EB4F" : "SlackKit\/Packages\/IP-0.5.0\/",
|
||||
"4B8317071C2E8191C0071D14B19E229FF434BD5B" : "SlackKit\/Packages\/WebSocket-0.5.1\/",
|
||||
"8951CEB15DDB2D711276960E67900B8AA34FF427" : "SlackKit\/Packages\/MediaType-0.5.0\/",
|
||||
"10A2CB6C4392F0E306FF8D509BE5E88232276EA4" : "SlackKit\/Packages\/String-0.5.1\/",
|
||||
"269ADF55FAAC04D68303BE30B6BD693E717155DE" : "SlackKit\/Packages\/CHTTPParser-0.5.0\/",
|
||||
"997F6D72B6636BD16A82A9AFFBB55CCAE1A5596D" : "SlackKit\/Packages\/TCPSSL-0.5.1\/",
|
||||
"704B817219E4CBC31AA92368EF54A4E2835A8C3D" : "SlackKit\/Packages\/POSIX-0.5.0\/"
|
||||
},
|
||||
"DVTSourceControlWorkspaceBlueprintNameKey" : "SlackKit",
|
||||
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
|
||||
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "SlackKit.xcodeproj",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Zewo\/String.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "10A2CB6C4392F0E306FF8D509BE5E88232276EA4"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/VeniceX\/IP.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "15033DFF73D1DAA84A806DC9B6994613D8A2EB4F"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Zewo\/CHTTPParser.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "269ADF55FAAC04D68303BE30B6BD693E717155DE"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Zewo\/WebSocket",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "4B8317071C2E8191C0071D14B19E229FF434BD5B"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Zewo\/POSIX.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "704B817219E4CBC31AA92368EF54A4E2835A8C3D"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/pvzig\/SlackRTMKit.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "7C8CB4917A87F5E5383E6800524CC50437576509"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Zewo\/MediaType.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "8951CEB15DDB2D711276960E67900B8AA34FF427"
|
||||
},
|
||||
{
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/VeniceX\/TCPSSL.git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
|
||||
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "997F6D72B6636BD16A82A9AFFBB55CCAE1A5596D"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,467 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "9999"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_C7"
|
||||
BuildableName = "libC7.dylib"
|
||||
BlueprintName = "C7"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_Jay"
|
||||
BuildableName = "libJay.dylib"
|
||||
BlueprintName = "Jay"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "NO"
|
||||
buildForRunning = "NO"
|
||||
buildForProfiling = "NO"
|
||||
buildForArchiving = "NO"
|
||||
buildForAnalyzing = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_JayExample"
|
||||
BuildableName = "JayExample"
|
||||
BlueprintName = "JayExample"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_String"
|
||||
BuildableName = "libString.dylib"
|
||||
BlueprintName = "String"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_StructuredData"
|
||||
BuildableName = "libStructuredData.dylib"
|
||||
BlueprintName = "StructuredData"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_MediaType"
|
||||
BuildableName = "libMediaType.dylib"
|
||||
BlueprintName = "MediaType"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_CURIParser"
|
||||
BuildableName = "libCURIParser.dylib"
|
||||
BlueprintName = "CURIParser"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_URI"
|
||||
BuildableName = "libURI.dylib"
|
||||
BlueprintName = "URI"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_S4"
|
||||
BuildableName = "libS4.dylib"
|
||||
BlueprintName = "S4"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_HTTP"
|
||||
BuildableName = "libHTTP.dylib"
|
||||
BlueprintName = "HTTP"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_CLibvenice"
|
||||
BuildableName = "libCLibvenice.dylib"
|
||||
BlueprintName = "CLibvenice"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_Venice"
|
||||
BuildableName = "libVenice.dylib"
|
||||
BlueprintName = "Venice"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_POSIX"
|
||||
BuildableName = "libPOSIX.dylib"
|
||||
BlueprintName = "POSIX"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_IP"
|
||||
BuildableName = "libIP.dylib"
|
||||
BlueprintName = "IP"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_TCP"
|
||||
BuildableName = "libTCP.dylib"
|
||||
BlueprintName = "TCP"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_CHTTPParser"
|
||||
BuildableName = "libCHTTPParser.dylib"
|
||||
BlueprintName = "CHTTPParser"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_HTTPParser"
|
||||
BuildableName = "libHTTPParser.dylib"
|
||||
BlueprintName = "HTTPParser"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_HTTPSerializer"
|
||||
BuildableName = "libHTTPSerializer.dylib"
|
||||
BlueprintName = "HTTPSerializer"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_HTTPClient"
|
||||
BuildableName = "libHTTPClient.dylib"
|
||||
BlueprintName = "HTTPClient"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_File"
|
||||
BuildableName = "libFile.dylib"
|
||||
BlueprintName = "File"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_OpenSSL"
|
||||
BuildableName = "libOpenSSL.dylib"
|
||||
BlueprintName = "OpenSSL"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_TCPSSL"
|
||||
BuildableName = "libTCPSSL.dylib"
|
||||
BlueprintName = "TCPSSL"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_HTTPSClient"
|
||||
BuildableName = "libHTTPSClient.dylib"
|
||||
BlueprintName = "HTTPSClient"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_Event"
|
||||
BuildableName = "libEvent.dylib"
|
||||
BlueprintName = "Event"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_Base64"
|
||||
BuildableName = "libBase64.dylib"
|
||||
BlueprintName = "Base64"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_WebSocket"
|
||||
BuildableName = "libWebSocket.dylib"
|
||||
BlueprintName = "WebSocket"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "NO"
|
||||
buildForRunning = "NO"
|
||||
buildForProfiling = "NO"
|
||||
buildForArchiving = "NO"
|
||||
buildForAnalyzing = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_Botly"
|
||||
BuildableName = "Botly"
|
||||
BlueprintName = "Botly"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_SlackKit"
|
||||
BuildableName = "libSlackKit.dylib"
|
||||
BlueprintName = "SlackKit"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
</Testables>
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_C7"
|
||||
BuildableName = "libC7.dylib"
|
||||
BlueprintName = "C7"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_C7"
|
||||
BuildableName = "libC7.dylib"
|
||||
BlueprintName = "C7"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "______Target_C7"
|
||||
BuildableName = "libC7.dylib"
|
||||
BlueprintName = "C7"
|
||||
ReferencedContainer = "container:SlackKit.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>SchemeUserState</key>
|
||||
<dict>
|
||||
<key>SlackKit.xcscheme</key>
|
||||
<dict></dict>
|
||||
</dict>
|
||||
<key>SuppressBuildableAutocreation</key>
|
||||
<dict></dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// Action.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct Action {
|
||||
|
||||
public let name: String?
|
||||
public let text: String?
|
||||
public let type: String?
|
||||
public let value: String?
|
||||
public let style: ActionStyle?
|
||||
public let confirm: Confirm?
|
||||
|
||||
internal init(action:[String: Any]?) {
|
||||
name = action?["name"] as? String
|
||||
text = action?["text"] as? String
|
||||
type = action?["type"] as? String
|
||||
value = action?["value"] as? String
|
||||
style = ActionStyle(rawValue: action?["style"] as? String ?? "")
|
||||
confirm = Confirm(confirm:action?["confirm"] as? [String: Any])
|
||||
}
|
||||
|
||||
public init(name: String, text: String, style: ActionStyle = .defaultStyle, value: String? = nil, confirm: Confirm? = nil) {
|
||||
self.type = "button"
|
||||
self.name = name
|
||||
self.text = text
|
||||
self.value = value
|
||||
self.style = style
|
||||
self.confirm = confirm
|
||||
}
|
||||
|
||||
internal var dictionary: [String: Any] {
|
||||
var dict = [String: Any]()
|
||||
dict["name"] = name
|
||||
dict["text"] = text
|
||||
dict["type"] = type
|
||||
dict["value"] = value
|
||||
dict["style"] = style?.rawValue
|
||||
dict["confirm"] = confirm?.dictionary
|
||||
return dict
|
||||
}
|
||||
|
||||
public struct Confirm {
|
||||
|
||||
public let title: String?
|
||||
public let text: String?
|
||||
public let okText: String?
|
||||
public let dismissText: String?
|
||||
|
||||
internal init(confirm:[String: Any]?) {
|
||||
title = confirm?["title"] as? String
|
||||
text = confirm?["text"] as? String
|
||||
okText = confirm?["ok_text"] as? String
|
||||
dismissText = confirm?["dismiss_text"] as? String
|
||||
}
|
||||
|
||||
public init(text: String, title: String? = nil, okText: String? = nil, dismissText: String? = nil) {
|
||||
self.text = text
|
||||
self.title = title
|
||||
self.okText = okText
|
||||
self.dismissText = dismissText
|
||||
}
|
||||
|
||||
internal var dictionary: [String: Any] {
|
||||
var dict = [String: Any]()
|
||||
dict["title"] = title
|
||||
dict["text"] = text
|
||||
dict["ok_text"] = okText
|
||||
dict["dismiss_text"] = dismissText
|
||||
return dict
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ActionStyle: String {
|
||||
case defaultStyle = "default"
|
||||
case primary = "primary"
|
||||
case danger = "danger"
|
||||
}
|
||||
|
||||
public enum ResponseType: String {
|
||||
case inChannel = "in_channel"
|
||||
case ephemeral = "ephemeral"
|
||||
}
|
||||
@@ -24,6 +24,8 @@
|
||||
public struct Attachment {
|
||||
|
||||
public let fallback: String?
|
||||
public let callbackID: String?
|
||||
public let type: String?
|
||||
public let color: String?
|
||||
public let pretext: String?
|
||||
public let authorName: String?
|
||||
@@ -33,11 +35,17 @@ public struct Attachment {
|
||||
public let titleLink: String?
|
||||
public let text: String?
|
||||
public let fields: [AttachmentField]?
|
||||
public let actions: [Action]?
|
||||
public let imageURL: String?
|
||||
public let thumbURL: String?
|
||||
|
||||
internal init?(attachment: [String: Any]?) {
|
||||
public let footer: String?
|
||||
public let footerIcon: String?
|
||||
public let ts: Int?
|
||||
|
||||
internal init(attachment: [String: Any]?) {
|
||||
fallback = attachment?["fallback"] as? String
|
||||
callbackID = attachment?["callback_id"] as? String
|
||||
type = attachment?["attachment_type"] as? String
|
||||
color = attachment?["color"] as? String
|
||||
pretext = attachment?["pretext"] as? String
|
||||
authorName = attachment?["author_name"] as? String
|
||||
@@ -48,13 +56,17 @@ public struct Attachment {
|
||||
text = attachment?["text"] as? String
|
||||
imageURL = attachment?["image_url"] as? String
|
||||
thumbURL = attachment?["thumb_url"] as? String
|
||||
fields = (attachment?["fields"] as? [[String: Any]])?.objectArrayFromDictionaryArray(intializer: {(field) -> AttachmentField? in
|
||||
return AttachmentField(field: field)
|
||||
})
|
||||
footer = attachment?["footer"] as? String
|
||||
footerIcon = attachment?["footer_icon"] as? String
|
||||
ts = attachment?["ts"] as? Int
|
||||
fields = (attachment?["fields"] as? [[String: Any]])?.map { AttachmentField(field: $0) }
|
||||
actions = (attachment?["actions"] as? [[String: Any]])?.map { Action(action: $0) }
|
||||
}
|
||||
|
||||
public init?(fallback: String, title:String, colorHex: String? = nil, pretext: String? = nil, authorName: String? = nil, authorLink: String? = nil, authorIcon: String? = nil, titleLink: String? = nil, text: String? = nil, fields: [AttachmentField]? = nil, imageURL: String? = nil, thumbURL: String? = nil) {
|
||||
public init(fallback: String, title:String, callbackID: String? = nil, type: String? = nil, colorHex: String? = nil, pretext: String? = nil, authorName: String? = nil, authorLink: String? = nil, authorIcon: String? = nil, titleLink: String? = nil, text: String? = nil, fields: [AttachmentField]? = nil, actions: [Action]? = nil, imageURL: String? = nil, thumbURL: String? = nil, footer: String? = nil, footerIcon:String? = nil, ts:Int? = nil) {
|
||||
self.fallback = fallback
|
||||
self.callbackID = callbackID
|
||||
self.type = type
|
||||
self.color = colorHex
|
||||
self.pretext = pretext
|
||||
self.authorName = authorName
|
||||
@@ -64,13 +76,19 @@ public struct Attachment {
|
||||
self.titleLink = titleLink
|
||||
self.text = text
|
||||
self.fields = fields
|
||||
self.actions = actions
|
||||
self.imageURL = imageURL
|
||||
self.thumbURL = thumbURL
|
||||
self.footer = footer
|
||||
self.footerIcon = footerIcon
|
||||
self.ts = ts
|
||||
}
|
||||
|
||||
internal func dictionary() -> [String: Any] {
|
||||
internal var dictionary: [String: Any] {
|
||||
var attachment = [String: Any]()
|
||||
attachment["fallback"] = fallback
|
||||
attachment["callback_id"] = callbackID
|
||||
attachment["attachment_type"] = type
|
||||
attachment["color"] = color
|
||||
attachment["pretext"] = pretext
|
||||
attachment["authorName"] = authorName
|
||||
@@ -79,48 +97,19 @@ public struct Attachment {
|
||||
attachment["title"] = title
|
||||
attachment["title_link"] = titleLink
|
||||
attachment["text"] = text
|
||||
attachment["fields"] = fieldJSONArray(fields: fields)
|
||||
attachment["fields"] = fields?.map{$0.dictionary}
|
||||
attachment["actions"] = actions?.map{$0.dictionary}
|
||||
attachment["image_url"] = imageURL
|
||||
attachment["thumb_url"] = thumbURL
|
||||
attachment["footer"] = footer
|
||||
attachment["footer_icon"] = footerIcon
|
||||
attachment["ts"] = ts
|
||||
return attachment
|
||||
}
|
||||
|
||||
private func fieldJSONArray(fields: [AttachmentField]?) -> [[String: Any]] {
|
||||
var returnValue = [[String: Any]]()
|
||||
if let f = fields {
|
||||
for field in f {
|
||||
returnValue.append(field.dictionary())
|
||||
}
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct AttachmentField {
|
||||
|
||||
public let title: String?
|
||||
public let value: String?
|
||||
public let short: Bool?
|
||||
|
||||
internal init?(field: [String: Any]?) {
|
||||
title = field?["title"] as? String
|
||||
value = field?["value"] as? String
|
||||
short = field?["short"] as? Bool
|
||||
}
|
||||
|
||||
public init(title:String, value:String, short: Bool? = nil) {
|
||||
self.title = title
|
||||
self.value = value.slackFormatEscaping()
|
||||
self.short = short
|
||||
}
|
||||
|
||||
internal func dictionary() -> [String: Any] {
|
||||
var field = [String: Any]()
|
||||
field["title"] = title
|
||||
field["value"] = value
|
||||
field["short"] = short
|
||||
return field
|
||||
}
|
||||
|
||||
public enum AttachmentColor: String {
|
||||
case good = "good"
|
||||
case warning = "warning"
|
||||
case danger = "danger"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// AttachmentField.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct AttachmentField {
|
||||
|
||||
public let title: String?
|
||||
public let value: String?
|
||||
public let short: Bool?
|
||||
|
||||
internal init(field: [String: Any]?) {
|
||||
title = field?["title"] as? String
|
||||
value = field?["value"] as? String
|
||||
short = field?["short"] as? Bool
|
||||
}
|
||||
|
||||
public init(title:String, value:String, short: Bool? = nil) {
|
||||
self.title = title
|
||||
self.value = value.slackFormatEscaping
|
||||
self.short = short
|
||||
}
|
||||
|
||||
internal var dictionary: [String: Any] {
|
||||
var field = [String: Any]()
|
||||
field["title"] = title
|
||||
field["value"] = value
|
||||
field["short"] = short
|
||||
return field
|
||||
}
|
||||
}
|
||||
@@ -24,13 +24,18 @@
|
||||
public struct Bot {
|
||||
|
||||
public let id: String?
|
||||
internal(set) public var botToken: String?
|
||||
internal(set) public var name: String?
|
||||
internal(set) public var icons: [String: Any]?
|
||||
|
||||
internal init?(bot: [String: Any]?) {
|
||||
internal init(bot: [String: Any]?) {
|
||||
id = bot?["id"] as? String
|
||||
name = bot?["name"] as? String
|
||||
icons = bot?["icons"] as? [String: Any]
|
||||
}
|
||||
|
||||
internal init(botUser: [String: Any]?) {
|
||||
id = botUser?["bot_user_id"] as? String
|
||||
botToken = botUser?["bot_access_token"] as? String
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,10 +38,10 @@ public struct Channel {
|
||||
internal(set) public var topic: Topic?
|
||||
internal(set) public var purpose: Topic?
|
||||
internal(set) public var isMember: Bool?
|
||||
internal(set) public var lastRead: String?
|
||||
public var lastRead: String?
|
||||
internal(set) public var latest: Message?
|
||||
internal(set) public var unread: Int?
|
||||
internal(set) public var unreadCountDisplay: Int?
|
||||
public var unread: Int?
|
||||
public var unreadCountDisplay: Int?
|
||||
internal(set) public var hasPins: Bool?
|
||||
internal(set) public var members: [String]?
|
||||
// Client use
|
||||
@@ -49,7 +49,7 @@ public struct Channel {
|
||||
internal(set) public var usersTyping = [String]()
|
||||
internal(set) public var messages = [String: Message]()
|
||||
|
||||
internal init?(channel: [String: Any]?) {
|
||||
internal init(channel: [String: Any]?) {
|
||||
id = channel?["id"] as? String
|
||||
name = channel?["name"] as? String
|
||||
created = channel?["created"] as? Int
|
||||
@@ -71,14 +71,14 @@ public struct Channel {
|
||||
hasPins = channel?["has_pins"] as? Bool
|
||||
members = channel?["members"] as? [String]
|
||||
|
||||
if (Message(message: channel?["latest"] as? [String: Any])?.ts == nil) {
|
||||
latest = Message(ts: channel?["latest"] as? String)
|
||||
if let latestMesssageDictionary = channel?["latest"] as? [String: Any] {
|
||||
latest = Message(dictionary: latestMesssageDictionary)
|
||||
} else {
|
||||
latest = Message(message: channel?["latest"] as? [String: Any])
|
||||
latest = Message(ts: channel?["latest"] as? String)
|
||||
}
|
||||
}
|
||||
|
||||
internal init?(id:String?) {
|
||||
internal init(id:String?) {
|
||||
self.id = id
|
||||
created = nil
|
||||
creator = nil
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
//
|
||||
// Client+EventDispatching.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
internal extension SlackClient {
|
||||
|
||||
func dispatch(_ anEvent: [String: Any]) {
|
||||
let event = Event(anEvent)
|
||||
let type = event.type ?? .unknown
|
||||
switch type {
|
||||
case .hello:
|
||||
connected = true
|
||||
pingRTMServer()
|
||||
connectionEventsDelegate?.connected(self)
|
||||
case .ok:
|
||||
messageSent(event)
|
||||
case .message:
|
||||
if (event.subtype != nil) {
|
||||
messageDispatcher(event)
|
||||
} else {
|
||||
messageReceived(event)
|
||||
}
|
||||
case .userTyping:
|
||||
userTyping(event)
|
||||
case .channelMarked, .imMarked, .groupMarked:
|
||||
channelMarked(event)
|
||||
case .channelCreated, .imCreated:
|
||||
channelCreated(event)
|
||||
case .channelJoined, .groupJoined:
|
||||
channelJoined(event)
|
||||
case .channelLeft, .groupLeft:
|
||||
channelLeft(event)
|
||||
case .channelDeleted:
|
||||
channelDeleted(event)
|
||||
case .channelRenamed, .groupRename:
|
||||
channelRenamed(event)
|
||||
case .channelArchive, .groupArchive:
|
||||
channelArchived(event, archived: true)
|
||||
case .channelUnarchive, .groupUnarchive:
|
||||
channelArchived(event, archived: false)
|
||||
case .channelHistoryChanged, .imHistoryChanged, .groupHistoryChanged:
|
||||
channelHistoryChanged(event)
|
||||
case .dndUpdated:
|
||||
doNotDisturbUpdated(event)
|
||||
case .dndUpatedUser:
|
||||
doNotDisturbUserUpdated(event)
|
||||
case .imOpen, .groupOpen:
|
||||
open(event, open: true)
|
||||
case .imClose, .groupClose:
|
||||
open(event, open: false)
|
||||
case .fileCreated:
|
||||
processFile(event)
|
||||
case .fileShared:
|
||||
processFile(event)
|
||||
case .fileUnshared:
|
||||
processFile(event)
|
||||
case .filePublic:
|
||||
processFile(event)
|
||||
case .filePrivate:
|
||||
filePrivate(event)
|
||||
case .fileChanged:
|
||||
processFile(event)
|
||||
case .fileDeleted:
|
||||
deleteFile(event)
|
||||
case .fileCommentAdded:
|
||||
fileCommentAdded(event)
|
||||
case .fileCommentEdited:
|
||||
fileCommentEdited(event)
|
||||
case .fileCommentDeleted:
|
||||
fileCommentDeleted(event)
|
||||
case .pinAdded:
|
||||
pinAdded(event)
|
||||
case .pinRemoved:
|
||||
pinRemoved(event)
|
||||
case .pong:
|
||||
pong(event)
|
||||
case .presenceChange:
|
||||
presenceChange(event)
|
||||
case .manualPresenceChange:
|
||||
manualPresenceChange(event)
|
||||
case .prefChange:
|
||||
changePreference(event)
|
||||
case .userChange:
|
||||
userChange(event)
|
||||
case .teamJoin:
|
||||
teamJoin(event)
|
||||
case .starAdded:
|
||||
itemStarred(event, star: true)
|
||||
case .starRemoved:
|
||||
itemStarred(event, star: false)
|
||||
case .reactionAdded:
|
||||
addedReaction(event)
|
||||
case .reactionRemoved:
|
||||
removedReaction(event)
|
||||
case .emojiChanged:
|
||||
emojiChanged(event)
|
||||
case .commandsChanged:
|
||||
// This functionality is only used by our web client.
|
||||
// The other APIs required to support slash command metadata are currently unstable.
|
||||
// Until they are released other clients should ignore this event.
|
||||
break
|
||||
case .teamPlanChange:
|
||||
teamPlanChange(event)
|
||||
case .teamPrefChange:
|
||||
teamPreferenceChange(event)
|
||||
case .teamRename:
|
||||
teamNameChange(event)
|
||||
case .teamDomainChange:
|
||||
teamDomainChange(event)
|
||||
case .emailDomainChange:
|
||||
emailDomainChange(event)
|
||||
case .teamProfileChange:
|
||||
teamProfileChange(event)
|
||||
case .teamProfileDelete:
|
||||
teamProfileDeleted(event)
|
||||
case .teamProfileReorder:
|
||||
teamProfileReordered(event)
|
||||
case .botAdded:
|
||||
bot(event)
|
||||
case .botChanged:
|
||||
bot(event)
|
||||
case .accountsChanged:
|
||||
// The accounts_changed event is used by our web client to maintain a list of logged-in accounts.
|
||||
// Other clients should ignore this event.
|
||||
break
|
||||
case .teamMigrationStarted:
|
||||
connect(pingInterval: pingInterval, timeout: timeout, reconnect: reconnect)
|
||||
case .reconnectURL:
|
||||
// The reconnect_url event is currently unsupported and experimental.
|
||||
break
|
||||
case .subteamCreated, .subteamUpdated:
|
||||
subteam(event)
|
||||
case .subteamSelfAdded:
|
||||
subteamAddedSelf(event)
|
||||
case .subteamSelfRemoved:
|
||||
subteamRemovedSelf(event)
|
||||
case .error:
|
||||
print("Error: \(anEvent)")
|
||||
case .unknown:
|
||||
print("Unsupported event of type: \(anEvent["type"] ?? "No Type Information")")
|
||||
}
|
||||
}
|
||||
|
||||
func messageDispatcher(_ event:Event) {
|
||||
guard let value = event.subtype, let subtype = MessageSubtype(rawValue:value) else {
|
||||
return
|
||||
}
|
||||
switch subtype {
|
||||
case .messageChanged:
|
||||
messageChanged(event)
|
||||
case .messageDeleted:
|
||||
messageDeleted(event)
|
||||
default:
|
||||
messageReceived(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,563 @@
|
||||
//
|
||||
// Client+EventHandling.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
import Foundation
|
||||
import Dispatch
|
||||
|
||||
internal extension SlackClient {
|
||||
|
||||
//MARK: - Pong
|
||||
func pong(_ event: Event) {
|
||||
pong = event.replyTo
|
||||
}
|
||||
|
||||
//MARK: - Messages
|
||||
func messageSent(_ event: Event) {
|
||||
guard let reply = event.replyTo, let message = sentMessages[NSNumber(value: reply).stringValue], let channel = message.channel, let ts = message.ts else {
|
||||
return
|
||||
}
|
||||
|
||||
message.ts = event.ts
|
||||
message.text = event.text
|
||||
channels[channel]?.messages[ts] = message
|
||||
messageEventsDelegate?.sent(message, client: self)
|
||||
}
|
||||
|
||||
func messageReceived(_ event: Event) {
|
||||
guard let channel = event.channel, let message = event.message, let id = channel.id, let ts = message.ts else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.messages[ts] = message
|
||||
messageEventsDelegate?.received(message, client:self)
|
||||
}
|
||||
|
||||
func messageChanged(_ event: Event) {
|
||||
guard let id = event.channel?.id, let nested = event.nestedMessage, let ts = nested.ts else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.messages[ts] = nested
|
||||
messageEventsDelegate?.changed(nested, client:self)
|
||||
}
|
||||
|
||||
func messageDeleted(_ event: Event) {
|
||||
guard let id = event.channel?.id, let key = event.message?.deletedTs, let message = channels[id]?.messages[key] else {
|
||||
return
|
||||
}
|
||||
|
||||
_ = channels[id]?.messages.removeValue(forKey: key)
|
||||
messageEventsDelegate?.deleted(message, client:self)
|
||||
}
|
||||
|
||||
//MARK: - Channels
|
||||
func userTyping(_ event: Event) {
|
||||
guard let channel = event.channel, let channelID = channel.id, let user = event.user, let userID = user.id ,
|
||||
channels.index(forKey: channelID) != nil && !channels[channelID]!.usersTyping.contains(userID) else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[channelID]?.usersTyping.append(userID)
|
||||
channelEventsDelegate?.userTypingIn(channel, user: user, client: self)
|
||||
|
||||
let timeout = DispatchTime.now() + Double(Int64(5.0 * Double(CLOCKS_PER_SEC))) / Double(CLOCKS_PER_SEC)
|
||||
DispatchQueue.main.asyncAfter(deadline: timeout, execute: {
|
||||
if let index = self.channels[channelID]?.usersTyping.index(of: userID) {
|
||||
self.channels[channelID]?.usersTyping.remove(at: index)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func channelMarked(_ event: Event) {
|
||||
guard let channel = event.channel, let id = channel.id, let timestamp = event.ts else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.lastRead = event.ts
|
||||
channelEventsDelegate?.marked(channel, timestamp: timestamp, client: self)
|
||||
}
|
||||
|
||||
func channelCreated(_ event: Event) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id] = channel
|
||||
channelEventsDelegate?.created(channel, client: self)
|
||||
}
|
||||
|
||||
func channelDeleted(_ event: Event) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
channels.removeValue(forKey: id)
|
||||
channelEventsDelegate?.deleted(channel, client: self)
|
||||
}
|
||||
|
||||
func channelJoined(_ event: Event) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id] = event.channel
|
||||
channelEventsDelegate?.joined(channel, client: self)
|
||||
}
|
||||
|
||||
func channelLeft(_ event: Event) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
if let userID = authenticatedUser?.id, let index = channels[id]?.members?.index(of: userID) {
|
||||
channels[id]?.members?.remove(at: index)
|
||||
}
|
||||
channelEventsDelegate?.left(channel, client: self)
|
||||
}
|
||||
|
||||
func channelRenamed(_ event: Event) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.name = channel.name
|
||||
channelEventsDelegate?.renamed(channel, client: self)
|
||||
}
|
||||
|
||||
func channelArchived(_ event: Event, archived: Bool) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.isArchived = archived
|
||||
channelEventsDelegate?.archived(channel, client: self)
|
||||
}
|
||||
|
||||
func channelHistoryChanged(_ event: Event) {
|
||||
guard let channel = event.channel else {
|
||||
return
|
||||
}
|
||||
channelEventsDelegate?.historyChanged(channel, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Do Not Disturb
|
||||
func doNotDisturbUpdated(_ event: Event) {
|
||||
guard let dndStatus = event.dndStatus else {
|
||||
return
|
||||
}
|
||||
|
||||
authenticatedUser?.doNotDisturbStatus = dndStatus
|
||||
doNotDisturbEventsDelegate?.updated(dndStatus, client: self)
|
||||
}
|
||||
|
||||
func doNotDisturbUserUpdated(_ event: Event) {
|
||||
guard let dndStatus = event.dndStatus, let user = event.user, let id = user.id else {
|
||||
return
|
||||
}
|
||||
|
||||
users[id]?.doNotDisturbStatus = dndStatus
|
||||
doNotDisturbEventsDelegate?.userUpdated(dndStatus, user: user, client: self)
|
||||
}
|
||||
|
||||
//MARK: - IM & Group Open/Close
|
||||
func open(_ event: Event, open: Bool) {
|
||||
guard let channel = event.channel, let id = channel.id else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.isOpen = open
|
||||
groupEventsDelegate?.opened(channel, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Files
|
||||
func processFile(_ event: Event) {
|
||||
guard let file = event.file, let id = file.id else {
|
||||
return
|
||||
}
|
||||
if let comment = file.initialComment, let commentID = comment.id {
|
||||
if files[id]?.comments[commentID] == nil {
|
||||
files[id]?.comments[commentID] = comment
|
||||
}
|
||||
}
|
||||
|
||||
files[id] = file
|
||||
fileEventsDelegate?.processed(file, client: self)
|
||||
}
|
||||
|
||||
func filePrivate(_ event: Event) {
|
||||
guard let file = event.file, let id = file.id else {
|
||||
return
|
||||
}
|
||||
|
||||
files[id]?.isPublic = false
|
||||
fileEventsDelegate?.madePrivate(file, client: self)
|
||||
}
|
||||
|
||||
func deleteFile(_ event: Event) {
|
||||
guard let file = event.file, let id = file.id else {
|
||||
return
|
||||
}
|
||||
|
||||
if files[id] != nil {
|
||||
files.removeValue(forKey: id)
|
||||
}
|
||||
fileEventsDelegate?.deleted(file, client: self)
|
||||
}
|
||||
|
||||
func fileCommentAdded(_ event: Event) {
|
||||
guard let file = event.file, let id = file.id, let comment = event.comment, let commentID = comment.id else {
|
||||
return
|
||||
}
|
||||
|
||||
files[id]?.comments[commentID] = comment
|
||||
fileEventsDelegate?.commentAdded(file, comment: comment, client: self)
|
||||
}
|
||||
|
||||
func fileCommentEdited(_ event: Event) {
|
||||
guard let file = event.file, let id = file.id, let comment = event.comment, let commentID = comment.id else {
|
||||
return
|
||||
}
|
||||
|
||||
files[id]?.comments[commentID]?.comment = comment.comment
|
||||
fileEventsDelegate?.commentAdded(file, comment: comment, client: self)
|
||||
}
|
||||
|
||||
func fileCommentDeleted(_ event: Event) {
|
||||
guard let file = event.file, let id = file.id, let comment = event.comment, let commentID = comment.id else {
|
||||
return
|
||||
}
|
||||
|
||||
_ = files[id]?.comments.removeValue(forKey: commentID)
|
||||
fileEventsDelegate?.commentDeleted(file, comment: comment, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Pins
|
||||
func pinAdded(_ event: Event) {
|
||||
guard let id = event.channelID, let item = event.item else {
|
||||
return
|
||||
}
|
||||
|
||||
channels[id]?.pinnedItems.append(item)
|
||||
pinEventsDelegate?.pinned(item, channel: channels[id], client: self)
|
||||
}
|
||||
|
||||
func pinRemoved(_ event: Event) {
|
||||
guard let id = event.channelID, let item = event.item else {
|
||||
return
|
||||
}
|
||||
|
||||
if let pins = channels[id]?.pinnedItems.filter({$0 != item}) {
|
||||
channels[id]?.pinnedItems = pins
|
||||
}
|
||||
pinEventsDelegate?.unpinned(item, channel: channels[id], client: self)
|
||||
}
|
||||
|
||||
//MARK: - Stars
|
||||
func itemStarred(_ event: Event, star: Bool) {
|
||||
guard let item = event.item, let type = item.type else {
|
||||
return
|
||||
}
|
||||
switch type {
|
||||
case "message":
|
||||
starMessage(item, star: star)
|
||||
case "file":
|
||||
starFile(item, star: star)
|
||||
case "file_comment":
|
||||
starComment(item)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
starEventsDelegate?.starred(item, starred: star, self)
|
||||
}
|
||||
|
||||
func starMessage(_ item: Item, star: Bool) {
|
||||
guard let message = item.message, let ts = message.ts, let channel = item.channel , channels[channel]?.messages[ts] != nil else {
|
||||
return
|
||||
}
|
||||
channels[channel]?.messages[ts]?.isStarred = star
|
||||
}
|
||||
|
||||
func starFile(_ item: Item, star: Bool) {
|
||||
guard let file = item.file, let id = file.id else {
|
||||
return
|
||||
}
|
||||
|
||||
files[id]?.isStarred = star
|
||||
if let stars = files[id]?.stars {
|
||||
if star == true {
|
||||
files[id]?.stars = stars + 1
|
||||
} else {
|
||||
if stars > 0 {
|
||||
files[id]?.stars = stars - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func starComment(_ item: Item) {
|
||||
guard let file = item.file, let id = file.id, let comment = item.comment, let commentID = comment.id else {
|
||||
return
|
||||
}
|
||||
files[id]?.comments[commentID] = comment
|
||||
}
|
||||
|
||||
//MARK: - Reactions
|
||||
func addedReaction(_ event: Event) {
|
||||
guard let item = event.item, let type = item.type, let reaction = event.reaction, let userID = event.user?.id, let itemUser = event.itemUser else {
|
||||
return
|
||||
}
|
||||
|
||||
switch type {
|
||||
case "message":
|
||||
guard let channel = item.channel, let ts = item.ts, let message = channels[channel]?.messages[ts] else {
|
||||
return
|
||||
}
|
||||
message.reactions.append(Reaction(name: reaction, user: userID))
|
||||
case "file":
|
||||
guard let id = item.file?.id else {
|
||||
return
|
||||
}
|
||||
files[id]?.reactions.append(Reaction(name: reaction, user: userID))
|
||||
case "file_comment":
|
||||
guard let id = item.file?.id, let commentID = item.fileCommentID else {
|
||||
return
|
||||
}
|
||||
files[id]?.comments[commentID]?.reactions.append(Reaction(name: reaction, user: userID))
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
reactionEventsDelegate?.added(reaction, item: item, itemUser: itemUser, client: self)
|
||||
}
|
||||
|
||||
func removedReaction(_ event: Event) {
|
||||
guard let item = event.item, let type = item.type, let key = event.reaction, let userID = event.user?.id, let itemUser = event.itemUser else {
|
||||
return
|
||||
}
|
||||
|
||||
switch type {
|
||||
case "message":
|
||||
guard let channel = item.channel, let ts = item.ts, let message = channels[channel]?.messages[ts] else {
|
||||
return
|
||||
}
|
||||
message.reactions = message.reactions.filter({$0.name != key && $0.user != userID})
|
||||
case "file":
|
||||
guard let itemFile = item.file, let id = itemFile.id else {
|
||||
return
|
||||
}
|
||||
files[id]?.reactions = files[id]!.reactions.filter({$0.name != key && $0.user != userID})
|
||||
case "file_comment":
|
||||
guard let id = item.file?.id, let commentID = item.fileCommentID else {
|
||||
return
|
||||
}
|
||||
files[id]?.comments[commentID]?.reactions = files[id]!.comments[commentID]!.reactions.filter({$0.name != key && $0.user != userID})
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
reactionEventsDelegate?.removed(key, item: item, itemUser: itemUser, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Preferences
|
||||
func changePreference(_ event: Event) {
|
||||
guard let name = event.name else {
|
||||
return
|
||||
}
|
||||
|
||||
authenticatedUser?.preferences?[name] = event.value
|
||||
slackEventsDelegate?.preferenceChanged(name, value: event.value, client: self)
|
||||
}
|
||||
|
||||
//Mark: - User Change
|
||||
func userChange(_ event: Event) {
|
||||
guard let user = event.user, let id = user.id else {
|
||||
return
|
||||
}
|
||||
|
||||
let preferences = users[id]?.preferences
|
||||
users[id] = user
|
||||
users[id]?.preferences = preferences
|
||||
slackEventsDelegate?.userChanged(user, client: self)
|
||||
}
|
||||
|
||||
//MARK: - User Presence
|
||||
func presenceChange(_ event: Event) {
|
||||
guard let user = event.user, let id = user.id, let presence = event.presence else {
|
||||
return
|
||||
}
|
||||
|
||||
users[id]?.presence = event.presence
|
||||
slackEventsDelegate?.presenceChanged(user, presence: presence, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Team
|
||||
func teamJoin(_ event: Event) {
|
||||
guard let user = event.user, let id = user.id else {
|
||||
return
|
||||
}
|
||||
|
||||
users[id] = user
|
||||
teamEventsDelegate?.userJoined(user, client: self)
|
||||
}
|
||||
|
||||
func teamPlanChange(_ event: Event) {
|
||||
guard let plan = event.plan else {
|
||||
return
|
||||
}
|
||||
|
||||
team?.plan = plan
|
||||
teamEventsDelegate?.planChanged(plan, client: self)
|
||||
}
|
||||
|
||||
func teamPreferenceChange(_ event: Event) {
|
||||
guard let name = event.name else {
|
||||
return
|
||||
}
|
||||
|
||||
team?.prefs?[name] = event.value
|
||||
teamEventsDelegate?.preferencesChanged(name, value: event.value, client: self)
|
||||
}
|
||||
|
||||
func teamNameChange(_ event: Event) {
|
||||
guard let name = event.name else {
|
||||
return
|
||||
}
|
||||
|
||||
team?.name = name
|
||||
teamEventsDelegate?.nameChanged(name, client: self)
|
||||
}
|
||||
|
||||
func teamDomainChange(_ event: Event) {
|
||||
guard let domain = event.domain else {
|
||||
return
|
||||
}
|
||||
|
||||
team?.domain = domain
|
||||
teamEventsDelegate?.domainChanged(domain, client: self)
|
||||
}
|
||||
|
||||
func emailDomainChange(_ event: Event) {
|
||||
guard let domain = event.emailDomain else {
|
||||
return
|
||||
}
|
||||
|
||||
team?.emailDomain = domain
|
||||
teamEventsDelegate?.emailDomainChanged(domain, client: self)
|
||||
}
|
||||
|
||||
func emojiChanged(_ event: Event) {
|
||||
teamEventsDelegate?.emojiChanged(self)
|
||||
}
|
||||
|
||||
//MARK: - Bots
|
||||
func bot(_ event: Event) {
|
||||
guard let bot = event.bot, let id = bot.id else {
|
||||
return
|
||||
}
|
||||
|
||||
bots[id] = bot
|
||||
slackEventsDelegate?.botEvent(bot, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Subteams
|
||||
func subteam(_ event: Event) {
|
||||
guard let subteam = event.subteam, let id = subteam.id else {
|
||||
return
|
||||
}
|
||||
|
||||
userGroups[id] = subteam
|
||||
subteamEventsDelegate?.event(subteam, client: self)
|
||||
}
|
||||
|
||||
func subteamAddedSelf(_ event: Event) {
|
||||
guard let subteamID = event.subteamID, let _ = authenticatedUser?.userGroups else {
|
||||
return
|
||||
}
|
||||
|
||||
authenticatedUser?.userGroups![subteamID] = subteamID
|
||||
subteamEventsDelegate?.selfAdded(subteamID, client: self)
|
||||
}
|
||||
|
||||
func subteamRemovedSelf(_ event: Event) {
|
||||
guard let subteamID = event.subteamID else {
|
||||
return
|
||||
}
|
||||
|
||||
_ = authenticatedUser?.userGroups?.removeValue(forKey: subteamID)
|
||||
subteamEventsDelegate?.selfRemoved(subteamID, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Team Profiles
|
||||
func teamProfileChange(_ event: Event) {
|
||||
guard let profile = event.profile else {
|
||||
return
|
||||
}
|
||||
|
||||
for user in users {
|
||||
for key in profile.fields.keys {
|
||||
users[user.0]?.profile?.customProfile?.fields[key]?.updateProfileField(profile.fields[key])
|
||||
}
|
||||
}
|
||||
|
||||
teamProfileEventsDelegate?.changed(profile, client: self)
|
||||
}
|
||||
|
||||
func teamProfileDeleted(_ event: Event) {
|
||||
guard let profile = event.profile else {
|
||||
return
|
||||
}
|
||||
|
||||
for user in users {
|
||||
if let id = profile.fields.first?.0 {
|
||||
users[user.0]?.profile?.customProfile?.fields[id] = nil
|
||||
}
|
||||
}
|
||||
|
||||
teamProfileEventsDelegate?.deleted(profile, client: self)
|
||||
}
|
||||
|
||||
func teamProfileReordered(_ event: Event) {
|
||||
guard let profile = event.profile else {
|
||||
return
|
||||
}
|
||||
|
||||
for user in users {
|
||||
for key in profile.fields.keys {
|
||||
users[user.0]?.profile?.customProfile?.fields[key]?.ordering = profile.fields[key]?.ordering
|
||||
}
|
||||
}
|
||||
|
||||
teamProfileEventsDelegate?.reordered(profile, client: self)
|
||||
}
|
||||
|
||||
//MARK: - Authenticated User
|
||||
func manualPresenceChange(_ event: Event) {
|
||||
guard let presence = event.presence, let user = authenticatedUser else {
|
||||
return
|
||||
}
|
||||
|
||||
authenticatedUser?.presence = presence
|
||||
slackEventsDelegate?.manualPresenceChanged(user, presence: presence, client: self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Client+Utilities.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public enum ClientError: Error {
|
||||
case channelDoesNotExist
|
||||
case userDoesNotExist
|
||||
}
|
||||
|
||||
public extension SlackClient {
|
||||
|
||||
//MARK: - User & Channel
|
||||
public func getChannelIDWith(name: String) throws -> String {
|
||||
guard let id = channels.filter({$0.1.name == strip(string:name)}).first?.0 else {
|
||||
throw ClientError.channelDoesNotExist
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
public func getUserIDWith(name: String) throws -> String {
|
||||
guard let id = users.filter({$0.1.name == strip(string:name)}).first?.0 else {
|
||||
throw ClientError.userDoesNotExist
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
public func getImIDForUserWith(id: String, success: @escaping (_ imID: String?)->Void, failure: @escaping (SlackError)->Void) {
|
||||
let ims = channels.filter{$0.1.isIM == true}
|
||||
let channel = ims.filter{$0.1.user == id}.first
|
||||
if let channel = channel {
|
||||
success(channel.0)
|
||||
} else {
|
||||
webAPI.openIM(userID: id, success: success, failure: failure)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Utilities
|
||||
internal func strip(string: String) -> String {
|
||||
var strippedString = string
|
||||
if string[string.startIndex] == "@" || string[string.startIndex] == "#" {
|
||||
strippedString = string.substring(from: string.characters.index(string.startIndex, offsetBy: 1))
|
||||
}
|
||||
return strippedString
|
||||
}
|
||||
}
|
||||
+140
-136
@@ -21,9 +21,9 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
import C7
|
||||
import Jay
|
||||
import WebSocket
|
||||
import Foundation
|
||||
import Venice
|
||||
import WebSocketClient
|
||||
|
||||
public class SlackClient {
|
||||
|
||||
@@ -40,18 +40,19 @@ public class SlackClient {
|
||||
internal(set) public var sentMessages = [String: Message]()
|
||||
|
||||
//MARK: - Delegates
|
||||
public var slackEventsDelegate: SlackEventsDelegate?
|
||||
public var messageEventsDelegate: MessageEventsDelegate?
|
||||
public var doNotDisturbEventsDelegate: DoNotDisturbEventsDelegate?
|
||||
public var channelEventsDelegate: ChannelEventsDelegate?
|
||||
public var groupEventsDelegate: GroupEventsDelegate?
|
||||
public var fileEventsDelegate: FileEventsDelegate?
|
||||
public var pinEventsDelegate: PinEventsDelegate?
|
||||
public var starEventsDelegate: StarEventsDelegate?
|
||||
public var reactionEventsDelegate: ReactionEventsDelegate?
|
||||
public var teamEventsDelegate: TeamEventsDelegate?
|
||||
public var subteamEventsDelegate: SubteamEventsDelegate?
|
||||
public var teamProfileEventsDelegate: TeamProfileEventsDelegate?
|
||||
public weak var connectionEventsDelegate: ConnectionEventsDelegate?
|
||||
public weak var slackEventsDelegate: SlackEventsDelegate?
|
||||
public weak var messageEventsDelegate: MessageEventsDelegate?
|
||||
public weak var doNotDisturbEventsDelegate: DoNotDisturbEventsDelegate?
|
||||
public weak var channelEventsDelegate: ChannelEventsDelegate?
|
||||
public weak var groupEventsDelegate: GroupEventsDelegate?
|
||||
public weak var fileEventsDelegate: FileEventsDelegate?
|
||||
public weak var pinEventsDelegate: PinEventsDelegate?
|
||||
public weak var starEventsDelegate: StarEventsDelegate?
|
||||
public weak var reactionEventsDelegate: ReactionEventsDelegate?
|
||||
public weak var teamEventsDelegate: TeamEventsDelegate?
|
||||
public weak var subteamEventsDelegate: SubteamEventsDelegate?
|
||||
public weak var teamProfileEventsDelegate: TeamProfileEventsDelegate?
|
||||
|
||||
internal var token = "SLACK_AUTH_TOKEN"
|
||||
|
||||
@@ -60,188 +61,143 @@ public class SlackClient {
|
||||
}
|
||||
|
||||
public var webAPI: SlackWebAPI {
|
||||
return SlackWebAPI(slackClient: self)
|
||||
return SlackWebAPI(token: token)
|
||||
}
|
||||
|
||||
internal var webSocket: WebSocket.Client?
|
||||
internal let api = NetworkInterface()
|
||||
private var dispatcher: EventDispatcher?
|
||||
internal var client: WebSocketClient?
|
||||
internal var socket: WebSocket?
|
||||
|
||||
//private let pingPongQueue = dispatch_queue_create("com.launchsoft.SlackKit", DISPATCH_QUEUE_SERIAL)
|
||||
internal var ping: Double?
|
||||
internal var pong: Double?
|
||||
|
||||
internal var pingInterval: Double?
|
||||
internal var timeout: Double?
|
||||
internal var reconnect: Bool?
|
||||
internal var pingInterval: Double = 30
|
||||
internal var timeout: Double = 300
|
||||
internal var reconnect: Bool = false
|
||||
|
||||
required public init(apiToken: String) {
|
||||
self.token = apiToken
|
||||
}
|
||||
|
||||
public func connect(simpleLatest: Bool? = nil, noUnreads: Bool? = nil, mpimAware: Bool? = nil, pingInterval: Double? = nil, timeout: Double? = nil, reconnect: Bool? = nil) {
|
||||
public func connect(simpleLatest: Bool? = nil, noUnreads: Bool? = nil, mpimAware: Bool? = nil, pingInterval: Double = 30, timeout: Double = 300, reconnect: Bool = false) {
|
||||
self.pingInterval = pingInterval
|
||||
self.timeout = timeout
|
||||
self.reconnect = reconnect
|
||||
dispatcher = EventDispatcher(client: self)
|
||||
webAPI.rtmStart(simpleLatest: simpleLatest, noUnreads: noUnreads, mpimAware: mpimAware, success: {
|
||||
(response) -> Void in
|
||||
self.initialSetup(json: response)
|
||||
if let socketURL = response["url"] as? String {
|
||||
webAPI.rtmStart(simpleLatest: simpleLatest, noUnreads: noUnreads, mpimAware: mpimAware, success: { (response) in
|
||||
self.initialSetup(JSON: response)
|
||||
if let socketURL = response["url"] as? String, let url = URL(string: socketURL) {
|
||||
do {
|
||||
let uri = try URI(socketURL)
|
||||
self.webSocket = try WebSocket.Client(uri: uri, onConnect: {(socket) in
|
||||
self.setupSocket(socket: socket)
|
||||
/*if let pingInterval = self.pingInterval {
|
||||
self.pingRTMServerAtInterval(interval: pingInterval)
|
||||
}*/
|
||||
self.client = try WebSocketClient(url: url, didConnect: { (socket) in
|
||||
self.setupSocket(socket)
|
||||
})
|
||||
try self.webSocket?.connect(uri.description)
|
||||
} catch _ {
|
||||
|
||||
try self.client?.connect()
|
||||
} catch let error {
|
||||
print("WebSocket client could not connect: \(error)")
|
||||
}
|
||||
}
|
||||
}, failure:nil)
|
||||
}, failure: {(error) in
|
||||
print("rtm.start failed with error: \(error)")
|
||||
})
|
||||
}
|
||||
|
||||
//TODO: Currently Unsupported
|
||||
/*public func disconnect() {
|
||||
//webSocket?.disconnect()
|
||||
public func disconnect() {
|
||||
_ = try? socket?.close()
|
||||
}
|
||||
|
||||
//MARK: - RTM Message send
|
||||
//MARK: - RTM message send
|
||||
public func sendMessage(message: String, channelID: String) {
|
||||
if (connected) {
|
||||
if connected {
|
||||
if let data = formatMessageToSlackJsonString(message: message, channel: channelID) {
|
||||
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
||||
//webSocket?.writeString(string)
|
||||
do {
|
||||
try socket?.send(data.base64EncodedString())
|
||||
} catch let error {
|
||||
print("Message failed to send: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
private func formatMessageToSlackJsonString(message: String, channel: String) -> Data? {
|
||||
let json: [String: Any] = [
|
||||
"id": Time.slackTimestamp,
|
||||
"id": Date().slackTimestamp,
|
||||
"type": "message",
|
||||
"channel": channel,
|
||||
"text": message.slackFormatEscaping()
|
||||
"text": message.slackFormatEscaping
|
||||
]
|
||||
|
||||
do {
|
||||
let bytes = try Jay().dataFromJson(json)
|
||||
return Data(bytes)
|
||||
return try JSONSerialization.data(withJSONObject: json, options: [])
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func addSentMessage(dictionary: [String: Any]) {
|
||||
private func addSentMessage(_ dictionary: [String: Any]) {
|
||||
var message = dictionary
|
||||
let ts = message["id"] as? Int
|
||||
message.removeValue(forKey:"id")
|
||||
message["ts"] = "\(ts)"
|
||||
guard let id = message["id"] as? NSNumber else {
|
||||
return
|
||||
}
|
||||
let ts = String(describing: id)
|
||||
message.removeValue(forKey: "id")
|
||||
message["ts"] = ts
|
||||
message["user"] = self.authenticatedUser?.id
|
||||
sentMessages["\(ts)"] = Message(message: message)
|
||||
sentMessages[ts] = Message(dictionary: message)
|
||||
}
|
||||
|
||||
//MARK: - RTM Ping
|
||||
/*private func pingRTMServerAtInterval(interval: NSTimeInterval) {
|
||||
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(interval * Double(NSEC_PER_SEC)))
|
||||
dispatch_after(delay, pingPongQueue, {
|
||||
if self.connected && self.timeoutCheck() {
|
||||
self.sendRTMPing()
|
||||
self.pingRTMServerAtInterval(interval: interval)
|
||||
} else {
|
||||
//self.disconnect()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private func sendRTMPing() {
|
||||
if connected {
|
||||
let json: [String: Any] = [
|
||||
"id": NSDate().slackTimestamp(),
|
||||
"type": "ping",
|
||||
]
|
||||
do {
|
||||
let data = try NSJSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
|
||||
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
|
||||
if let writePing = string as? String {
|
||||
ping = json["id"] as? Double
|
||||
//webSocket?.writeString(writePing)
|
||||
}
|
||||
}
|
||||
catch _ {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func timeoutCheck() -> Bool {
|
||||
if let pong = pong, ping = ping, timeout = timeout {
|
||||
if pong - ping < timeout {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
// Ping-pong or timeout not configured
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}*/
|
||||
|
||||
//MARK: - Client setup
|
||||
private func initialSetup(json: [String: Any]) {
|
||||
team = Team(team: json["team"] as? [String: Any])
|
||||
authenticatedUser = User(user: json["self"] as? [String: Any])
|
||||
authenticatedUser?.doNotDisturbStatus = DoNotDisturbStatus(status: json["dnd"] as? [String: Any])
|
||||
enumerateObjects(array: json["users"] as? Array) { (user) in self.addUser(aUser: user) }
|
||||
enumerateObjects(array: json["channels"] as? Array) { (channel) in self.addChannel(aChannel: channel) }
|
||||
enumerateObjects(array: json["groups"] as? Array) { (group) in self.addChannel(aChannel: group) }
|
||||
enumerateObjects(array: json["mpims"] as? Array) { (mpim) in self.addChannel(aChannel: mpim) }
|
||||
enumerateObjects(array: json["ims"] as? Array) { (ims) in self.addChannel(aChannel: ims) }
|
||||
enumerateObjects(array: json["bots"] as? Array) { (bots) in self.addBot(aBot: bots) }
|
||||
enumerateSubteams(subteams: json["subteams"] as? [String: Any])
|
||||
private func initialSetup(JSON: [String: Any]) {
|
||||
team = Team(team: JSON["team"] as? [String: Any])
|
||||
authenticatedUser = User(user: JSON["self"] as? [String: Any])
|
||||
authenticatedUser?.doNotDisturbStatus = DoNotDisturbStatus(status: JSON["dnd"] as? [String: Any])
|
||||
enumerateObjects(JSON["users"] as? Array) { (user) in self.addUser(user) }
|
||||
enumerateObjects(JSON["channels"] as? Array) { (channel) in self.addChannel(channel) }
|
||||
enumerateObjects(JSON["groups"] as? Array) { (group) in self.addChannel(group) }
|
||||
enumerateObjects(JSON["mpims"] as? Array) { (mpim) in self.addChannel(mpim) }
|
||||
enumerateObjects(JSON["ims"] as? Array) { (ims) in self.addChannel(ims) }
|
||||
enumerateObjects(JSON["bots"] as? Array) { (bots) in self.addBot(bots) }
|
||||
enumerateSubteams(JSON["subteams"] as? [String: Any])
|
||||
}
|
||||
|
||||
private func addUser(aUser: [String: Any]) {
|
||||
if let user = User(user: aUser), id = user.id {
|
||||
private func addUser(_ aUser: [String: Any]) {
|
||||
let user = User(user: aUser)
|
||||
if let id = user.id {
|
||||
users[id] = user
|
||||
}
|
||||
}
|
||||
|
||||
private func addChannel(aChannel: [String: Any]) {
|
||||
if let channel = Channel(channel: aChannel), id = channel.id {
|
||||
private func addChannel(_ aChannel: [String: Any]) {
|
||||
let channel = Channel(channel: aChannel)
|
||||
if let id = channel.id {
|
||||
channels[id] = channel
|
||||
}
|
||||
}
|
||||
|
||||
private func addBot(aBot: [String: Any]) {
|
||||
if let bot = Bot(bot: aBot), id = bot.id {
|
||||
private func addBot(_ aBot: [String: Any]) {
|
||||
let bot = Bot(bot: aBot)
|
||||
if let id = bot.id {
|
||||
bots[id] = bot
|
||||
}
|
||||
}
|
||||
|
||||
private func enumerateSubteams(subteams: [String: Any]?) {
|
||||
private func enumerateSubteams(_ subteams: [String: Any]?) {
|
||||
if let subteams = subteams {
|
||||
if let all = subteams["all"] as? [[String: Any]] {
|
||||
for item in all {
|
||||
let u = UserGroup(userGroup: item)
|
||||
self.userGroups[u!.id!] = u
|
||||
if let id = u.id {
|
||||
self.userGroups[id] = u
|
||||
}
|
||||
}
|
||||
}
|
||||
if let auth = subteams["self"] as? [String] {
|
||||
for item in auth {
|
||||
authenticatedUser?.userGroups = [String: String]()
|
||||
authenticatedUser?.userGroups![item] = item
|
||||
authenticatedUser?.userGroups?[item] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Utilities
|
||||
private func enumerateObjects(array: [Any]?, initalizer: ([String: Any])-> Void) {
|
||||
private func enumerateObjects(_ array: [Any]?, initalizer: ([String: Any])-> Void) {
|
||||
if let array = array {
|
||||
for object in array {
|
||||
if let dictionary = object as? [String: Any] {
|
||||
@@ -251,42 +207,90 @@ public class SlackClient {
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - RTM Ping
|
||||
internal func pingRTMServer() {
|
||||
co {
|
||||
self.sendRTMPing()
|
||||
nap(for: self.pingInterval.seconds)
|
||||
guard self.connected && self.isConnectionTimedOut else {
|
||||
self.disconnect()
|
||||
return
|
||||
}
|
||||
self.pingRTMServer()
|
||||
}
|
||||
}
|
||||
|
||||
private func sendRTMPing() {
|
||||
guard connected else {
|
||||
return
|
||||
}
|
||||
let json: [String: Any] = [
|
||||
"id": Date().slackTimestamp,
|
||||
"type": "ping"
|
||||
]
|
||||
guard let data = try? JSONSerialization.data(withJSONObject: json, options: []) else {
|
||||
return
|
||||
}
|
||||
if let string = String(data: data, encoding: String.Encoding.utf8) {
|
||||
ping = json["id"] as? Double
|
||||
do {
|
||||
try socket?.send(string)
|
||||
} catch let error {
|
||||
print("Failed to send ping with error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var isConnectionTimedOut: Bool {
|
||||
if let pong = pong, let ping = ping {
|
||||
if pong - ping < timeout {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - WebSocket
|
||||
private func setupSocket(socket: Socket) {
|
||||
private func setupSocket(_ socket: WebSocket) {
|
||||
socket.onText {(message) in
|
||||
self.websocketDidReceive(message: message)
|
||||
}
|
||||
socket.onPing { (data) in try socket.pong() }
|
||||
socket.onPong { (data) in try socket.ping() }
|
||||
socket.onClose{ (code: CloseCode?, reason: String?) in
|
||||
self.websocketDidDisconnect(closeCode: code, error: reason)
|
||||
}
|
||||
socket.onPing { (data) in try socket.pong() }
|
||||
socket.onPong { (data) in try socket.ping() }
|
||||
self.socket = socket
|
||||
}
|
||||
|
||||
private func websocketDidReceive(message: String) {
|
||||
do {
|
||||
let json = try Jay().jsonFromData(message.data.bytes)
|
||||
guard let message = message.data(using: .utf8) else {
|
||||
print("Failed to decode message")
|
||||
return
|
||||
}
|
||||
let json = try JSONSerialization.jsonObject(with: message, options: [])
|
||||
if let event = json as? [String: Any] {
|
||||
dispatcher?.dispatch(event:event)
|
||||
dispatch(event)
|
||||
}
|
||||
}
|
||||
catch _ {
|
||||
|
||||
catch let error {
|
||||
print("Failed to dispatch message: \(error)")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func websocketDidDisconnect(closeCode: CloseCode?, error: String?) {
|
||||
connected = false
|
||||
authenticated = false
|
||||
webSocket = nil
|
||||
dispatcher = nil
|
||||
client = nil
|
||||
socket = nil
|
||||
authenticatedUser = nil
|
||||
slackEventsDelegate?.clientDisconnected()
|
||||
connectionEventsDelegate?.disconnected(self)
|
||||
if reconnect == true {
|
||||
connect(pingInterval: pingInterval, timeout: timeout, reconnect: reconnect)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,136 +0,0 @@
|
||||
//
|
||||
// ClientExtensions.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
import C7
|
||||
#if os(Linux)
|
||||
import Glibc
|
||||
#else
|
||||
import Darwin.C
|
||||
#endif
|
||||
|
||||
extension SlackClient {
|
||||
|
||||
//MARK: - User & Channel
|
||||
public func getChannelIDByName(name: String) -> String? {
|
||||
return channels.filter{$0.1.name == stripString(string: name)}.first?.0
|
||||
}
|
||||
|
||||
public func getUserIDByName(name: String) -> String? {
|
||||
return users.filter{$0.1.name == stripString(string: name)}.first?.0
|
||||
}
|
||||
|
||||
public func getImIDForUserWithID(id: String, success: (imID: String?)->Void, failure: (error: SlackError)->Void) {
|
||||
let ims = channels.filter{$0.1.isIM == true}
|
||||
let channel = ims.filter{$0.1.user == id}.first
|
||||
if let channel = channel {
|
||||
success(imID: channel.0)
|
||||
} else {
|
||||
webAPI.openIM(userID: id, success: success, failure: failure)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Utilities
|
||||
internal func stripString(string: String) -> String? {
|
||||
var strippedString = string
|
||||
if string[string.startIndex] == "@" || string[string.startIndex] == "#" {
|
||||
strippedString.characters.remove(at: string.startIndex.advanced(by:1))
|
||||
}
|
||||
return strippedString
|
||||
}
|
||||
}
|
||||
|
||||
public enum AttachmentColor: String {
|
||||
case Good = "good"
|
||||
case Warning = "warning"
|
||||
case Danger = "danger"
|
||||
}
|
||||
|
||||
public typealias Time=Double
|
||||
|
||||
public extension Double {
|
||||
|
||||
static func slackTimestamp() -> Double {
|
||||
#if os(Linux)
|
||||
return Double(time(nil))
|
||||
#else
|
||||
var clock: clock_serv_t = clock_serv_t()
|
||||
var timeSpecBuffer: mach_timespec_t = mach_timespec_t(tv_sec: 0, tv_nsec: 0)
|
||||
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clock)
|
||||
clock_get_time(clock, &timeSpecBuffer)
|
||||
mach_port_deallocate(mach_task_self_, clock)
|
||||
|
||||
return Double(timeSpecBuffer.tv_sec) + Double(timeSpecBuffer.tv_nsec) * 0.000000001
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal extension String {
|
||||
|
||||
func slackFormatEscaping() -> String {
|
||||
var escapedString = self
|
||||
escapedString.replace(string: "&", with: "&")
|
||||
escapedString.replace(string: "<", with: "<")
|
||||
escapedString.replace(string: ">", with: ">")
|
||||
return escapedString
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public extension String {
|
||||
|
||||
func contains(query: String, caseSensitive: Bool = false) -> Bool {
|
||||
if query.isEmpty { return true }
|
||||
let (s, q) = caseSensitive ? (self, query) : (self.lowercased(), query.lowercased())
|
||||
var chars = s.characters; let qchars = q.characters
|
||||
|
||||
while !chars.isEmpty {
|
||||
if chars.starts(with: qchars) { return true }
|
||||
chars.removeFirst()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func prefixedBy(query: String, caseSensitive: Bool = false) -> Bool {
|
||||
let (s, q) = caseSensitive ? (self, query) : (self.lowercased(), query.lowercased())
|
||||
return s.characters.starts(with: q.characters)
|
||||
}
|
||||
}
|
||||
|
||||
internal extension Array {
|
||||
|
||||
func objectArrayFromDictionaryArray<T>(intializer:([String: Any])->T?) -> [T] {
|
||||
var returnValue = [T]()
|
||||
for object in self {
|
||||
if let dictionary = object as? [String: Any] {
|
||||
if let value = intializer(dictionary) {
|
||||
returnValue.append(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Comment.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct Comment: Equatable {
|
||||
|
||||
public let id: String?
|
||||
public let user: String?
|
||||
internal(set) public var created: Int?
|
||||
internal(set) public var comment: String?
|
||||
internal(set) public var starred: Bool?
|
||||
internal(set) public var stars: Int?
|
||||
internal(set) public var reactions = [Reaction]()
|
||||
|
||||
internal init(comment:[String: Any]?) {
|
||||
id = comment?["id"] as? String
|
||||
created = comment?["created"] as? Int
|
||||
user = comment?["user"] as? String
|
||||
starred = comment?["is_starred"] as? Bool
|
||||
stars = comment?["num_stars"] as? Int
|
||||
self.comment = comment?["comment"] as? String
|
||||
}
|
||||
|
||||
internal init(id: String?) {
|
||||
self.id = id
|
||||
self.user = nil
|
||||
}
|
||||
|
||||
public static func ==(lhs: Comment, rhs: Comment) -> Bool {
|
||||
return lhs.id == rhs.id
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// CustomProfile.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct CustomProfile {
|
||||
|
||||
internal(set) public var fields = [String: CustomProfileField]()
|
||||
|
||||
internal init(profile: [String: Any]?) {
|
||||
if let eventFields = profile?["fields"] as? [Any] {
|
||||
for field in eventFields {
|
||||
var cpf: CustomProfileField?
|
||||
if let fieldDictionary = field as? [String: Any] {
|
||||
cpf = CustomProfileField(field: fieldDictionary)
|
||||
} else {
|
||||
cpf = CustomProfileField(id: field as? String)
|
||||
}
|
||||
if let id = cpf?.id { fields[id] = cpf }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal init(customFields: [String: Any]?) {
|
||||
if let customFields = customFields {
|
||||
for key in customFields.keys {
|
||||
let cpf = CustomProfileField(field: customFields[key] as? [String: Any])
|
||||
self.fields[key] = cpf
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// CustomProfileField.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct CustomProfileField {
|
||||
|
||||
internal(set) public var id: String?
|
||||
internal(set) public var alt: String?
|
||||
internal(set) public var value: String?
|
||||
internal(set) public var hidden: Bool?
|
||||
internal(set) public var hint: String?
|
||||
internal(set) public var label: String?
|
||||
internal(set) public var options: String?
|
||||
internal(set) public var ordering: Int?
|
||||
internal(set) public var possibleValues: [String]?
|
||||
internal(set) public var type: String?
|
||||
|
||||
internal init(field: [String: Any]?) {
|
||||
id = field?["id"] as? String
|
||||
alt = field?["alt"] as? String
|
||||
value = field?["value"] as? String
|
||||
hidden = field?["is_hidden"] as? Bool
|
||||
hint = field?["hint"] as? String
|
||||
label = field?["label"] as? String
|
||||
options = field?["options"] as? String
|
||||
ordering = field?["ordering"] as? Int
|
||||
possibleValues = field?["possible_values"] as? [String]
|
||||
type = field?["type"] as? String
|
||||
}
|
||||
|
||||
internal init(id: String?) {
|
||||
self.id = id
|
||||
}
|
||||
|
||||
internal mutating func updateProfileField(_ profile: CustomProfileField?) {
|
||||
id = profile?.id != nil ? profile?.id : id
|
||||
alt = profile?.alt != nil ? profile?.alt : alt
|
||||
value = profile?.value != nil ? profile?.value : value
|
||||
hidden = profile?.hidden != nil ? profile?.hidden : hidden
|
||||
hint = profile?.hint != nil ? profile?.hint : hint
|
||||
label = profile?.label != nil ? profile?.label : label
|
||||
options = profile?.options != nil ? profile?.options : options
|
||||
ordering = profile?.ordering != nil ? profile?.ordering : ordering
|
||||
possibleValues = profile?.possibleValues != nil ? profile?.possibleValues : possibleValues
|
||||
type = profile?.type != nil ? profile?.type : type
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// DoNotDisturbStatus.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct DoNotDisturbStatus {
|
||||
|
||||
internal(set) public var enabled: Bool?
|
||||
internal(set) public var nextDoNotDisturbStart: Int?
|
||||
internal(set) public var nextDoNotDisturbEnd: Int?
|
||||
internal(set) public var snoozeEnabled: Bool?
|
||||
internal(set) public var snoozeEndtime: Int?
|
||||
|
||||
internal init(status: [String: Any]?) {
|
||||
enabled = status?["dnd_enabled"] as? Bool
|
||||
nextDoNotDisturbStart = status?["next_dnd_start_ts"] as? Int
|
||||
nextDoNotDisturbEnd = status?["next_dnd_end_ts"] as? Int
|
||||
snoozeEnabled = status?["snooze_enabled"] as? Bool
|
||||
snoozeEndtime = status?["snooze_endtime"] as? Int
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// SlackKit.h
|
||||
// Edited.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
@@ -21,10 +21,13 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
//! Project version number for SlackKit.
|
||||
FOUNDATION_EXPORT double SlackKitVersionNumber;
|
||||
|
||||
//! Project version string for SlackKit.
|
||||
FOUNDATION_EXPORT const unsigned char SlackKitVersionString[];
|
||||
public struct Edited {
|
||||
|
||||
public let user: String?
|
||||
public let ts: String?
|
||||
|
||||
internal init(edited:[String: Any]?) {
|
||||
user = edited?["user"] as? String
|
||||
ts = edited?["ts"] as? String
|
||||
}
|
||||
}
|
||||
+111
-121
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Message.swift
|
||||
// Event.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
@@ -22,107 +22,107 @@
|
||||
// THE SOFTWARE.
|
||||
|
||||
internal enum EventType: String {
|
||||
case Hello = "hello"
|
||||
case Message = "message"
|
||||
case UserTyping = "user_typing"
|
||||
case ChannelMarked = "channel_marked"
|
||||
case ChannelCreated = "channel_created"
|
||||
case ChannelJoined = "channel_joined"
|
||||
case ChannelLeft = "channel_left"
|
||||
case ChannelDeleted = "channel_deleted"
|
||||
case ChannelRenamed = "channel_rename"
|
||||
case ChannelArchive = "channel_archive"
|
||||
case ChannelUnarchive = "channel_unarchive"
|
||||
case ChannelHistoryChanged = "channel_history_changed"
|
||||
case DNDUpdated = "dnd_updated"
|
||||
case DNDUpatedUser = "dnd_updated_user"
|
||||
case IMCreated = "im_created"
|
||||
case IMOpen = "im_open"
|
||||
case IMClose = "im_close"
|
||||
case IMMarked = "im_marked"
|
||||
case IMHistoryChanged = "im_history_changed"
|
||||
case GroupJoined = "group_joined"
|
||||
case GroupLeft = "group_left"
|
||||
case GroupOpen = "group_open"
|
||||
case GroupClose = "group_close"
|
||||
case GroupArchive = "group_archive"
|
||||
case GroupUnarchive = "group_unarchive"
|
||||
case GroupRename = "group_rename"
|
||||
case GroupMarked = "group_marked"
|
||||
case GroupHistoryChanged = "group_history_changed"
|
||||
case FileCreated = "file_created"
|
||||
case FileShared = "file_shared"
|
||||
case FileUnshared = "file_unshared"
|
||||
case FilePublic = "file_public"
|
||||
case FilePrivate = "file_private"
|
||||
case FileChanged = "file_change"
|
||||
case FileDeleted = "file_deleted"
|
||||
case FileCommentAdded = "file_comment_added"
|
||||
case FileCommentEdited = "file_comment_edited"
|
||||
case FileCommentDeleted = "file_comment_deleted"
|
||||
case PinAdded = "pin_added"
|
||||
case PinRemoved = "pin_removed"
|
||||
case Pong = "pong"
|
||||
case PresenceChange = "presence_change"
|
||||
case ManualPresenceChange = "manual_presence_change"
|
||||
case PrefChange = "pref_change"
|
||||
case UserChange = "user_change"
|
||||
case TeamJoin = "team_join"
|
||||
case StarAdded = "star_added"
|
||||
case StarRemoved = "star_removed"
|
||||
case ReactionAdded = "reaction_added"
|
||||
case ReactionRemoved = "reaction_removed"
|
||||
case EmojiChanged = "emoji_changed"
|
||||
case CommandsChanged = "commands_changed"
|
||||
case TeamPlanChange = "team_plan_change"
|
||||
case TeamPrefChange = "team_pref_change"
|
||||
case TeamRename = "team_rename"
|
||||
case TeamDomainChange = "team_domain_change"
|
||||
case EmailDomainChange = "email_domain_change"
|
||||
case TeamProfileChange = "team_profile_change"
|
||||
case TeamProfileDelete = "team_profile_delete"
|
||||
case TeamProfileReorder = "team_profile_reorder"
|
||||
case BotAdded = "bot_added"
|
||||
case BotChanged = "bot_changed"
|
||||
case AccountsChanged = "accounts_changed"
|
||||
case TeamMigrationStarted = "team_migration_started"
|
||||
case ReconnectURL = "reconnect_url"
|
||||
case SubteamCreated = "subteam_created"
|
||||
case SubteamUpdated = "subteam_updated"
|
||||
case SubteamSelfAdded = "subteam_self_added"
|
||||
case SubteamSelfRemoved = "subteam_self_removed"
|
||||
case Ok = "ok"
|
||||
case Error = "error"
|
||||
case hello = "hello"
|
||||
case message = "message"
|
||||
case userTyping = "user_typing"
|
||||
case channelMarked = "channel_marked"
|
||||
case channelCreated = "channel_created"
|
||||
case channelJoined = "channel_joined"
|
||||
case channelLeft = "channel_left"
|
||||
case channelDeleted = "channel_deleted"
|
||||
case channelRenamed = "channel_rename"
|
||||
case channelArchive = "channel_archive"
|
||||
case channelUnarchive = "channel_unarchive"
|
||||
case channelHistoryChanged = "channel_history_changed"
|
||||
case dndUpdated = "dnd_updated"
|
||||
case dndUpatedUser = "dnd_updated_user"
|
||||
case imCreated = "im_created"
|
||||
case imOpen = "im_open"
|
||||
case imClose = "im_close"
|
||||
case imMarked = "im_marked"
|
||||
case imHistoryChanged = "im_history_changed"
|
||||
case groupJoined = "group_joined"
|
||||
case groupLeft = "group_left"
|
||||
case groupOpen = "group_open"
|
||||
case groupClose = "group_close"
|
||||
case groupArchive = "group_archive"
|
||||
case groupUnarchive = "group_unarchive"
|
||||
case groupRename = "group_rename"
|
||||
case groupMarked = "group_marked"
|
||||
case groupHistoryChanged = "group_history_changed"
|
||||
case fileCreated = "file_created"
|
||||
case fileShared = "file_shared"
|
||||
case fileUnshared = "file_unshared"
|
||||
case filePublic = "file_public"
|
||||
case filePrivate = "file_private"
|
||||
case fileChanged = "file_change"
|
||||
case fileDeleted = "file_deleted"
|
||||
case fileCommentAdded = "file_comment_added"
|
||||
case fileCommentEdited = "file_comment_edited"
|
||||
case fileCommentDeleted = "file_comment_deleted"
|
||||
case pinAdded = "pin_added"
|
||||
case pinRemoved = "pin_removed"
|
||||
case pong = "pong"
|
||||
case presenceChange = "presence_change"
|
||||
case manualPresenceChange = "manual_presence_change"
|
||||
case prefChange = "pref_change"
|
||||
case userChange = "user_change"
|
||||
case teamJoin = "team_join"
|
||||
case starAdded = "star_added"
|
||||
case starRemoved = "star_removed"
|
||||
case reactionAdded = "reaction_added"
|
||||
case reactionRemoved = "reaction_removed"
|
||||
case emojiChanged = "emoji_changed"
|
||||
case commandsChanged = "commands_changed"
|
||||
case teamPlanChange = "team_plan_change"
|
||||
case teamPrefChange = "team_pref_change"
|
||||
case teamRename = "team_rename"
|
||||
case teamDomainChange = "team_domain_change"
|
||||
case emailDomainChange = "email_domain_change"
|
||||
case teamProfileChange = "team_profile_change"
|
||||
case teamProfileDelete = "team_profile_delete"
|
||||
case teamProfileReorder = "team_profile_reorder"
|
||||
case botAdded = "bot_added"
|
||||
case botChanged = "bot_changed"
|
||||
case accountsChanged = "accounts_changed"
|
||||
case teamMigrationStarted = "team_migration_started"
|
||||
case reconnectURL = "reconnect_url"
|
||||
case subteamCreated = "subteam_created"
|
||||
case subteamUpdated = "subteam_updated"
|
||||
case subteamSelfAdded = "subteam_self_added"
|
||||
case subteamSelfRemoved = "subteam_self_removed"
|
||||
case ok = "ok"
|
||||
case error = "error"
|
||||
case unknown = "unknown"
|
||||
}
|
||||
|
||||
internal enum MessageSubtype: String {
|
||||
case BotMessage = "bot_message"
|
||||
case MeMessage = "me_message"
|
||||
case MessageChanged = "message_changed"
|
||||
case MessageDeleted = "message_deleted"
|
||||
case ChannelJoin = "channel_join"
|
||||
case ChannelLeave = "channel_leave"
|
||||
case ChannelTopic = "channel_topic"
|
||||
case ChannelPurpose = "channel_purpose"
|
||||
case ChannelName = "channel_name"
|
||||
case ChannelArchive = "channel_archive"
|
||||
case ChannelUnarchive = "channel_unarchive"
|
||||
case GroupJoin = "group_join"
|
||||
case GroupLeave = "group_leave"
|
||||
case GroupTopic = "group_topic"
|
||||
case GroupPurpose = "group_purpose"
|
||||
case GroupName = "group_name"
|
||||
case GroupArchive = "group_archive"
|
||||
case GroupUnarchive = "group_unarchive"
|
||||
case FileShare = "file_share"
|
||||
case FileComment = "file_comment"
|
||||
case FileMention = "file_mention"
|
||||
case PinnedItem = "pinned_item"
|
||||
case UnpinnedItem = "unpinned_item"
|
||||
case botMessage = "bot_message"
|
||||
case meMessage = "me_message"
|
||||
case messageChanged = "message_changed"
|
||||
case messageDeleted = "message_deleted"
|
||||
case channelJoin = "channel_join"
|
||||
case channelLeave = "channel_leave"
|
||||
case channelTopic = "channel_topic"
|
||||
case channelPurpose = "channel_purpose"
|
||||
case channelName = "channel_name"
|
||||
case channelArchive = "channel_archive"
|
||||
case channelUnarchive = "channel_unarchive"
|
||||
case groupJoin = "group_join"
|
||||
case groupLeave = "group_leave"
|
||||
case groupTopic = "group_topic"
|
||||
case groupPurpose = "group_purpose"
|
||||
case groupName = "group_name"
|
||||
case groupArchive = "group_archive"
|
||||
case groupUnarchive = "group_unarchive"
|
||||
case fileShare = "file_share"
|
||||
case fileComment = "file_comment"
|
||||
case fileMention = "file_mention"
|
||||
case pinnedItem = "pinned_item"
|
||||
case unpinnedItem = "unpinned_item"
|
||||
}
|
||||
|
||||
internal struct Event {
|
||||
|
||||
internal class Event {
|
||||
let type: EventType?
|
||||
let ts: String?
|
||||
let subtype: String?
|
||||
@@ -160,12 +160,8 @@ internal struct Event {
|
||||
let subteamID: String?
|
||||
var profile: CustomProfile?
|
||||
|
||||
init(event:[String: Any]) {
|
||||
if let eventType = event["type"] as? String {
|
||||
type = EventType(rawValue:eventType)
|
||||
} else {
|
||||
type = EventType(rawValue: "ok")
|
||||
}
|
||||
init(_ event:[String: Any]) {
|
||||
type = EventType(rawValue: event["type"] as? String ?? "ok")
|
||||
ts = event["ts"] as? String
|
||||
subtype = event["subtype"] as? String
|
||||
channelID = event["channel_id"] as? String
|
||||
@@ -194,34 +190,28 @@ internal struct Event {
|
||||
item = Item(item: event["item"] as? [String: Any])
|
||||
subteam = UserGroup(userGroup: event["subteam"] as? [String: Any])
|
||||
subteamID = event["subteam_id"] as? String
|
||||
message = Message(message: event)
|
||||
nestedMessage = Message(message: event["message"] as? [String: Any])
|
||||
message = Message(dictionary: event)
|
||||
nestedMessage = Message(dictionary: event["message"] as? [String: Any])
|
||||
profile = CustomProfile(profile: event["profile"] as? [String: Any])
|
||||
file = File(id: event["file"] as? String)
|
||||
|
||||
// Comment, Channel, User, and File can come across as Strings or Dictionaries
|
||||
if (Comment(comment: event["comment"] as? [String: Any])?.id == nil) {
|
||||
// Comment, Channel, and User can come across as Strings or Dictionaries
|
||||
if let commentDictionary = event["comment"] as? [String: Any] {
|
||||
comment = Comment(comment: commentDictionary)
|
||||
} else {
|
||||
comment = Comment(id: event["comment"] as? String)
|
||||
} else {
|
||||
comment = Comment(comment: event["comment"] as? [String: Any])
|
||||
}
|
||||
|
||||
if (User(user: event["user"] as? [String: Any])?.id == nil) {
|
||||
if let userDictionary = event["user"] as? [String: Any] {
|
||||
user = User(user: userDictionary)
|
||||
} else {
|
||||
user = User(id: event["user"] as? String)
|
||||
} else {
|
||||
user = User(user: event["user"] as? [String: Any])
|
||||
}
|
||||
|
||||
if (File(file: event["file"] as? [String: Any])?.id == nil) {
|
||||
file = File(id: event["file"] as? String)
|
||||
if let channelDictionary = event["channel"] as? [String: Any] {
|
||||
channel = Channel(channel: channelDictionary)
|
||||
} else {
|
||||
file = File(file: event["file"] as? [String: Any])
|
||||
}
|
||||
|
||||
if (Channel(channel: event["channel"] as? [String: Any])?.id == nil) {
|
||||
channel = Channel(id: event["channel"] as? String)
|
||||
} else {
|
||||
channel = Channel(channel: event["channel"] as? [String: Any])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,85 +21,89 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public protocol SlackEventsDelegate {
|
||||
func clientConnected()
|
||||
func clientDisconnected()
|
||||
func preferenceChanged(preference: String, value: Any)
|
||||
func userChanged(user: User)
|
||||
func presenceChanged(user: User?, presence: String?)
|
||||
func manualPresenceChanged(user: User?, presence: String?)
|
||||
func botEvent(bot: Bot)
|
||||
public protocol ConnectionEventsDelegate: class {
|
||||
func connected(_ client: SlackClient)
|
||||
func disconnected(_ client: SlackClient)
|
||||
func connectionFailed(_ client: SlackClient, error: SlackError)
|
||||
}
|
||||
|
||||
public protocol MessageEventsDelegate {
|
||||
func messageSent(message: Message)
|
||||
func messageReceived(message: Message)
|
||||
func messageChanged(message: Message)
|
||||
func messageDeleted(message: Message?)
|
||||
public protocol MessageEventsDelegate: class {
|
||||
func sent(_ message: Message, client: SlackClient)
|
||||
func received(_ message: Message, client: SlackClient)
|
||||
func changed(_ message: Message, client: SlackClient)
|
||||
func deleted(_ message: Message?, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol ChannelEventsDelegate {
|
||||
func userTyping(channel: Channel?, user: User?)
|
||||
func channelMarked(channel: Channel, timestamp: String?)
|
||||
func channelCreated(channel: Channel)
|
||||
func channelDeleted(channel: Channel)
|
||||
func channelRenamed(channel: Channel)
|
||||
func channelArchived(channel: Channel)
|
||||
func channelHistoryChanged(channel: Channel)
|
||||
func channelJoined(channel: Channel)
|
||||
func channelLeft(channel: Channel)
|
||||
public protocol ChannelEventsDelegate: class {
|
||||
func userTypingIn(_ channel: Channel, user: User, client: SlackClient)
|
||||
func marked(_ channel: Channel, timestamp: String, client: SlackClient)
|
||||
func created(_ channel: Channel, client: SlackClient)
|
||||
func deleted(_ channel: Channel, client: SlackClient)
|
||||
func renamed(_ channel: Channel, client: SlackClient)
|
||||
func archived(_ channel: Channel, client: SlackClient)
|
||||
func historyChanged(_ channel: Channel, client: SlackClient)
|
||||
func joined(_ channel: Channel, client: SlackClient)
|
||||
func left(_ channel: Channel, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol DoNotDisturbEventsDelegate {
|
||||
func doNotDisturbUpdated(dndStatus: DoNotDisturbStatus)
|
||||
func doNotDisturbUserUpdated(dndStatus: DoNotDisturbStatus, user: User?)
|
||||
public protocol DoNotDisturbEventsDelegate: class {
|
||||
func updated(_ status: DoNotDisturbStatus, client: SlackClient)
|
||||
func userUpdated(_ status: DoNotDisturbStatus, user: User, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol GroupEventsDelegate {
|
||||
func groupOpened(group: Channel)
|
||||
public protocol GroupEventsDelegate: class {
|
||||
func opened(_ group: Channel, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol FileEventsDelegate {
|
||||
func fileProcessed(file: File)
|
||||
func fileMadePrivate(file: File)
|
||||
func fileDeleted(file: File)
|
||||
func fileCommentAdded(file: File, comment: Comment)
|
||||
func fileCommentEdited(file: File, comment: Comment)
|
||||
func fileCommentDeleted(file: File, comment: Comment)
|
||||
public protocol FileEventsDelegate: class {
|
||||
func processed(_ file: File, client: SlackClient)
|
||||
func madePrivate(_ file: File, client: SlackClient)
|
||||
func deleted(_ file: File, client: SlackClient)
|
||||
func commentAdded(_ file: File, comment: Comment, client: SlackClient)
|
||||
func commentEdited(_ file: File, comment: Comment, client: SlackClient)
|
||||
func commentDeleted(_ file: File, comment: Comment, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol PinEventsDelegate {
|
||||
func itemPinned(item: Item?, channel: Channel?)
|
||||
func itemUnpinned(item: Item?, channel: Channel?)
|
||||
public protocol PinEventsDelegate: class {
|
||||
func pinned(_ item: Item, channel: Channel?, client: SlackClient)
|
||||
func unpinned(_ item: Item, channel: Channel?, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol StarEventsDelegate {
|
||||
func itemStarred(item: Item, star: Bool)
|
||||
public protocol StarEventsDelegate: class {
|
||||
func starred(_ item: Item, starred: Bool, _ client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol ReactionEventsDelegate {
|
||||
func reactionAdded(reaction: String?, item: Item?, itemUser: String?)
|
||||
func reactionRemoved(reaction: String?, item: Item?, itemUser: String?)
|
||||
public protocol ReactionEventsDelegate: class {
|
||||
func added(_ reaction: String, item: Item, itemUser: String, client: SlackClient)
|
||||
func removed(_ reaction: String, item: Item, itemUser: String, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol TeamEventsDelegate {
|
||||
func teamJoined(user: User)
|
||||
func teamPlanChanged(plan: String)
|
||||
func teamPreferencesChanged(preference: String, value: Any)
|
||||
func teamNameChanged(name: String)
|
||||
func teamDomainChanged(domain: String)
|
||||
func teamEmailDomainChanged(domain: String)
|
||||
func teamEmojiChanged()
|
||||
public protocol SlackEventsDelegate: class {
|
||||
func preferenceChanged(_ preference: String, value: Any?, client: SlackClient)
|
||||
func userChanged(_ user: User, client: SlackClient)
|
||||
func presenceChanged(_ user: User, presence: String, client: SlackClient)
|
||||
func manualPresenceChanged(_ user: User, presence: String, client: SlackClient)
|
||||
func botEvent(_ bot: Bot, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol SubteamEventsDelegate {
|
||||
func subteamEvent(userGroup: UserGroup)
|
||||
func subteamSelfAdded(subteamID: String)
|
||||
func subteamSelfRemoved(subteamID: String)
|
||||
public protocol TeamEventsDelegate: class {
|
||||
func userJoined(_ user: User, client: SlackClient)
|
||||
func planChanged(_ plan: String, client: SlackClient)
|
||||
func preferencesChanged(_ preference: String, value: Any?, client: SlackClient)
|
||||
func nameChanged(_ name: String, client: SlackClient)
|
||||
func domainChanged(_ domain: String, client: SlackClient)
|
||||
func emailDomainChanged(_ domain: String, client: SlackClient)
|
||||
func emojiChanged(_ client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol TeamProfileEventsDelegate {
|
||||
func teamProfileChanged(profile: CustomProfile?)
|
||||
func teamProfileDeleted(profile: CustomProfile?)
|
||||
func teamProfileReordered(profile: CustomProfile?)
|
||||
public protocol SubteamEventsDelegate: class {
|
||||
func event(_ userGroup: UserGroup, client: SlackClient)
|
||||
func selfAdded(_ subteamID: String, client: SlackClient)
|
||||
func selfRemoved(_ subteamID: String, client: SlackClient)
|
||||
}
|
||||
|
||||
public protocol TeamProfileEventsDelegate: class {
|
||||
func changed(_ profile: CustomProfile, client: SlackClient)
|
||||
func deleted(_ profile: CustomProfile, client: SlackClient)
|
||||
func reordered(_ profile: CustomProfile, client: SlackClient)
|
||||
}
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
//
|
||||
// EventDispatcher.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
internal class EventDispatcher {
|
||||
let client: SlackClient
|
||||
let handler: EventHandler
|
||||
|
||||
required init(client: SlackClient) {
|
||||
self.client = client
|
||||
handler = EventHandler(client: client)
|
||||
}
|
||||
|
||||
func dispatch(event: [String: Any]) {
|
||||
let event = Event(event: event)
|
||||
if let type = event.type {
|
||||
switch type {
|
||||
case .Hello:
|
||||
handler.connected()
|
||||
case .Ok:
|
||||
handler.messageSent(event: event)
|
||||
case .Message:
|
||||
if (event.subtype != nil) {
|
||||
messageDispatcher(event: event)
|
||||
} else {
|
||||
handler.messageReceived(event: event)
|
||||
}
|
||||
case .UserTyping:
|
||||
handler.userTyping(event: event)
|
||||
case .ChannelMarked, .IMMarked, .GroupMarked:
|
||||
handler.channelMarked(event: event)
|
||||
case .ChannelCreated, .IMCreated:
|
||||
handler.channelCreated(event: event)
|
||||
case .ChannelJoined, .GroupJoined:
|
||||
handler.channelJoined(event: event)
|
||||
case .ChannelLeft, .GroupLeft:
|
||||
handler.channelLeft(event: event)
|
||||
case .ChannelDeleted:
|
||||
handler.channelDeleted(event: event)
|
||||
case .ChannelRenamed, .GroupRename:
|
||||
handler.channelRenamed(event: event)
|
||||
case .ChannelArchive, .GroupArchive:
|
||||
handler.channelArchived(event: event, archived: true)
|
||||
case .ChannelUnarchive, .GroupUnarchive:
|
||||
handler.channelArchived(event: event, archived: false)
|
||||
case .ChannelHistoryChanged, .IMHistoryChanged, .GroupHistoryChanged:
|
||||
handler.channelHistoryChanged(event: event)
|
||||
case .DNDUpdated:
|
||||
handler.doNotDisturbUpdated(event: event)
|
||||
case .DNDUpatedUser:
|
||||
handler.doNotDisturbUserUpdated(event: event)
|
||||
case .IMOpen, .GroupOpen:
|
||||
handler.open(event: event, open: true)
|
||||
case .IMClose, .GroupClose:
|
||||
handler.open(event: event, open: false)
|
||||
case .FileCreated:
|
||||
handler.processFile(event: event)
|
||||
case .FileShared:
|
||||
handler.processFile(event: event)
|
||||
case .FileUnshared:
|
||||
handler.processFile(event: event)
|
||||
case .FilePublic:
|
||||
handler.processFile(event: event)
|
||||
case .FilePrivate:
|
||||
handler.filePrivate(event: event)
|
||||
case .FileChanged:
|
||||
handler.processFile(event: event)
|
||||
case .FileDeleted:
|
||||
handler.deleteFile(event: event)
|
||||
case .FileCommentAdded:
|
||||
handler.fileCommentAdded(event: event)
|
||||
case .FileCommentEdited:
|
||||
handler.fileCommentEdited(event: event)
|
||||
case .FileCommentDeleted:
|
||||
handler.fileCommentDeleted(event: event)
|
||||
case .PinAdded:
|
||||
handler.pinAdded(event: event)
|
||||
case .PinRemoved:
|
||||
handler.pinRemoved(event: event)
|
||||
case .Pong:
|
||||
handler.pong(event: event)
|
||||
case .PresenceChange:
|
||||
handler.presenceChange(event: event)
|
||||
case .ManualPresenceChange:
|
||||
handler.manualPresenceChange(event: event)
|
||||
case .PrefChange:
|
||||
handler.changePreference(event: event)
|
||||
case .UserChange:
|
||||
handler.userChange(event: event)
|
||||
case .TeamJoin:
|
||||
handler.teamJoin(event: event)
|
||||
case .StarAdded:
|
||||
handler.itemStarred(event: event, star: true)
|
||||
case .StarRemoved:
|
||||
handler.itemStarred(event: event, star: false)
|
||||
case .ReactionAdded:
|
||||
handler.addedReaction(event: event)
|
||||
case .ReactionRemoved:
|
||||
handler.removedReaction(event: event)
|
||||
case .EmojiChanged:
|
||||
handler.emojiChanged(event: event)
|
||||
case .CommandsChanged:
|
||||
// This functionality is only used by our web client.
|
||||
// The other APIs required to support slash command metadata are currently unstable.
|
||||
// Until they are released other clients should ignore this event.
|
||||
break
|
||||
case .TeamPlanChange:
|
||||
handler.teamPlanChange(event: event)
|
||||
case .TeamPrefChange:
|
||||
handler.teamPreferenceChange(event: event)
|
||||
case .TeamRename:
|
||||
handler.teamNameChange(event: event)
|
||||
case .TeamDomainChange:
|
||||
handler.teamDomainChange(event: event)
|
||||
case .EmailDomainChange:
|
||||
handler.emailDomainChange(event: event)
|
||||
case .TeamProfileChange:
|
||||
handler.teamProfileChange(event: event)
|
||||
case .TeamProfileDelete:
|
||||
handler.teamProfileDeleted(event: event)
|
||||
case .TeamProfileReorder:
|
||||
handler.teamProfileReordered(event: event)
|
||||
case .BotAdded:
|
||||
handler.bot(event: event)
|
||||
case .BotChanged:
|
||||
handler.bot(event: event)
|
||||
case .AccountsChanged:
|
||||
// The accounts_changed event is used by our web client to maintain a list of logged-in accounts.
|
||||
// Other clients should ignore this event.
|
||||
break
|
||||
case .TeamMigrationStarted:
|
||||
client.connect(pingInterval: client.pingInterval, timeout: client.timeout, reconnect: client.reconnect)
|
||||
case .ReconnectURL:
|
||||
// The reconnect_url event is currently unsupported and experimental.
|
||||
break
|
||||
case .SubteamCreated, .SubteamUpdated:
|
||||
handler.subteam(event: event)
|
||||
case .SubteamSelfAdded:
|
||||
handler.subteamAddedSelf(event: event)
|
||||
case.SubteamSelfRemoved:
|
||||
handler.subteamRemovedSelf(event: event)
|
||||
case .Error:
|
||||
print("Error: \(event)")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func messageDispatcher(event:Event) {
|
||||
let subtype = MessageSubtype(rawValue: event.subtype!)!
|
||||
switch subtype {
|
||||
case .MessageChanged:
|
||||
handler.messageChanged(event: event)
|
||||
case .MessageDeleted:
|
||||
handler.messageDeleted(event: event)
|
||||
default:
|
||||
handler.messageReceived(event: event)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,647 +0,0 @@
|
||||
//
|
||||
// EventHandler.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
internal class EventHandler {
|
||||
let client: SlackClient
|
||||
required init(client: SlackClient) {
|
||||
self.client = client
|
||||
}
|
||||
|
||||
//MARK: - Initial connection
|
||||
func connected() {
|
||||
client.connected = true
|
||||
|
||||
if let delegate = client.slackEventsDelegate {
|
||||
delegate.clientConnected()
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Pong
|
||||
func pong(event: Event) {
|
||||
client.pong = event.replyTo
|
||||
}
|
||||
|
||||
//MARK: - Messages
|
||||
func messageSent(event: Event) {
|
||||
if let reply = event.replyTo, message = client.sentMessages["\(reply)"], channel = message.channel, ts = message.ts {
|
||||
message.ts = event.ts
|
||||
message.text = event.text
|
||||
client.channels[channel]?.messages[ts] = message
|
||||
|
||||
if let delegate = client.messageEventsDelegate {
|
||||
delegate.messageSent(message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func messageReceived(event: Event) {
|
||||
if let channel = event.channel, message = event.message, id = channel.id, ts = message.ts {
|
||||
client.channels[id]?.messages[ts] = message
|
||||
|
||||
if let delegate = client.messageEventsDelegate {
|
||||
delegate.messageReceived(message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func messageChanged(event: Event) {
|
||||
if let id = event.channel?.id, nested = event.nestedMessage, ts = nested.ts {
|
||||
client.channels[id]?.messages[ts] = nested
|
||||
|
||||
if let delegate = client.messageEventsDelegate {
|
||||
delegate.messageChanged(message: nested)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func messageDeleted(event: Event) {
|
||||
if let id = event.channel?.id, key = event.message?.deletedTs {
|
||||
let message = client.channels[id]?.messages[key]
|
||||
client.channels[id]?.messages.removeValue(forKey:key)
|
||||
|
||||
if let delegate = client.messageEventsDelegate {
|
||||
delegate.messageDeleted(message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Channels
|
||||
func userTyping(event: Event) {
|
||||
if let channelID = event.channel?.id, userID = event.user?.id {
|
||||
if let _ = client.channels[channelID] {
|
||||
if (!client.channels[channelID]!.usersTyping.contains(userID)) {
|
||||
client.channels[channelID]?.usersTyping.append(userID)
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.userTyping(channel: event.channel, user: event.user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(5.0 * Double(NSEC_PER_SEC)))
|
||||
dispatch_after(timeout, dispatch_get_main_queue()) {
|
||||
if let index = self.client.channels[channelID]?.usersTyping.index(of:userID) {
|
||||
self.client.channels[channelID]?.usersTyping.remove(at: index)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
func channelMarked(event: Event) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels[id]?.lastRead = event.ts
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelMarked(channel: channel, timestamp: event.ts)
|
||||
}
|
||||
}
|
||||
//TODO: Recalculate unreads
|
||||
}
|
||||
|
||||
func channelCreated(event: Event) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels[id] = channel
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelCreated(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func channelDeleted(event: Event) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels.removeValue(forKey:id)
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelDeleted(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func channelJoined(event: Event) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels[id] = event.channel
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelJoined(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func channelLeft(event: Event) {
|
||||
if let channel = event.channel, id = channel.id, userID = client.authenticatedUser?.id {
|
||||
if let index = client.channels[id]?.members?.index(of:userID) {
|
||||
client.channels[id]?.members?.remove(at: index)
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelLeft(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func channelRenamed(event: Event) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels[id]?.name = channel.name
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelRenamed(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func channelArchived(event: Event, archived: Bool) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels[id]?.isArchived = archived
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelArchived(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func channelHistoryChanged(event: Event) {
|
||||
if let channel = event.channel {
|
||||
//TODO: Reload chat history if there are any cached messages before latest
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelHistoryChanged(channel: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Do Not Disturb
|
||||
func doNotDisturbUpdated(event: Event) {
|
||||
if let dndStatus = event.dndStatus {
|
||||
client.authenticatedUser?.doNotDisturbStatus = dndStatus
|
||||
|
||||
if let delegate = client.doNotDisturbEventsDelegate {
|
||||
delegate.doNotDisturbUpdated(dndStatus: dndStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func doNotDisturbUserUpdated(event: Event) {
|
||||
if let dndStatus = event.dndStatus, user = event.user, id = user.id {
|
||||
client.users[id]?.doNotDisturbStatus = dndStatus
|
||||
|
||||
if let delegate = client.doNotDisturbEventsDelegate {
|
||||
delegate.doNotDisturbUserUpdated(dndStatus: dndStatus, user: user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - IM & Group Open/Close
|
||||
func open(event: Event, open: Bool) {
|
||||
if let channel = event.channel, id = channel.id {
|
||||
client.channels[id]?.isOpen = open
|
||||
|
||||
if let delegate = client.groupEventsDelegate {
|
||||
delegate.groupOpened(group: channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Files
|
||||
func processFile(event: Event) {
|
||||
if let file = event.file, id = file.id {
|
||||
if let comment = file.initialComment, commentID = comment.id {
|
||||
if client.files[id]?.comments[commentID] == nil {
|
||||
client.files[id]?.comments[commentID] = comment
|
||||
}
|
||||
}
|
||||
|
||||
client.files[id] = file
|
||||
|
||||
if let delegate = client.fileEventsDelegate {
|
||||
delegate.fileProcessed(file: file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func filePrivate(event: Event) {
|
||||
if let file = event.file, id = file.id {
|
||||
client.files[id]?.isPublic = false
|
||||
|
||||
if let delegate = client.fileEventsDelegate {
|
||||
delegate.fileMadePrivate(file: file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func deleteFile(event: Event) {
|
||||
if let file = event.file, id = file.id {
|
||||
if client.files[id] != nil {
|
||||
client.files.removeValue(forKey:id)
|
||||
}
|
||||
|
||||
if let delegate = client.fileEventsDelegate {
|
||||
delegate.fileDeleted(file: file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fileCommentAdded(event: Event) {
|
||||
if let file = event.file, id = file.id, comment = event.comment, commentID = comment.id {
|
||||
client.files[id]?.comments[commentID] = comment
|
||||
|
||||
if let delegate = client.fileEventsDelegate {
|
||||
delegate.fileCommentAdded(file: file, comment: comment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fileCommentEdited(event: Event) {
|
||||
if let file = event.file, id = file.id, comment = event.comment, commentID = comment.id {
|
||||
client.files[id]?.comments[commentID]?.comment = comment.comment
|
||||
|
||||
if let delegate = client.fileEventsDelegate {
|
||||
delegate.fileCommentEdited(file: file, comment: comment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fileCommentDeleted(event: Event) {
|
||||
if let file = event.file, id = file.id, comment = event.comment, commentID = comment.id {
|
||||
client.files[id]?.comments.removeValue(forKey:commentID)
|
||||
|
||||
if let delegate = client.fileEventsDelegate {
|
||||
delegate.fileCommentDeleted(file: file, comment: comment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Pins
|
||||
func pinAdded(event: Event) {
|
||||
if let id = event.channelID, item = event.item {
|
||||
client.channels[id]?.pinnedItems.append(item)
|
||||
|
||||
if let delegate = client.pinEventsDelegate {
|
||||
delegate.itemPinned(item: item, channel: client.channels[id])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func pinRemoved(event: Event) {
|
||||
if let id = event.channelID {
|
||||
if let pins = client.channels[id]?.pinnedItems.filter({$0 != event.item}) {
|
||||
client.channels[id]?.pinnedItems = pins
|
||||
}
|
||||
|
||||
if let delegate = client.pinEventsDelegate {
|
||||
delegate.itemUnpinned(item: event.item, channel: client.channels[id])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Stars
|
||||
func itemStarred(event: Event, star: Bool) {
|
||||
if let item = event.item, type = item.type {
|
||||
switch type {
|
||||
case "message":
|
||||
starMessage(item: item, star: star)
|
||||
case "file":
|
||||
starFile(item: item, star: star)
|
||||
case "file_comment":
|
||||
starComment(item: item)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if let delegate = client.starEventsDelegate {
|
||||
delegate.itemStarred(item: item, star: star)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func starMessage(item: Item, star: Bool) {
|
||||
if let message = item.message, ts = message.ts, channel = item.channel {
|
||||
if let _ = client.channels[channel]?.messages[ts] {
|
||||
client.channels[channel]?.messages[ts]?.isStarred = star
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func starFile(item: Item, star: Bool) {
|
||||
if let file = item.file, id = file.id {
|
||||
client.files[id]?.isStarred = star
|
||||
if let stars = client.files[id]?.stars {
|
||||
if star == true {
|
||||
client.files[id]?.stars = stars + 1
|
||||
} else {
|
||||
if stars > 0 {
|
||||
client.files[id]?.stars = stars - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func starComment(item: Item) {
|
||||
if let file = item.file, id = file.id, comment = item.comment, commentID = comment.id {
|
||||
client.files[id]?.comments[commentID] = comment
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Reactions
|
||||
func addedReaction(event: Event) {
|
||||
if let item = event.item, type = item.type, key = event.reaction, userID = event.user?.id {
|
||||
switch type {
|
||||
case "message":
|
||||
if let channel = item.channel, ts = item.ts {
|
||||
if let message = client.channels[channel]?.messages[ts] {
|
||||
if (message.reactions[key]) == nil {
|
||||
message.reactions[key] = Reaction(name: event.reaction, user: userID)
|
||||
} else {
|
||||
message.reactions[key]?.users[userID] = userID
|
||||
}
|
||||
}
|
||||
}
|
||||
case "file":
|
||||
if let id = item.file?.id, file = client.files[id] {
|
||||
if file.reactions[key] == nil {
|
||||
client.files[id]?.reactions[key] = Reaction(name: event.reaction, user: userID)
|
||||
} else {
|
||||
client.files[id]?.reactions[key]?.users[userID] = userID
|
||||
}
|
||||
}
|
||||
case "file_comment":
|
||||
if let id = item.file?.id, file = client.files[id], commentID = item.fileCommentID {
|
||||
if file.comments[commentID]?.reactions[key] == nil {
|
||||
client.files[id]?.comments[commentID]?.reactions[key] = Reaction(name: event.reaction, user: userID)
|
||||
} else {
|
||||
client.files[id]?.comments[commentID]?.reactions[key]?.users[userID] = userID
|
||||
}
|
||||
}
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if let delegate = client.reactionEventsDelegate {
|
||||
delegate.reactionAdded(reaction: event.reaction, item: event.item, itemUser: event.itemUser)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removedReaction(event: Event) {
|
||||
if let item = event.item, type = item.type, key = event.reaction, userID = event.user?.id {
|
||||
switch type {
|
||||
case "message":
|
||||
if let channel = item.channel, ts = item.ts {
|
||||
if let message = client.channels[channel]?.messages[ts] {
|
||||
if (message.reactions[key]) != nil {
|
||||
message.reactions[key]?.users.removeValue(forKey:userID)
|
||||
}
|
||||
if (message.reactions[key]?.users.count == 0) {
|
||||
message.reactions.removeValue(forKey:key)
|
||||
}
|
||||
}
|
||||
}
|
||||
case "file":
|
||||
if let itemFile = item.file, id = itemFile.id, file = client.files[id] {
|
||||
if file.reactions[key] != nil {
|
||||
client.files[id]?.reactions[key]?.users.removeValue(forKey:userID)
|
||||
}
|
||||
if client.files[id]?.reactions[key]?.users.count == 0 {
|
||||
client.files[id]?.reactions.removeValue(forKey:key)
|
||||
}
|
||||
}
|
||||
case "file_comment":
|
||||
if let id = item.file?.id, file = client.files[id], commentID = item.fileCommentID {
|
||||
if file.comments[commentID]?.reactions[key] != nil {
|
||||
client.files[id]?.comments[commentID]?.reactions[key]?.users.removeValue(forKey:userID)
|
||||
}
|
||||
if client.files[id]?.comments[commentID]?.reactions[key]?.users.count == 0 {
|
||||
client.files[id]?.comments[commentID]?.reactions.removeValue(forKey:key)
|
||||
}
|
||||
}
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if let delegate = client.reactionEventsDelegate {
|
||||
delegate.reactionAdded(reaction: event.reaction, item: event.item, itemUser: event.itemUser)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Preferences
|
||||
func changePreference(event: Event) {
|
||||
if let name = event.name {
|
||||
client.authenticatedUser?.preferences?[name] = event.value
|
||||
|
||||
if let delegate = client.slackEventsDelegate, value = event.value {
|
||||
delegate.preferenceChanged(preference: name, value: value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Mark: - User Change
|
||||
func userChange(event: Event) {
|
||||
if let user = event.user, id = user.id {
|
||||
let preferences = client.users[id]?.preferences
|
||||
client.users[id] = user
|
||||
client.users[id]?.preferences = preferences
|
||||
|
||||
if let delegate = client.slackEventsDelegate {
|
||||
delegate.userChanged(user: user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - User Presence
|
||||
func presenceChange(event: Event) {
|
||||
if let user = event.user, id = user.id {
|
||||
client.users[id]?.presence = event.presence
|
||||
|
||||
if let delegate = client.slackEventsDelegate {
|
||||
delegate.presenceChanged(user: user, presence: event.presence)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Team
|
||||
func teamJoin(event: Event) {
|
||||
if let user = event.user, id = user.id {
|
||||
client.users[id] = user
|
||||
|
||||
if let delegate = client.teamEventsDelegate {
|
||||
delegate.teamJoined(user: user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func teamPlanChange(event: Event) {
|
||||
if let plan = event.plan {
|
||||
client.team?.plan = plan
|
||||
|
||||
if let delegate = client.teamEventsDelegate {
|
||||
delegate.teamPlanChanged(plan: plan)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func teamPreferenceChange(event: Event) {
|
||||
if let name = event.name {
|
||||
client.team?.prefs?[name] = event.value
|
||||
|
||||
if let delegate = client.teamEventsDelegate, value = event.value {
|
||||
delegate.teamPreferencesChanged(preference: name, value: value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func teamNameChange(event: Event) {
|
||||
if let name = event.name {
|
||||
client.team?.name = name
|
||||
|
||||
if let delegate = client.teamEventsDelegate {
|
||||
delegate.teamNameChanged(name: name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func teamDomainChange(event: Event) {
|
||||
if let domain = event.domain {
|
||||
client.team?.domain = domain
|
||||
|
||||
if let delegate = client.teamEventsDelegate {
|
||||
delegate.teamDomainChanged(domain: domain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func emailDomainChange(event: Event) {
|
||||
if let domain = event.emailDomain {
|
||||
client.team?.emailDomain = domain
|
||||
|
||||
if let delegate = client.teamEventsDelegate {
|
||||
delegate.teamEmailDomainChanged(domain: domain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func emojiChanged(event: Event) {
|
||||
//TODO: Call emoji.list here
|
||||
|
||||
if let delegate = client.teamEventsDelegate {
|
||||
delegate.teamEmojiChanged()
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Bots
|
||||
func bot(event: Event) {
|
||||
if let bot = event.bot, id = bot.id {
|
||||
client.bots[id] = bot
|
||||
|
||||
if let delegate = client.slackEventsDelegate {
|
||||
delegate.botEvent(bot: bot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Subteams
|
||||
func subteam(event: Event) {
|
||||
if let subteam = event.subteam, id = subteam.id {
|
||||
client.userGroups[id] = subteam
|
||||
|
||||
if let delegate = client.subteamEventsDelegate {
|
||||
delegate.subteamEvent(userGroup: subteam)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func subteamAddedSelf(event: Event) {
|
||||
if let subteamID = event.subteamID, _ = client.authenticatedUser?.userGroups {
|
||||
client.authenticatedUser?.userGroups![subteamID] = subteamID
|
||||
|
||||
if let delegate = client.subteamEventsDelegate {
|
||||
delegate.subteamSelfAdded(subteamID: subteamID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func subteamRemovedSelf(event: Event) {
|
||||
if let subteamID = event.subteamID {
|
||||
client.authenticatedUser?.userGroups?.removeValue(forKey:subteamID)
|
||||
|
||||
if let delegate = client.subteamEventsDelegate {
|
||||
delegate.subteamSelfRemoved(subteamID: subteamID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Team Profiles
|
||||
func teamProfileChange(event: Event) {
|
||||
for user in client.users {
|
||||
if let fields = event.profile?.fields {
|
||||
for key in fields.keys {
|
||||
client.users[user.0]?.profile?.customProfile?.fields[key]?.updateProfileField(profile: fields[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let delegate = client.teamProfileEventsDelegate {
|
||||
delegate.teamProfileChanged(profile: event.profile)
|
||||
}
|
||||
}
|
||||
|
||||
func teamProfileDeleted(event: Event) {
|
||||
for user in client.users {
|
||||
if let id = event.profile?.fields.first?.0 {
|
||||
client.users[user.0]?.profile?.customProfile?.fields[id] = nil
|
||||
}
|
||||
}
|
||||
|
||||
if let delegate = client.teamProfileEventsDelegate {
|
||||
delegate.teamProfileDeleted(profile: event.profile)
|
||||
}
|
||||
}
|
||||
|
||||
func teamProfileReordered(event: Event) {
|
||||
for user in client.users {
|
||||
if let keys = event.profile?.fields.keys {
|
||||
for key in keys {
|
||||
client.users[user.0]?.profile?.customProfile?.fields[key]?.ordering = event.profile?.fields[key]?.ordering
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let delegate = client.teamProfileEventsDelegate {
|
||||
delegate.teamProfileReordered(profile: event.profile)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Authenticated User
|
||||
func manualPresenceChange(event: Event) {
|
||||
client.authenticatedUser?.presence = event.presence
|
||||
|
||||
if let delegate = client.slackEventsDelegate {
|
||||
delegate.manualPresenceChanged(user: client.authenticatedUser, presence: event.presence)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Extensions.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Date {
|
||||
|
||||
var slackTimestamp: Double {
|
||||
return NSNumber(value: timeIntervalSince1970).doubleValue
|
||||
}
|
||||
}
|
||||
|
||||
internal extension String {
|
||||
|
||||
var slackFormatEscaping: String {
|
||||
var escapedString = replacingOccurrences(of: "&", with: "&")
|
||||
escapedString = replacingOccurrences(of: "<", with: "<")
|
||||
escapedString = replacingOccurrences(of: ">", with: ">")
|
||||
return escapedString
|
||||
}
|
||||
}
|
||||
+58
-16
@@ -21,8 +21,8 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct File {
|
||||
|
||||
public struct File: Equatable {
|
||||
|
||||
public let id: String?
|
||||
public let created: Int?
|
||||
public let name: String?
|
||||
@@ -44,6 +44,22 @@ public struct File {
|
||||
public let thumb360gif: String?
|
||||
public let thumb360w: String?
|
||||
public let thumb360h: String?
|
||||
public let thumb480: String?
|
||||
public let thumb480gif: String?
|
||||
public let thumb480w: String?
|
||||
public let thumb480h: String?
|
||||
public let thumb720: String?
|
||||
public let thumb720gif: String?
|
||||
public let thumb720w: String?
|
||||
public let thumb720h: String?
|
||||
public let thumb960: String?
|
||||
public let thumb960gif: String?
|
||||
public let thumb960w: String?
|
||||
public let thumb960h: String?
|
||||
public let thumb1024: String?
|
||||
public let thumb1024gif: String?
|
||||
public let thumb1024w: String?
|
||||
public let thumb1024h: String?
|
||||
public let permalink: String?
|
||||
public let editLink: String?
|
||||
public let preview: String?
|
||||
@@ -60,9 +76,9 @@ public struct File {
|
||||
internal(set) public var isStarred: Bool?
|
||||
internal(set) public var pinnedTo: [String]?
|
||||
internal(set) public var comments = [String: Comment]()
|
||||
internal(set) public var reactions = [String: Reaction]()
|
||||
internal(set) public var reactions = [Reaction]()
|
||||
|
||||
public init?(file:[String: Any]?) {
|
||||
public init(file:[String: Any]?) {
|
||||
id = file?["id"] as? String
|
||||
created = file?["created"] as? Int
|
||||
name = file?["name"] as? String
|
||||
@@ -84,6 +100,22 @@ public struct File {
|
||||
thumb360gif = file?["thumb_360_gif"] as? String
|
||||
thumb360w = file?["thumb_360_w"] as? String
|
||||
thumb360h = file?["thumb_360_h"] as? String
|
||||
thumb480 = file?["thumb_480"] as? String
|
||||
thumb480gif = file?["thumb_480_gif"] as? String
|
||||
thumb480w = file?["thumb_480_w"] as? String
|
||||
thumb480h = file?["thumb_480_h"] as? String
|
||||
thumb720 = file?["thumb_720"] as? String
|
||||
thumb720gif = file?["thumb_720_gif"] as? String
|
||||
thumb720w = file?["thumb_720_w"] as? String
|
||||
thumb720h = file?["thumb_720_h"] as? String
|
||||
thumb960 = file?["thumb_960"] as? String
|
||||
thumb960gif = file?["thumb_960_gif"] as? String
|
||||
thumb960w = file?["thumb_960_w"] as? String
|
||||
thumb960h = file?["thumb_960_h"] as? String
|
||||
thumb1024 = file?["thumb_1024"] as? String
|
||||
thumb1024gif = file?["thumb_1024_gif"] as? String
|
||||
thumb1024w = file?["thumb_1024_w"] as? String
|
||||
thumb1024h = file?["thumb_1024_h"] as? String
|
||||
permalink = file?["permalink"] as? String
|
||||
editLink = file?["edit_link"] as? String
|
||||
preview = file?["preview"] as? String
|
||||
@@ -99,13 +131,10 @@ public struct File {
|
||||
stars = file?["num_stars"] as? Int
|
||||
isStarred = file?["is_starred"] as? Bool
|
||||
pinnedTo = file?["pinned_to"] as? [String]
|
||||
if let reactions = file?["reactions"] as? [[String: Any]] {
|
||||
self.reactions = Reaction.reactionsFromArray(array: reactions)
|
||||
}
|
||||
|
||||
reactions = Reaction.reactionsFromArray(file?["reactions"] as? [[String: Any]])
|
||||
}
|
||||
|
||||
internal init?(id:String?) {
|
||||
internal init(id:String?) {
|
||||
self.id = id
|
||||
created = nil
|
||||
name = nil
|
||||
@@ -126,6 +155,22 @@ public struct File {
|
||||
thumb360gif = nil
|
||||
thumb360w = nil
|
||||
thumb360h = nil
|
||||
thumb480 = nil
|
||||
thumb480gif = nil
|
||||
thumb480w = nil
|
||||
thumb480h = nil
|
||||
thumb720 = nil
|
||||
thumb720gif = nil
|
||||
thumb720w = nil
|
||||
thumb720h = nil
|
||||
thumb960 = nil
|
||||
thumb960gif = nil
|
||||
thumb960w = nil
|
||||
thumb960h = nil
|
||||
thumb1024 = nil
|
||||
thumb1024gif = nil
|
||||
thumb1024w = nil
|
||||
thumb1024h = nil
|
||||
permalink = nil
|
||||
editLink = nil
|
||||
preview = nil
|
||||
@@ -134,11 +179,8 @@ public struct File {
|
||||
linesMore = nil
|
||||
initialComment = nil
|
||||
}
|
||||
|
||||
public static func ==(lhs: File, rhs: File) -> Bool {
|
||||
return lhs.id == rhs.id
|
||||
}
|
||||
}
|
||||
|
||||
extension File: Equatable {}
|
||||
|
||||
public func ==(lhs: File, rhs: File) -> Bool {
|
||||
return lhs.id == rhs.id
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// History.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct History {
|
||||
|
||||
internal(set) public var latest: Date?
|
||||
internal(set) public var messages = [Message]()
|
||||
public let hasMore: Bool?
|
||||
|
||||
internal init(history: [String: Any]?) {
|
||||
if let latestStr = history?["latest"] as? String, let latestDouble = Double(latestStr) {
|
||||
latest = Date(timeIntervalSince1970: TimeInterval(latestDouble))
|
||||
}
|
||||
if let msgs = history?["messages"] as? [[String: Any]] {
|
||||
for message in msgs {
|
||||
messages.append(Message(dictionary: message))
|
||||
}
|
||||
}
|
||||
hasMore = history?["has_more"] as? Bool
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Item.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct Item: Equatable {
|
||||
|
||||
public let type: String?
|
||||
public let ts: String?
|
||||
public let channel: String?
|
||||
public let message: Message?
|
||||
public let file: File?
|
||||
public let comment: Comment?
|
||||
public let fileCommentID: String?
|
||||
|
||||
internal init(item:[String: Any]?) {
|
||||
type = item?["type"] as? String
|
||||
ts = item?["ts"] as? String
|
||||
channel = item?["channel"] as? String
|
||||
message = Message(dictionary: item?["message"] as? [String: Any])
|
||||
|
||||
// Comment and File can come across as Strings or Dictionaries
|
||||
if let commentDictionary = item?["comment"] as? [String: Any] {
|
||||
comment = Comment(comment: commentDictionary)
|
||||
} else {
|
||||
comment = Comment(id: item?["comment"] as? String)
|
||||
}
|
||||
|
||||
if let fileDictionary = item?["file"] as? [String: Any] {
|
||||
file = File(file: fileDictionary)
|
||||
} else {
|
||||
file = File(id: item?["file"] as? String)
|
||||
}
|
||||
|
||||
fileCommentID = item?["file_comment"] as? String
|
||||
}
|
||||
|
||||
public static func ==(lhs: Item, rhs: Item) -> Bool {
|
||||
return lhs.type == rhs.type && lhs.channel == rhs.channel && lhs.file == rhs.file && lhs.comment == rhs.comment && lhs.message == rhs.message
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public class Message {
|
||||
public final class Message: Equatable {
|
||||
|
||||
public let type = "message"
|
||||
public let subtype: String?
|
||||
@@ -45,38 +45,42 @@ public class Message {
|
||||
internal(set) var pinnedTo: [String]?
|
||||
public let comment: Comment?
|
||||
public let file: File?
|
||||
internal(set) public var reactions = [String: Reaction]()
|
||||
internal(set) public var reactions = [Reaction]()
|
||||
internal(set) public var attachments: [Attachment]?
|
||||
internal(set) public var responseType: ResponseType?
|
||||
internal(set) public var replaceOriginal: Bool?
|
||||
internal(set) public var deleteOriginal: Bool?
|
||||
|
||||
public init?(message: [String: Any]?) {
|
||||
subtype = message?["subtype"] as? String
|
||||
ts = message?["ts"] as? String
|
||||
user = message?["user"] as? String
|
||||
channel = message?["channel"] as? String
|
||||
hidden = message?["hidden"] as? Bool
|
||||
text = message?["text"] as? String
|
||||
botID = message?["bot_id"] as? String
|
||||
username = message?["username"] as? String
|
||||
icons = message?["icons"] as? [String: Any]
|
||||
deletedTs = message?["deleted_ts"] as? String
|
||||
purpose = message?["purpose"] as? String
|
||||
topic = message?["topic"] as? String
|
||||
name = message?["name"] as? String
|
||||
members = message?["members"] as? [String]
|
||||
oldName = message?["old_name"] as? String
|
||||
upload = message?["upload"] as? Bool
|
||||
itemType = message?["item_type"] as? String
|
||||
isStarred = message?["is_starred"] as? Bool
|
||||
pinnedTo = message?["pinned_to"] as? [String]
|
||||
comment = Comment(comment: message?["comment"] as? [String: Any])
|
||||
file = File(file: message?["file"] as? [String: Any])
|
||||
reactions = messageReactions(reactions: message?["reactions"] as? [[String: Any]])
|
||||
attachments = (message?["attachments"] as? [[String: Any]])?.objectArrayFromDictionaryArray(intializer: {(attachment) -> Attachment? in
|
||||
return Attachment(attachment: attachment)
|
||||
})
|
||||
public init(dictionary: [String: Any]?) {
|
||||
subtype = dictionary?["subtype"] as? String
|
||||
ts = dictionary?["ts"] as? String
|
||||
user = dictionary?["user"] as? String
|
||||
channel = dictionary?["channel"] as? String
|
||||
hidden = dictionary?["hidden"] as? Bool
|
||||
text = dictionary?["text"] as? String
|
||||
botID = dictionary?["bot_id"] as? String
|
||||
username = dictionary?["username"] as? String
|
||||
icons = dictionary?["icons"] as? [String: Any]
|
||||
deletedTs = dictionary?["deleted_ts"] as? String
|
||||
purpose = dictionary?["purpose"] as? String
|
||||
topic = dictionary?["topic"] as? String
|
||||
name = dictionary?["name"] as? String
|
||||
members = dictionary?["members"] as? [String]
|
||||
oldName = dictionary?["old_name"] as? String
|
||||
upload = dictionary?["upload"] as? Bool
|
||||
itemType = dictionary?["item_type"] as? String
|
||||
isStarred = dictionary?["is_starred"] as? Bool
|
||||
pinnedTo = dictionary?["pinned_to"] as? [String]
|
||||
comment = Comment(comment: dictionary?["comment"] as? [String: Any])
|
||||
file = File(file: dictionary?["file"] as? [String: Any])
|
||||
reactions = Reaction.reactionsFromArray(dictionary?["reactions"] as? [[String: Any]])
|
||||
attachments = (dictionary?["attachments"] as? [[String: Any]])?.map{Attachment(attachment: $0)}
|
||||
responseType = ResponseType(rawValue: dictionary?["response_type"] as? String ?? "")
|
||||
replaceOriginal = dictionary?["replace_original"] as? Bool
|
||||
deleteOriginal = dictionary?["delete_original"] as? Bool
|
||||
}
|
||||
|
||||
internal init?(ts:String?) {
|
||||
internal init(ts:String?) {
|
||||
self.ts = ts
|
||||
subtype = nil
|
||||
user = nil
|
||||
@@ -91,21 +95,7 @@ public class Message {
|
||||
file = nil
|
||||
}
|
||||
|
||||
private func messageReactions(reactions: [[String: Any]]?) -> [String: Reaction] {
|
||||
var returnValue = [String: Reaction]()
|
||||
if let r = reactions {
|
||||
for react in r {
|
||||
if let reaction = Reaction(reaction: react), reactionName = reaction.name {
|
||||
returnValue[reactionName] = reaction
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue
|
||||
public static func ==(lhs: Message, rhs: Message) -> Bool {
|
||||
return lhs.ts == rhs.ts && lhs.user == rhs.user && lhs.text == rhs.text
|
||||
}
|
||||
}
|
||||
|
||||
extension Message: Equatable {}
|
||||
|
||||
public func ==(lhs: Message, rhs: Message) -> Bool {
|
||||
return lhs.ts == rhs.ts && lhs.user == rhs.user && lhs.text == rhs.text
|
||||
}
|
||||
|
||||
@@ -22,170 +22,136 @@
|
||||
// THE SOFTWARE.
|
||||
|
||||
import Foundation
|
||||
import HTTPSClient
|
||||
import Jay
|
||||
import HTTPClient
|
||||
import WebSocketClient
|
||||
|
||||
internal struct NetworkInterface {
|
||||
|
||||
private let apiUrl = "https://slack.com/api/"
|
||||
private let client: HTTPSClient.Client?
|
||||
private let client: HTTPClient.Client?
|
||||
|
||||
init() {
|
||||
do {
|
||||
self.client = try Client(uri: URI("https://slack.com"))
|
||||
self.client = try Client(url: URL(string: "https://slack.com")!)
|
||||
} catch {
|
||||
self.client = nil
|
||||
}
|
||||
}
|
||||
|
||||
internal func request(endpoint: SlackAPIEndpoint, token: String, parameters: [String: Any]?, successClosure: ([String: Any])->Void, errorClosure: (SlackError)->Void) {
|
||||
var requestString = "\(apiUrl)\(endpoint.rawValue)?token=\(token)"
|
||||
if let params = parameters {
|
||||
requestString += requestStringFromParameters(parameters: params)
|
||||
internal func request(_ endpoint: Endpoint, parameters: [String: Any?], successClosure: @escaping ([String: Any])->Void, errorClosure: @escaping (SlackError)->Void) {
|
||||
var components = URLComponents(string: "\(apiUrl)\(endpoint.rawValue)")
|
||||
if parameters.count > 0 {
|
||||
components?.queryItems = filterNilParameters(parameters).map { URLQueryItem(name: $0.0, value: "\($0.1)") }
|
||||
}
|
||||
guard let requestString = components?.string else {
|
||||
errorClosure(SlackError.clientNetworkError)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
var response: Response?
|
||||
response = try client?.get(requestString)
|
||||
|
||||
let data = try response?.body.becomeBuffer()
|
||||
if let data = data {
|
||||
let json = try Jay().jsonFromData(data.bytes)
|
||||
if let result = json as? [String: Any] {
|
||||
if (result["ok"] as? Bool == true) {
|
||||
successClosure(result)
|
||||
} else {
|
||||
if let errorString = result["error"] as? String {
|
||||
throw ErrorDispatcher.dispatch(error: errorString)
|
||||
} else {
|
||||
throw SlackError.UnknownError
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let contentNegotiation = ContentNegotiationMiddleware(mediaTypes: [.json, .urlEncodedForm], mode: .client)
|
||||
let response = try client?.get(requestString, middleware: [contentNegotiation])
|
||||
successClosure(try handleResponse(response))
|
||||
} catch let error {
|
||||
if let slackError = error as? SlackError {
|
||||
errorClosure(slackError)
|
||||
} else {
|
||||
errorClosure(SlackError.UnknownError)
|
||||
errorClosure(SlackError.unknownError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal func postRequest(endpoint: SlackAPIEndpoint, token: String, parameters: [String: Any]?, successClosure: ([String: Any])->Void, errorClosure: (SlackError)->Void) {
|
||||
let requestString = "\(apiUrl)\(endpoint.rawValue)?token=\(token)"
|
||||
do {
|
||||
var response: Response?
|
||||
let headers: Headers = ["Content-Type": "application/x-www-form-urlencoded"]
|
||||
var body = ""
|
||||
if let params = parameters {
|
||||
body = requestStringFromParameters(parameters: params)
|
||||
} else {
|
||||
body = ""
|
||||
}
|
||||
|
||||
response = try client?.post(requestString, headers: headers, body: body)
|
||||
|
||||
let data = try response?.body.becomeBuffer()
|
||||
if let data = data {
|
||||
let json = try Jay().jsonFromData(data.bytes)
|
||||
if let result = json as? [String: Any] {
|
||||
if (result["ok"] as? Bool == true) {
|
||||
successClosure(result)
|
||||
} else {
|
||||
if let errorString = result["error"] as? String {
|
||||
throw ErrorDispatcher.dispatch(error: errorString)
|
||||
} else {
|
||||
throw SlackError.UnknownError
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch let error {
|
||||
if let slackError = error as? SlackError {
|
||||
errorClosure(slackError)
|
||||
} else {
|
||||
errorClosure(SlackError.UnknownError)
|
||||
}
|
||||
internal func uploadRequest(data: Data, parameters: [String: Any?], successClosure: @escaping ([String: Any])->Void, errorClosure: @escaping (SlackError)->Void) {
|
||||
var components = URLComponents(string: "\(apiUrl)\(Endpoint.filesUpload.rawValue)")
|
||||
if parameters.count > 0 {
|
||||
components?.queryItems = filterNilParameters(parameters).map { URLQueryItem(name: $0.0, value: "\($0.1)") }
|
||||
}
|
||||
guard let requestString = components?.string else {
|
||||
errorClosure(SlackError.clientNetworkError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Currently Unsupported
|
||||
/*internal func uploadRequest(token: String, data: NSData, parameters: [String: Any]?, successClosure: ([String: Any])->Void, errorClosure: (SlackError)->Void) {
|
||||
var requestString = "\(apiUrl)\(SlackAPIEndpoint.FilesUpload.rawValue)?token=\(token)"
|
||||
if let params = parameters {
|
||||
requestString = requestString + requestStringFromParameters(parameters: params)
|
||||
}
|
||||
|
||||
let request = NSMutableURLRequest(url: NSURL(string: requestString)!)
|
||||
request.httpMethod = "POST"
|
||||
let boundaryConstant = randomBoundary()
|
||||
let contentType = "multipart/form-data; boundary=" + boundaryConstant
|
||||
let boundaryStart = "--\(boundaryConstant)\r\n"
|
||||
let boundaryEnd = "--\(boundaryConstant)--\r\n"
|
||||
let contentDispositionString = "Content-Disposition: form-data; name=\"file\"; filename=\"\(parameters!["filename"])\"\r\n"
|
||||
let contentTypeString = "Content-Type: \(parameters!["filetype"])\r\n\r\n"
|
||||
|
||||
let requestBodyData : NSMutableData = NSMutableData()
|
||||
requestBodyData.append(boundaryStart.data(using: NSUTF8StringEncoding)!)
|
||||
requestBodyData.append(contentDispositionString.data(using: NSUTF8StringEncoding)!)
|
||||
requestBodyData.append(contentTypeString.data(using: NSUTF8StringEncoding)!)
|
||||
requestBodyData.append(data)
|
||||
requestBodyData.append("\r\n".data(using: NSUTF8StringEncoding)!)
|
||||
requestBodyData.append(boundaryEnd.data(using: NSUTF8StringEncoding)!)
|
||||
let boundaryEnd = "\r\n--\(boundaryConstant)--\r\n"
|
||||
let contentDispositionString = "Content-Disposition: form-data; name=\"file\"; filename=\"\(parameters["filename"])\"\r\n"
|
||||
let contentTypeString = "Content-Type: \(parameters["filetype"])\r\n\r\n"
|
||||
|
||||
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = requestBodyData
|
||||
guard let boundaryStartData = boundaryStart.data(using: .utf8), let dispositionData = contentDispositionString.data(using: .utf8), let contentTypeData = contentTypeString.data(using: .utf8), let boundaryEndData = boundaryEnd.data(using: .utf8) else {
|
||||
errorClosure(SlackError.clientNetworkError)
|
||||
return
|
||||
}
|
||||
var requestBodyData = Data()
|
||||
requestBodyData.append(contentsOf: boundaryStartData)
|
||||
requestBodyData.append(contentsOf: dispositionData)
|
||||
requestBodyData.append(contentsOf: contentTypeData)
|
||||
requestBodyData.append(contentsOf: data)
|
||||
requestBodyData.append(contentsOf: boundaryEndData)
|
||||
|
||||
let header: Headers = ["Content-Type":"multipart/form-data; boundary=\(boundaryConstant)"]
|
||||
let body = Buffer([UInt8](requestBodyData))
|
||||
|
||||
NSURLSession.shared().dataTask(with: request) {
|
||||
(data, response, internalError) -> Void in
|
||||
guard let data = data else {
|
||||
return
|
||||
do {
|
||||
let response = try client?.post(requestString, headers: header, body: body)
|
||||
successClosure(try handleResponse(response))
|
||||
} catch let error {
|
||||
if let slackError = error as? SlackError {
|
||||
errorClosure(slackError)
|
||||
} else {
|
||||
errorClosure(SlackError.unknownError)
|
||||
}
|
||||
do {
|
||||
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
|
||||
if (result["ok"] as! Bool == true) {
|
||||
successClosure(result)
|
||||
} else {
|
||||
if let errorString = result["error"] as? String {
|
||||
throw ErrorDispatcher.dispatch(error: errorString)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleResponse(_ response: Response?) throws -> [String: Any] {
|
||||
guard var response = response else {
|
||||
throw SlackError.clientNetworkError
|
||||
}
|
||||
do {
|
||||
let buffer = try response.body.becomeBuffer(deadline: 3.seconds.fromNow())
|
||||
switch response.statusCode {
|
||||
case 200:
|
||||
let data = Data(bytes: buffer.bytes)
|
||||
if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] {
|
||||
if json["ok"] as? Bool == true {
|
||||
return json
|
||||
} else if let errorString = json["error"] as? String {
|
||||
throw SlackError(rawValue: errorString) ?? .unknownError
|
||||
} else {
|
||||
throw SlackError.UnknownError
|
||||
throw SlackError.clientJSONError
|
||||
}
|
||||
}
|
||||
} catch let error {
|
||||
if let slackError = error as? SlackError {
|
||||
errorClosure(slackError)
|
||||
} else {
|
||||
errorClosure(SlackError.UnknownError)
|
||||
throw SlackError.unknownError
|
||||
}
|
||||
case 429:
|
||||
throw SlackError.tooManyRequests
|
||||
default:
|
||||
throw SlackError.clientNetworkError
|
||||
}
|
||||
}.resume()
|
||||
} catch let error {
|
||||
if let slackError = error as? SlackError {
|
||||
throw slackError
|
||||
} else {
|
||||
throw SlackError.unknownError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func filterNilParameters(_ parameters: [String: Any?]) -> [String: Any] {
|
||||
var finalParameters = [String: Any]()
|
||||
for (key, value) in parameters {
|
||||
if let unwrapped = value {
|
||||
finalParameters[key] = unwrapped
|
||||
}
|
||||
}
|
||||
return finalParameters
|
||||
}
|
||||
|
||||
private func randomBoundary() -> String {
|
||||
return String(format: "slackkit.boundary.%08x%08x", arc4random(), arc4random())
|
||||
}*/
|
||||
|
||||
private func requestStringFromParameters(parameters: [String: Any]) -> String {
|
||||
var requestString = ""
|
||||
for key in parameters.keys {
|
||||
if let value = parameters[key] as? String {
|
||||
#if os(Linux)
|
||||
if let encodedValue = value.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
|
||||
requestString += "&\(key)=\(encodedValue)"
|
||||
}
|
||||
#else
|
||||
if let encodedValue = value.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed()) {
|
||||
requestString += "&\(key)=\(encodedValue)"
|
||||
}
|
||||
#endif
|
||||
} else if let value = parameters[key] as? Int {
|
||||
requestString += "&\(key)=\(value)"
|
||||
}
|
||||
}
|
||||
|
||||
return requestString
|
||||
#if os(Linux)
|
||||
return "slackkit.boundary.\(Int(random()))\(Int(random()))"
|
||||
#else
|
||||
return "slackkit.boundary.\(arc4random())\(arc4random())"
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// Reaction.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct Reaction: Equatable {
|
||||
|
||||
public let name: String?
|
||||
internal(set) public var user: String?
|
||||
|
||||
internal init(reaction:[String: Any]?) {
|
||||
name = reaction?["name"] as? String
|
||||
}
|
||||
|
||||
internal init(name: String, user: String) {
|
||||
self.name = name
|
||||
self.user = user
|
||||
}
|
||||
|
||||
static func reactionsFromArray(_ array: [[String: Any]]?) -> [Reaction] {
|
||||
var reactions = [Reaction]()
|
||||
if let array = array {
|
||||
for reaction in array {
|
||||
if let users = reaction["users"] as? [String], let name = reaction["name"] as? String {
|
||||
for user in users {
|
||||
reactions.append(Reaction(name: name, user: user))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return reactions
|
||||
}
|
||||
|
||||
public static func ==(lhs: Reaction, rhs: Reaction) -> Bool {
|
||||
return lhs.name == rhs.name
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// SlackError.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public enum SlackError: String, Error {
|
||||
case accountInactive = "account_inactive"
|
||||
case alreadyArchived = "already_archived"
|
||||
case alreadyInChannel = "already_in_channel"
|
||||
case alreadyPinned = "already_pinned"
|
||||
case alreadyReacted = "already_reacted"
|
||||
case alreadyStarred = "already_starred"
|
||||
case badClientSecret = "bad_client_secret"
|
||||
case badRedirectURI = "bad_redirect_uri"
|
||||
case badTimeStamp = "bad_timestamp"
|
||||
case cantArchiveGeneral = "cant_archive_general"
|
||||
case cantDelete = "cant_delete"
|
||||
case cantDeleteFile = "cant_delete_file"
|
||||
case cantDeleteMessage = "cant_delete_message"
|
||||
case cantInvite = "cant_invite"
|
||||
case cantInviteSelf = "cant_invite_self"
|
||||
case cantKickFromGeneral = "cant_kick_from_general"
|
||||
case cantKickFromLastChannel = "cant_kick_from_last_channel"
|
||||
case cantKickSelf = "cant_kick_self"
|
||||
case cantLeaveGeneral = "cant_leave_general"
|
||||
case cantLeaveLastChannel = "cant_leave_last_channel"
|
||||
case cantUpdateMessage = "cant_update_message"
|
||||
case channelNotFound = "channel_not_found"
|
||||
case complianceExportsPreventDeletion = "compliance_exports_prevent_deletion"
|
||||
case editWindowClosed = "edit_window_closed"
|
||||
case fileCommentNotFound = "file_comment_not_found"
|
||||
case fileDeleted = "file_deleted"
|
||||
case fileNotFound = "file_not_found"
|
||||
case fileNotShared = "file_not_shared"
|
||||
case groupContainsOthers = "group_contains_others"
|
||||
case invalidArgName = "invalid_arg_name"
|
||||
case invalidArrayArg = "invalid_array_arg"
|
||||
case invalidAuth = "invalid_auth"
|
||||
case invalidChannel = "invalid_channel"
|
||||
case invalidCharSet = "invalid_charset"
|
||||
case invalidClientID = "invalid_client_id"
|
||||
case invalidCode = "invalid_code"
|
||||
case invalidFormData = "invalid_form_data"
|
||||
case invalidName = "invalid_name"
|
||||
case invalidPostType = "invalid_post_type"
|
||||
case invalidPresence = "invalid_presence"
|
||||
case invalidTS = "invalid_timestamp"
|
||||
case invalidTSLatest = "invalid_ts_latest"
|
||||
case invalidTSOldest = "invalid_ts_oldest"
|
||||
case isArchived = "is_archived"
|
||||
case lastMember = "last_member"
|
||||
case lastRAChannel = "last_ra_channel"
|
||||
case messageNotFound = "message_not_found"
|
||||
case messageTooLong = "msg_too_long"
|
||||
case migrationInProgress = "migration_in_progress"
|
||||
case missingDuration = "missing_duration"
|
||||
case missingPostType = "missing_post_type"
|
||||
case missingScope = "missing_scope"
|
||||
case nameTaken = "name_taken"
|
||||
case noChannel = "no_channel"
|
||||
case noComment = "no_comment"
|
||||
case noItemSpecified = "no_item_specified"
|
||||
case noReaction = "no_reaction"
|
||||
case noText = "no_text"
|
||||
case notArchived = "not_archived"
|
||||
case notAuthed = "not_authed"
|
||||
case notEnoughUsers = "not_enough_users"
|
||||
case notInChannel = "not_in_channel"
|
||||
case notInGroup = "not_in_group"
|
||||
case notPinned = "not_pinned"
|
||||
case notStarred = "not_starred"
|
||||
case overPaginationLimit = "over_pagination_limit"
|
||||
case paidOnly = "paid_only"
|
||||
case permissionDenied = "perimssion_denied"
|
||||
case postingToGeneralChannelDenied = "posting_to_general_channel_denied"
|
||||
case rateLimited = "rate_limited"
|
||||
case requestTimeout = "request_timeout"
|
||||
case restrictedAction = "restricted_action"
|
||||
case snoozeEndFailed = "snooze_end_failed"
|
||||
case snoozeFailed = "snooze_failed"
|
||||
case snoozeNotActive = "snooze_not_active"
|
||||
case tooLong = "too_long"
|
||||
case tooManyEmoji = "too_many_emoji"
|
||||
case tooManyReactions = "too_many_reactions"
|
||||
case tooManyUsers = "too_many_users"
|
||||
case unknownError
|
||||
case unknownType = "unknown_type"
|
||||
case userDisabled = "user_disabled"
|
||||
case userDoesNotOwnChannel = "user_does_not_own_channel"
|
||||
case userIsBot = "user_is_bot"
|
||||
case userIsRestricted = "user_is_restricted"
|
||||
case userIsUltraRestricted = "user_is_ultra_restricted"
|
||||
case userListNotSupplied = "user_list_not_supplied"
|
||||
case userNotFound = "user_not_found"
|
||||
case userNotVisible = "user_not_visible"
|
||||
// Client
|
||||
case clientNetworkError
|
||||
case clientJSONError
|
||||
case clientOAuthError
|
||||
// HTTP
|
||||
case tooManyRequests
|
||||
case unknownHTTPError
|
||||
}
|
||||
+425
-464
File diff suppressed because it is too large
Load Diff
@@ -1,288 +0,0 @@
|
||||
//
|
||||
// SlackWebAPIErrorHandling.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public enum SlackError: ErrorProtocol {
|
||||
case AccountInactive
|
||||
case AlreadyArchived
|
||||
case AlreadyInChannel
|
||||
case AlreadyPinned
|
||||
case AlreadyReacted
|
||||
case AlreadyStarred
|
||||
case BadClientSecret
|
||||
case BadRedirectURI
|
||||
case BadTimeStamp
|
||||
case CantArchiveGeneral
|
||||
case CantDelete
|
||||
case CantDeleteFile
|
||||
case CantDeleteMessage
|
||||
case CantInvite
|
||||
case CantInviteSelf
|
||||
case CantKickFromGeneral
|
||||
case CantKickFromLastChannel
|
||||
case CantKickSelf
|
||||
case CantLeaveGeneral
|
||||
case CantLeaveLastChannel
|
||||
case CantUpdateMessage
|
||||
case ChannelNotFound
|
||||
case ComplianceExportsPreventDeletion
|
||||
case EditWindowClosed
|
||||
case FileCommentNotFound
|
||||
case FileDeleted
|
||||
case FileNotFound
|
||||
case FileNotShared
|
||||
case GroupContainsOthers
|
||||
case InvalidArrayArg
|
||||
case InvalidAuth
|
||||
case InvalidChannel
|
||||
case InvalidCharSet
|
||||
case InvalidClientID
|
||||
case InvalidCode
|
||||
case InvalidFormData
|
||||
case InvalidName
|
||||
case InvalidPostType
|
||||
case InvalidPresence
|
||||
case InvalidTS
|
||||
case InvalidTSLatest
|
||||
case InvalidTSOldest
|
||||
case IsArchived
|
||||
case LastMember
|
||||
case LastRAChannel
|
||||
case MessageNotFound
|
||||
case MessageTooLong
|
||||
case MigrationInProgress
|
||||
case MissingDuration
|
||||
case MissingPostType
|
||||
case NameTaken
|
||||
case NoChannel
|
||||
case NoComment
|
||||
case NoItemSpecified
|
||||
case NoReaction
|
||||
case NoText
|
||||
case NotArchived
|
||||
case NotAuthed
|
||||
case NotEnoughUsers
|
||||
case NotInChannel
|
||||
case NotInGroup
|
||||
case NotPinned
|
||||
case NotStarred
|
||||
case OverPaginationLimit
|
||||
case PaidOnly
|
||||
case PermissionDenied
|
||||
case PostingToGeneralChannelDenied
|
||||
case RateLimited
|
||||
case RequestTimeout
|
||||
case RestrictedAction
|
||||
case SnoozeEndFailed
|
||||
case SnoozeFailed
|
||||
case SnoozeNotActive
|
||||
case TooLong
|
||||
case TooManyEmoji
|
||||
case TooManyReactions
|
||||
case TooManyUsers
|
||||
case UnknownError
|
||||
case UnknownType
|
||||
case UserDisabled
|
||||
case UserDoesNotOwnChannel
|
||||
case UserIsBot
|
||||
case UserIsRestricted
|
||||
case UserIsUltraRestricted
|
||||
case UserListNotSupplied
|
||||
case UserNotFound
|
||||
case UserNotVisible
|
||||
}
|
||||
|
||||
internal struct ErrorDispatcher {
|
||||
|
||||
static func dispatch(error: String) -> SlackError {
|
||||
switch error {
|
||||
case "account_inactive":
|
||||
return .AccountInactive
|
||||
case "already_in_channel":
|
||||
return .AlreadyInChannel
|
||||
case "already_pinned":
|
||||
return .AlreadyPinned
|
||||
case "already_reacted":
|
||||
return .AlreadyReacted
|
||||
case "already_starred":
|
||||
return .AlreadyStarred
|
||||
case "bad_client_secret":
|
||||
return .BadClientSecret
|
||||
case "bad_redirect_uri":
|
||||
return .BadRedirectURI
|
||||
case "bad_timestamp":
|
||||
return .BadTimeStamp
|
||||
case "cant_delete":
|
||||
return .CantDelete
|
||||
case "cant_delete_file":
|
||||
return .CantDeleteFile
|
||||
case "cant_delete_message":
|
||||
return .CantDeleteMessage
|
||||
case "cant_invite":
|
||||
return .CantInvite
|
||||
case "cant_invite_self":
|
||||
return .CantInviteSelf
|
||||
case "cant_kick_from_general":
|
||||
return .CantKickFromGeneral
|
||||
case "cant_kick_from_last_channel":
|
||||
return .CantKickFromLastChannel
|
||||
case "cant_kick_self":
|
||||
return .CantKickSelf
|
||||
case "cant_leave_general":
|
||||
return .CantLeaveGeneral
|
||||
case "cant_leave_last_channel":
|
||||
return .CantLeaveLastChannel
|
||||
case "cant_update_message":
|
||||
return .CantUpdateMessage
|
||||
case "compliance_exports_prevent_deletion":
|
||||
return .ComplianceExportsPreventDeletion
|
||||
case "channel_not_found":
|
||||
return .ChannelNotFound
|
||||
case "edit_window_closed":
|
||||
return .EditWindowClosed
|
||||
case "file_comment_not_found":
|
||||
return .FileCommentNotFound
|
||||
case "file_deleted":
|
||||
return .FileDeleted
|
||||
case "file_not_found":
|
||||
return .FileNotFound
|
||||
case "file_not_shared":
|
||||
return .FileNotShared
|
||||
case "group_contains_others":
|
||||
return .GroupContainsOthers
|
||||
case "invalid_array_arg":
|
||||
return .InvalidArrayArg
|
||||
case "invalid_auth":
|
||||
return .InvalidAuth
|
||||
case "invalid_channel":
|
||||
return .InvalidChannel
|
||||
case "invalid_charset":
|
||||
return .InvalidCharSet
|
||||
case "invalid_client_id":
|
||||
return .InvalidClientID
|
||||
case "invalid_code":
|
||||
return .InvalidCode
|
||||
case "invalid_form_data":
|
||||
return .InvalidFormData
|
||||
case "invalid_name":
|
||||
return .InvalidName
|
||||
case "invalid_post_type":
|
||||
return .InvalidPostType
|
||||
case "invalid_presence":
|
||||
return .InvalidPresence
|
||||
case "invalid_timestamp":
|
||||
return .InvalidTS
|
||||
case "invalid_ts_latest":
|
||||
return .InvalidTSLatest
|
||||
case "invalid_ts_oldest":
|
||||
return .InvalidTSOldest
|
||||
case "is_archived":
|
||||
return .IsArchived
|
||||
case "last_member":
|
||||
return .LastMember
|
||||
case "last_ra_channel":
|
||||
return .LastRAChannel
|
||||
case "message_not_found":
|
||||
return .MessageNotFound
|
||||
case "msg_too_long":
|
||||
return .MessageTooLong
|
||||
case "migration_in_progress":
|
||||
return .MigrationInProgress
|
||||
case "missing_duration":
|
||||
return .MissingDuration
|
||||
case "missing_post_type":
|
||||
return .MissingPostType
|
||||
case "name_taken":
|
||||
return .NameTaken
|
||||
case "no_channel":
|
||||
return .NoChannel
|
||||
case "no_comment":
|
||||
return .NoComment
|
||||
case "no_reaction":
|
||||
return .NoReaction
|
||||
case "no_item_specified":
|
||||
return .NoItemSpecified
|
||||
case "no_text":
|
||||
return .NoText
|
||||
case "not_archived":
|
||||
return .NotArchived
|
||||
case "not_authed":
|
||||
return .NotAuthed
|
||||
case "not_enough_users":
|
||||
return .NotEnoughUsers
|
||||
case "not_in_channel":
|
||||
return .NotInChannel
|
||||
case "not_in_group":
|
||||
return .NotInGroup
|
||||
case "not_pinned":
|
||||
return .NotPinned
|
||||
case "not_starred":
|
||||
return .NotStarred
|
||||
case "over_pagination_limit":
|
||||
return .OverPaginationLimit
|
||||
case "paid_only":
|
||||
return .PaidOnly
|
||||
case "perimssion_denied":
|
||||
return .PermissionDenied
|
||||
case "posting_to_general_channel_denied":
|
||||
return .PostingToGeneralChannelDenied
|
||||
case "rate_limited":
|
||||
return .RateLimited
|
||||
case "request_timeout":
|
||||
return .RequestTimeout
|
||||
case "snooze_end_failed":
|
||||
return .SnoozeEndFailed
|
||||
case "snooze_failed":
|
||||
return .SnoozeFailed
|
||||
case "snooze_not_active":
|
||||
return .SnoozeNotActive
|
||||
case "too_long":
|
||||
return .TooLong
|
||||
case "too_many_emoji":
|
||||
return .TooManyEmoji
|
||||
case "too_many_reactions":
|
||||
return .TooManyReactions
|
||||
case "too_many_users":
|
||||
return .TooManyUsers
|
||||
case "unknown_type":
|
||||
return .UnknownType
|
||||
case "user_disabled":
|
||||
return .UserDisabled
|
||||
case "user_does_not_own_channel":
|
||||
return .UserDoesNotOwnChannel
|
||||
case "user_is_bot":
|
||||
return .UserIsBot
|
||||
case "user_is_restricted":
|
||||
return .UserIsRestricted
|
||||
case "user_is_ultra_restricted":
|
||||
return .UserIsUltraRestricted
|
||||
case "user_list_not_supplied":
|
||||
return .UserListNotSupplied
|
||||
case "user_not_found":
|
||||
return .UserNotFound
|
||||
case "user_not_visible":
|
||||
return .UserNotVisible
|
||||
default:
|
||||
return .UnknownError
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
public struct Team {
|
||||
|
||||
public let id: String
|
||||
public let id: String?
|
||||
internal(set) public var name: String?
|
||||
internal(set) public var domain: String?
|
||||
internal(set) public var emailDomain: String?
|
||||
@@ -33,8 +33,8 @@ public struct Team {
|
||||
internal(set) public var plan: String?
|
||||
internal(set) public var icon: TeamIcon?
|
||||
|
||||
internal init?(team: [String: Any]?) {
|
||||
id = team?["id"] as! String
|
||||
internal init(team: [String: Any]?) {
|
||||
id = team?["id"] as? String
|
||||
name = team?["name"] as? String
|
||||
domain = team?["domain"] as? String
|
||||
emailDomain = team?["email_domain"] as? String
|
||||
@@ -45,26 +45,3 @@ public struct Team {
|
||||
icon = TeamIcon(icon: team?["icon"] as? [String: Any])
|
||||
}
|
||||
}
|
||||
|
||||
public struct TeamIcon {
|
||||
internal(set) public var image34: String?
|
||||
internal(set) public var image44: String?
|
||||
internal(set) public var image68: String?
|
||||
internal(set) public var image88: String?
|
||||
internal(set) public var image102: String?
|
||||
internal(set) public var image132: String?
|
||||
internal(set) public var imageOriginal: String?
|
||||
internal(set) public var imageDefault: Bool?
|
||||
|
||||
internal init?(icon: [String: Any]?) {
|
||||
image34 = icon?["image_34"] as? String
|
||||
image44 = icon?["image_44"] as? String
|
||||
image68 = icon?["image_68"] as? String
|
||||
image88 = icon?["image_88"] as? String
|
||||
image102 = icon?["image_102"] as? String
|
||||
image132 = icon?["image_132"] as? String
|
||||
imageOriginal = icon?["image_original"] as? String
|
||||
imageDefault = icon?["image_default"] as? Bool
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// TeamIcon.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public struct TeamIcon {
|
||||
|
||||
internal(set) public var image34: String?
|
||||
internal(set) public var image44: String?
|
||||
internal(set) public var image68: String?
|
||||
internal(set) public var image88: String?
|
||||
internal(set) public var image102: String?
|
||||
internal(set) public var image132: String?
|
||||
internal(set) public var imageOriginal: String?
|
||||
internal(set) public var imageDefault: Bool?
|
||||
|
||||
internal init(icon: [String: Any]?) {
|
||||
image34 = icon?["image_34"] as? String
|
||||
image44 = icon?["image_44"] as? String
|
||||
image68 = icon?["image_68"] as? String
|
||||
image88 = icon?["image_88"] as? String
|
||||
image102 = icon?["image_102"] as? String
|
||||
image132 = icon?["image_132"] as? String
|
||||
imageOriginal = icon?["image_original"] as? String
|
||||
imageDefault = icon?["image_default"] as? Bool
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// SlackKit.h
|
||||
// Topic.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
@@ -21,10 +21,15 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
//! Project version number for SlackKit.
|
||||
FOUNDATION_EXPORT double SlackKitVersionNumber;
|
||||
|
||||
//! Project version string for SlackKit.
|
||||
FOUNDATION_EXPORT const unsigned char SlackKitVersionString[];
|
||||
public struct Topic {
|
||||
|
||||
public let value: String?
|
||||
public let creator: String?
|
||||
public let lastSet: Int?
|
||||
|
||||
internal init(topic: [String: Any]?) {
|
||||
value = topic?["value"] as? String
|
||||
creator = topic?["creator"] as? String
|
||||
lastSet = topic?["last_set"] as? Int
|
||||
}
|
||||
}
|
||||
@@ -1,272 +0,0 @@
|
||||
//
|
||||
// Types.swift
|
||||
//
|
||||
// Copyright © 2016 Peter Zignego. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
// MARK: - Edited
|
||||
public struct Edited {
|
||||
public let user: String?
|
||||
public let ts: String?
|
||||
|
||||
internal init?(edited:[String: Any]?) {
|
||||
user = edited?["user"] as? String
|
||||
ts = edited?["ts"] as? String
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - History
|
||||
public struct History {
|
||||
internal(set) public var latest: Double?
|
||||
internal(set) public var messages = [Message]()
|
||||
public let hasMore: Bool?
|
||||
|
||||
internal init?(history: [String: Any]?) {
|
||||
if let latestStr = history?["latest"] as? String, latestDouble = Double(latestStr) {
|
||||
latest = latestDouble
|
||||
}
|
||||
if let msgs = history?["messages"] as? [Any] {
|
||||
for message in msgs {
|
||||
if let message = Message(message: message as? [String: Any]) {
|
||||
messages.append(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
hasMore = history?["has_more"] as? Bool
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Reaction
|
||||
public struct Reaction {
|
||||
public let name: String?
|
||||
internal(set) public var users = [String: String]()
|
||||
|
||||
internal init?(reaction:[String: Any]?) {
|
||||
name = reaction?["name"] as? String
|
||||
}
|
||||
|
||||
internal init?(name: String?, user: String) {
|
||||
self.name = name
|
||||
users[user] = user
|
||||
}
|
||||
|
||||
internal init?(name: String?, users: [String: String]) {
|
||||
self.name = name
|
||||
self.users = users
|
||||
}
|
||||
|
||||
static func reactionsFromArray(array: [[String: Any]]) -> [String: Reaction] {
|
||||
var reactions = [String: Reaction]()
|
||||
var userDictionary = [String: String]()
|
||||
for reaction in array {
|
||||
if let users = reaction["users"] as? [String] {
|
||||
for user in users {
|
||||
userDictionary[user] = user
|
||||
}
|
||||
}
|
||||
if let name = reaction["name"] as? String {
|
||||
reactions[name] = Reaction(name: name, users: userDictionary)
|
||||
}
|
||||
}
|
||||
return reactions
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Reaction: Equatable {}
|
||||
|
||||
public func ==(lhs: Reaction, rhs: Reaction) -> Bool {
|
||||
return lhs.name == rhs.name
|
||||
}
|
||||
|
||||
// MARK: - Comment
|
||||
public struct Comment {
|
||||
public let id: String?
|
||||
public let user: String?
|
||||
internal(set) public var created: Int?
|
||||
internal(set) public var comment: String?
|
||||
internal(set) public var starred: Bool?
|
||||
internal(set) public var stars: Int?
|
||||
internal(set) public var reactions = [String: Reaction]()
|
||||
|
||||
internal init?(comment:[String: Any]?) {
|
||||
id = comment?["id"] as? String
|
||||
created = comment?["created"] as? Int
|
||||
user = comment?["user"] as? String
|
||||
starred = comment?["is_starred"] as? Bool
|
||||
stars = comment?["num_stars"] as? Int
|
||||
self.comment = comment?["comment"] as? String
|
||||
}
|
||||
|
||||
internal init?(id: String?) {
|
||||
self.id = id
|
||||
self.user = nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Comment: Equatable {}
|
||||
|
||||
public func ==(lhs: Comment, rhs: Comment) -> Bool {
|
||||
return lhs.id == rhs.id
|
||||
}
|
||||
|
||||
// MARK: - Item
|
||||
public struct Item {
|
||||
public let type: String?
|
||||
public let ts: String?
|
||||
public let channel: String?
|
||||
public let message: Message?
|
||||
public let file: File?
|
||||
public let comment: Comment?
|
||||
public let fileCommentID: String?
|
||||
|
||||
internal init?(item:[String: Any]?) {
|
||||
type = item?["type"] as? String
|
||||
ts = item?["ts"] as? String
|
||||
channel = item?["channel"] as? String
|
||||
|
||||
message = Message(message: item?["message"] as? [String: Any])
|
||||
|
||||
// Comment and File can come across as Strings or Dictionaries
|
||||
if (Comment(comment: item?["comment"] as? [String: Any])?.id == nil) {
|
||||
comment = Comment(id: item?["comment"] as? String)
|
||||
} else {
|
||||
comment = Comment(comment: item?["comment"] as? [String: Any])
|
||||
}
|
||||
if (File(file: item?["file"] as? [String: Any])?.id == nil) {
|
||||
file = File(id: item?["file"] as? String)
|
||||
} else {
|
||||
file = File(file: item?["file"] as? [String: Any])
|
||||
}
|
||||
|
||||
fileCommentID = item?["file_comment"] as? String
|
||||
}
|
||||
}
|
||||
|
||||
extension Item: Equatable {}
|
||||
|
||||
public func ==(lhs: Item, rhs: Item) -> Bool {
|
||||
return lhs.type == rhs.type && lhs.channel == rhs.channel && lhs.file == rhs.file && lhs.comment == rhs.comment && lhs.message == rhs.message
|
||||
}
|
||||
|
||||
// MARK: - Topic
|
||||
public struct Topic {
|
||||
public let value: String?
|
||||
public let creator: String?
|
||||
public let lastSet: Int?
|
||||
|
||||
internal init?(topic: [String: Any]?) {
|
||||
value = topic?["value"] as? String
|
||||
creator = topic?["creator"] as? String
|
||||
lastSet = topic?["last_set"] as? Int
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Do Not Disturb Status
|
||||
public struct DoNotDisturbStatus {
|
||||
internal(set) public var enabled: Bool?
|
||||
internal(set) public var nextDoNotDisturbStart: Int?
|
||||
internal(set) public var nextDoNotDisturbEnd: Int?
|
||||
internal(set) public var snoozeEnabled: Bool?
|
||||
internal(set) public var snoozeEndtime: Int?
|
||||
|
||||
internal init?(status: [String: Any]?) {
|
||||
enabled = status?["dnd_enabled"] as? Bool
|
||||
nextDoNotDisturbStart = status?["next_dnd_start_ts"] as? Int
|
||||
nextDoNotDisturbEnd = status?["next_dnd_end_ts"] as? Int
|
||||
snoozeEnabled = status?["snooze_enabled"] as? Bool
|
||||
snoozeEndtime = status?["snooze_endtime"] as? Int
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK - Custom Team Profile
|
||||
public struct CustomProfile {
|
||||
internal(set) public var fields = [String: CustomProfileField]()
|
||||
|
||||
internal init?(profile: [String: Any]?) {
|
||||
if let eventFields = profile?["fields"] as? [Any] {
|
||||
for field in eventFields {
|
||||
if let cpf = CustomProfileField(field: field as? [String: Any]), id = cpf.id {
|
||||
fields[id] = cpf
|
||||
} else {
|
||||
if let cpf = CustomProfileField(id: field as? String), id = cpf.id {
|
||||
fields[id] = cpf
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal init?(customFields: [String: Any]?) {
|
||||
if let customFields = customFields {
|
||||
for key in customFields.keys {
|
||||
if let cpf = CustomProfileField(field: customFields[key] as? [String: Any]) {
|
||||
self.fields[key] = cpf
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct CustomProfileField {
|
||||
internal(set) public var id: String?
|
||||
internal(set) public var alt: String?
|
||||
internal(set) public var value: String?
|
||||
internal(set) public var hidden: Bool?
|
||||
internal(set) public var hint: String?
|
||||
internal(set) public var label: String?
|
||||
internal(set) public var options: String?
|
||||
internal(set) public var ordering: Int?
|
||||
internal(set) public var possibleValues: [String]?
|
||||
internal(set) public var type: String?
|
||||
|
||||
internal init?(field: [String: Any]?) {
|
||||
id = field?["id"] as? String
|
||||
alt = field?["alt"] as? String
|
||||
value = field?["value"] as? String
|
||||
hidden = field?["is_hidden"] as? Bool
|
||||
hint = field?["hint"] as? String
|
||||
label = field?["label"] as? String
|
||||
options = field?["options"] as? String
|
||||
ordering = field?["ordering"] as? Int
|
||||
possibleValues = field?["possible_values"] as? [String]
|
||||
type = field?["type"] as? String
|
||||
}
|
||||
|
||||
internal init?(id: String?) {
|
||||
self.id = id
|
||||
}
|
||||
|
||||
internal mutating func updateProfileField(profile: CustomProfileField?) {
|
||||
id = profile?.id != nil ? profile?.id : id
|
||||
alt = profile?.alt != nil ? profile?.alt : alt
|
||||
value = profile?.value != nil ? profile?.value : value
|
||||
hidden = profile?.hidden != nil ? profile?.hidden : hidden
|
||||
hint = profile?.hint != nil ? profile?.hint : hint
|
||||
label = profile?.label != nil ? profile?.label : label
|
||||
options = profile?.options != nil ? profile?.options : options
|
||||
ordering = profile?.ordering != nil ? profile?.ordering : ordering
|
||||
possibleValues = profile?.possibleValues != nil ? profile?.possibleValues : possibleValues
|
||||
type = profile?.type != nil ? profile?.type : type
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@
|
||||
public struct User {
|
||||
|
||||
public struct Profile {
|
||||
|
||||
internal(set) public var firstName: String?
|
||||
internal(set) public var lastName: String?
|
||||
internal(set) public var realName: String?
|
||||
@@ -37,7 +38,7 @@ public struct User {
|
||||
internal(set) public var image192: String?
|
||||
internal(set) public var customProfile: CustomProfile?
|
||||
|
||||
internal init?(profile: [String: Any]?) {
|
||||
internal init(profile: [String: Any]?) {
|
||||
firstName = profile?["first_name"] as? String
|
||||
lastName = profile?["last_name"] as? String
|
||||
realName = profile?["real_name"] as? String
|
||||
@@ -53,7 +54,6 @@ public struct User {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public let id: String?
|
||||
internal(set) public var name: String?
|
||||
internal(set) public var deleted: Bool?
|
||||
@@ -77,7 +77,7 @@ public struct User {
|
||||
// Client properties
|
||||
internal(set) public var userGroups: [String: String]?
|
||||
|
||||
internal init?(user: [String: Any]?) {
|
||||
internal init(user: [String: Any]?) {
|
||||
id = user?["id"] as? String
|
||||
name = user?["name"] as? String
|
||||
deleted = user?["deleted"] as? Bool
|
||||
@@ -99,8 +99,8 @@ public struct User {
|
||||
preferences = user?["prefs"] as? [String: Any]
|
||||
}
|
||||
|
||||
internal init?(id: String?) {
|
||||
internal init(id: String?) {
|
||||
self.id = id
|
||||
self.isBot = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,9 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
public struct UserGroup {
|
||||
|
||||
public let id: String?
|
||||
|
||||
internal(set) public var teamID: String?
|
||||
public let isUserGroup: Bool?
|
||||
internal(set) public var name: String?
|
||||
@@ -43,7 +41,7 @@ public struct UserGroup {
|
||||
internal(set) public var users: [String]?
|
||||
internal(set) public var userCount: Int?
|
||||
|
||||
internal init?(userGroup: [String: Any]?) {
|
||||
internal init(userGroup: [String: Any]?) {
|
||||
id = userGroup?["id"] as? String
|
||||
teamID = userGroup?["team_id"] as? String
|
||||
isUserGroup = userGroup?["is_usergroup"] as? Bool
|
||||
@@ -64,5 +62,4 @@ public struct UserGroup {
|
||||
userCount = Int(count)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user