relatica/lib/serializers/mastodon/timeline_entry_mastodon_ext...

133 wiersze
4.9 KiB
Dart

import 'package:logging/logging.dart';
import '../../globals.dart';
import '../../models/engagement_summary.dart';
import '../../models/link_data.dart';
import '../../models/location_data.dart';
import '../../models/timeline_entry.dart';
import '../../models/visibility.dart';
import '../../services/connections_manager.dart';
import '../../services/hashtag_service.dart';
import '../../utils/active_profile_selector.dart';
import '../../utils/dateutils.dart';
import 'connection_mastodon_extensions.dart';
import 'hashtag_mastodon_extensions.dart';
import 'link_preview_mastodon_extensions.dart';
import 'media_attachment_mastodon_extension.dart';
final _logger = Logger('TimelineEntryMastodonExtensions');
extension TimelineEntryMastodonExtensions on TimelineEntry {
static TimelineEntry fromJson(Map<String, dynamic> json) {
final int timestamp = json.containsKey('created_at')
? OffsetDateTimeUtils.epochSecTimeFromTimeZoneString(json['created_at'])
.fold(
onSuccess: (value) => value,
onError: (error) {
_logger.severe("Couldn't read date time string: $error");
return 0;
})
: 0;
final id = json['id'] ?? '';
final youReshared = json['reblogged'] ?? false;
final visibility = ['public', 'unlisted'].contains(json['visibility'])
? Visibility.public()
: Visibility.private();
final parentId = json['in_reply_to_id'] ?? '';
final parentAuthor = json['in_reply_to_account_id'] ?? '';
final parentAuthorId = json['in_reply_to_account_id'] ?? '';
final body = json['content'] ?? '';
final author = json['account']['display_name'];
final authorId = json['account']['id'];
const title = '';
final spoilerText = json['spoiler_text'] ?? '';
final externalLink = json['uri'] ?? '';
final actualLocationData = LocationData();
final modificationTimestamp = timestamp;
final backdatedTimestamp = timestamp;
final isFavorited = json['favourited'] ?? false;
final linkData = json['card'] == null
? <LinkData>[]
: [LinkData.fromMastodonJson(json['card'])];
final mediaAttachments = (json['media_attachments'] as List<dynamic>? ?? [])
.map((json) =>
MediaAttachmentMastodonExtension.fromJson(json, visibility))
.toList();
final favoritesCount = json['favourites_count'] ?? 0;
final repliesCount = json['replies_count'] ?? 0;
final rebloggedCount = json['reblogs_count'] ?? 0;
final engagementSummary = EngagementSummary(
favoritesCount: favoritesCount,
rebloggedCount: rebloggedCount,
repliesCount: repliesCount,
);
final linkPreviewData =
LinkPreviewMastodonExtensions.fromJson(json['card']);
final connectionManager =
getIt<ActiveProfileSelector<ConnectionsManager>>().activeEntry.fold(
onSuccess: (m) => m,
onError: (error) {
_logger.severe('Error getting connection manager: $error');
return null;
});
final connection = ConnectionMastodonExtensions.fromJson(json['account']);
connectionManager?.upsertConnection(connection);
late final String reshareAuthor;
late final String reshareAuthorId;
late final String reshareOriginalPostId;
if (json['reblog'] != null) {
final rebloggedUser =
ConnectionMastodonExtensions.fromJson(json['reblog']['account']);
connectionManager?.upsertConnection(rebloggedUser);
reshareAuthor = rebloggedUser.name;
reshareAuthorId = rebloggedUser.id;
reshareOriginalPostId = json['reblog']['id'] ?? id;
} else {
reshareAuthorId = '';
reshareAuthor = '';
reshareOriginalPostId = '';
}
final List<dynamic>? tagsJson = json['tags'];
final tags = <String>[];
if (tagsJson?.isNotEmpty ?? false) {
final tagManager = getIt<HashtagService>();
for (final tagJson in tagsJson!) {
final tag = HashtagMastodonExtensions.fromJson(tagJson);
tags.add(tag.tag);
tagManager.add(tag);
}
}
return TimelineEntry(
creationTimestamp: timestamp,
modificationTimestamp: modificationTimestamp,
backdatedTimestamp: backdatedTimestamp,
locationData: actualLocationData,
spoilerText: spoilerText,
body: body,
youReshared: youReshared,
visibility: visibility,
id: id,
reshareOriginalPostId: reshareOriginalPostId,
parentId: parentId,
parentAuthorId: parentAuthorId,
reshareAuthor: reshareAuthor,
reshareAuthorId: reshareAuthorId,
isFavorited: isFavorited,
externalLink: externalLink,
author: author,
authorId: authorId,
parentAuthor: parentAuthor,
title: title,
links: linkData,
tags: tags,
mediaAttachments: mediaAttachments,
engagementSummary: engagementSummary,
linkPreviewData: linkPreviewData,
);
}
}