Skip to content

Commit

Permalink
Remove images from push notifications, improved UnifiedPush logic (#1400
Browse files Browse the repository at this point in the history
)
  • Loading branch information
micahmo authored Jun 8, 2024
1 parent 92526c7 commit 7502997
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/account/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Account {
userId: userId,
);

String get actorId => 'https://$instance/u/$username';

static Future<Account?> insertAccount(Account account) async {
// If we are given a brand new account to insert with an existing id, something is wrong.
assert(account.id.isEmpty);
Expand Down
3 changes: 2 additions & 1 deletion lib/notification/utils/local_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:thunder/main.dart';
import 'package:thunder/notification/enums/notification_type.dart';
import 'package:thunder/notification/shared/android_notification.dart';
import 'package:thunder/notification/shared/notification_payload.dart';
import 'package:thunder/notification/utils/notification_utils.dart';
import 'package:thunder/utils/instance.dart';

const String _lastPollTimeId = 'thunder_last_notifications_poll_time';
Expand Down Expand Up @@ -97,7 +98,7 @@ Future<void> pollRepliesAndShowNotifications() async {

for (CommentReplyView commentReplyView in replies) {
final String commentContent = cleanCommentContent(commentReplyView.comment);
final String htmlComment = markdownToHtml(commentContent);
final String htmlComment = cleanImagesFromHtml(markdownToHtml(commentContent));
final String plaintextComment = parse(parse(htmlComment).body?.text).documentElement?.text ?? commentContent;

final BigTextStyleInformation bigTextStyleInformation = BigTextStyleInformation(
Expand Down
19 changes: 19 additions & 0 deletions lib/notification/utils/notification_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This Regex should match HTML img tags in the following formats, where the alt text is placed in Group 2
// <img src="URL" />
// <img alt="a" src="URL" />
// <img alt="" src="URL" />
// <img src="URL" alt="" />
// <img src="URL" alt="a" />
//
// See: https://regex101.com/r/rSMP3Z/1
RegExp imgTag = RegExp(r'<img\s+([^>]*\s)?alt="([^"]*)"(?:\s[^>]*)?\/>|<img\s+([^>]*?)\/>');

/// Removes `img` tags from HTML content and replaces them with an italicize version of their alt text, or just the word "image".
String cleanImagesFromHtml(String htmlContent) {
return htmlContent.replaceAllMapped(imgTag, (match) {
if (match.group(2)?.isNotEmpty == true) {
return '<i>${match.group(2)}</i>';
}
return '<i>image</i>';
});
}
12 changes: 8 additions & 4 deletions lib/notification/utils/unified_push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:thunder/comment/utils/comment.dart';
import 'package:thunder/main.dart';
import 'package:thunder/notification/shared/notification_payload.dart';
import 'package:thunder/notification/utils/notification_utils.dart';
import 'package:unifiedpush/unifiedpush.dart';
import 'package:markdown/markdown.dart';

Expand Down Expand Up @@ -97,7 +98,7 @@ void initUnifiedPushNotifications({required StreamController<NotificationRespons
SlimCommentReplyView commentReplyView = SlimCommentReplyView.fromJson(data['reply']);

final String commentContent = cleanComment(commentReplyView.commentContent, commentReplyView.commentRemoved, commentReplyView.commentDeleted);
final String htmlComment = markdownToHtml(commentContent);
final String htmlComment = cleanImagesFromHtml(markdownToHtml(commentContent));
final String plaintextComment = parse(parse(htmlComment).body?.text).documentElement?.text ?? commentContent;

final BigTextStyleInformation bigTextStyleInformation = BigTextStyleInformation(
Expand All @@ -108,7 +109,10 @@ void initUnifiedPushNotifications({required StreamController<NotificationRespons
);

List<Account> accounts = await Account.accounts();
Account account = accounts.firstWhere((Account account) => account.username == commentReplyView.recipientName);
Account account = accounts.firstWhere((Account account) => account.actorId == commentReplyView.recipientActorId);

// Create a notification group for the account
showNotificationGroups(accounts: [account], inboxTypes: [NotificationInboxType.reply], type: NotificationType.unifiedPush);

showAndroidNotification(
id: commentReplyView.commentReplyId,
Expand All @@ -132,7 +136,7 @@ void initUnifiedPushNotifications({required StreamController<NotificationRespons
PersonMentionView personMentionView = PersonMentionView.fromJson(data['mention']);

final String commentContent = cleanCommentContent(personMentionView.comment);
final String htmlComment = markdownToHtml(commentContent);
final String htmlComment = cleanImagesFromHtml(markdownToHtml(commentContent));
final String plaintextComment = parse(parse(htmlComment).body?.text).documentElement?.text ?? commentContent;

final BigTextStyleInformation bigTextStyleInformation = BigTextStyleInformation(
Expand All @@ -143,7 +147,7 @@ void initUnifiedPushNotifications({required StreamController<NotificationRespons
);

List<Account> accounts = await Account.accounts();
Account account = accounts.firstWhere((Account account) => account.username == personMentionView.recipient.name);
Account account = accounts.firstWhere((Account account) => account.actorId == personMentionView.recipient.actorId);

showAndroidNotification(
id: personMentionView.comment.id,
Expand Down
3 changes: 2 additions & 1 deletion lib/shared/media_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flex_color_scheme/flex_color_scheme.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:html/parser.dart';
import 'package:markdown/markdown.dart' hide Text;
import 'package:thunder/notification/utils/notification_utils.dart';

import 'package:thunder/shared/link_information.dart';
import 'package:thunder/utils/colors.dart';
Expand Down Expand Up @@ -103,7 +104,7 @@ class _MediaViewState extends State<MediaView> with SingleTickerProviderStateMix

String? plainTextComment;
if (widget.postViewMedia.postView.post.body?.isNotEmpty == true) {
final String htmlComment = markdownToHtml(widget.postViewMedia.postView.post.body!);
final String htmlComment = cleanImagesFromHtml(markdownToHtml(widget.postViewMedia.postView.post.body!));
plainTextComment = parse(parse(htmlComment).body?.text).documentElement?.text ?? widget.postViewMedia.postView.post.body!;
}

Expand Down

0 comments on commit 7502997

Please sign in to comment.