2024-12-13 03:16:54 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-11-19 05:00:17 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:result_monad/result_monad.dart';
|
2024-11-27 02:18:08 +00:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2024-12-19 21:24:37 +00:00
|
|
|
import 'package:stack_trace/stack_trace.dart';
|
2022-11-19 05:00:17 +00:00
|
|
|
|
|
|
|
import '../globals.dart';
|
2023-04-29 01:28:43 +00:00
|
|
|
import '../models/auth/profile.dart';
|
2024-11-27 02:44:28 +00:00
|
|
|
import '../models/exec_error.dart';
|
2024-12-10 13:49:20 +00:00
|
|
|
import '../models/networking/paged_response.dart';
|
|
|
|
import '../models/networking/pages_manager.dart';
|
|
|
|
import '../models/networking/paging_data.dart';
|
2024-11-27 02:44:28 +00:00
|
|
|
import '../models/user_notification.dart';
|
|
|
|
import '../serializers/mastodon/follow_request_mastodon_extensions.dart';
|
|
|
|
import 'direct_message_services.dart';
|
2024-12-10 11:54:45 +00:00
|
|
|
import 'feature_checker_services.dart';
|
2024-12-07 14:08:05 +00:00
|
|
|
import 'follow_requests_services.dart';
|
2024-12-13 03:16:54 +00:00
|
|
|
import 'networking/friendica_notifications_client_services.dart';
|
2024-11-27 02:44:28 +00:00
|
|
|
import 'settings_services.dart';
|
2024-11-27 02:18:08 +00:00
|
|
|
|
|
|
|
part 'notification_services.g.dart';
|
|
|
|
|
|
|
|
const _itemsPerQuery = 50;
|
|
|
|
const _minimumDmsAndCrsUpdateDuration = Duration(seconds: 30);
|
|
|
|
final _logger = Logger('NotificationManager');
|
|
|
|
|
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
class NotificationsManager extends _$NotificationsManager {
|
2023-04-29 01:06:21 +00:00
|
|
|
final dms = <UserNotification>[];
|
|
|
|
final connectionRequests = <UserNotification>[];
|
|
|
|
final unread = <UserNotification>[];
|
|
|
|
final read = <UserNotification>[];
|
2023-08-04 16:34:51 +00:00
|
|
|
var lastDmsUpdate = DateTime(1900);
|
|
|
|
var lastCrUpdate = DateTime(1900);
|
2024-11-27 02:18:08 +00:00
|
|
|
|
|
|
|
bool get hasNotifications =>
|
2024-12-19 20:36:33 +00:00
|
|
|
dms.isNotEmpty || connectionRequests.isNotEmpty || unread.isNotEmpty;
|
|
|
|
|
|
|
|
List<UserNotification> get notifications =>
|
|
|
|
[...connectionRequests, ...dms, ...unread, ...read];
|
2024-11-27 02:18:08 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Result<List<UserNotification>, ExecError>> build(
|
|
|
|
Profile profile) async {
|
2024-11-27 02:44:28 +00:00
|
|
|
_logger.info('Building');
|
2022-11-19 05:00:17 +00:00
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
await _initialize();
|
|
|
|
return Result.ok(notifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _initialize() async {
|
|
|
|
final result = await loadUnreadNotifications(false);
|
|
|
|
if (result.isSuccess && unread.isEmpty && read.isEmpty) {
|
|
|
|
await loadOlderNotifications(withListenerNotification: false);
|
|
|
|
}
|
2024-11-27 02:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void refreshNotifications() async {
|
|
|
|
clear(withListenerNotification: true);
|
2022-11-19 05:00:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-17 03:47:44 +00:00
|
|
|
Future<void> refreshConnectionRequestNotifications() async {
|
|
|
|
_logger.info('refreshConnectionRequestNotifications');
|
|
|
|
connectionRequests.clear();
|
2024-12-19 20:36:33 +00:00
|
|
|
await _postFetchOperations([], true, updateDms: false);
|
2024-12-17 03:47:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
void clear({bool withListenerNotification = true}) {
|
2023-04-29 01:06:21 +00:00
|
|
|
dms.clear();
|
|
|
|
connectionRequests.clear();
|
|
|
|
unread.clear();
|
|
|
|
read.clear();
|
2024-12-19 20:36:33 +00:00
|
|
|
_initialize();
|
2023-08-04 16:34:51 +00:00
|
|
|
}
|
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
FutureResult<bool, ExecError> loadUnreadNotifications(
|
2023-08-04 16:34:51 +00:00
|
|
|
bool withListenerNotification) async {
|
2023-03-21 18:27:38 +00:00
|
|
|
final notificationsFromRefresh = <UserNotification>[];
|
2023-08-04 16:34:51 +00:00
|
|
|
|
2024-12-13 03:16:54 +00:00
|
|
|
final pm = _buildPageManager(ref, profile, false);
|
2024-12-10 11:54:45 +00:00
|
|
|
final useActualRequests = ref.read(featureCheckProvider(
|
|
|
|
profile,
|
|
|
|
RelaticaFeatures.usingActualFollowRequests,
|
|
|
|
));
|
2023-08-04 16:34:51 +00:00
|
|
|
var hasMore = true;
|
|
|
|
var first = true;
|
2023-11-16 22:37:22 +00:00
|
|
|
const maxCalls = 3;
|
|
|
|
var count = 0;
|
|
|
|
while (hasMore && count < maxCalls) {
|
2023-08-04 16:34:51 +00:00
|
|
|
final result =
|
2024-11-27 02:18:08 +00:00
|
|
|
first ? await pm.initialize(_itemsPerQuery) : await pm.nextFromEnd();
|
2023-08-04 16:34:51 +00:00
|
|
|
|
|
|
|
first = false;
|
|
|
|
result.match(
|
2024-07-26 14:15:24 +00:00
|
|
|
onSuccess: (nd) =>
|
|
|
|
_logger.fine('Got ${nd.data.length} notifications'),
|
2024-12-19 21:24:37 +00:00
|
|
|
onError: (e) => _logger.severe(
|
|
|
|
'Error getting notification: $e',
|
|
|
|
Trace.current(),
|
|
|
|
));
|
2023-08-04 16:34:51 +00:00
|
|
|
final response = result.getValueOrElse(() => PagedResponse([]));
|
|
|
|
response.data
|
|
|
|
.where((n) =>
|
|
|
|
!useActualRequests || n.type != NotificationType.follow_request)
|
|
|
|
.forEach(notificationsFromRefresh.add);
|
|
|
|
hasMore = response.next != null;
|
2023-11-16 22:37:22 +00:00
|
|
|
count++;
|
2022-11-19 05:00:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
// filter out connection requests if going to use the real service for that when doing the query
|
|
|
|
// get earliest and latest notification ID from unread notifications
|
|
|
|
|
|
|
|
// query all notifications over that in page increments of 25
|
|
|
|
// query unread notifications in increments of 25 after the latest ID
|
|
|
|
return await _postFetchOperations(
|
|
|
|
notificationsFromRefresh,
|
|
|
|
withListenerNotification,
|
2024-12-19 20:36:33 +00:00
|
|
|
).mapValue((value) => value.isNotEmpty);
|
2022-11-19 05:00:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-27 02:18:08 +00:00
|
|
|
FutureResult<List<UserNotification>, ExecError> _postFetchOperations(
|
|
|
|
List<UserNotification> notificationsFromRefresh,
|
2024-12-17 03:47:44 +00:00
|
|
|
bool withListenerNotification, {
|
|
|
|
bool updateDms = true,
|
|
|
|
bool updateFollowRequests = true,
|
|
|
|
}) async {
|
|
|
|
if (updateDms) {
|
|
|
|
if (DateTime.now().difference(lastDmsUpdate) >
|
|
|
|
_minimumDmsAndCrsUpdateDuration) {
|
|
|
|
await ref
|
2024-12-17 16:43:28 +00:00
|
|
|
.read(directMessageThreadIdsProvider(profile).notifier)
|
2024-12-17 03:47:44 +00:00
|
|
|
.update();
|
|
|
|
lastDmsUpdate = DateTime.now();
|
|
|
|
}
|
2024-11-27 02:18:08 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 11:54:45 +00:00
|
|
|
final useActualRequests = ref.read(featureCheckProvider(
|
|
|
|
profile,
|
|
|
|
RelaticaFeatures.usingActualFollowRequests,
|
|
|
|
));
|
2024-12-17 03:47:44 +00:00
|
|
|
if (updateFollowRequests) {
|
|
|
|
if (useActualRequests) {
|
|
|
|
if (DateTime.now().difference(lastCrUpdate) >
|
|
|
|
_minimumDmsAndCrsUpdateDuration) {
|
|
|
|
await ref.read(followRequestsProvider(profile).notifier).update();
|
|
|
|
lastCrUpdate = DateTime.now();
|
|
|
|
}
|
2024-11-27 02:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final notifications = <String, UserNotification>{};
|
|
|
|
|
|
|
|
notificationsFromRefresh.removeWhere((n) =>
|
|
|
|
n.type == NotificationType.direct_message ||
|
|
|
|
(useActualRequests && n.type == NotificationType.follow_request));
|
|
|
|
for (final n in notificationsFromRefresh) {
|
|
|
|
notifications[n.id] = n;
|
|
|
|
}
|
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
for (final n in _buildUnreadMessageNotifications(useActualRequests)) {
|
2024-11-27 02:18:08 +00:00
|
|
|
notifications[n.id] = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_processNewNotifications(notifications.values);
|
|
|
|
|
|
|
|
return Result.ok(notifications.values.toList());
|
|
|
|
}
|
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
FutureResult<bool, ExecError> loadNewerNotifications({
|
2023-08-04 16:34:51 +00:00
|
|
|
bool withListenerNotification = true,
|
|
|
|
}) async {
|
|
|
|
final (_, highestId) =
|
|
|
|
unread.isNotEmpty ? calcLowHigh(unread) : calcLowHigh(read);
|
2023-10-31 01:44:16 +00:00
|
|
|
final pm = _buildPageManager(
|
2024-12-13 03:16:54 +00:00
|
|
|
ref,
|
2023-08-04 16:34:51 +00:00
|
|
|
profile,
|
|
|
|
true,
|
2023-11-16 14:39:25 +00:00
|
|
|
initialPages: read.isEmpty && unread.isEmpty
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
PagedResponse(
|
|
|
|
<String>[],
|
2023-11-16 22:37:22 +00:00
|
|
|
previous: PagingData(
|
|
|
|
minId: highestId,
|
2024-11-27 02:18:08 +00:00
|
|
|
limit: _itemsPerQuery,
|
2023-11-16 22:37:22 +00:00
|
|
|
),
|
2023-11-16 14:39:25 +00:00
|
|
|
)
|
|
|
|
],
|
2023-08-04 16:34:51 +00:00
|
|
|
);
|
2023-02-10 14:36:41 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
final result = await (unread.isEmpty && read.isEmpty
|
2024-11-27 02:18:08 +00:00
|
|
|
? pm.initialize(_itemsPerQuery)
|
2023-08-04 16:34:51 +00:00
|
|
|
: pm.previousFromBeginning())
|
2023-05-08 18:24:45 +00:00
|
|
|
.andThenAsync(
|
2023-08-04 16:34:51 +00:00
|
|
|
(page) async =>
|
|
|
|
await _postFetchOperations(page.data, withListenerNotification),
|
2023-05-08 18:24:45 +00:00
|
|
|
)
|
|
|
|
.withError(
|
|
|
|
(error) => _logger.info('Error getting more updates: $error'));
|
2024-12-19 20:36:33 +00:00
|
|
|
return result.mapValue((value) => value.isNotEmpty).execErrorCast();
|
2023-02-10 14:36:41 +00:00
|
|
|
}
|
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
FutureResult<bool, ExecError> loadOlderNotifications(
|
2023-08-04 16:34:51 +00:00
|
|
|
{bool withListenerNotification = true}) async {
|
2023-11-16 22:37:22 +00:00
|
|
|
if (unread.isNotEmpty) {
|
|
|
|
final result =
|
|
|
|
await _loadOlderUnreadNotifications(withListenerNotification);
|
2024-06-28 17:28:36 +00:00
|
|
|
final nonDmAndConnectionNotifications = result
|
|
|
|
.getValueOrElse(() => [])
|
|
|
|
.where((n) =>
|
|
|
|
n.type != NotificationType.follow_request &&
|
|
|
|
n.type != NotificationType.direct_message)
|
|
|
|
.toList();
|
|
|
|
if (nonDmAndConnectionNotifications.isNotEmpty) {
|
2024-12-19 20:36:33 +00:00
|
|
|
return Result.ok(true);
|
2023-11-16 22:37:22 +00:00
|
|
|
}
|
2023-08-04 16:34:51 +00:00
|
|
|
}
|
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
return _loadOlderReadAndUnreadNotifications(withListenerNotification)
|
|
|
|
.mapValue((value) => value.isNotEmpty);
|
2023-08-04 16:34:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 04:46:34 +00:00
|
|
|
FutureResult<bool, ExecError> markSeen(UserNotification notification) async {
|
2024-12-13 03:16:54 +00:00
|
|
|
final result = await ref
|
|
|
|
.read(clearNotificationProvider(profile, notification).future)
|
2023-08-04 16:34:51 +00:00
|
|
|
.withResult((_) {
|
|
|
|
unread.remove(notification);
|
|
|
|
read.add(notification.copy(dismissed: true));
|
|
|
|
read.sort();
|
2024-12-19 20:36:33 +00:00
|
|
|
state = AsyncData(Result.ok(notifications));
|
2023-08-04 16:34:51 +00:00
|
|
|
});
|
2022-11-19 05:00:17 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
return result.execErrorCast();
|
2022-11-19 05:00:17 +00:00
|
|
|
}
|
2022-11-19 19:16:46 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
FutureResult<bool, ExecError> markAllAsRead() async {
|
2024-12-13 03:16:54 +00:00
|
|
|
final result = await ref
|
|
|
|
.read(clearNotificationsProvider(profile).future)
|
2024-11-27 02:18:08 +00:00
|
|
|
.withResult((_) {
|
2023-08-04 16:34:51 +00:00
|
|
|
unread.map((n) => n.copy(dismissed: true)).forEach(read.add);
|
|
|
|
unread.clear();
|
|
|
|
read.sort();
|
2024-12-19 20:36:33 +00:00
|
|
|
state = AsyncData(Result.ok(notifications));
|
2023-08-04 16:34:51 +00:00
|
|
|
});
|
2023-02-14 13:38:08 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
return result.execErrorCast();
|
2022-11-19 19:16:46 +00:00
|
|
|
}
|
2023-02-08 15:41:29 +00:00
|
|
|
|
2024-12-19 20:36:33 +00:00
|
|
|
List<UserNotification> _buildUnreadMessageNotifications(
|
2023-03-21 18:27:38 +00:00
|
|
|
bool useActualRequests) {
|
2024-12-17 16:43:28 +00:00
|
|
|
final myId = profile.userId;
|
2024-11-27 02:18:08 +00:00
|
|
|
final dmsResult = ref
|
2024-12-17 16:43:28 +00:00
|
|
|
.watch(directMessageThreadIdsProvider(profile))
|
|
|
|
.map((id) => ref.watch(directMessageThreadServiceProvider(profile, id)))
|
2024-11-27 02:18:08 +00:00
|
|
|
.where((t) => !t.allSeen)
|
|
|
|
.map((t) {
|
|
|
|
final fromAccount = t.participants.firstWhere((p) => p.id != myId);
|
|
|
|
final latestMessage =
|
|
|
|
t.messages.reduce((s, m) => s.createdAt > m.createdAt ? s : m);
|
|
|
|
return UserNotification(
|
|
|
|
id: (fromAccount.hashCode ^ t.parentUri.hashCode ^ t.title.hashCode)
|
|
|
|
.toString(),
|
|
|
|
type: NotificationType.direct_message,
|
|
|
|
fromId: fromAccount.id,
|
|
|
|
fromName: fromAccount.name,
|
|
|
|
fromUrl: fromAccount.profileUrl,
|
|
|
|
timestamp: latestMessage.createdAt,
|
|
|
|
iid: t.parentUri,
|
|
|
|
dismissed: false,
|
|
|
|
content: '${fromAccount.name} sent you a direct message',
|
|
|
|
link: '');
|
|
|
|
});
|
2023-02-08 15:41:29 +00:00
|
|
|
|
2023-03-21 18:27:38 +00:00
|
|
|
final followRequestResult = !useActualRequests
|
2024-10-02 17:17:48 +00:00
|
|
|
? <UserNotification>[]
|
2024-12-07 14:08:05 +00:00
|
|
|
: ref
|
|
|
|
.watch(followRequestListProvider(profile))
|
|
|
|
.map((r) => r.toUserNotification())
|
|
|
|
.toList();
|
2023-03-21 18:27:38 +00:00
|
|
|
|
2024-11-27 02:18:08 +00:00
|
|
|
return [...dmsResult, ...followRequestResult];
|
2023-02-08 15:41:29 +00:00
|
|
|
}
|
2023-02-10 14:36:41 +00:00
|
|
|
|
2023-04-29 01:06:21 +00:00
|
|
|
Future<void> _processNewNotifications(
|
2023-08-04 16:34:51 +00:00
|
|
|
Iterable<UserNotification> notifications) async {
|
2024-11-27 02:18:08 +00:00
|
|
|
final groupNotifications = ref.watch(notificationGroupingSettingProvider);
|
2023-05-08 18:24:45 +00:00
|
|
|
final dmsMap = <String, UserNotification>{};
|
|
|
|
final crMap = <String, UserNotification>{};
|
|
|
|
final unreadMap = <String, UserNotification>{};
|
|
|
|
final readMap = <String, UserNotification>{};
|
|
|
|
|
|
|
|
final st = Stopwatch()..start();
|
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
for (int i = 0; i < dms.length; i++) {
|
|
|
|
dmsMap[dms[i].id] = dms[i];
|
|
|
|
}
|
2023-05-08 18:24:45 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
if (st.elapsedMilliseconds > maxProcessingMillis) {
|
|
|
|
await Future.delayed(processingSleep, () => st.reset());
|
|
|
|
}
|
2023-05-08 18:24:45 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
for (int i = 0; i < connectionRequests.length; i++) {
|
|
|
|
crMap[connectionRequests[i].id] = connectionRequests[i];
|
|
|
|
}
|
2023-05-08 18:24:45 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
if (st.elapsedMilliseconds > maxProcessingMillis) {
|
|
|
|
await Future.delayed(processingSleep, () => st.reset());
|
|
|
|
}
|
2023-05-08 18:24:45 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
for (int i = 0; i < unread.length; i++) {
|
|
|
|
unreadMap[unread[i].id] = unread[i];
|
|
|
|
}
|
2023-05-08 18:24:45 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
if (st.elapsedMilliseconds > maxProcessingMillis) {
|
|
|
|
await Future.delayed(processingSleep, () => st.reset());
|
2023-04-29 01:06:21 +00:00
|
|
|
}
|
2023-08-04 16:34:51 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < read.length; i++) {
|
|
|
|
readMap[read[i].id] = read[i];
|
|
|
|
}
|
|
|
|
|
2023-05-08 18:24:45 +00:00
|
|
|
dms.clear();
|
|
|
|
connectionRequests.clear();
|
|
|
|
unread.clear();
|
|
|
|
read.clear();
|
2023-04-29 01:06:21 +00:00
|
|
|
|
|
|
|
for (final n in notifications) {
|
2023-05-08 18:24:45 +00:00
|
|
|
if (st.elapsedMilliseconds > maxProcessingMillis) {
|
|
|
|
await Future.delayed(processingSleep, () => st.reset());
|
|
|
|
}
|
|
|
|
dmsMap.remove(n.id);
|
|
|
|
crMap.remove(n.id);
|
|
|
|
unreadMap.remove(n.id);
|
|
|
|
readMap.remove(n.id);
|
2023-04-29 01:06:21 +00:00
|
|
|
if (n.dismissed) {
|
2023-05-08 18:24:45 +00:00
|
|
|
readMap[n.id] = n;
|
2023-04-29 01:06:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (n.type) {
|
|
|
|
case NotificationType.direct_message:
|
2023-05-08 18:24:45 +00:00
|
|
|
dmsMap[n.id] = n;
|
2023-04-29 01:06:21 +00:00
|
|
|
break;
|
|
|
|
case NotificationType.follow_request:
|
2023-05-08 18:24:45 +00:00
|
|
|
crMap[n.id] = n;
|
2023-04-29 01:06:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-08 18:24:45 +00:00
|
|
|
unreadMap[n.id] = n;
|
2023-04-29 01:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 18:24:45 +00:00
|
|
|
dms
|
|
|
|
..addAll(dmsMap.values)
|
|
|
|
..sort();
|
|
|
|
connectionRequests
|
|
|
|
..addAll(crMap.values)
|
|
|
|
..sort();
|
|
|
|
unread
|
|
|
|
..addAll(unreadMap.values)
|
2024-06-27 16:06:26 +00:00
|
|
|
..sort(
|
|
|
|
(n1, n2) => _compareByTypeStatusAndDate(n1, n2, groupNotifications));
|
2023-05-08 18:24:45 +00:00
|
|
|
read
|
|
|
|
..addAll(readMap.values)
|
2024-06-27 16:06:26 +00:00
|
|
|
..sort(
|
|
|
|
(n1, n2) => _compareByTypeStatusAndDate(n1, n2, groupNotifications));
|
2024-12-19 20:36:33 +00:00
|
|
|
state = AsyncData(Result.ok(this.notifications));
|
2023-11-16 22:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<List<UserNotification>, ExecError> _loadOlderUnreadNotifications(
|
|
|
|
bool withListenerNotification) async {
|
2024-11-27 02:44:28 +00:00
|
|
|
_logger.finest('Loading Older Unread Notifications');
|
2023-11-16 22:37:22 +00:00
|
|
|
final (lowestId, _) = calcLowHigh(unread);
|
|
|
|
final pm = _buildPageManager(
|
2024-12-13 03:16:54 +00:00
|
|
|
ref,
|
2023-11-16 22:37:22 +00:00
|
|
|
profile,
|
|
|
|
false,
|
|
|
|
initialPages: [
|
|
|
|
PagedResponse(
|
|
|
|
<String>[],
|
|
|
|
next: PagingData(
|
|
|
|
maxId: lowestId,
|
2024-11-27 02:18:08 +00:00
|
|
|
limit: _itemsPerQuery,
|
2023-11-16 22:37:22 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
final result = await pm
|
|
|
|
.nextFromEnd()
|
|
|
|
.andThenAsync(
|
|
|
|
(page) async =>
|
|
|
|
await _postFetchOperations(page.data, withListenerNotification),
|
|
|
|
)
|
|
|
|
.withError(
|
|
|
|
(error) => _logger.info('Error getting more updates: $error'));
|
|
|
|
|
2024-11-27 02:44:28 +00:00
|
|
|
_logger.finest(
|
|
|
|
'Loaded Older Unread Notifications: ${result.getValueOrElse(() => []).length}');
|
2023-11-16 22:37:22 +00:00
|
|
|
return result.execErrorCast();
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<List<UserNotification>, ExecError>
|
|
|
|
_loadOlderReadAndUnreadNotifications(
|
|
|
|
bool withListenerNotification) async {
|
2024-11-27 02:44:28 +00:00
|
|
|
_logger.finest('Loading Older Read and Unread Notifications');
|
2023-11-16 22:37:22 +00:00
|
|
|
final (lowestId, _) =
|
|
|
|
read.isNotEmpty ? calcLowHigh(read) : calcLowHigh(unread);
|
|
|
|
final pm = _buildPageManager(
|
2024-12-13 03:16:54 +00:00
|
|
|
ref,
|
2023-11-16 22:37:22 +00:00
|
|
|
profile,
|
|
|
|
true,
|
|
|
|
initialPages: read.isEmpty && unread.isEmpty
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
PagedResponse(
|
|
|
|
<String>[],
|
|
|
|
next: PagingData(
|
|
|
|
maxId: lowestId,
|
2024-11-27 02:18:08 +00:00
|
|
|
limit: _itemsPerQuery,
|
2023-11-16 22:37:22 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
final result = await (read.isEmpty && unread.isEmpty
|
2024-11-27 02:18:08 +00:00
|
|
|
? pm.initialize(_itemsPerQuery)
|
2023-11-16 22:37:22 +00:00
|
|
|
: pm.nextFromEnd())
|
|
|
|
.andThenAsync(
|
|
|
|
(page) async =>
|
|
|
|
await _postFetchOperations(page.data, withListenerNotification),
|
|
|
|
)
|
|
|
|
.withError(
|
|
|
|
(error) => _logger.info('Error getting more updates: $error'));
|
2024-11-27 02:44:28 +00:00
|
|
|
_logger.finest(
|
|
|
|
'Loaded Older Read and Unread Notifications: ${result.getValueOrElse(() => []).length}');
|
2023-11-16 22:37:22 +00:00
|
|
|
return result.execErrorCast();
|
2023-04-29 01:06:21 +00:00
|
|
|
}
|
2023-08-04 16:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(int lowest, int highest) calcLowHigh(List<UserNotification> notifications) {
|
|
|
|
int highestNotificationId = -1;
|
|
|
|
int lowestNotificationId = 0x7FFFFFFFFFFFFFFF;
|
|
|
|
final ids = notifications
|
|
|
|
.where((n) =>
|
|
|
|
n.type != NotificationType.direct_message &&
|
|
|
|
n.type != NotificationType.follow_request)
|
|
|
|
.map((n) => int.parse(n.id));
|
|
|
|
|
|
|
|
for (var id in ids) {
|
|
|
|
if (id > highestNotificationId) {
|
|
|
|
highestNotificationId = id;
|
|
|
|
}
|
2023-04-29 01:06:21 +00:00
|
|
|
|
2023-08-04 16:34:51 +00:00
|
|
|
if (id < lowestNotificationId) {
|
|
|
|
lowestNotificationId = id;
|
|
|
|
}
|
2023-02-10 14:36:41 +00:00
|
|
|
}
|
2023-08-04 16:34:51 +00:00
|
|
|
|
|
|
|
return (lowestNotificationId, highestNotificationId);
|
2022-11-19 05:00:17 +00:00
|
|
|
}
|
2023-08-04 16:34:51 +00:00
|
|
|
|
|
|
|
PagesManager<List<UserNotification>, String> _buildPageManager(
|
2024-12-13 03:16:54 +00:00
|
|
|
Ref ref,
|
|
|
|
Profile profile,
|
|
|
|
bool includeAll, {
|
|
|
|
List<PagedResponse> initialPages = const [],
|
|
|
|
}) =>
|
2023-08-04 16:34:51 +00:00
|
|
|
PagesManager<List<UserNotification>, String>(
|
|
|
|
initialPages: initialPages,
|
|
|
|
idMapper: (nn) => nn.map((n) => n.id).toList(),
|
2024-12-13 03:16:54 +00:00
|
|
|
onRequest: (pd) async => await ref
|
|
|
|
.read(notificationsClientProvider(profile, pd, includeAll).future),
|
2023-08-04 16:34:51 +00:00
|
|
|
);
|
2024-06-27 16:06:26 +00:00
|
|
|
|
|
|
|
int _compareByTypeStatusAndDate(
|
|
|
|
UserNotification n1, UserNotification n2, bool groupNotifications) {
|
|
|
|
final n1Weight = _notificationTypeToWeight(n1.type);
|
|
|
|
final n2Weight = _notificationTypeToWeight(n2.type);
|
|
|
|
if (!groupNotifications || n1Weight == n2Weight) {
|
|
|
|
return n1.compareTo(n2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (n2Weight - n1Weight).sign.toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
num _notificationTypeToWeight(NotificationType type) {
|
|
|
|
return switch (type) {
|
|
|
|
NotificationType.follow_request => 1000,
|
|
|
|
NotificationType.follow => 100,
|
|
|
|
NotificationType.direct_message => 50,
|
|
|
|
NotificationType.mention => 10,
|
|
|
|
NotificationType.status => 4,
|
|
|
|
NotificationType.reshare => 3,
|
|
|
|
NotificationType.reblog => 3,
|
|
|
|
NotificationType.favourite => 2,
|
|
|
|
NotificationType.unknown => 1,
|
|
|
|
};
|
|
|
|
}
|