-
Notifications
You must be signed in to change notification settings - Fork 0
/
hinge-wrapper.ts
342 lines (302 loc) · 7.48 KB
/
hinge-wrapper.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Wrapper class for Hinge Data
*
* Stores a list of Interactions for data analysis and contains
* some methods for filtering Interactions.
*/
export class HingeData {
/**
* All imported Interactions
*/
interactions: Interaction[] = new Array<Interaction>();
/**
* Set up by converting raw data from matches.json to TS objects
*
* @param data Imported data from matches.json
*/
constructor (data: any) {
if (Array.isArray(data)) {
for (let i = 0; i < data.length; i++) {
this.interactions.push(Interaction.fromRaw(data[i]));
}
}
}
/**
* Get the total number of interactions
*
* @returns Number of interactions
*/
totalInteractions(): number {
return this.interactions.length;
}
/**
* Gets all interactions initiated by the user
*
* @returns A list of interactions with sent likes
*/
getSentLikes(): Interaction[] {
return this.interactions.filter(
(interaction) => interaction.isSentLike());
}
/**
* Gets all interactions not initiated by the user
*
* @returns A list of interactions with received likes
*/
getReceivedLikes(): Interaction[] {
return this.interactions.filter(
(interaction) => !interaction.isSentLike());
}
/**
* Gets all matches
*
* @returns A list of interactions with matches
*/
getMatches(): Interaction[] {
return this.interactions.filter(
(interaction) => interaction.isMatch());
}
/**
* Gets all rejections by the user
*
* @returns A list of interactions with rejections
*/
getRejections(): Interaction[] {
return this.interactions.filter(
(interaction) => interaction.isRejection());
}
/**
* Gets all interactions with WeMet set to Yes
*
* @returns A list of interactions with WeMet set to Yes
*/
getDates(): Interaction[] {
return this.interactions.filter(
(interaction) => interaction.isDate());
}
/**
* Gets all sent likes with a comment
*
* @returns A list of interactions with likes including a comment
*/
getSentLikesWithComment(): Interaction[] {
return this.interactions.filter((interaction) =>
interaction.isSentLike() && interaction.like!.comment != '');
}
/**
* Gets all sent likes without a comment
*
* @returns A list of interactions with likes without a comment
*/
getSentLikesWithoutComment(): Interaction[] {
return this.interactions.filter((interaction) =>
interaction.isSentLike() && interaction.like!.comment == '');
}
}
/**
* Wrapper class for interactions (likes, matches)
*/
export class Interaction {
/**
* The timestamp when either user matched
*/
matchDate: Date | null;
/**
* The timestamp when the user hit X or unmatched
*/
removeDate: Date | null;
/**
* Set if the user sent a like
*/
like: Like | null;
/**
* Messages the user sent (does not include comment from Like)
*/
chats: Message[];
/**
* An option the user marked using the We Met feature
*/
weMet: WeMet | null;
constructor (
matchDate: Date | null,
removeDate: Date | null,
like: Like | null,
chats: Message[],
weMet: WeMet | null) {
this.matchDate = matchDate;
this.removeDate = removeDate;
this.like = like;
this.chats = chats;
this.weMet = weMet;
}
/**
* Checks if this interaction started with a like sent
* by the user
*
* @returns True if the interaction includes a like
*/
isSentLike(): boolean {
return this.like != null;
}
/**
* Checks if the user matched with another user.
*
* @returns True if matchDate is set
*/
isMatch(): boolean {
return this.matchDate != null;
}
/**
* Checks if the user hit the X or unmatched.
*
* @returns True if removeDate is set
*/
isRejection(): boolean {
return this.removeDate != null;
}
/**
* Checks if the user notes that they met.
*
* @returns True if weMet is set with a flag of 'Yes'
*/
isDate(): boolean {
return this.weMet != null && this.weMet.didMeet == 'Yes';
}
/**
* Builds an Interaction from raw object data in matches.json
*
* @param data
* @returns A built Interaction object
*/
static fromRaw(data: any) {
let matchTime = null;
if (data.match !== undefined && data.match[0] !== undefined) {
matchTime = data.match[0].timestamp ?? null;
}
let matchDate = matchTime !== null
? new Date(Date.parse(matchTime))
: null;
let removeTime = null;
if (data.block !== undefined && data.block[0] !== undefined) {
removeTime = data.block[0].timestamp ?? null;
}
let removeDate = removeTime !== null
? new Date(Date.parse(removeTime))
: null;
let like = null;
if (data.like !== undefined) {
like = Like.fromRaw(data.like[0]);
}
let chats = new Array<Message>();
if (Array.isArray(data.chats)) {
data.chats.forEach(
(chat : any) => chats.push(Message.fromRaw(chat)));
}
let weMet = null;
if (data.we_met !== undefined) {
weMet = WeMet.fromRaw(data.we_met[0]);
}
return new Interaction(
matchDate,
removeDate,
like,
chats,
weMet
);
}
}
/**
* Wrapper class for a like interaction
*/
export class Like {
/**
* Any comment the user sent with the like
*/
comment: string;
/**
* The recorded time of the Like being sent
*/
timestamp: Date | null;
constructor (comment: string, timestamp: Date | null) {
this.comment = comment;
this.timestamp = timestamp;
}
/**
* Builds a Like from raw object data in matches.json
*
* Note that these are only likes sent by the user as
* received likes are implied
*
* @param data
* @returns A build Like object
*/
static fromRaw(data: any) {
let comment = data.comment ?? '';
let timestamp = data.timestamp === undefined ?
null : new Date(Date.parse(data.timestamp));
return new Like(comment, timestamp);
}
}
/**
* Wrapper class for chat messages
*/
export class Message {
/**
* The text content of the message
*/
text: string;
/**
* When the message was sent
*/
timestamp: Date | null;
constructor (text: string, timestamp: Date | null) {
this.text = text;
this.timestamp = timestamp;
}
/**
* Builds a Message from raw object data in matches.json
*
* Note that these are only messages sent by the user as
* received messages are not downloaded
*
* @param data
* @returns A built Message object
*/
static fromRaw(data: any) {
let text = data.body ?? '';
let date = data.timestamp === undefined ?
null : new Date(Date.parse(data.timestamp));
return new Message(text, date);
}
}
/**
* Wrapper class for WeMet object
*/
export class WeMet {
/**
* Value for WeMet (can be 'Yes', 'No', or 'Not Yet')
*/
didMeet: string;
/**
* Timestamp of user setting the WeMet value
*/
timestamp: Date | null;
constructor (didMeet: string, timestamp: Date | null) {
this.didMeet = didMeet;
this.timestamp = timestamp;
}
/**
* Builds a WeMet object from raw object data in matches.json
*
* These are based on the user's input
*
* @param data
* @returns A built WeMet object
*/
static fromRaw(data: any) {
let didMeet = data.did_meet_subject ?? '';
let date = data.timestamp === undefined ?
null : new Date(Date.parse(data.timestamp));
return new WeMet(didMeet, date);
}
}