Skip to content

Commit a76b5d7

Browse files
committed
Filter nudges and fix serializing of command actions
1 parent 71e0e4c commit a76b5d7

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

Sources/CommandBarIOS/CommandBar/InternalSDK.swift

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ final class CommandBarInternalSDK : CommandBarInternalSDKDelegate {
6464
guard let config = CommandBarInternalSDK.shared.config else { return }
6565

6666
// TODO: Can probably hook up some sort of background queue to enqueue to from here
67-
if let nudge = config.nudges_v2.filter({ nudge in
68-
return nudge.is_live && !nudge.archived && nudge.trigger == .whenConditionsPass
69-
}).first {
67+
if let nudge = filterNudges().first {
7068
NudgeWindowManager.shared.renderNudge(nudge)
7169
}
7270
}
@@ -75,14 +73,55 @@ final class CommandBarInternalSDK : CommandBarInternalSDKDelegate {
7573
func triggerNudges(withEvent event: String) {
7674
guard let config = CommandBarInternalSDK.shared.config else { return }
7775

78-
if let nudge = config.nudges_v2.filter({ nudge in
79-
return nudge.trigger == .onEvent(PushTrigger.OnEventMeta(event: event)) && nudge.is_live && !nudge.archived
80-
}).first {
76+
if let nudge = filterNudges().first {
8177
NudgeWindowManager.shared.renderNudge(nudge)
8278
}
8379
}
8480

8581

82+
func filterNudges() -> [Nudge] {
83+
guard let config = CommandBarInternalSDK.shared.config else { return [] }
84+
85+
return config.nudges_v2.filter({ nudge in
86+
if (!nudge.is_live || nudge.archived) {
87+
return false
88+
}
89+
90+
let hasUnsupportedStep = nudge.steps.contains(where: { step in
91+
if (step.form_factor.type == .pin) {
92+
return true
93+
}
94+
let hasUnsupportedContent = step.content.contains(where: { content in
95+
if (content.type == .button) {
96+
if let actionMeta = content.meta as? NudgeContentButtonBlockMeta {
97+
if (actionMeta.action.isSameType(as: "execute_command")) {
98+
return true
99+
} else if (actionMeta.action.isSameType(as: "click")) {
100+
return true
101+
} else if (actionMeta.action.isSameType(as: "open_bar")) {
102+
return true
103+
} else if (actionMeta.action.isSameType(as: "questlist")) {
104+
return true
105+
} else if (actionMeta.action.isSameType(as: "snooze")) {
106+
return true
107+
} else if (actionMeta.action.isSameType(as: "open_chat")) {
108+
return true
109+
} else {
110+
return false
111+
}
112+
}
113+
} else if (content.type == .contentList || content.type == .helpDoc) {
114+
return true
115+
}
116+
117+
return true
118+
})
119+
return hasUnsupportedContent;
120+
})
121+
return hasUnsupportedStep
122+
})
123+
}
124+
86125
public func trackEvent(event: String) {
87126
CommandBarInternalSDK.shared.triggerNudges(withEvent: event)
88127
}

Sources/CommandBarIOS/Types/Actions.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ struct CommandAction: Codable {
102102
var meta: Meta
103103

104104
struct Meta: Codable {
105-
var type: String
106105
var command: String
107106
}
108107
}
@@ -126,6 +125,27 @@ struct QuestlistAction: Codable {
126125
var value: Int
127126
}
128127

128+
extension Action {
129+
func isSameType(as action: String) -> Bool {
130+
switch (self, action) {
131+
case (.command, "execute_command"),
132+
(.none, "no_action"),
133+
(.click, "click"),
134+
(.link, "link"),
135+
(.openChat, "open_chat"),
136+
(.dismiss, "dismiss"),
137+
(.snooze, "snooze"),
138+
(.questlist, "questlist"),
139+
(.nudge, "nudge"),
140+
(.goToNudgeStep, "go_to_step"),
141+
(.stepBack, "step_back"),
142+
(.openBar, "open_bar"): return true
143+
default: return false
144+
}
145+
}
146+
}
147+
148+
129149
enum Action: Codable {
130150
case command(CommandAction)
131151
case none(NoAction)
@@ -145,7 +165,7 @@ enum Action: Codable {
145165
}
146166

147167
enum ActionTypes: String, Codable {
148-
case command = "command"
168+
case command = "execute_command"
149169
case none = "no_action"
150170
case click = "click"
151171
case link = "link"

0 commit comments

Comments
 (0)