fediverse-archiving-tools/bin/models.dart

69 lines
1.8 KiB
Dart
Raw Normal View History

class FriendicaEntry {
final Map<String, dynamic> originalJson;
final int id;
final String text;
final int commentCount;
final List<String> images;
FriendicaEntry(
{required this.originalJson,
required this.text,
required this.id,
required this.commentCount,
required this.images});
FriendicaEntry.fromJson(Map<String, dynamic> json)
: originalJson = json,
id = json['id'] ?? -1,
text = json['text'] ?? '',
commentCount = _commentCountFromJson(json),
images = _imagesFromJson(json);
@override
String toString() {
return '''
FriendicaPost{
id: $id,
text: $text,
commentCount: $commentCount,
images: $images,
}
''';
}
static int _commentCountFromJson(Map<String, dynamic> json) {
final readCount = json['friendica_comments'] ?? 0;
final count = readCount is int ? readCount : int.tryParse(readCount) ?? 0;
return count;
}
static List<String> _imagesFromJson(Map<String, dynamic> json) {
final List<dynamic> attachments = json['attachments'] ?? [];
return attachments
.where((a) => a['mimetype']?.startsWith('image') ?? false)
.map((a) => a['url']?.toString() ?? '')
.where((urlString) => urlString.isNotEmpty)
.toList();
}
}
class ImageEntry {
final String postId;
final String localFilename;
final String url;
ImageEntry(
{required this.postId, required this.localFilename, required this.url});
ImageEntry.fromJson(Map<String, dynamic> json)
: postId = json['postId'] ?? '',
localFilename = json['localFilename'] ?? '',
url = json['url'] ?? '';
Map<String, dynamic> toJson() => {
'postId': postId,
'localFilename': localFilename,
'url': url,
};
}