Skip to content
Merged
34 changes: 29 additions & 5 deletions lib/src/api/notifications.dart
Comment thread
JollyDevelopment marked this conversation as resolved.
Comment thread
JollyDevelopment marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ class APINotifications {
nextPage: lemmyCalcNextIntPage(result.items, page));

case ServerSoftware.piefed:
throw UnimplementedError();
final path = '/user/notifications';
final status = switch (filter) {
NotificationsFilter.new_ => 'New',
NotificationsFilter.read => 'Read',
NotificationsFilter.all || null => 'All',
};

final query = {'page': page, 'status_request': status};

final response = await client.get(path, queryParams: query);

return NotificationListModel.fromPiefed(response.bodyJson);
Comment thread
JollyDevelopment marked this conversation as resolved.
}
}

Expand All @@ -85,7 +96,11 @@ class APINotifications {
(response.bodyJson['private_messages'] as int);

case ServerSoftware.piefed:
throw UnimplementedError();
const path = '/user/notifications_count';

final response = await client.get(path);

return response.bodyJson['count'] as int;
}
}

Expand All @@ -106,7 +121,11 @@ class APINotifications {
return;

case ServerSoftware.piefed:
throw UnimplementedError();
const path = '/user/mark_all_notifications_read';

final response = await client.put(path);

return;
}
}

Expand All @@ -119,7 +138,6 @@ class APINotifications {
case ServerSoftware.mbin:
final path =
'/notifications/$notificationId/${readState ? 'read' : 'unread'}';

final response = await client.put(path);

return NotificationModel.fromMbin(response.bodyJson);
Expand Down Expand Up @@ -158,7 +176,13 @@ class APINotifications {
};

case ServerSoftware.piefed:
throw UnimplementedError();
final path = '/user/notification_state';

final body = {'notif_id': notificationId, 'read_state': readState};

final response = await client.put(path, body: body);

return NotificationModel.fromPiefed(response.bodyJson);
}
}

Expand Down
33 changes: 33 additions & 0 deletions lib/src/models/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class NotificationListModel with _$NotificationListModel {
],
nextPage: null,
);

factory NotificationListModel.fromPiefed(JsonMap json) =>
NotificationListModel(
items: (json['items'] as List<dynamic>)
.map((notif) => NotificationModel.fromPiefed(notif as JsonMap))
.toList(),
// if next_page is None we have reached the end of the notifications
// so set nextPage to null. Otherwise set it to the next page number
// to request
nextPage: (json['next_page'] as String?) != 'None'
? json['next_page'] as String?
: null);
}

@freezed
Expand Down Expand Up @@ -97,12 +109,25 @@ class NotificationModel with _$NotificationModel {
creator: UserModel.fromLemmy(json['creator'] as JsonMap),
);
}

factory NotificationModel.fromPiefed(JsonMap json) {
final subject = json;

return NotificationModel(
id: json['notif_id'] as int,
Comment thread
JollyDevelopment marked this conversation as resolved.
type: notificationTypeMap[json['notif_subtype']],
isRead: json['status'] == 'Read',
subject: subject,
creator: UserModel.fromPiefed(json['author'] as JsonMap));
}
}

enum NotificationStatus { all, new_, read }

enum NotificationType {
mention,
postMention, //piefed
commentMention, //piefed
reply, // Lemmy specific type
entryCreated,
entryEdited,
Expand Down Expand Up @@ -151,6 +176,14 @@ const notificationTypeMap = {
'report_rejected_notification': NotificationType.reportRejected,
'report_approved_notification': NotificationType.reportApproved,
'new_signup': NotificationType.newSignup,
'new_post_from_followed_user': NotificationType.entryCreated,
'new_post_in_followed_community': NotificationType.entryCreated,
'new_post_in_followed_topic': NotificationType.entryCreated,
'top_level_comment_on_followed_post': NotificationType.entryCommentCreated,
'new_reply_on_followed_comment': NotificationType.entryCommentReply,
'new_post_in_followed_feed': NotificationType.entryCreated,
'comment_mention': NotificationType.commentMention,
'post_mention': NotificationType.postMention,
};

enum NotificationControlStatus {
Expand Down
60 changes: 56 additions & 4 deletions lib/src/screens/account/notification/notification_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import 'notification_count_controller.dart';

const notificationTitle = {
NotificationType.mention: 'mentioned you',
NotificationType.postMention: 'mentioned you in a post',
NotificationType.commentMention: 'mentioned you in a comment',
NotificationType.reply: 'replied to you',
NotificationType.entryCreated: 'created a thread',
NotificationType.entryEdited: 'edited your thread',
Expand Down Expand Up @@ -82,8 +84,7 @@ class _NotificationItemState extends State<NotificationItem> {
widget.item.subject['comment']['content'] as String,
_ => throw Exception('invalid notification type for lemmy'),
},
ServerSoftware.piefed =>
throw UnsupportedError('no notification support for piefed'),
ServerSoftware.piefed => (widget.item.subject['notif_body']) as String,
};

final void Function()? onTap = switch (software) {
Expand Down Expand Up @@ -165,8 +166,59 @@ class _NotificationItemState extends State<NotificationItem> {
},
_ => throw Exception('invalid notification type for lemmy'),
},
ServerSoftware.piefed =>
throw UnsupportedError('no notification support for piefed'),
ServerSoftware.piefed => switch (widget.item.type!) {
NotificationType.entryCreated => () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostPage(
postType: PostType.thread,
postId: widget.item.subject['post_id'] as int,
),
),
);
},
NotificationType.entryCommentCreated => () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostCommentScreen(
PostType.thread,
widget.item.subject['comment_id'] as int,
),
),
);
},
NotificationType.entryCommentReply => () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostCommentScreen(
PostType.thread,
widget.item.subject['comment_id'] as int,
),
),
);
},
NotificationType.postMention => () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostPage(
postType: PostType.thread,
postId: widget.item.subject['post_id'] as int,
),
),
);
},
NotificationType.commentMention => () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostCommentScreen(
PostType.thread,
widget.item.subject['comment_id'] as int,
),
),
);
},
_ => throw Exception('invalid notification type for piefed'),
}
};

return Card.outlined(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ SelectionMenu<NotificationsFilter> notificationFilterSelect(
title: l(context).filter_new,
icon: Symbols.nest_eco_leaf_rounded,
),
if (context.read<AppController>().serverSoftware == ServerSoftware.mbin)
if (context.read<AppController>().serverSoftware ==
ServerSoftware.mbin ||
context.read<AppController>().serverSoftware ==
ServerSoftware.piefed)
SelectionMenuItem(
value: NotificationsFilter.read,
title: l(context).filter_read,
Expand Down