Skip to content
Open
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
40 changes: 39 additions & 1 deletion lib/common/notifications/notification_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class NotificationHelper {
Event? unSendNotification;
bool forwardedToAmber = false;

final Map<String, bool> _pushDeniedBySigner = {};
String? _lastDeviceId;

void resetPushDenialCache() {
_pushDeniedBySigner.clear();
_lastDeviceId = null;
}

Future<void> init() async {
startHeartBeat();
nc.addConnectStatusListener(
Expand Down Expand Up @@ -72,6 +80,13 @@ class NotificationHelper {
String content,
) async {
try {
if (canSign()) {
final pubkey = currentSigner!.getPublicKey();
if (_pushDeniedBySigner[pubkey] == true) {
return null;
}
}

final enContent = await currentSigner!.encrypt04(content, receiver);
final tags = Nip4.toTags(receiver, '', null);

Expand All @@ -96,6 +111,15 @@ class NotificationHelper {

return event;
} catch (e) {
final errorStr = e.toString().toLowerCase();
if (errorStr.contains('no permission') ||
errorStr.contains('permission denied') ||
errorStr.contains('denied')) {
if (canSign()) {
final pubkey = currentSigner!.getPublicKey();
_pushDeniedBySigner[pubkey] = true;
}
}
lg.i(e);
return null;
}
Expand Down Expand Up @@ -155,12 +179,26 @@ class NotificationHelper {
String deviceId,
List<int> kinds,
) async {
final c = nostrRepository.currentAppCustomization;
if (!(c?.enablePushNotification ?? false)) {
return false;
}

if (deviceId.isNotEmpty && deviceId == _lastDeviceId) {
if (canSign()) {
final pubkey = currentSigner!.getPublicKey();
if (_pushDeniedBySigner[pubkey] == true) {
return false;
}
}
}

if (serverPubkey.isEmpty || forwardedToAmber) {
forwardedToAmber = false;
return false;
}

final c = nostrRepository.currentAppCustomization;
_lastDeviceId = deviceId;

final map = {
'online': deviceId.isEmpty ? 0 : 1,
Expand Down
7 changes: 6 additions & 1 deletion lib/logic/notifications_cubit/notifications_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,16 @@ class NotificationsCubit extends Cubit<NotificationsState> {
}

void setPushNotifications(String deviceId) {
final c = nostrRepository.currentAppCustomization;

if (!(c?.enablePushNotification ?? false)) {
return;
}

final kinds = <int>{
EventKind.DIRECT_MESSAGE,
EventKind.PRIVATE_DIRECT_MESSAGE,
};
final c = nostrRepository.currentAppCustomization;

if (c?.notifMentionsReplies ?? false) {
kinds.addAll(
Expand Down
5 changes: 5 additions & 0 deletions lib/logic/settings_cubit/settings_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:nostr_core_enhanced/nostr/event_signer/remote_event_signer.dart'
import 'package:nostr_core_enhanced/nostr/nostr.dart';
import 'package:nostr_core_enhanced/utils/utils.dart';

import '../../common/notifications/notification_helper.dart';
import '../../initializers.dart';
import '../../models/app_client_model.dart';
import '../../models/app_models/settings_data.dart';
Expand Down Expand Up @@ -248,6 +249,8 @@ class SettingsCubit extends Cubit<SettingsState> {
nc.setSigner(currentSigner);
currentUserRelayList.pubkey = currentSigner!.getPublicKey();

NotificationHelper.sharedInstance.resetPushDenialCache();

try {
nostrRepository.setCurrentAppCustomizationFromCache(broadcast: true);
nostrRepository.setCurrentUserDraft();
Expand Down Expand Up @@ -292,6 +295,8 @@ class SettingsCubit extends Cubit<SettingsState> {
currentSigner = null;
nc.setSigner(currentSigner);
nostrRepository.setCurrentSignerState(null);

NotificationHelper.sharedInstance.resetPushDenialCache();
privateKeyIndex = index;

c.call();
Expand Down