Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few client-side notifications improvements #1400

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -85,7 +86,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 @@ -96,7 +97,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 @@ -120,7 +124,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 @@ -131,7 +135,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/links.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
Loading