-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathIterableInAppMessage.ts
More file actions
210 lines (191 loc) · 6.29 KB
/
IterableInAppMessage.ts
File metadata and controls
210 lines (191 loc) · 6.29 KB
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
import { type ViewToken } from 'react-native';
// expo throws an error if this is not imported directly due to circular
// dependencies
// See https://github.com/expo/expo/issues/35100
import { IterableUtil } from '../../core/classes/IterableUtil';
import { IterableInAppTriggerType } from '../enums';
import type { IterableInAppMessageRaw } from '../types';
import { IterableInAppTrigger } from './IterableInAppTrigger';
import { IterableInboxMetadata } from './IterableInboxMetadata';
/**
* Iterable in-app message
*/
export class IterableInAppMessage {
/**
* The ID for the in-app message
*/
readonly messageId: string;
/**
* The campaign ID for this message
*/
readonly campaignId: number;
/**
* Information regarding the triggering of this in-app message
*/
readonly trigger: IterableInAppTrigger;
/**
* When was this message created?
*/
readonly createdAt?: Date;
/**
* When to expire this in-app (`undefined` means do not expire)
*/
readonly expiresAt?: Date;
/**
* Whether to save this message to inbox
*/
readonly saveToInbox: boolean;
/**
* Metadata such as title, subtitle etc. needed to display this in-app message in inbox.
*/
readonly inboxMetadata?: IterableInboxMetadata;
/**
* Custom Payload for this message.
*
* @example
* If the custom payload was the following:
* ```json
* {
* "customDisplay": true,
* "promotionTitle": "Summer Sale",
* "promotionText": "Everything is 50% off."
* }
* ```
* You could use the following code to determine whether to hide/show the message:
* ```typescript
* config.inAppHandler = (message: IterableInAppMessage) => {
* if (message.customPayload.customDisplay == true) {
* return IterableInAppShowResponse.skip
* } else {
* return Iterable.InAppShowResponse.show
* }
* };
* ```
* You could then handle the showing of this message through a custom function. EG:
* ```typescript
* Alert.alert(
* message.customPayload.promotionTitle,
* message.customPayload.promotionText,
* );
* ```
*/
readonly customPayload?: unknown;
/**
* Whether this inbox message has been read
*/
readonly read: boolean;
/**
* The priority value of this in-app message
*/
readonly priorityLevel: number;
/**
* Constructs an instance of IterableInAppMessage.
*
* @param messageId - The unique identifier for the message.
* @param campaignId - The identifier for the campaign associated with the message.
* @param trigger - The trigger that caused the message to be displayed.
* @param createdAt - The date and time when the message was created.
* @param expiresAt - The date and time when the message expires.
* @param saveToInbox - A boolean indicating whether the message should be saved to the inbox.
* @param inboxMetadata - Metadata associated with the inbox message.
* @param customPayload - A custom payload associated with the message.
* @param read - A boolean indicating whether the message has been read.
* @param priorityLevel - The priority level of the message.
*/
constructor(
messageId: string,
campaignId: number,
trigger: IterableInAppTrigger,
createdAt: Date | undefined,
expiresAt: Date | undefined,
saveToInbox: boolean,
inboxMetadata: IterableInboxMetadata | undefined,
customPayload: unknown | undefined,
read: boolean,
priorityLevel: number
) {
this.campaignId = campaignId;
this.messageId = messageId;
this.trigger = trigger;
this.createdAt = createdAt;
this.expiresAt = expiresAt;
this.saveToInbox = saveToInbox;
this.inboxMetadata = inboxMetadata;
this.customPayload = customPayload;
this.read = read;
this.priorityLevel = priorityLevel;
}
/**
* Creates an instance of `IterableInAppMessage` from a given `ViewToken`.
*
* @param viewToken - The `ViewToken` containing the in-app message data.
* @returns A new instance of `IterableInAppMessage` populated with data from the `viewToken`.
*/
static fromViewToken(viewToken: ViewToken) {
const inAppMessage = viewToken?.item?.inAppMessage as IterableInAppMessage;
return new IterableInAppMessage(
inAppMessage?.messageId,
inAppMessage?.campaignId,
inAppMessage?.trigger,
inAppMessage?.createdAt,
inAppMessage?.expiresAt,
inAppMessage?.saveToInbox,
inAppMessage?.inboxMetadata,
inAppMessage?.customPayload,
inAppMessage?.read,
inAppMessage?.priorityLevel
);
}
/**
* Do you want the in-app message to be saved to the inbox without triggering a notification?
*
* @returns `true` if the message should be saved to the inbox without triggering a notification; otherwise, `false`.
*/
isSilentInbox(): boolean {
return (
// MOB-10424: Figure out if this is purposeful
// eslint-disable-next-line eqeqeq
this.saveToInbox && this.trigger.type == IterableInAppTriggerType.never
);
}
/**
* Creates an instance of `IterableInAppMessage` from a dictionary object.
*
* @param dict - The dictionary containing the properties of the in-app message.
* @returns An instance of `IterableInAppMessage` populated with the provided properties.
*/
static fromDict(dict: IterableInAppMessageRaw): IterableInAppMessage {
const messageId = dict.messageId;
const campaignId = dict.campaignId;
const trigger = IterableInAppTrigger.fromDict(dict.trigger);
const createdAt = dict.createdAt
? new Date(dict.createdAt)
: undefined;
const expiresAt = dict.expiresAt
? new Date(dict.expiresAt)
: undefined;
const saveToInbox = IterableUtil.readBoolean(dict, 'saveToInbox');
const inboxMetadataDict = dict.inboxMetadata;
let inboxMetadata: IterableInboxMetadata | undefined;
if (inboxMetadataDict) {
inboxMetadata = IterableInboxMetadata.fromDict(inboxMetadataDict);
} else {
inboxMetadata = undefined;
}
const customPayload = dict.customPayload;
const read = IterableUtil.readBoolean(dict, 'read');
const priorityLevel = dict.priorityLevel ?? 0;
return new IterableInAppMessage(
messageId,
campaignId,
trigger,
createdAt,
expiresAt,
saveToInbox,
inboxMetadata,
customPayload,
read,
priorityLevel
);
}
}