Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2be8d0170 | |||
| 45be1b7a3f | |||
| 09aa72d43e | |||
| bf4b55bbd6 | |||
| a82279fad1 | |||
| 500e489d5d | |||
| 874f4f51e1 | |||
| 654f419f4e | |||
| a7c25fe33b | |||
| d2037f4cc5 | |||
| b87232dfb5 | |||
| 9e5678739f | |||
| 5cc8582d65 | |||
| aa078934c0 | |||
| 5c157caea3 | |||
| b567113b5f | |||
| 5b08fb6031 | |||
| b48f33fb72 | |||
| deccb727a1 | |||
| 8f1df8d138 | |||
| 0048710e24 | |||
| f6da0ddd32 | |||
| 513485e704 | |||
| fb3719c29d | |||
| 76fdc55f9e | |||
| 687b57fc1f |
@@ -1,40 +1,44 @@
|
||||
//
|
||||
// AppDelegate.swift
|
||||
// OSX-Sample
|
||||
// AppDelegate.swift
|
||||
// OSX-Sample
|
||||
//
|
||||
// Created by Peter Zignego on 2/18/16.
|
||||
// Copyright © 2016 Launch Software LLC. All rights reserved.
|
||||
// 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 Cocoa
|
||||
import SlackKit
|
||||
|
||||
@NSApplicationMain
|
||||
class AppDelegate: NSObject, NSApplicationDelegate, SlackEventsDelegate {
|
||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
|
||||
@IBOutlet weak var window: NSWindow!
|
||||
|
||||
let client = Client(apiToken: "")
|
||||
let learderboard = Leaderboard(token: "SLACK_AUTH_TOKEN")
|
||||
|
||||
func applicationDidFinishLaunching(aNotification: NSNotification) {
|
||||
client.connect()
|
||||
client.slackEventsDelegate = self
|
||||
learderboard.client.connect()
|
||||
}
|
||||
|
||||
func applicationWillTerminate(aNotification: NSNotification) {
|
||||
// Insert code here to tear down your application
|
||||
}
|
||||
|
||||
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) {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
//
|
||||
// Leaderboard.swift
|
||||
// OSX-Sample
|
||||
//
|
||||
// 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 SlackKit
|
||||
import Foundation
|
||||
|
||||
class Leaderboard: MessageEventsDelegate {
|
||||
|
||||
var leaderboard: [String: Int] = [String: Int]()
|
||||
let atSet = NSCharacterSet(charactersInString: "@")
|
||||
|
||||
let client: Client
|
||||
|
||||
init(token: String) {
|
||||
client = Client(apiToken: token)
|
||||
client.messageEventsDelegate = self
|
||||
}
|
||||
|
||||
enum Command: String {
|
||||
case Leaderboard = "leaderboard"
|
||||
}
|
||||
|
||||
enum Trigger: String {
|
||||
case PlusPlus = "++"
|
||||
case MinusMinus = "--"
|
||||
}
|
||||
|
||||
// MARK: MessageEventsDelegate
|
||||
func messageReceived(message: Message) {
|
||||
listen(message)
|
||||
}
|
||||
|
||||
func messageSent(message: Message){}
|
||||
func messageChanged(message: Message){}
|
||||
func messageDeleted(message: Message?){}
|
||||
|
||||
// MARK: Leaderboard Internal Logic
|
||||
private func listen(message: Message) {
|
||||
if let id = client.authenticatedUser?.id, text = message.text {
|
||||
if text.lowercaseString.containsString(Command.Leaderboard.rawValue) && text.containsString(id) == true {
|
||||
handleCommand(.Leaderboard, channel: message.channel)
|
||||
}
|
||||
}
|
||||
if message.text?.containsString(Trigger.PlusPlus.rawValue) == true {
|
||||
handleMessageWithTrigger(message, trigger: .PlusPlus)
|
||||
}
|
||||
if message.text?.containsString(Trigger.MinusMinus.rawValue) == true {
|
||||
handleMessageWithTrigger(message, trigger: .MinusMinus)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleMessageWithTrigger(message: Message, trigger: Trigger) {
|
||||
if let text = message.text,
|
||||
end = text.rangeOfString(trigger.rawValue)?.startIndex.predecessor(),
|
||||
start = text.rangeOfCharacterFromSet(atSet, options: .BackwardsSearch, range: text.startIndex..<end)?.startIndex {
|
||||
let string = text.substringWithRange(start...end)
|
||||
let users = client.users.values.filter{$0.id == self.userID(string)}
|
||||
if users.count > 0 {
|
||||
let idString = userID(string)
|
||||
initalizationForValue(&leaderboard, value: idString)
|
||||
scoringForValue(&leaderboard, value: idString, trigger: trigger)
|
||||
} else {
|
||||
initalizationForValue(&leaderboard, value: string)
|
||||
scoringForValue(&leaderboard, value: string, trigger: trigger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func handleCommand(command: Command, channel:String?) {
|
||||
switch command {
|
||||
case .Leaderboard:
|
||||
if let id = channel {
|
||||
client.webAPI.sendMessage(id, text: "", linkNames: true, attachments: [constructLeaderboardAttachment()], success: {(response) in
|
||||
|
||||
}, failure: { (error) in
|
||||
print("Leaderboard failed to post due to error:\(error)")
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func initalizationForValue(inout dictionary: [String: Int], value: String) {
|
||||
if dictionary[value] == nil {
|
||||
dictionary[value] = 0
|
||||
}
|
||||
}
|
||||
|
||||
private func scoringForValue(inout dictionary: [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(topItems(&leaderboard)), short: true)
|
||||
let 💩 = AttachmentField(title: "💩", value: swapIDsForNames(bottomItems(&leaderboard)), short: true)
|
||||
return Attachment(fallback: "Leaderboard", title: "Leaderboard", colorHex: AttachmentColor.Good.rawValue, text: "", fields: [💯, 💩])
|
||||
}
|
||||
|
||||
private func topItems(inout dictionary: [String: Int]) -> String {
|
||||
let sortedKeys = Array(dictionary.keys).sort({dictionary[$0] > dictionary[$1]}).filter({dictionary[$0] > 0})
|
||||
let sortedValues = Array(dictionary.values).sort({$0 > $1}).filter({$0 > 0})
|
||||
return leaderboardString(sortedKeys, values: sortedValues)
|
||||
}
|
||||
|
||||
private func bottomItems(inout dictionary: [String: Int]) -> String {
|
||||
let sortedKeys = Array(dictionary.keys).sort({dictionary[$0] < dictionary[$1]}).filter({dictionary[$0] < 0})
|
||||
let sortedValues = Array(dictionary.values).sort({$0 < $1}).filter({$0 < 0})
|
||||
return leaderboardString(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 {
|
||||
returnString = returnString.stringByReplacingOccurrencesOfString(key, withString: "@"+name, options: NSStringCompareOptions.LiteralSearch, range: returnString.startIndex..<returnString.endIndex)
|
||||
}
|
||||
}
|
||||
return returnString
|
||||
}
|
||||
|
||||
private func userID(string: String) -> String {
|
||||
return string.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet)
|
||||
}
|
||||
|
||||
}
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
PODS:
|
||||
- Starscream (1.0.2)
|
||||
- Starscream (1.1.2)
|
||||
|
||||
DEPENDENCIES:
|
||||
- Starscream
|
||||
|
||||
SPEC CHECKSUMS:
|
||||
Starscream: 40e2c4c1c770d811f24116b8b7dbeb4542c56767
|
||||
Starscream: 58a12fd35a3cb6aaa105716c2d42765f7c1c732f
|
||||
|
||||
COCOAPODS: 0.39.0
|
||||
|
||||
@@ -12,7 +12,7 @@ import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 0)
|
||||
.Package(url: "https://github.com/pvzig/SlackKit.git", majorVersion: 1)
|
||||
]
|
||||
)
|
||||
```
|
||||
@@ -40,7 +40,6 @@ To use SlackKit you'll need a bearer token which identifies a single user. You c
|
||||
Once you have a token, initialize a client instance using it:
|
||||
```swift
|
||||
let client = Client(apiToken: "YOUR_SLACK_API_TOKEN")
|
||||
|
||||
```
|
||||
|
||||
If you want to receive messages from the Slack RTM API, connect to it.
|
||||
@@ -48,6 +47,11 @@ 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
|
||||
@@ -65,6 +69,9 @@ SlackKit currently supports the a subset of the Slack Web APIs that are availabl
|
||||
- chat.postMessage
|
||||
- chat.update
|
||||
- emoji.list
|
||||
- files.comments.add
|
||||
- files.comments.edit
|
||||
- files.comments.delete
|
||||
- files.delete
|
||||
- files.upload
|
||||
- groups.close
|
||||
@@ -187,8 +194,8 @@ func itemStarred(item: Item, star: Bool)
|
||||
|
||||
#####ReactionEventsDelegate
|
||||
```swift
|
||||
func reactionAdded(reaction: String?, item: Item?)
|
||||
func reactionRemoved(reaction: String?, item: Item?)
|
||||
func reactionAdded(reaction: String?, item: Item?, itemUser: String?)
|
||||
func reactionRemoved(reaction: String?, item: Item?, itemUser: String?)
|
||||
```
|
||||
|
||||
#####TeamEventsDelegate
|
||||
@@ -209,6 +216,18 @@ func subteamSelfAdded(subteamID: String)
|
||||
func subteamSelfRemoved(subteamID: String)
|
||||
```
|
||||
|
||||
###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:
|
||||
|
||||
```swift
|
||||
let learderboard = Leaderboard(token: "SLACK_AUTH_TOKEN")
|
||||
```
|
||||
|
||||
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)
|
||||
|
||||
|
||||
+3
-2
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "SlackKit"
|
||||
s.version = "0.9.8"
|
||||
s.version = "1.0.1"
|
||||
s.summary = "a Slack client library for iOS and OS X written in Swift"
|
||||
s.homepage = "https://github.com/pvzig/SlackKit"
|
||||
s.license = 'MIT'
|
||||
@@ -12,5 +12,6 @@ Pod::Spec.new do |s|
|
||||
s.requires_arc = true
|
||||
s.source_files = 'SlackKit/Sources/*.swift'
|
||||
s.frameworks = 'Foundation'
|
||||
s.dependency 'Starscream', '~> 1.0.2'
|
||||
s.dependency 'Starscream', '~> 1.1.2'
|
||||
end
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
26BBA19F1C398E3C00BF7225 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26BBA1921C398E3C00BF7225 /* User.swift */; };
|
||||
26BBA1A01C398E3C00BF7225 /* UserGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26BBA1931C398E3C00BF7225 /* UserGroup.swift */; };
|
||||
26DF40351C7A0FA300E19241 /* Attachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26DF40341C7A0FA300E19241 /* Attachment.swift */; };
|
||||
26F4BAC31C9DEBD1000910BA /* Leaderboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26F4BAC21C9DEBD1000910BA /* Leaderboard.swift */; };
|
||||
FFE3AC870D1C42EF276CCA2D /* Pods_SlackKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 407A2ABFC0611867E2BE34D0 /* Pods_SlackKit.framework */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
@@ -58,6 +59,7 @@
|
||||
26BBA1921C398E3C00BF7225 /* User.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = User.swift; path = Sources/User.swift; sourceTree = "<group>"; };
|
||||
26BBA1931C398E3C00BF7225 /* UserGroup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UserGroup.swift; path = Sources/UserGroup.swift; sourceTree = "<group>"; };
|
||||
26DF40341C7A0FA300E19241 /* Attachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Attachment.swift; path = Sources/Attachment.swift; sourceTree = "<group>"; };
|
||||
26F4BAC21C9DEBD1000910BA /* Leaderboard.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Leaderboard.swift; sourceTree = "<group>"; };
|
||||
407A2ABFC0611867E2BE34D0 /* Pods_SlackKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SlackKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
4347F92F3932C96C23B10B2A /* Pods-SlackKit.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SlackKit.release.xcconfig"; path = "Pods/Target Support Files/Pods-SlackKit/Pods-SlackKit.release.xcconfig"; sourceTree = "<group>"; };
|
||||
F59B6A12F1C4C4E24C58E1BF /* Pods-SlackKit.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SlackKit.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SlackKit/Pods-SlackKit.debug.xcconfig"; sourceTree = "<group>"; };
|
||||
@@ -95,6 +97,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
2601D6261C7688610012BF22 /* AppDelegate.swift */,
|
||||
26F4BAC21C9DEBD1000910BA /* Leaderboard.swift */,
|
||||
2601D6281C7688610012BF22 /* Assets.xcassets */,
|
||||
2601D62A1C7688610012BF22 /* MainMenu.xib */,
|
||||
2601D62D1C7688610012BF22 /* Info.plist */,
|
||||
@@ -302,6 +305,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
2601D6271C7688610012BF22 /* AppDelegate.swift in Sources */,
|
||||
26F4BAC31C9DEBD1000910BA /* Leaderboard.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.9.8</string>
|
||||
<string>1.0.1</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -34,7 +34,7 @@ public struct Attachment {
|
||||
public let title: String?
|
||||
public let titleLink: String?
|
||||
public let text: String?
|
||||
public let fields: [[String: AnyObject]]?
|
||||
public let fields: [AttachmentField]?
|
||||
public let imageURL: String?
|
||||
public let thumbURL: String?
|
||||
|
||||
@@ -48,12 +48,14 @@ public struct Attachment {
|
||||
title = attachment?["title"] as? String
|
||||
titleLink = attachment?["title_link"] as? String
|
||||
text = attachment?["text"] as? String
|
||||
fields = attachment?["fields"] as? [[String: AnyObject]]
|
||||
imageURL = attachment?["image_url"] as? String
|
||||
thumbURL = attachment?["thumb_url"] as? String
|
||||
fields = (attachment?["fields"] as? [[String: AnyObject]])?.objectArrayFromDictionaryArray({(field) -> AttachmentField? in
|
||||
return AttachmentField(field: field)
|
||||
})
|
||||
}
|
||||
|
||||
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: [[String: AnyObject]]? = nil, imageURL: String? = nil, thumbURL: String? = nil) {
|
||||
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) {
|
||||
self.fallback = fallback
|
||||
self.color = colorHex
|
||||
self.pretext = pretext
|
||||
@@ -79,10 +81,48 @@ public struct Attachment {
|
||||
attachment["title"] = title
|
||||
attachment["title_link"] = titleLink
|
||||
attachment["text"] = text
|
||||
attachment["fields"] = fields
|
||||
attachment["fields"] = fieldJSONArray(fields)
|
||||
attachment["image_url"] = imageURL
|
||||
attachment["thumb_url"] = thumbURL
|
||||
return attachment
|
||||
}
|
||||
|
||||
private func fieldJSONArray(fields: [AttachmentField]?) -> [[String: AnyObject]] {
|
||||
var returnValue = [[String: AnyObject]]()
|
||||
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: AnyObject]?) {
|
||||
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: AnyObject] {
|
||||
var field = [String: AnyObject]()
|
||||
field["title"] = title
|
||||
field["value"] = value
|
||||
field["short"] = short
|
||||
return field
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public struct Channel {
|
||||
internal(set) public var unread: Int?
|
||||
internal(set) public var unreadCountDisplay: Int?
|
||||
internal(set) public var hasPins: Bool?
|
||||
internal(set) public var members = [String]()
|
||||
internal(set) public var members: [String]?
|
||||
// Client use
|
||||
internal(set) public var pinnedItems = [Item]()
|
||||
internal(set) public var usersTyping = [String]()
|
||||
@@ -66,15 +66,16 @@ public struct Channel {
|
||||
purpose = Topic(topic: channel?["purpose"] as? [String: AnyObject])
|
||||
isMember = channel?["is_member"] as? Bool
|
||||
lastRead = channel?["last_read"] as? String
|
||||
latest = Message(message: channel?["latest"] as? [String: AnyObject])
|
||||
unread = channel?["unread_count"] as? Int
|
||||
unreadCountDisplay = channel?["unread_count_display"] as? Int
|
||||
hasPins = channel?["has_pins"] as? Bool
|
||||
members = channel?["members"] as? [String]
|
||||
|
||||
if let members = channel?["members"] as? [String] {
|
||||
self.members = members
|
||||
if (Message(message: channel?["latest"] as? [String: AnyObject])?.ts == nil) {
|
||||
latest = Message(ts: channel?["latest"] as? String)
|
||||
} else {
|
||||
latest = Message(message: channel?["latest"] as? [String: AnyObject])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal init?(id:String?) {
|
||||
|
||||
+111
-65
@@ -50,9 +50,10 @@ public class Client: WebSocketDelegate {
|
||||
public var reactionEventsDelegate: ReactionEventsDelegate?
|
||||
public var teamEventsDelegate: TeamEventsDelegate?
|
||||
public var subteamEventsDelegate: SubteamEventsDelegate?
|
||||
public var teamProfileEventsDelegate: TeamProfileEventsDelegate?
|
||||
|
||||
internal var token = "SLACK_AUTH_TOKEN"
|
||||
|
||||
|
||||
public func setAuthToken(token: String) {
|
||||
self.token = token
|
||||
}
|
||||
@@ -62,17 +63,27 @@ public class Client: WebSocketDelegate {
|
||||
}
|
||||
|
||||
internal var webSocket: WebSocket?
|
||||
internal let api = NetworkInterface()
|
||||
private var dispatcher: EventDispatcher?
|
||||
|
||||
internal let api = NetworkInterface()
|
||||
private let pingPongQueue = dispatch_queue_create("com.launchsoft.SlackKit", DISPATCH_QUEUE_SERIAL)
|
||||
internal var ping: Double?
|
||||
internal var pong: Double?
|
||||
|
||||
internal var pingInterval: NSTimeInterval?
|
||||
internal var timeout: NSTimeInterval?
|
||||
internal var reconnect: Bool?
|
||||
|
||||
required public init(apiToken: String) {
|
||||
self.token = apiToken
|
||||
}
|
||||
|
||||
public func connect() {
|
||||
public func connect(simpleLatest simpleLatest: Bool? = nil, noUnreads: Bool? = nil, mpimAware: Bool? = nil, pingInterval: NSTimeInterval? = nil, timeout: NSTimeInterval? = nil, reconnect: Bool? = nil) {
|
||||
self.pingInterval = pingInterval
|
||||
self.timeout = timeout
|
||||
self.reconnect = reconnect
|
||||
dispatcher = EventDispatcher(client: self)
|
||||
webAPI.rtmStart(success: {
|
||||
webAPI.rtmStart(simpleLatest, noUnreads: noUnreads, mpimAware: mpimAware, success: {
|
||||
(response) -> Void in
|
||||
self.initialSetup(response)
|
||||
if let socketURL = response["url"] as? String {
|
||||
@@ -84,19 +95,24 @@ public class Client: WebSocketDelegate {
|
||||
}, failure:nil)
|
||||
}
|
||||
|
||||
//MARK: - Message send
|
||||
public func disconnect() {
|
||||
webSocket?.disconnect()
|
||||
}
|
||||
|
||||
//MARK: - RTM Message send
|
||||
public func sendMessage(message: String, channelID: String) {
|
||||
if (connected) {
|
||||
if let data = formatMessageToSlackJsonString(msg: message, channel: channelID) {
|
||||
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
|
||||
webSocket?.writeString(string as! String)
|
||||
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
||||
webSocket?.writeString(string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func formatMessageToSlackJsonString(message: (msg: String, channel: String)) -> NSData? {
|
||||
let json: [String: AnyObject] = [
|
||||
"id": NSDate().timeIntervalSince1970,
|
||||
"id": NSDate().slackTimestamp(),
|
||||
"type": "message",
|
||||
"channel": message.channel,
|
||||
"text": message.msg.slackFormatEscaping()
|
||||
@@ -120,75 +136,85 @@ public class Client: WebSocketDelegate {
|
||||
sentMessages[ts!.stringValue] = Message(message: 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)
|
||||
} else {
|
||||
self.disconnect()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private func sendRTMPing() {
|
||||
if connected {
|
||||
let json: [String: AnyObject] = [
|
||||
"id": NSDate().slackTimestamp(),
|
||||
"type": "ping",
|
||||
]
|
||||
do {
|
||||
let data = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.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
|
||||
internal func initialSetup(json: [String: AnyObject]) {
|
||||
private func initialSetup(json: [String: AnyObject]) {
|
||||
team = Team(team: json["team"] as? [String: AnyObject])
|
||||
authenticatedUser = User(user: json["self"] as? [String: AnyObject])
|
||||
authenticatedUser?.doNotDisturbStatus = DoNotDisturbStatus(status: json["dnd"] as? [String: AnyObject])
|
||||
enumerateUsers(json["users"] as? Array)
|
||||
enumerateChannels(json["channels"] as? Array)
|
||||
enumerateGroups(json["groups"] as? Array)
|
||||
enumerateMPIMs(json["mpims"] as? Array)
|
||||
enumerateIMs(json["ims"] as? Array)
|
||||
enumerateBots(json["bots"] as? Array)
|
||||
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: AnyObject])
|
||||
}
|
||||
|
||||
internal func enumerateUsers(users: [AnyObject]?) {
|
||||
if let users = users {
|
||||
for user in users {
|
||||
let u = User(user: user as? [String: AnyObject])
|
||||
self.users[u!.id!] = u
|
||||
}
|
||||
private func addUser(aUser: [String: AnyObject]) {
|
||||
if let user = User(user: aUser), id = user.id {
|
||||
users[id] = user
|
||||
}
|
||||
}
|
||||
|
||||
internal func enumerateChannels(channels: [AnyObject]?) {
|
||||
if let channels = channels {
|
||||
for channel in channels {
|
||||
let c = Channel(channel: channel as? [String: AnyObject])
|
||||
self.channels[c!.id!] = c
|
||||
}
|
||||
private func addChannel(aChannel: [String: AnyObject]) {
|
||||
if let channel = Channel(channel: aChannel), id = channel.id {
|
||||
channels[id] = channel
|
||||
}
|
||||
}
|
||||
|
||||
internal func enumerateGroups(groups: [AnyObject]?) {
|
||||
if let groups = groups {
|
||||
for group in groups {
|
||||
let g = Channel(channel: group as? [String: AnyObject])
|
||||
self.channels[g!.id!] = g
|
||||
}
|
||||
private func addBot(aBot: [String: AnyObject]) {
|
||||
if let bot = Bot(bot: aBot), id = bot.id {
|
||||
bots[id] = bot
|
||||
}
|
||||
}
|
||||
|
||||
internal func enumerateIMs(ims: [AnyObject]?) {
|
||||
if let ims = ims {
|
||||
for im in ims {
|
||||
let i = Channel(channel: im as? [String: AnyObject])
|
||||
self.channels[i!.id!] = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal func enumerateMPIMs(mpims: [AnyObject]?) {
|
||||
if let mpims = mpims {
|
||||
for mpim in mpims {
|
||||
let m = Channel(channel: mpim as? [String: AnyObject])
|
||||
self.channels[m!.id!] = m
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal func enumerateBots(bots: [AnyObject]?) {
|
||||
if let bots = bots {
|
||||
for bot in bots {
|
||||
let b = Bot(bot: bot as? [String: AnyObject])
|
||||
self.bots[b!.id!] = b
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal func enumerateSubteams(subteams: [String: AnyObject]?) {
|
||||
private func enumerateSubteams(subteams: [String: AnyObject]?) {
|
||||
if let subteams = subteams {
|
||||
if let all = subteams["all"] as? [[String: AnyObject]] {
|
||||
for item in all {
|
||||
@@ -205,15 +231,33 @@ public class Client: WebSocketDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Utilities
|
||||
private func enumerateObjects(array: [AnyObject]?, initalizer: ([String: AnyObject])-> Void) {
|
||||
if let array = array {
|
||||
for object in array {
|
||||
if let dictionary = object as? [String: AnyObject] {
|
||||
initalizer(dictionary)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - WebSocketDelegate
|
||||
public func websocketDidConnect(socket: WebSocket) {}
|
||||
public func websocketDidConnect(socket: WebSocket) {
|
||||
if let pingInterval = pingInterval {
|
||||
pingRTMServerAtInterval(pingInterval)
|
||||
}
|
||||
}
|
||||
|
||||
public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
|
||||
connected = false
|
||||
authenticated = false
|
||||
webSocket = nil
|
||||
if let delegate = slackEventsDelegate {
|
||||
delegate.clientDisconnected()
|
||||
dispatcher = nil
|
||||
authenticatedUser = nil
|
||||
slackEventsDelegate?.clientDisconnected()
|
||||
if reconnect == true {
|
||||
connect(pingInterval: pingInterval, timeout: timeout, reconnect: reconnect)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +266,9 @@ public class Client: WebSocketDelegate {
|
||||
return
|
||||
}
|
||||
do {
|
||||
try dispatcher?.dispatch(NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [String: AnyObject])
|
||||
if let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as? [String: AnyObject] {
|
||||
dispatcher?.dispatch(json)
|
||||
}
|
||||
}
|
||||
catch _ {
|
||||
|
||||
|
||||
@@ -26,20 +26,11 @@ import Foundation
|
||||
extension Client {
|
||||
|
||||
//MARK: - User & Channel
|
||||
public func getChannelOrUserIdByName(name: String) -> String? {
|
||||
if (name[name.startIndex] == "@") {
|
||||
return getUserIdByName(name)
|
||||
} else if (name[name.startIndex] == "C") {
|
||||
return getChannelIDByName(name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func getChannelIDByName(name: String) -> String? {
|
||||
return channels.filter{$0.1.name == stripString(name)}.first?.0
|
||||
}
|
||||
|
||||
public func getUserIdByName(name: String) -> String? {
|
||||
public func getUserIDByName(name: String) -> String? {
|
||||
return users.filter{$0.1.name == stripString(name)}.first?.0
|
||||
}
|
||||
|
||||
@@ -54,13 +45,25 @@ extension Client {
|
||||
}
|
||||
|
||||
//MARK: - Utilities
|
||||
internal func stripString(var string: String) -> String {
|
||||
if string[string.startIndex] == "@" {
|
||||
string = string.substringFromIndex(string.startIndex.advancedBy(1))
|
||||
} else if string[string.startIndex] == "#" {
|
||||
string = string.substringFromIndex(string.startIndex.advancedBy(1))
|
||||
internal func stripString(string: String) -> String? {
|
||||
var strippedString = string
|
||||
if string[string.startIndex] == "@" || string[string.startIndex] == "#" {
|
||||
strippedString = string.substringFromIndex(string.startIndex.advancedBy(1))
|
||||
}
|
||||
return string
|
||||
return strippedString
|
||||
}
|
||||
}
|
||||
|
||||
public enum AttachmentColor: String {
|
||||
case Good = "good"
|
||||
case Warning = "warning"
|
||||
case Danger = "danger"
|
||||
}
|
||||
|
||||
public extension NSDate {
|
||||
|
||||
func slackTimestamp() -> Double {
|
||||
return NSNumber(double: timeIntervalSince1970).doubleValue
|
||||
}
|
||||
|
||||
}
|
||||
@@ -76,10 +79,18 @@ internal extension String {
|
||||
|
||||
}
|
||||
|
||||
public extension NSDate {
|
||||
|
||||
func slackTimestamp() -> String {
|
||||
return NSNumber(double: timeIntervalSince1970).stringValue
|
||||
}
|
||||
internal extension Array {
|
||||
|
||||
func objectArrayFromDictionaryArray<T>(intializer:([String: AnyObject])->T?) -> [T] {
|
||||
var returnValue = [T]()
|
||||
for object in self {
|
||||
if let dictionary = object as? [String: AnyObject] {
|
||||
if let value = intializer(dictionary) {
|
||||
returnValue.append(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ internal enum EventType: String {
|
||||
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"
|
||||
@@ -78,10 +79,14 @@ internal enum EventType: String {
|
||||
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"
|
||||
@@ -148,10 +153,12 @@ internal struct Event {
|
||||
let file: File?
|
||||
let message: Message?
|
||||
let nestedMessage: Message?
|
||||
let itemUser: String?
|
||||
let item: Item?
|
||||
let dndStatus: DoNotDisturbStatus?
|
||||
let subteam: UserGroup?
|
||||
let subteamID: String?
|
||||
var profile: CustomProfile?
|
||||
|
||||
init(event:[String: AnyObject]) {
|
||||
if let eventType = event["type"] as? String {
|
||||
@@ -183,11 +190,13 @@ internal struct Event {
|
||||
bot = Bot(bot: event["bot"] as? [String: AnyObject])
|
||||
edited = Edited(edited:event["edited"] as? [String: AnyObject])
|
||||
dndStatus = DoNotDisturbStatus(status: event["dnd_status"] as? [String: AnyObject])
|
||||
itemUser = event["item_user"] as? String
|
||||
item = Item(item: event["item"] as? [String: AnyObject])
|
||||
subteam = UserGroup(userGroup: event["subteam"] as? [String: AnyObject])
|
||||
subteamID = event["subteam_id"] as? String
|
||||
message = Message(message: event)
|
||||
nestedMessage = Message(message: event["message"] as? [String: AnyObject])
|
||||
profile = CustomProfile(profile: event["profile"] as? [String: AnyObject])
|
||||
|
||||
// Comment, Channel, User, and File can come across as Strings or Dictionaries
|
||||
if (Comment(comment: event["comment"] as? [String: AnyObject])?.id == nil) {
|
||||
|
||||
@@ -80,8 +80,8 @@ public protocol StarEventsDelegate {
|
||||
}
|
||||
|
||||
public protocol ReactionEventsDelegate {
|
||||
func reactionAdded(reaction: String?, item: Item?)
|
||||
func reactionRemoved(reaction: String?, item: Item?)
|
||||
func reactionAdded(reaction: String?, item: Item?, itemUser: String?)
|
||||
func reactionRemoved(reaction: String?, item: Item?, itemUser: String?)
|
||||
}
|
||||
|
||||
public protocol TeamEventsDelegate {
|
||||
@@ -99,3 +99,9 @@ public protocol SubteamEventsDelegate {
|
||||
func subteamSelfAdded(subteamID: String)
|
||||
func subteamSelfRemoved(subteamID: String)
|
||||
}
|
||||
|
||||
public protocol TeamProfileEventsDelegate {
|
||||
func teamProfileChanged(profile: CustomProfile?)
|
||||
func teamProfileDeleted(profile: CustomProfile?)
|
||||
func teamProfileReordered(profile: CustomProfile?)
|
||||
}
|
||||
|
||||
@@ -96,6 +96,8 @@ internal class EventDispatcher {
|
||||
handler.pinAdded(event)
|
||||
case .PinRemoved:
|
||||
handler.pinRemoved(event)
|
||||
case .Pong:
|
||||
handler.pong(event)
|
||||
case .PresenceChange:
|
||||
handler.presenceChange(event)
|
||||
case .ManualPresenceChange:
|
||||
@@ -117,7 +119,9 @@ internal class EventDispatcher {
|
||||
case .EmojiChanged:
|
||||
handler.emojiChanged(event)
|
||||
case .CommandsChanged:
|
||||
// Not implemented per Slack documentation.
|
||||
// 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)
|
||||
@@ -129,15 +133,25 @@ internal class EventDispatcher {
|
||||
handler.teamDomainChange(event)
|
||||
case .EmailDomainChange:
|
||||
handler.emailDomainChange(event)
|
||||
case .TeamProfileChange:
|
||||
handler.teamProfileChange(event)
|
||||
case .TeamProfileDelete:
|
||||
handler.teamProfileDeleted(event)
|
||||
case .TeamProfileReorder:
|
||||
handler.teamProfileReordered(event)
|
||||
case .BotAdded:
|
||||
handler.bot(event)
|
||||
case .BotChanged:
|
||||
handler.bot(event)
|
||||
case .AccountsChanged:
|
||||
// Not implemented per Slack documentation.
|
||||
// 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()
|
||||
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)
|
||||
case .SubteamSelfAdded:
|
||||
|
||||
@@ -38,8 +38,12 @@ internal class EventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Messages
|
||||
//MARK: - Pong
|
||||
func pong(event: Event) {
|
||||
client.pong = event.replyTo
|
||||
}
|
||||
|
||||
//MARK: - Messages
|
||||
func messageSent(event: Event) {
|
||||
if let reply = event.replyTo, message = client.sentMessages[NSNumber(double: reply).stringValue], channel = message.channel, ts = message.ts {
|
||||
message.ts = event.ts
|
||||
@@ -148,8 +152,8 @@ internal class EventHandler {
|
||||
|
||||
func channelLeft(event: Event) {
|
||||
if let channel = event.channel, id = channel.id, userID = client.authenticatedUser?.id {
|
||||
if let index = client.channels[id]?.members.indexOf(userID) {
|
||||
client.channels[id]?.members.removeAtIndex(index)
|
||||
if let index = client.channels[id]?.members?.indexOf(userID) {
|
||||
client.channels[id]?.members?.removeAtIndex(index)
|
||||
|
||||
if let delegate = client.channelEventsDelegate {
|
||||
delegate.channelLeft(channel)
|
||||
@@ -397,7 +401,7 @@ internal class EventHandler {
|
||||
}
|
||||
|
||||
if let delegate = client.reactionEventsDelegate {
|
||||
delegate.reactionAdded(event.reaction, item: event.item)
|
||||
delegate.reactionAdded(event.reaction, item: event.item, itemUser: event.itemUser)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -440,7 +444,7 @@ internal class EventHandler {
|
||||
}
|
||||
|
||||
if let delegate = client.reactionEventsDelegate {
|
||||
delegate.reactionAdded(event.reaction, item: event.item)
|
||||
delegate.reactionAdded(event.reaction, item: event.item, itemUser: event.itemUser)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -592,6 +596,47 @@ internal class EventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
//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(fields[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let delegate = client.teamProfileEventsDelegate {
|
||||
delegate.teamProfileChanged(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(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(event.profile)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Authenticated User
|
||||
func manualPresenceChange(event: Event) {
|
||||
client.authenticatedUser?.presence = event.presence
|
||||
|
||||
@@ -62,7 +62,7 @@ public struct File {
|
||||
internal(set) public var comments = [String: Comment]()
|
||||
internal(set) public var reactions = [String: Reaction]()
|
||||
|
||||
init?(file:[String: AnyObject]?) {
|
||||
public init?(file:[String: AnyObject]?) {
|
||||
id = file?["id"] as? String
|
||||
created = file?["created"] as? Int
|
||||
name = file?["name"] as? String
|
||||
@@ -105,7 +105,7 @@ public struct File {
|
||||
|
||||
}
|
||||
|
||||
init?(id:String?) {
|
||||
internal init?(id:String?) {
|
||||
self.id = id
|
||||
created = nil
|
||||
name = nil
|
||||
|
||||
@@ -21,13 +21,6 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
public enum ItemType: String {
|
||||
case ChannelMessage = "C"
|
||||
case PrivateGroupMessage = "G"
|
||||
case File = "F"
|
||||
case FileComments = "Fc"
|
||||
}
|
||||
|
||||
public class Message {
|
||||
|
||||
public let type = "message"
|
||||
@@ -53,7 +46,7 @@ public class Message {
|
||||
public let comment: Comment?
|
||||
public let file: File?
|
||||
internal(set) public var reactions = [String: Reaction]()
|
||||
internal(set) public var attachments: [Attachment] = []
|
||||
internal(set) public var attachments: [Attachment]?
|
||||
|
||||
public init?(message: [String: AnyObject]?) {
|
||||
subtype = message?["subtype"] as? String
|
||||
@@ -78,7 +71,24 @@ public class Message {
|
||||
comment = Comment(comment: message?["comment"] as? [String: AnyObject])
|
||||
file = File(file: message?["file"] as? [String: AnyObject])
|
||||
reactions = messageReactions(message?["reactions"] as? [[String: AnyObject]])
|
||||
attachments = messageAttachments(message?["attachments"] as? [[String: AnyObject]])
|
||||
attachments = (message?["attachments"] as? [[String: AnyObject]])?.objectArrayFromDictionaryArray({(attachment) -> Attachment? in
|
||||
return Attachment(attachment: attachment)
|
||||
})
|
||||
}
|
||||
|
||||
internal init?(ts:String?) {
|
||||
self.ts = ts
|
||||
subtype = nil
|
||||
user = nil
|
||||
channel = nil
|
||||
botID = nil
|
||||
username = nil
|
||||
icons = nil
|
||||
deletedTs = nil
|
||||
upload = nil
|
||||
itemType = nil
|
||||
comment = nil
|
||||
file = nil
|
||||
}
|
||||
|
||||
private func messageReactions(reactions: [[String: AnyObject]]?) -> [String: Reaction] {
|
||||
@@ -92,19 +102,6 @@ public class Message {
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
private func messageAttachments(attachments: [[String: AnyObject]]?) -> [Attachment] {
|
||||
var returnValue:[Attachment] = []
|
||||
if let a = attachments {
|
||||
for attachment in a {
|
||||
if let attachment = Attachment(attachment: attachment) {
|
||||
returnValue.append(attachment)
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Message: Equatable {}
|
||||
|
||||
@@ -35,7 +35,12 @@ internal enum SlackAPIEndpoint: String {
|
||||
case ChatDelete = "chat.delete"
|
||||
case ChatPostMessage = "chat.postMessage"
|
||||
case ChatUpdate = "chat.update"
|
||||
case DNDInfo = "dnd.info"
|
||||
case DNDTeamInfo = "dnd.teamInfo"
|
||||
case EmojiList = "emoji.list"
|
||||
case FilesCommentsAdd = "files.comments.add"
|
||||
case FilesCommentsEdit = "files.comments.edit"
|
||||
case FilesCommentsDelete = "files.comments.delete"
|
||||
case FilesDelete = "files.delete"
|
||||
case FilesUpload = "files.upload"
|
||||
case GroupsClose = "groups.close"
|
||||
@@ -105,8 +110,9 @@ public class SlackWebAPI {
|
||||
}
|
||||
|
||||
//MARK: - RTM
|
||||
public func rtmStart(success success: ((response: [String: AnyObject])->Void)?, failure: FailureClosure?) {
|
||||
client.api.request(.RTMStart, token: client.token, parameters: nil, successClosure: {
|
||||
public func rtmStart(simpleLatest: Bool? = nil, noUnreads: Bool? = nil, mpimAware: Bool? = nil, success: ((response: [String: AnyObject])->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject?] = ["simple_latest": simpleLatest, "no_unreads": noUnreads, "mpim_aware": mpimAware]
|
||||
client.api.request(.RTMStart, token: client.token, parameters: filterNilParameters(parameters), successClosure: {
|
||||
(response) -> Void in
|
||||
success?(response: response)
|
||||
}) {(error) -> Void in
|
||||
@@ -189,8 +195,8 @@ public class SlackWebAPI {
|
||||
}
|
||||
}
|
||||
|
||||
public func sendMessage(channel: String, text: String, username: String? = nil, asUser: Bool = false, parse: ParseMode = .Full, linkNames: Bool = false, attachments: [Attachment?]? = nil, unfurlLinks: Bool = false, unfurlMedia: Bool = false, iconURL: String? = nil, iconEmoji: String? = nil, success: (((ts: String?, channel: String?))->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject?] = ["channel":channel, "text":text.slackFormatEscaping(), "as_user":asUser, "parse":parse.rawValue, "link_names":linkNames, "unfurl_links":unfurlLinks, "unfurlMedia":unfurlMedia, "username":username, "attachments":encodeAttachments(attachments), "icon_url":iconURL, "icon_emoji":iconEmoji]
|
||||
public func sendMessage(channel: String, text: String, username: String? = nil, asUser: Bool? = nil, parse: ParseMode? = nil, linkNames: Bool? = nil, attachments: [Attachment?]? = nil, unfurlLinks: Bool? = nil, unfurlMedia: Bool? = nil, iconURL: String? = nil, iconEmoji: String? = nil, success: (((ts: String?, channel: String?))->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject?] = ["channel":channel, "text":text.slackFormatEscaping(), "as_user":asUser, "parse":parse?.rawValue, "link_names":linkNames, "unfurl_links":unfurlLinks, "unfurlMedia":unfurlMedia, "username":username, "attachments":encodeAttachments(attachments), "icon_url":iconURL, "icon_emoji":iconEmoji]
|
||||
client.api.request(.ChatPostMessage, token: client.token, parameters: filterNilParameters(parameters), successClosure: {
|
||||
(response) -> Void in
|
||||
success?((ts: response["ts"] as? String, response["channel"] as? String))
|
||||
@@ -209,6 +215,27 @@ public class SlackWebAPI {
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Do Not Disturb
|
||||
public func dndInfo(user: String? = nil, success: ((status: DoNotDisturbStatus?)->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject?] = ["user": user]
|
||||
client.api.request(.DNDInfo, token: client.token, parameters: filterNilParameters(parameters), successClosure: {
|
||||
(response) -> Void in
|
||||
success?(status: DoNotDisturbStatus(status: response))
|
||||
}) {(error) -> Void in
|
||||
failure?(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
public func dndTeamInfo(users: [String]? = nil, success: ((statuses: [String: DoNotDisturbStatus]?)->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject?] = ["users":users?.joinWithSeparator(",")]
|
||||
client.api.request(.DNDTeamInfo, token: client.token, parameters: filterNilParameters(parameters), successClosure: {
|
||||
(response) -> Void in
|
||||
success?(statuses: self.enumerateDNDStauses(response["users"] as? [String: AnyObject]))
|
||||
}) {(error) -> Void in
|
||||
failure?(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Emoji
|
||||
public func emojiList(success: ((emojiList: [String: AnyObject]?)->Void)?, failure: FailureClosure?) {
|
||||
client.api.request(.EmojiList, token: client.token, parameters: nil, successClosure: {
|
||||
@@ -240,6 +267,37 @@ public class SlackWebAPI {
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - File Comments
|
||||
public func addFileComment(fileID: String, comment: String, success: ((comment: Comment?)->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject] = ["file":fileID, "comment":comment.slackFormatEscaping()]
|
||||
client.api.request(.FilesCommentsAdd, token: client.token, parameters: parameters, successClosure: {
|
||||
(response) -> Void in
|
||||
success?(comment: Comment(comment: response["comment"] as? [String: AnyObject]))
|
||||
}) {(error) -> Void in
|
||||
failure?(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
public func editFileComment(fileID: String, commentID: String, comment: String, success: ((comment: Comment?)->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject] = ["file":fileID, "id":commentID, "comment":comment.slackFormatEscaping()]
|
||||
client.api.request(.FilesCommentsEdit, token: client.token, parameters: parameters, successClosure: {
|
||||
(response) -> Void in
|
||||
success?(comment: Comment(comment: response["comment"] as? [String: AnyObject]))
|
||||
}) {(error) -> Void in
|
||||
failure?(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
public func deleteFileComment(fileID: String, commentID: String, success: ((deleted: Bool?)->Void)?, failure: FailureClosure?) {
|
||||
let parameters: [String: AnyObject] = ["file":fileID, "id": commentID]
|
||||
client.api.request(.FilesCommentsDelete, token: client.token, parameters: parameters, successClosure: {
|
||||
(response) -> Void in
|
||||
success?(deleted: true)
|
||||
}) {(error) -> Void in
|
||||
failure?(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: - Groups
|
||||
public func closeGroup(groupID: String, success: ((closed: Bool)->Void)?, failure: FailureClosure?) {
|
||||
close(.GroupsClose, channelID: groupID, success: {
|
||||
@@ -634,6 +692,7 @@ public class SlackWebAPI {
|
||||
return finalParameters
|
||||
}
|
||||
|
||||
//MARK: - Encode Attachments
|
||||
private func encodeAttachments(attachments: [Attachment?]?) -> NSString? {
|
||||
if let attachments = attachments {
|
||||
var attachmentArray: [[String: AnyObject]] = []
|
||||
@@ -653,4 +712,15 @@ public class SlackWebAPI {
|
||||
return nil
|
||||
}
|
||||
|
||||
//MARK: - Enumerate Do Not Distrub Status
|
||||
private func enumerateDNDStauses(statuses: [String: AnyObject]?) -> [String: DoNotDisturbStatus] {
|
||||
var retVal = [String: DoNotDisturbStatus]()
|
||||
if let keys = statuses?.keys {
|
||||
for key in keys {
|
||||
retVal[key] = DoNotDisturbStatus(status: statuses?[key] as? [String: AnyObject])
|
||||
}
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ public enum SlackError: ErrorType {
|
||||
case BadRedirectURI
|
||||
case BadTimeStamp
|
||||
case CantArchiveGeneral
|
||||
case CantDelete
|
||||
case CantDeleteFile
|
||||
case CantDeleteMessage
|
||||
case CantInvite
|
||||
@@ -75,6 +76,7 @@ public enum SlackError: ErrorType {
|
||||
case MissingPostType
|
||||
case NameTaken
|
||||
case NoChannel
|
||||
case NoComment
|
||||
case NoItemSpecified
|
||||
case NoReaction
|
||||
case NoText
|
||||
@@ -131,6 +133,8 @@ internal struct ErrorDispatcher {
|
||||
return .BadRedirectURI
|
||||
case "bad_timestamp":
|
||||
return .BadTimeStamp
|
||||
case "cant_delete":
|
||||
return .CantDelete
|
||||
case "cant_delete_file":
|
||||
return .CantDeleteFile
|
||||
case "cant_delete_message":
|
||||
@@ -213,6 +217,8 @@ internal struct ErrorDispatcher {
|
||||
return .NameTaken
|
||||
case "no_channel":
|
||||
return .NoChannel
|
||||
case "no_comment":
|
||||
return .NoComment
|
||||
case "no_reaction":
|
||||
return .NoReaction
|
||||
case "no_item_specified":
|
||||
|
||||
@@ -198,3 +198,76 @@ public struct DoNotDisturbStatus {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK - Custom Team Profile
|
||||
public struct CustomProfile {
|
||||
internal(set) public var fields = [String: CustomProfileField]()
|
||||
|
||||
internal init?(profile: [String: AnyObject]?) {
|
||||
if let eventFields = profile?["fields"] as? [AnyObject] {
|
||||
for field in eventFields {
|
||||
if let cpf = CustomProfileField(field: field as? [String: AnyObject]), 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: AnyObject]?) {
|
||||
if let customFields = customFields {
|
||||
for key in customFields.keys {
|
||||
if let cpf = CustomProfileField(field: customFields[key] as? [String: AnyObject]) {
|
||||
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: AnyObject]?) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ public struct User {
|
||||
internal(set) public var image48: String?
|
||||
internal(set) public var image72: String?
|
||||
internal(set) public var image192: String?
|
||||
internal(set) public var customProfile: CustomProfile?
|
||||
|
||||
internal init?(profile: [String: AnyObject]?) {
|
||||
firstName = profile?["first_name"] as? String
|
||||
@@ -48,9 +49,11 @@ public struct User {
|
||||
image48 = profile?["image_48"] as? String
|
||||
image72 = profile?["image_72"] as? String
|
||||
image192 = profile?["image_192"] as? String
|
||||
customProfile = CustomProfile(customFields: profile?["fields"] as? [String: AnyObject])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public let id: String?
|
||||
internal(set) public var name: String?
|
||||
internal(set) public var deleted: Bool?
|
||||
|
||||
Reference in New Issue
Block a user