2022-11-30 00:56:14 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
import '../../globals.dart';
|
|
|
|
import '../../models/user_notification.dart';
|
|
|
|
import '../../services/connections_manager.dart';
|
2023-03-14 03:49:52 +00:00
|
|
|
import '../../utils/active_profile_selector.dart';
|
2022-11-30 00:56:14 +00:00
|
|
|
import '../../utils/dateutils.dart';
|
2023-03-21 18:27:38 +00:00
|
|
|
import '../../utils/html_to_edit_text_helper.dart';
|
|
|
|
import '../../utils/string_utils.dart';
|
2022-11-30 00:56:14 +00:00
|
|
|
import 'connection_mastodon_extensions.dart';
|
|
|
|
import 'timeline_entry_mastodon_extensions.dart';
|
|
|
|
|
|
|
|
final _logger = Logger('NotificationMastodonExtension');
|
|
|
|
|
|
|
|
extension NotificationMastodonExtension on UserNotification {
|
|
|
|
static UserNotification 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;
|
|
|
|
|
2022-11-30 19:32:40 +00:00
|
|
|
final type = NotificationType.parse(json['type']);
|
2022-11-30 00:56:14 +00:00
|
|
|
final from = ConnectionMastodonExtensions.fromJson(json['account']);
|
2023-03-12 01:50:31 +00:00
|
|
|
getIt<ActiveProfileSelector<ConnectionsManager>>()
|
|
|
|
.activeEntry
|
2023-04-19 00:33:09 +00:00
|
|
|
.andThenSuccess((manager) => manager.upsertConnection(from));
|
2022-11-30 19:32:40 +00:00
|
|
|
var statusId = '';
|
|
|
|
var statusLink = '';
|
|
|
|
var content = '';
|
|
|
|
switch (type) {
|
|
|
|
case NotificationType.follow:
|
2023-03-21 18:27:38 +00:00
|
|
|
content = '${from.name}(${from.handle}) is now following you';
|
2022-11-30 19:32:40 +00:00
|
|
|
break;
|
|
|
|
case NotificationType.follow_request:
|
2023-03-21 18:27:38 +00:00
|
|
|
content = '${from.name}(${from.handle}) submitted a follow request ';
|
2022-11-30 19:32:40 +00:00
|
|
|
break;
|
|
|
|
case NotificationType.unknown:
|
|
|
|
content = '${from.name} has unknown interaction notification';
|
|
|
|
break;
|
|
|
|
case NotificationType.reshare:
|
2022-12-13 13:29:16 +00:00
|
|
|
case NotificationType.reblog:
|
2023-06-26 21:40:47 +00:00
|
|
|
case NotificationType.favourite:
|
|
|
|
case NotificationType.mention:
|
2022-11-30 19:32:40 +00:00
|
|
|
case NotificationType.status:
|
|
|
|
final status = TimelineEntryMastodonExtensions.fromJson(json['status']);
|
|
|
|
statusId = status.id;
|
|
|
|
statusLink = status.externalLink;
|
2023-01-07 18:01:32 +00:00
|
|
|
final referenceType = type == NotificationType.mention
|
|
|
|
? ''
|
|
|
|
: status.parentId.isEmpty
|
|
|
|
? 'post'
|
|
|
|
: 'comment';
|
|
|
|
final baseContent = type == NotificationType.mention
|
|
|
|
? "${from.name} ${type.toVerb()}"
|
|
|
|
: "${from.name} ${type.toVerb()} ${status.author}'s";
|
2023-03-21 18:27:38 +00:00
|
|
|
final bodyText = htmlToSimpleText(status.body).truncate(length: 100);
|
2023-06-26 21:40:47 +00:00
|
|
|
content = "$baseContent $referenceType: $bodyText";
|
2022-11-30 19:32:40 +00:00
|
|
|
break;
|
2023-03-12 01:50:31 +00:00
|
|
|
case NotificationType.direct_message:
|
|
|
|
// this is a Relatica internal type so nothing to do here
|
|
|
|
break;
|
2022-11-30 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 00:56:14 +00:00
|
|
|
return UserNotification(
|
|
|
|
id: json['id'].toString(),
|
|
|
|
type: type,
|
|
|
|
fromId: from.id,
|
|
|
|
fromName: from.name,
|
|
|
|
fromUrl: from.profileUrl.toString(),
|
|
|
|
timestamp: timestamp,
|
2022-11-30 19:32:40 +00:00
|
|
|
iid: statusId,
|
2022-11-30 00:56:14 +00:00
|
|
|
dismissed: json['dismissed'] ?? false,
|
|
|
|
content: content,
|
2022-11-30 19:32:40 +00:00
|
|
|
link: statusLink,
|
2022-11-30 00:56:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|