@capawesome/capacitor-intercom 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapawesomeCapacitorIntercom.podspec +19 -0
- package/LICENSE +21 -0
- package/Package.swift +30 -0
- package/README.md +932 -0
- package/android/build.gradle +60 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/Intercom.java +441 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/IntercomHelper.java +38 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/IntercomPlugin.java +307 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/CustomExceptions.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/events/UnreadConversationCountChangeEvent.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/HandlePushNotificationOptions.java +25 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/InitializeOptions.java +34 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/IsIntercomPushNotificationOptions.java +25 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/LogEventOptions.java +35 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/LoginUserOptions.java +33 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/PresentContentOptions.java +71 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/PresentMessageComposerOptions.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/PresentOptions.java +19 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/SendPushTokenToIntercomOptions.java +24 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/SetBottomPaddingOptions.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/SetInAppMessagesVisibleOptions.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/SetLauncherVisibleOptions.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/SetUserHashOptions.java +24 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/SetUserJwtOptions.java +24 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/options/UpdateUserOptions.java +108 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/results/GetUnreadConversationCountResult.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/classes/results/IsIntercomPushNotificationResult.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/interfaces/EmptyCallback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/intercom/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +1345 -0
- package/dist/esm/definitions.d.ts +549 -0
- package/dist/esm/definitions.js +27 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +34 -0
- package/dist/esm/web.js +175 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +215 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +217 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Classes/Events/UnreadConversationCountChangeEvent.swift +16 -0
- package/ios/Plugin/Classes/Options/HandlePushNotificationOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/InitializeOptions.swift +15 -0
- package/ios/Plugin/Classes/Options/IsIntercomPushNotificationOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/LogEventOptions.swift +15 -0
- package/ios/Plugin/Classes/Options/LoginUserOptions.swift +17 -0
- package/ios/Plugin/Classes/Options/PresentContentOptions.swift +29 -0
- package/ios/Plugin/Classes/Options/PresentMessageComposerOptions.swift +10 -0
- package/ios/Plugin/Classes/Options/PresentOptions.swift +10 -0
- package/ios/Plugin/Classes/Options/SendPushTokenToIntercomOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/SetBottomPaddingOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/SetInAppMessagesVisibleOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/SetLauncherVisibleOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/SetUserHashOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/SetUserJwtOptions.swift +13 -0
- package/ios/Plugin/Classes/Options/UpdateUserOptions.swift +38 -0
- package/ios/Plugin/Classes/Results/GetUnreadConversationCountResult.swift +16 -0
- package/ios/Plugin/Classes/Results/IsIntercomPushNotificationResult.swift +16 -0
- package/ios/Plugin/Enums/CustomError.swift +75 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Intercom.swift +323 -0
- package/ios/Plugin/IntercomHelper.swift +17 -0
- package/ios/Plugin/IntercomPlugin.swift +261 -0
- package/ios/Plugin/Protocols/Result.swift +5 -0
- package/package.json +105 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import Intercom
|
|
4
|
+
|
|
5
|
+
@objc public class IntercomImpl: NSObject {
|
|
6
|
+
private var initialized = false
|
|
7
|
+
private let plugin: IntercomPlugin
|
|
8
|
+
|
|
9
|
+
init(plugin: IntercomPlugin) {
|
|
10
|
+
self.plugin = plugin
|
|
11
|
+
super.init()
|
|
12
|
+
NotificationCenter.default.addObserver(
|
|
13
|
+
self,
|
|
14
|
+
selector: #selector(handleUnreadConversationCountDidChange),
|
|
15
|
+
name: NSNotification.Name.IntercomUnreadConversationCountDidChange,
|
|
16
|
+
object: nil
|
|
17
|
+
)
|
|
18
|
+
NotificationCenter.default.addObserver(
|
|
19
|
+
self,
|
|
20
|
+
selector: #selector(handleWindowDidShow),
|
|
21
|
+
name: NSNotification.Name.IntercomWindowDidShow,
|
|
22
|
+
object: nil
|
|
23
|
+
)
|
|
24
|
+
NotificationCenter.default.addObserver(
|
|
25
|
+
self,
|
|
26
|
+
selector: #selector(handleWindowDidHide),
|
|
27
|
+
name: NSNotification.Name.IntercomWindowDidHide,
|
|
28
|
+
object: nil
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
deinit {
|
|
33
|
+
NotificationCenter.default.removeObserver(self)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@objc public func getUnreadConversationCount(completion: @escaping (_ result: GetUnreadConversationCountResult?, _ error: Error?) -> Void) {
|
|
37
|
+
guard initialized else {
|
|
38
|
+
completion(nil, CustomError.notInitialized)
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
let count = Int(Intercom.unreadConversationCount())
|
|
42
|
+
completion(GetUnreadConversationCountResult(count: count), nil)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@objc public func handlePushNotification(_ options: HandlePushNotificationOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
46
|
+
Intercom.handlePushNotification(options.data)
|
|
47
|
+
completion(nil)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@objc public func hide(completion: @escaping (_ error: Error?) -> Void) {
|
|
51
|
+
guard initialized else {
|
|
52
|
+
completion(CustomError.notInitialized)
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
DispatchQueue.main.async {
|
|
56
|
+
Intercom.hide()
|
|
57
|
+
completion(nil)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@objc public func initialize(_ options: InitializeOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
62
|
+
guard let iosApiKey = options.iosApiKey else {
|
|
63
|
+
completion(CustomError.iosApiKeyMissing)
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
Intercom.setApiKey(iosApiKey, forAppId: options.appId)
|
|
67
|
+
initialized = true
|
|
68
|
+
completion(nil)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@objc public func isIntercomPushNotification(_ options: IsIntercomPushNotificationOptions, completion: @escaping (_ result: IsIntercomPushNotificationResult?, _ error: Error?) -> Void) {
|
|
72
|
+
let isIntercom = Intercom.isIntercomPushNotification(options.data)
|
|
73
|
+
completion(IsIntercomPushNotificationResult(intercom: isIntercom), nil)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@objc public func logEvent(_ options: LogEventOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
77
|
+
guard initialized else {
|
|
78
|
+
completion(CustomError.notInitialized)
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
if let data = options.data {
|
|
82
|
+
Intercom.logEvent(withName: options.name, metaData: data)
|
|
83
|
+
} else {
|
|
84
|
+
Intercom.logEvent(withName: options.name)
|
|
85
|
+
}
|
|
86
|
+
completion(nil)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@objc public func loginUnidentifiedUser(completion: @escaping (_ error: Error?) -> Void) {
|
|
90
|
+
guard initialized else {
|
|
91
|
+
completion(CustomError.notInitialized)
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
Intercom.loginUnidentifiedUser { result in
|
|
95
|
+
switch result {
|
|
96
|
+
case .success:
|
|
97
|
+
completion(nil)
|
|
98
|
+
case .failure(let error):
|
|
99
|
+
completion(CustomError.loginFailed(message: error.localizedDescription))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@objc public func loginUser(_ options: LoginUserOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
105
|
+
guard initialized else {
|
|
106
|
+
completion(CustomError.notInitialized)
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
let attributes = ICMUserAttributes()
|
|
110
|
+
attributes.userId = options.userId
|
|
111
|
+
attributes.email = options.email
|
|
112
|
+
Intercom.loginUser(with: attributes) { result in
|
|
113
|
+
switch result {
|
|
114
|
+
case .success:
|
|
115
|
+
completion(nil)
|
|
116
|
+
case .failure(let error):
|
|
117
|
+
completion(CustomError.loginFailed(message: error.localizedDescription))
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@objc public func logout(completion: @escaping (_ error: Error?) -> Void) {
|
|
123
|
+
guard initialized else {
|
|
124
|
+
completion(CustomError.notInitialized)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
Intercom.logout()
|
|
128
|
+
completion(nil)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@objc public func present(_ options: PresentOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
132
|
+
guard initialized else {
|
|
133
|
+
completion(CustomError.notInitialized)
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
DispatchQueue.main.async {
|
|
137
|
+
Intercom.present(self.mapSpace(options.space))
|
|
138
|
+
completion(nil)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@objc public func presentContent(_ options: PresentContentOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
143
|
+
guard initialized else {
|
|
144
|
+
completion(CustomError.notInitialized)
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
guard let content = mapContent(options) else {
|
|
148
|
+
completion(CustomError.typeInvalid)
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
DispatchQueue.main.async {
|
|
152
|
+
Intercom.presentContent(content)
|
|
153
|
+
completion(nil)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@objc public func presentMessageComposer(_ options: PresentMessageComposerOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
158
|
+
guard initialized else {
|
|
159
|
+
completion(CustomError.notInitialized)
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
DispatchQueue.main.async {
|
|
163
|
+
Intercom.presentMessageComposer(options.initialMessage)
|
|
164
|
+
completion(nil)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@objc public func sendPushTokenToIntercom(_ options: SendPushTokenToIntercomOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
169
|
+
let deviceToken = IntercomHelper.hexStringToData(options.token)
|
|
170
|
+
Intercom.setDeviceToken(deviceToken) { result in
|
|
171
|
+
switch result {
|
|
172
|
+
case .success:
|
|
173
|
+
completion(nil)
|
|
174
|
+
case .failure(let error):
|
|
175
|
+
completion(error)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@objc public func setBottomPadding(_ options: SetBottomPaddingOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
181
|
+
DispatchQueue.main.async {
|
|
182
|
+
Intercom.setBottomPadding(options.padding)
|
|
183
|
+
completion(nil)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
@objc public func setInAppMessagesVisible(_ options: SetInAppMessagesVisibleOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
188
|
+
Intercom.setInAppMessagesVisible(options.visible)
|
|
189
|
+
completion(nil)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
@objc public func setLauncherVisible(_ options: SetLauncherVisibleOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
193
|
+
Intercom.setLauncherVisible(options.visible)
|
|
194
|
+
completion(nil)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
@objc public func setUserHash(_ options: SetUserHashOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
198
|
+
guard initialized else {
|
|
199
|
+
completion(CustomError.notInitialized)
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
Intercom.setUserHash(options.userHash)
|
|
203
|
+
completion(nil)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@objc public func setUserJwt(_ options: SetUserJwtOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
207
|
+
guard initialized else {
|
|
208
|
+
completion(CustomError.notInitialized)
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
Intercom.setUserJwt(options.jwt)
|
|
212
|
+
completion(nil)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
@objc public func updateUser(_ options: UpdateUserOptions, completion: @escaping (_ error: Error?) -> Void) {
|
|
216
|
+
guard initialized else {
|
|
217
|
+
completion(CustomError.notInitialized)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
let attributes: ICMUserAttributes
|
|
221
|
+
do {
|
|
222
|
+
attributes = try buildUserAttributes(options)
|
|
223
|
+
} catch {
|
|
224
|
+
completion(error)
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
Intercom.updateUser(with: attributes) { result in
|
|
228
|
+
switch result {
|
|
229
|
+
case .success:
|
|
230
|
+
completion(nil)
|
|
231
|
+
case .failure(let error):
|
|
232
|
+
completion(CustomError.updateFailed(message: error.localizedDescription))
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private func buildCompany(_ company: JSObject) throws -> ICMCompany {
|
|
238
|
+
guard let id = company["id"] as? String else {
|
|
239
|
+
throw CustomError.companyIdMissing
|
|
240
|
+
}
|
|
241
|
+
let result = ICMCompany()
|
|
242
|
+
result.companyId = id
|
|
243
|
+
if let name = company["name"] as? String {
|
|
244
|
+
result.name = name
|
|
245
|
+
}
|
|
246
|
+
if let plan = company["plan"] as? String {
|
|
247
|
+
result.plan = plan
|
|
248
|
+
}
|
|
249
|
+
if let monthlySpend = company["monthlySpend"] as? NSNumber {
|
|
250
|
+
result.monthlySpend = monthlySpend
|
|
251
|
+
}
|
|
252
|
+
if let createdAt = company["createdAt"] as? NSNumber {
|
|
253
|
+
result.createdAt = Date(timeIntervalSince1970: createdAt.doubleValue)
|
|
254
|
+
}
|
|
255
|
+
if let customAttributes = company["customAttributes"] as? JSObject {
|
|
256
|
+
result.customAttributes = customAttributes
|
|
257
|
+
}
|
|
258
|
+
return result
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private func buildUserAttributes(_ options: UpdateUserOptions) throws -> ICMUserAttributes {
|
|
262
|
+
let attributes = ICMUserAttributes()
|
|
263
|
+
attributes.name = options.name
|
|
264
|
+
attributes.email = options.email
|
|
265
|
+
attributes.userId = options.userId
|
|
266
|
+
attributes.phone = options.phone
|
|
267
|
+
attributes.languageOverride = options.languageOverride
|
|
268
|
+
attributes.signedUpAt = options.signedUpAt
|
|
269
|
+
if let unsubscribedFromEmails = options.unsubscribedFromEmails {
|
|
270
|
+
attributes.unsubscribedFromEmails = unsubscribedFromEmails
|
|
271
|
+
}
|
|
272
|
+
if let customAttributes = options.customAttributes {
|
|
273
|
+
attributes.customAttributes = customAttributes
|
|
274
|
+
}
|
|
275
|
+
if let companies = options.companies {
|
|
276
|
+
attributes.companies = try companies.map { try buildCompany($0) }
|
|
277
|
+
}
|
|
278
|
+
return attributes
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
@objc private func handleUnreadConversationCountDidChange() {
|
|
282
|
+
let count = Int(Intercom.unreadConversationCount())
|
|
283
|
+
plugin.notifyUnreadConversationCountChangeListeners(UnreadConversationCountChangeEvent(count: count))
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
@objc private func handleWindowDidHide() {
|
|
287
|
+
plugin.notifyMessengerHiddenListeners()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
@objc private func handleWindowDidShow() {
|
|
291
|
+
plugin.notifyMessengerShownListeners()
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private func mapContent(_ options: PresentContentOptions) -> Intercom.Content? {
|
|
295
|
+
switch options.type {
|
|
296
|
+
case "article":
|
|
297
|
+
return .article(id: options.id ?? "")
|
|
298
|
+
case "carousel":
|
|
299
|
+
return .carousel(id: options.id ?? "")
|
|
300
|
+
case "survey":
|
|
301
|
+
return .survey(id: options.id ?? "")
|
|
302
|
+
case "conversation":
|
|
303
|
+
return .conversation(id: options.id ?? "")
|
|
304
|
+
case "help-center-collections":
|
|
305
|
+
return .helpCenterCollections(ids: options.ids ?? [])
|
|
306
|
+
default:
|
|
307
|
+
return nil
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private func mapSpace(_ space: String) -> Space {
|
|
312
|
+
switch space {
|
|
313
|
+
case "messages":
|
|
314
|
+
return .messages
|
|
315
|
+
case "help-center":
|
|
316
|
+
return .helpCenter
|
|
317
|
+
case "tickets":
|
|
318
|
+
return .tickets
|
|
319
|
+
default:
|
|
320
|
+
return .home
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public class IntercomHelper {
|
|
4
|
+
public static func hexStringToData(_ hexString: String) -> Data {
|
|
5
|
+
var data = Data()
|
|
6
|
+
var index = hexString.startIndex
|
|
7
|
+
while index < hexString.endIndex {
|
|
8
|
+
let nextIndex = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex) ?? hexString.endIndex
|
|
9
|
+
let byteString = hexString[index..<nextIndex]
|
|
10
|
+
if let byte = UInt8(byteString, radix: 16) {
|
|
11
|
+
data.append(byte)
|
|
12
|
+
}
|
|
13
|
+
index = nextIndex
|
|
14
|
+
}
|
|
15
|
+
return data
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc(IntercomPlugin)
|
|
5
|
+
public class IntercomPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
+
public let identifier = "IntercomPlugin"
|
|
7
|
+
public let jsName = "Intercom"
|
|
8
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
9
|
+
CAPPluginMethod(name: "getUnreadConversationCount", returnType: CAPPluginReturnPromise),
|
|
10
|
+
CAPPluginMethod(name: "handlePushNotification", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "hide", returnType: CAPPluginReturnPromise),
|
|
12
|
+
CAPPluginMethod(name: "initialize", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "isIntercomPushNotification", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "logEvent", returnType: CAPPluginReturnPromise),
|
|
15
|
+
CAPPluginMethod(name: "loginUnidentifiedUser", returnType: CAPPluginReturnPromise),
|
|
16
|
+
CAPPluginMethod(name: "loginUser", returnType: CAPPluginReturnPromise),
|
|
17
|
+
CAPPluginMethod(name: "logout", returnType: CAPPluginReturnPromise),
|
|
18
|
+
CAPPluginMethod(name: "present", returnType: CAPPluginReturnPromise),
|
|
19
|
+
CAPPluginMethod(name: "presentContent", returnType: CAPPluginReturnPromise),
|
|
20
|
+
CAPPluginMethod(name: "presentMessageComposer", returnType: CAPPluginReturnPromise),
|
|
21
|
+
CAPPluginMethod(name: "sendPushTokenToIntercom", returnType: CAPPluginReturnPromise),
|
|
22
|
+
CAPPluginMethod(name: "setBottomPadding", returnType: CAPPluginReturnPromise),
|
|
23
|
+
CAPPluginMethod(name: "setInAppMessagesVisible", returnType: CAPPluginReturnPromise),
|
|
24
|
+
CAPPluginMethod(name: "setLauncherVisible", returnType: CAPPluginReturnPromise),
|
|
25
|
+
CAPPluginMethod(name: "setUserHash", returnType: CAPPluginReturnPromise),
|
|
26
|
+
CAPPluginMethod(name: "setUserJwt", returnType: CAPPluginReturnPromise),
|
|
27
|
+
CAPPluginMethod(name: "updateUser", returnType: CAPPluginReturnPromise)
|
|
28
|
+
]
|
|
29
|
+
public static let eventMessengerHidden = "messengerHidden"
|
|
30
|
+
public static let eventMessengerShown = "messengerShown"
|
|
31
|
+
public static let eventUnreadConversationCountChange = "unreadConversationCountChange"
|
|
32
|
+
public let tag = "Intercom"
|
|
33
|
+
private var implementation: IntercomImpl?
|
|
34
|
+
|
|
35
|
+
override public func load() {
|
|
36
|
+
super.load()
|
|
37
|
+
self.implementation = IntercomImpl(plugin: self)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@objc func getUnreadConversationCount(_ call: CAPPluginCall) {
|
|
41
|
+
implementation?.getUnreadConversationCount(completion: { result, error in
|
|
42
|
+
if let error = error {
|
|
43
|
+
self.rejectCall(call, error)
|
|
44
|
+
} else {
|
|
45
|
+
self.resolveCall(call, result)
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@objc func handlePushNotification(_ call: CAPPluginCall) {
|
|
51
|
+
do {
|
|
52
|
+
let options = try HandlePushNotificationOptions(call)
|
|
53
|
+
implementation?.handlePushNotification(options, completion: { error in
|
|
54
|
+
self.handleCompletion(call, error)
|
|
55
|
+
})
|
|
56
|
+
} catch {
|
|
57
|
+
rejectCall(call, error)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@objc func hide(_ call: CAPPluginCall) {
|
|
62
|
+
implementation?.hide(completion: { error in
|
|
63
|
+
self.handleCompletion(call, error)
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@objc func initialize(_ call: CAPPluginCall) {
|
|
68
|
+
do {
|
|
69
|
+
let options = try InitializeOptions(call)
|
|
70
|
+
implementation?.initialize(options, completion: { error in
|
|
71
|
+
self.handleCompletion(call, error)
|
|
72
|
+
})
|
|
73
|
+
} catch {
|
|
74
|
+
rejectCall(call, error)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@objc func isIntercomPushNotification(_ call: CAPPluginCall) {
|
|
79
|
+
do {
|
|
80
|
+
let options = try IsIntercomPushNotificationOptions(call)
|
|
81
|
+
implementation?.isIntercomPushNotification(options, completion: { result, error in
|
|
82
|
+
if let error = error {
|
|
83
|
+
self.rejectCall(call, error)
|
|
84
|
+
} else {
|
|
85
|
+
self.resolveCall(call, result)
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
} catch {
|
|
89
|
+
rejectCall(call, error)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
@objc func logEvent(_ call: CAPPluginCall) {
|
|
94
|
+
do {
|
|
95
|
+
let options = try LogEventOptions(call)
|
|
96
|
+
implementation?.logEvent(options, completion: { error in
|
|
97
|
+
self.handleCompletion(call, error)
|
|
98
|
+
})
|
|
99
|
+
} catch {
|
|
100
|
+
rejectCall(call, error)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@objc func loginUnidentifiedUser(_ call: CAPPluginCall) {
|
|
105
|
+
implementation?.loginUnidentifiedUser(completion: { error in
|
|
106
|
+
self.handleCompletion(call, error)
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@objc func loginUser(_ call: CAPPluginCall) {
|
|
111
|
+
do {
|
|
112
|
+
let options = try LoginUserOptions(call)
|
|
113
|
+
implementation?.loginUser(options, completion: { error in
|
|
114
|
+
self.handleCompletion(call, error)
|
|
115
|
+
})
|
|
116
|
+
} catch {
|
|
117
|
+
rejectCall(call, error)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@objc func logout(_ call: CAPPluginCall) {
|
|
122
|
+
implementation?.logout(completion: { error in
|
|
123
|
+
self.handleCompletion(call, error)
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public func notifyMessengerHiddenListeners() {
|
|
128
|
+
notifyListeners(Self.eventMessengerHidden, data: [:])
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public func notifyMessengerShownListeners() {
|
|
132
|
+
notifyListeners(Self.eventMessengerShown, data: [:])
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public func notifyUnreadConversationCountChangeListeners(_ event: UnreadConversationCountChangeEvent) {
|
|
136
|
+
notifyListeners(Self.eventUnreadConversationCountChange, data: event.toJSObject() as? [String: Any])
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@objc func present(_ call: CAPPluginCall) {
|
|
140
|
+
let options = PresentOptions(call)
|
|
141
|
+
implementation?.present(options, completion: { error in
|
|
142
|
+
self.handleCompletion(call, error)
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
@objc func presentContent(_ call: CAPPluginCall) {
|
|
147
|
+
do {
|
|
148
|
+
let options = try PresentContentOptions(call)
|
|
149
|
+
implementation?.presentContent(options, completion: { error in
|
|
150
|
+
self.handleCompletion(call, error)
|
|
151
|
+
})
|
|
152
|
+
} catch {
|
|
153
|
+
rejectCall(call, error)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@objc func presentMessageComposer(_ call: CAPPluginCall) {
|
|
158
|
+
let options = PresentMessageComposerOptions(call)
|
|
159
|
+
implementation?.presentMessageComposer(options, completion: { error in
|
|
160
|
+
self.handleCompletion(call, error)
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@objc func sendPushTokenToIntercom(_ call: CAPPluginCall) {
|
|
165
|
+
do {
|
|
166
|
+
let options = try SendPushTokenToIntercomOptions(call)
|
|
167
|
+
implementation?.sendPushTokenToIntercom(options, completion: { error in
|
|
168
|
+
self.handleCompletion(call, error)
|
|
169
|
+
})
|
|
170
|
+
} catch {
|
|
171
|
+
rejectCall(call, error)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@objc func setBottomPadding(_ call: CAPPluginCall) {
|
|
176
|
+
do {
|
|
177
|
+
let options = try SetBottomPaddingOptions(call)
|
|
178
|
+
implementation?.setBottomPadding(options, completion: { error in
|
|
179
|
+
self.handleCompletion(call, error)
|
|
180
|
+
})
|
|
181
|
+
} catch {
|
|
182
|
+
rejectCall(call, error)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@objc func setInAppMessagesVisible(_ call: CAPPluginCall) {
|
|
187
|
+
do {
|
|
188
|
+
let options = try SetInAppMessagesVisibleOptions(call)
|
|
189
|
+
implementation?.setInAppMessagesVisible(options, completion: { error in
|
|
190
|
+
self.handleCompletion(call, error)
|
|
191
|
+
})
|
|
192
|
+
} catch {
|
|
193
|
+
rejectCall(call, error)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
@objc func setLauncherVisible(_ call: CAPPluginCall) {
|
|
198
|
+
do {
|
|
199
|
+
let options = try SetLauncherVisibleOptions(call)
|
|
200
|
+
implementation?.setLauncherVisible(options, completion: { error in
|
|
201
|
+
self.handleCompletion(call, error)
|
|
202
|
+
})
|
|
203
|
+
} catch {
|
|
204
|
+
rejectCall(call, error)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@objc func setUserHash(_ call: CAPPluginCall) {
|
|
209
|
+
do {
|
|
210
|
+
let options = try SetUserHashOptions(call)
|
|
211
|
+
implementation?.setUserHash(options, completion: { error in
|
|
212
|
+
self.handleCompletion(call, error)
|
|
213
|
+
})
|
|
214
|
+
} catch {
|
|
215
|
+
rejectCall(call, error)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
@objc func setUserJwt(_ call: CAPPluginCall) {
|
|
220
|
+
do {
|
|
221
|
+
let options = try SetUserJwtOptions(call)
|
|
222
|
+
implementation?.setUserJwt(options, completion: { error in
|
|
223
|
+
self.handleCompletion(call, error)
|
|
224
|
+
})
|
|
225
|
+
} catch {
|
|
226
|
+
rejectCall(call, error)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
@objc func updateUser(_ call: CAPPluginCall) {
|
|
231
|
+
let options = UpdateUserOptions(call)
|
|
232
|
+
implementation?.updateUser(options, completion: { error in
|
|
233
|
+
self.handleCompletion(call, error)
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private func handleCompletion(_ call: CAPPluginCall, _ error: Error?) {
|
|
238
|
+
if let error = error {
|
|
239
|
+
rejectCall(call, error)
|
|
240
|
+
} else {
|
|
241
|
+
resolveCall(call)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
|
|
246
|
+
CAPLog.print("[", self.tag, "] ", error)
|
|
247
|
+
call.reject(error.localizedDescription, (error as? CustomError)?.code)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private func resolveCall(_ call: CAPPluginCall) {
|
|
251
|
+
call.resolve()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private func resolveCall(_ call: CAPPluginCall, _ result: Result?) {
|
|
255
|
+
if let result = result?.toJSObject() as? JSObject {
|
|
256
|
+
call.resolve(result)
|
|
257
|
+
} else {
|
|
258
|
+
call.resolve()
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|