diff --git a/lib/models/friendica_entry.dart b/lib/models/friendica_entry.dart new file mode 100644 index 0000000..5735353 --- /dev/null +++ b/lib/models/friendica_entry.dart @@ -0,0 +1,52 @@ +class FriendicaEntry { + final Map originalJson; + final int id; + final String text; + final String url; + final int commentCount; + final List images; + + FriendicaEntry( + {required this.originalJson, + required this.text, + required this.id, + required this.commentCount, + required this.url, + required this.images}); + + FriendicaEntry.fromJson(Map json) + : originalJson = json, + id = json['id'] ?? -1, + text = json['text'] ?? '', + url = json['external_url'] ?? '', + commentCount = _commentCountFromJson(json), + images = _imagesFromJson(json); + + @override + String toString() { + return ''' + FriendicaPost{ + id: $id, + text: $text, + url: $url, + commentCount: $commentCount, + images: $images, + } + '''; + } + + static int _commentCountFromJson(Map json) { + final readCount = json['friendica_comments'] ?? 0; + final count = readCount is int ? readCount : int.tryParse(readCount) ?? 0; + return count; + } + + static List _imagesFromJson(Map json) { + final List attachments = json['attachments'] ?? []; + return attachments + .where((a) => a['mimetype']?.startsWith('image') ?? false) + .map((a) => a['url']?.toString() ?? '') + .where((urlString) => urlString.isNotEmpty) + .toList(); + } +}