-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
291 lines (272 loc) · 8.04 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
Category,
ClientDevice,
ConsoleLogger,
Credentials,
CustomUserAgentDetails,
getAmplifyUserAgent,
InAppMessagingAction,
PushNotificationAction,
StorageHelper,
transferKeyToUpperCase,
} from '@aws-amplify/core';
import { Cache } from '@aws-amplify/cache';
import {
Event as AWSPinpointAnalyticsEvent,
putEvents,
PutEventsInput,
updateEndpoint,
UpdateEndpointInput,
} from '@aws-amplify/core/internals/aws-clients/pinpoint';
import { v4 as uuid } from 'uuid';
import {
NotificationsCategory,
NotificationsSubCategory,
NotificationsProvider,
UserInfo,
} from '../../types';
import { AWSPinpointUserInfo } from './types';
export default abstract class AWSPinpointProviderCommon
implements NotificationsProvider
{
static category: NotificationsCategory = 'Notifications';
static providerName = 'AWSPinpoint';
protected clientInfo;
protected config: Record<string, any> = {};
protected endpointInitialized = false;
protected initialized = false;
protected logger: ConsoleLogger;
constructor(logger) {
this.config = { storage: new StorageHelper().getStorage() };
this.clientInfo = ClientDevice.clientInfo() ?? {};
this.logger = logger;
}
/**
* get the category of the plugin
*/
getCategory() {
return AWSPinpointProviderCommon.category;
}
/**
* get the sub-category of the plugin
*/
abstract getSubCategory(): NotificationsSubCategory;
/**
* get provider name of the plugin
*/
getProviderName(): string {
return AWSPinpointProviderCommon.providerName;
}
configure(config = {}): Record<string, any> {
this.config = { ...this.config, ...config };
this.logger.debug(
`configure ${this.getProviderName()}${this.getSubCategory()}Provider`,
this.config
);
return this.config;
}
identifyUser = async (userId: string, userInfo: UserInfo): Promise<void> => {
if (!this.initialized) {
await this.init();
}
try {
await this.updateEndpoint(userId, userInfo);
} catch (err) {
this.logger.error('Error identifying user', err);
throw err;
}
};
protected init = async (): Promise<void> => {
const { endpointId, storage } = this.config;
const providerName = this.getProviderName();
try {
// Only run sync() if it's available (i.e. React Native)
if (typeof storage.sync === 'function') {
await storage.sync();
}
// If an endpoint was not provided via configuration, try to get it from cache
if (!endpointId) {
this.config.endpointId = await this.getEndpointId();
}
this.initialized = true;
} catch (err) {
this.logger.error(`Failed to initialize ${providerName}`, err);
}
};
private getUserAgentValue = (): string => {
let customUserAgentDetails: CustomUserAgentDetails;
if (this.getSubCategory() === 'PushNotification') {
customUserAgentDetails = {
category: Category.PushNotification,
action: PushNotificationAction.None,
};
} else {
customUserAgentDetails = {
category: Category.InAppMessaging,
action: InAppMessagingAction.None,
};
}
return getAmplifyUserAgent(customUserAgentDetails);
};
protected recordAnalyticsEvent = async (
event: AWSPinpointAnalyticsEvent
): Promise<void> => {
// Update credentials
this.config.credentials = await this.getCredentials();
// Assert required configuration properties to make `putEvents` request are present
this.assertNotEmptyConfiguration();
const { appId, credentials, endpointId, region } = this.config;
try {
// Create the PutEvents input
const input: PutEventsInput = {
ApplicationId: appId,
EventsRequest: {
BatchItem: {
[endpointId]: {
Endpoint: {},
Events: {
[uuid()]: event,
},
},
},
},
};
this.logger.debug('recording analytics event');
await putEvents(
{ credentials, region, userAgentValue: this.getUserAgentValue() },
input
);
} catch (err) {
this.logger.error('Error recording analytics event', err);
throw err;
}
};
protected updateEndpoint = async (
userId: string = null,
userInfo: AWSPinpointUserInfo = null
): Promise<void> => {
const credentials = await this.getCredentials();
// Shallow compare to determine if credentials stored here are outdated
const credentialsUpdated =
!this.config.credentials ||
Object.keys(credentials).some(
key => credentials[key] !== this.config.credentials[key]
);
// If endpoint is already initialized, and nothing else is changing, just early return
if (
this.endpointInitialized &&
!credentialsUpdated &&
!userId &&
!userInfo
) {
return;
}
// Update credentials
this.config.credentials = credentials;
// Assert required configuration properties to make `updateEndpoint` request are present
this.assertNotEmptyConfiguration();
const { appId, endpointId, endpointInfo = {}, region } = this.config;
try {
const { address, attributes, demographic, location, metrics, optOut } =
userInfo ?? {};
const { appVersion, make, model, platform, version } = this.clientInfo;
// Create the UpdateEndpoint input, prioritizing passed in user info and falling back to
// defaults (if any) obtained from the config
const input: UpdateEndpointInput = {
ApplicationId: appId,
EndpointId: endpointId,
EndpointRequest: {
RequestId: uuid(),
EffectiveDate: new Date().toISOString(),
ChannelType: endpointInfo.channelType,
Address: address ?? endpointInfo.address,
Attributes: {
...endpointInfo.attributes,
...attributes,
},
Demographic: {
AppVersion: appVersion,
Make: make,
Model: model,
ModelVersion: version,
Platform: platform,
...transferKeyToUpperCase({
...endpointInfo.demographic,
...demographic,
}),
},
Location: transferKeyToUpperCase({
...endpointInfo.location,
...location,
}),
Metrics: {
...endpointInfo.metrics,
...metrics,
},
OptOut: optOut ?? endpointInfo.optOut,
User: {
UserId: userId ?? endpointInfo.userId ?? credentials.identityId,
UserAttributes: attributes ?? endpointInfo.userAttributes,
},
},
};
this.logger.debug('updating endpoint');
await updateEndpoint(
{ credentials, region, userAgentValue: this.getUserAgentValue() },
input
);
this.endpointInitialized = true;
} catch (err) {
throw err;
}
};
private getEndpointId = async (): Promise<string> => {
const { appId } = this.config;
// Each Pinpoint channel requires its own Endpoint ID
// However, Push will share the Analytics endpoint for now so as to not break existing customers
const cacheKey =
this.getSubCategory() === 'PushNotification'
? `${this.getProviderName()}_${appId}`
: `${this.getSubCategory()}:${this.getProviderName()}:${appId}`;
// First attempt to retrieve the ID from cache
const cachedEndpointId = await Cache.getItem(cacheKey);
// Found in cache, just return it
if (cachedEndpointId) {
return cachedEndpointId;
}
// Otherwise, generate a new ID and store it in long-lived cache before returning it
const endpointId = uuid();
// Set a longer TTL to avoid endpoint id being deleted after the default TTL (3 days)
// Also set its priority to the highest to reduce its chance of being deleted when cache is full
const ttl = 1000 * 60 * 60 * 24 * 365 * 100; // 100 years
const expiration = new Date().getTime() + ttl;
Cache.setItem(cacheKey, endpointId, {
expires: expiration,
priority: 1,
});
return endpointId;
};
private getCredentials = async () => {
try {
const credentials = await Credentials.get();
if (!credentials) {
this.logger.debug('no credentials found');
return null;
}
return Credentials.shear(credentials);
} catch (err) {
this.logger.error('Error getting credentials:', err);
return null;
}
};
private assertNotEmptyConfiguration = () => {
const { appId, credentials, region } = this.config;
if (!appId || !credentials || !region) {
throw new Error(
'One or more of credentials, appId or region is not configured'
);
}
};
}