-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathIterableInAppMessage.ts
173 lines (153 loc) · 4.44 KB
/
IterableInAppMessage.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
import { type ViewToken } from 'react-native';
import { IterableUtil } from '../../core';
import { IterableInAppTriggerType } from '../enums';
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;
/**
* when to trigger this in-app
*/
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.
*/
readonly customPayload?: unknown;
/**
* Whether this inbox message has been read
*/
readonly read: boolean;
/**
* the priority value this in-app message has
*/
readonly priorityLevel: number;
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;
}
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
);
}
isSilentInbox(): boolean {
return (
// TODO: Figure out if this is purposeful
// eslint-disable-next-line eqeqeq
this.saveToInbox && this.trigger.type == IterableInAppTriggerType.never
);
}
static fromDict(dict: {
messageId: string;
campaignId: number;
trigger: IterableInAppTrigger;
createdAt: number;
expiresAt: number;
saveToInbox: boolean;
inboxMetadata: {
title: string | undefined;
subtitle: string | undefined;
icon: string | undefined;
};
customPayload: unknown;
read: boolean;
priorityLevel: number;
}): IterableInAppMessage {
const messageId = dict.messageId as string;
const campaignId = dict.campaignId as number;
const trigger = IterableInAppTrigger.fromDict(dict.trigger);
let createdAt = dict.createdAt;
if (createdAt) {
const dateObject = new Date(0);
createdAt = dateObject.setUTCMilliseconds(createdAt);
}
let expiresAt = dict.expiresAt;
if (expiresAt) {
const dateObject = new Date(0);
expiresAt = dateObject.setUTCMilliseconds(expiresAt);
}
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 as number;
return new IterableInAppMessage(
messageId,
campaignId,
trigger,
// TODO: Speak to the team about `IterableInAppMessage` requiring a date
// object, but being passed a number in this case
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
createdAt,
expiresAt,
saveToInbox,
inboxMetadata,
customPayload,
read,
priorityLevel
);
}
}
export default IterableInAppMessage;