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

Commit

Permalink
Merge pull request #18 from pvzig/feature/rtm-team-profile
Browse files Browse the repository at this point in the history
Add support for team profile events
  • Loading branch information
pvzig committed Mar 16, 2016
2 parents 687b57f + 76fdc55 commit fb3719c
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 3 deletions.
1 change: 1 addition & 0 deletions SlackKit/Sources/Client.swift
Expand Up @@ -50,6 +50,7 @@ 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"

Expand Down
6 changes: 6 additions & 0 deletions SlackKit/Sources/Event.swift
Expand Up @@ -78,10 +78,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"
Expand Down Expand Up @@ -152,6 +156,7 @@ internal struct Event {
let dndStatus: DoNotDisturbStatus?
let subteam: UserGroup?
let subteamID: String?
var profile: CustomProfile?

init(event:[String: AnyObject]) {
if let eventType = event["type"] as? String {
Expand Down Expand Up @@ -188,6 +193,7 @@ internal struct Event {
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) {
Expand Down
6 changes: 6 additions & 0 deletions SlackKit/Sources/EventDelegate.swift
Expand Up @@ -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?)
}
16 changes: 14 additions & 2 deletions SlackKit/Sources/EventDispatcher.swift
Expand Up @@ -117,7 +117,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)
Expand All @@ -129,15 +131,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()
case .ReconnectURL:
// The reconnect_url event is currently unsupported and experimental.
break
case .SubteamCreated, .SubteamUpdated:
handler.subteam(event)
case .SubteamSelfAdded:
Expand Down
42 changes: 41 additions & 1 deletion SlackKit/Sources/EventHandler.swift
Expand Up @@ -39,7 +39,6 @@ internal class EventHandler {
}

//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
Expand Down Expand Up @@ -592,6 +591,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
Expand Down
73 changes: 73 additions & 0 deletions SlackKit/Sources/Types.swift
Expand Up @@ -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
}
}
3 changes: 3 additions & 0 deletions SlackKit/Sources/User.swift
Expand Up @@ -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
Expand All @@ -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?
Expand Down

0 comments on commit fb3719c

Please sign in to comment.