Announcements: Show snackbar on event and enhanced comment notification messages

This commit is contained in:
2026-03-21 19:28:18 +08:00
parent 3fb6fd5c93
commit beb21e48d0
4 changed files with 195 additions and 36 deletions
+54 -3
View File
@@ -207,15 +207,17 @@ class AnnouncementsController {
// Notify announcement author + previous commenters
try {
// Get the announcement author
// Get the announcement author and title
final announcement = await _client
.from('announcements')
.select('author_id')
.select('author_id, title')
.eq('id', announcementId)
.maybeSingle();
if (announcement == null) return;
final announcementAuthorId = announcement['author_id'] as String;
final announcementTitle =
announcement['title'] as String? ?? 'an announcement';
// Get all unique commenters on this announcement
final comments = await _client
@@ -233,13 +235,22 @@ class AnnouncementsController {
if (notifyIds.isEmpty) return;
// Fetch commenter's display name for a human-readable push body
final commenterData = await _client
.from('profiles')
.select('full_name')
.eq('id', authorId)
.maybeSingle();
final commenterName =
commenterData?['full_name'] as String? ?? 'Someone';
await _notifCtrl.createNotification(
userIds: notifyIds,
type: 'announcement_comment',
actorId: authorId,
fields: {'announcement_id': announcementId},
pushTitle: 'New Comment',
pushBody: body.length > 100 ? '${body.substring(0, 100)}...' : body,
pushBody: '$commenterName commented on "$announcementTitle"',
pushData: {
'announcement_id': announcementId,
'navigate_to': '/announcements',
@@ -251,6 +262,46 @@ class AnnouncementsController {
}
}
/// Re-sends push notifications for an existing announcement.
/// Intended for use by admins or the announcement author.
Future<void> resendAnnouncementNotification(
Announcement announcement) async {
final actorId = _client.auth.currentUser?.id;
if (actorId == null) return;
if (announcement.visibleRoles.isEmpty) return;
try {
final profiles = await _client
.from('profiles')
.select('id')
.inFilter('role', announcement.visibleRoles);
final userIds = (profiles as List)
.map((p) => p['id'] as String)
.where((id) => id != actorId)
.toList();
if (userIds.isEmpty) return;
await _notifCtrl.createNotification(
userIds: userIds,
type: 'announcement',
actorId: actorId,
fields: {'announcement_id': announcement.id},
pushTitle: 'Announcement',
pushBody: announcement.title.length > 100
? '${announcement.title.substring(0, 100)}...'
: announcement.title,
pushData: {
'announcement_id': announcement.id,
'navigate_to': '/announcements',
},
);
} catch (e) {
debugPrint('AnnouncementsController: resend notification error: $e');
throw AnnouncementNotificationException(e.toString());
}
}
/// Delete a comment.
Future<void> deleteComment(String id) async {
await _client.from('announcement_comments').delete().eq('id', id);