-
Notifications
You must be signed in to change notification settings - Fork 104
/
payload_data.dart
279 lines (257 loc) · 8.27 KB
/
payload_data.dart
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
import 'package:bluebubbles/helpers/helpers.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class PayloadData {
PayloadData({
required this.type,
this.urlData,
this.appData,
});
PayloadType type;
List<UrlPreviewData>? urlData;
List<iMessageAppData>? appData;
factory PayloadData.fromJson(dynamic json) {
if (json is Map) {
return PayloadData(
type: PayloadType.values[json["type"]],
urlData: json["urlData"]?.map((e) => UrlPreviewData.fromJson(e)).toList().cast<UrlPreviewData>(),
appData: json["appData"]?.map((e) => iMessageAppData.fromJson(e)).toList().cast<iMessageAppData>(),
);
} else {
List data = [];
final sanitized = replaceDollar(json);
if (sanitized.first["objects"][1] is Map && sanitized.first["objects"][1].containsKey("NS.keys")) {
data = extractUIDs(sanitized, []);
return PayloadData(
type: PayloadType.app,
appData: data.map((e) => iMessageAppData.fromJson(Map<String, dynamic>.from(e))).toList(),
);
} else {
for (Map m in sanitized) {
final objects = m['objects'];
data.add(extractUIDs(objects[2], objects));
}
return PayloadData(
type: PayloadType.url,
urlData: data.map((e) => UrlPreviewData.fromJson(Map<String, dynamic>.from(e))).toList(),
);
}
}
}
Map<String, dynamic> toJson() => {
"type": type.index,
"urlData": urlData?.map((e) => e.toJson()).toList(),
"appData": appData?.map((e) => e.toJson()).toList(),
};
}
class UrlPreviewData {
UrlPreviewData({
this.imageMetadata,
this.itemType,
this.videoMetadata,
this.iconMetadata,
this.originalUrl,
this.url,
this.title,
this.summary,
this.siteName,
});
MediaMetadata? imageMetadata;
MediaMetadata? videoMetadata;
MediaMetadata? iconMetadata;
String? itemType;
String? originalUrl;
String? url;
String? title;
String? summary;
String? siteName;
factory UrlPreviewData.fromJson(Map<String, dynamic> json) => UrlPreviewData(
imageMetadata: json["imageMetadata"] == null
? (json["specialization"]?["artwork"] != null ? MediaMetadata(size: const Size.square(1), url: json["specialization"]?["artwork"]?["NS.relative"]) : null)
: MediaMetadata.fromJson(Map<String, dynamic>.from(json["imageMetadata"])),
videoMetadata: json["videoMetadata"] == null ? null : MediaMetadata.fromJson(Map<String, dynamic>.from(json["videoMetadata"])),
iconMetadata: json["iconMetadata"] == null ? null : MediaMetadata.fromJson(Map<String, dynamic>.from(json["iconMetadata"])),
itemType: json["itemType"],
originalUrl: json["originalURL"]?["NS.relative"],
url: json["URL"]?["NS.relative"],
title: json["title"] ?? json["specialization"]?["name"],
summary: json["summary"] ?? json["specialization"]?["album"],
siteName: json["siteName"],
);
Map<String, dynamic> toJson() => {
"imageMetadata": imageMetadata?.toJson(),
"videoMetadata": videoMetadata?.toJson(),
"iconMetadata": iconMetadata?.toJson(),
"itemType": itemType,
"originalURL": {
"NS.relative": originalUrl
},
"URL": {
"NS.relative": url,
},
"title": title,
"summary": summary,
"siteName": siteName,
};
}
class MediaMetadata {
MediaMetadata({
this.size,
this.url,
});
Size? size;
String? url;
factory MediaMetadata.fromJson(Map<String, dynamic> json) => MediaMetadata(
size: json["size"] == null ? Size.zero : Size(double.parse(json["size"].split(",").first.toString().numericOnly()), double.parse(json["size"].split(",").last.toString().numericOnly())),
url: json["URL"]?["NS.relative"],
);
Map<String, dynamic> toJson() => {
"size": size.toString(),
"URL": {
"NS.relative": url,
},
};
}
// ignore: camel_case_types
class iMessageAppData {
iMessageAppData({
this.appName,
this.ldText,
this.userInfo,
this.url,
});
String? appName;
String? ldText;
String? url;
UserInfo? userInfo;
factory iMessageAppData.fromJson(Map<String, dynamic> json) => iMessageAppData(
appName: json["an"],
ldText: json["ldtext"],
userInfo: json["userInfo"] == null ? null : UserInfo.fromJson(Map<String, dynamic>.from(json["userInfo"])),
url: json["URL"]?["NS.relative"],
);
Map<String, dynamic> toJson() => {
"an": appName,
"ldtext": ldText,
"URL": {
"NS.relative": url,
},
"userInfo": userInfo?.toJson(),
};
}
class UserInfo {
UserInfo({
this.imageSubtitle,
this.imageTitle,
this.caption,
this.secondarySubcaption,
this.tertiarySubcaption,
this.subcaption,
});
String? imageSubtitle;
String? imageTitle;
String? caption;
String? secondarySubcaption;
String? tertiarySubcaption;
String? subcaption;
factory UserInfo.fromJson(Map<String, dynamic> json) => UserInfo(
imageSubtitle: json["image-subtitle"],
imageTitle: json["image-title"],
caption: json["caption"],
secondarySubcaption: json["secondary-subcaption"],
tertiarySubcaption: json["tertiary-subcaption"],
subcaption: json["subcaption"],
);
Map<String, dynamic> toJson() => {
"image-subtitle": imageSubtitle,
"image-title": imageTitle,
"caption": caption,
"secondary-subcaption": secondarySubcaption,
"tertiary-subcaption": tertiarySubcaption,
"subcaption": subcaption,
};
}
dynamic replaceDollar<T>(T element, {bool isValue = false}) {
// if list, traverse thru each element
if (element is List) {
final newList = [];
for (dynamic item in element) {
newList.add(replaceDollar(item, isValue: true));
}
return newList;
// if map, traverse thru each key & value
} else if (element is Map) {
final newMap = {};
for (MapEntry item in element.entries) {
newMap[replaceDollar(item.key)] = replaceDollar(item.value, isValue: true);
}
return newMap;
// only replace $ at beginning of string
} else if (element is String && !isValue) {
if (element.startsWith("\$")) {
element = element.replaceFirst("\$", "") as T;
}
return element;
}
// we don't care
return element;
}
dynamic extractUIDs<T>(T element, List objects) {
// if list, traverse thru each element and extract data
if (element is List) {
final newList = [];
for (dynamic item in element) {
newList.add(extractUIDs(item, objects));
}
return newList;
} else if (element is Map) {
// if map is {"UID": int}, return the associated object and replace the map
if (element["UID"] != null) {
var item = objects[element["UID"]];
if (item is Map || item is List) {
item = extractUIDs(item, objects);
}
return item;
// if map is nested bplist, extract the data
} else if (element["archiver"] == "NSKeyedArchiver") {
// nested bplist will have its own objects
objects = element['objects'];
// find keys and values and create new map with data
final nsKeys = objects[1]["NS.keys"].map((e) => e["UID"]).toList();
final nsObjects = objects[1]["NS.objects"].map((e) => e["UID"]).toList();
var data = {};
for (int i = 0; i < nsKeys.length; i++) {
data[objects[nsKeys[i]]] = objects[nsObjects[i]];
}
data = extractUIDs(data, objects);
return data;
// if map is {"NS.keys": [...], "NS.objects": [...], ...}
} else if (element.containsKey("NS.keys") && element.containsKey("NS.objects")) {
// fins keys and values from original objects
final nsKeys = element["NS.keys"].map((e) => e["UID"]).toList();
final nsObjects = element["NS.objects"].map((e) => e["UID"]).toList();
// keep the other data items
var data = element;
data.remove("NS.keys");
data.remove("NS.objects");
for (int i = 0; i < nsKeys.length; i++) {
data[objects[nsKeys[i]]] = objects[nsObjects[i]];
}
data = extractUIDs(data, objects);
return data;
// if regular map, extract data from any values
} else {
final newMap = {};
for (MapEntry item in element.entries) {
if (item.value is Map || item.value is List) {
newMap[item.key] = extractUIDs(item.value, objects);
} else {
newMap.addEntries([item]);
}
}
return newMap;
}
}
// we don't care
return element;
}