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

feat(notifications_push_repository): Init #2482

Merged
merged 3 commits into from
Sep 29, 2024
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
1 change: 1 addition & 0 deletions commitlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rules:
- nextcloud_test_presets
- notes_app
- notifications_app
- notifications_push_repository
- release
- sort_box
- talk_app
Expand Down
6 changes: 6 additions & 0 deletions packages/neon_framework/lib/src/storage/persistence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ abstract interface class Persistence<T extends Object> {
/// Whether a value exists at the given [key].
FutureOr<bool> containsKey(String key);

/// Returns all keys in the persistent storage.
FutureOr<List<String>> keys();

/// Clears all values from persistent storage.
FutureOr<bool> clear();

Expand Down Expand Up @@ -53,6 +56,9 @@ abstract class CachedPersistence<T extends Object> implements Persistence<T> {
/// when the app is restarted.
void setCache(String key, T value) => cache[key] = value;

@override
List<String> keys() => cache.keys.toList();

@override
bool containsKey(String key) => cache.containsKey(key);

Expand Down
6 changes: 6 additions & 0 deletions packages/neon_framework/lib/src/storage/settings_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ abstract interface class SettingsStore {

/// Removes an entry from persistent storage.
Future<bool> remove(String key);

/// Returns all keys in the persistent storage.
List<String> keys();
}

/// Default implementation of the [SettingsStore] backed by the given [persistence].
Expand Down Expand Up @@ -62,4 +65,7 @@ final class DefaultSettingsStore implements SettingsStore {

@override
Future<bool> setBool(String key, bool value) => persistence.setValue(key, value);

@override
List<String> keys() => persistence.keys();
}
1 change: 0 additions & 1 deletion packages/neon_framework/lib/src/testing/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:neon_framework/src/blocs/accounts.dart';
import 'package:neon_framework/src/models/account_cache.dart';
import 'package:neon_framework/src/models/disposable.dart';
import 'package:neon_framework/src/settings/models/exportable.dart';
import 'package:neon_framework/src/storage/persistence.dart';
import 'package:neon_framework/src/utils/account_options.dart';
import 'package:neon_framework/storage.dart';
import 'package:neon_framework/testing.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/neon_framework/lib/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
library;

export 'package:neon_framework/src/storage/keys.dart' show Storable;
export 'package:neon_framework/src/storage/persistence.dart';
export 'package:neon_framework/src/storage/request_cache.dart' show RequestCache;
export 'package:neon_framework/src/storage/settings_store.dart' show SettingsStore;
export 'package:neon_framework/src/storage/single_value_store.dart' show SingleValueStore;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include: package:neon_lints/dart.yaml

custom_lint:
rules:
- avoid_exports: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'src/models/models.dart' show PushNotification;
export 'src/notifications_push_repository.dart';
export 'src/utils/encryption.dart' show parseEncryptedPushNotifications;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'push_notification.dart';
export 'push_subscription.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:crypton/crypton.dart';
import 'package:nextcloud/notifications.dart' as notifications;

part 'push_notification.g.dart';

/// Data for a push notification.
abstract class PushNotification implements Built<PushNotification, PushNotificationBuilder> {
/// Creates a new [PushNotification].
factory PushNotification([void Function(PushNotificationBuilder)? updates]) = _$PushNotification;

const PushNotification._();

/// Creates a new PushNotification object from the given [json] data containing an encrypted [subject].
///
/// Use [PushNotification.fromJson] when the [subject] is not encrypted.
factory PushNotification.fromEncrypted(
Map<String, dynamic> json,
String accountID,
RSAPrivateKey privateKey,
) {
final subject = notifications.DecryptedSubject.fromEncrypted(privateKey, json['subject'] as String);

return PushNotification(
(b) => b
..accountID = accountID
..priority = json['priority'] as String
..type = json['type'] as String
..subject.replace(subject),
);
}

/// Creates a new PushNotification object from the given [json] data.
///
/// Use [PushNotification.fromEncrypted] when you the [subject] is still encrypted.
factory PushNotification.fromJson(Map<String, dynamic> json) => _serializers.deserializeWith(serializer, json)!;

/// Parses this object into a json like map.
Map<String, dynamic> toJson() => _serializers.serializeWith(serializer, this)! as Map<String, dynamic>;

/// The serializer for [PushNotification].
static Serializer<PushNotification> get serializer => _$pushNotificationSerializer;

/// The account associated with this notification.
String get accountID;

/// The priority of the notification.
String get priority;

/// The type of the notification.
String get type;

/// The subject of this notification.
notifications.DecryptedSubject get subject;
}

final Serializers _serializers = (Serializers().toBuilder()
..add(PushNotification.serializer)
..add(notifications.DecryptedSubject.serializer)
..addPlugin(StandardJsonPlugin()))
.build();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:meta/meta.dart';
import 'package:nextcloud/notifications.dart' as notifications;

part 'push_subscription.g.dart';

@internal
abstract class PushSubscription implements Built<PushSubscription, PushSubscriptionBuilder> {
factory PushSubscription([void Function(PushSubscriptionBuilder)? updates]) = _$PushSubscription;

const PushSubscription._();

factory PushSubscription.fromJson(Map<String, dynamic> json) => _serializers.deserializeWith(serializer, json)!;

Map<String, dynamic> toJson() => _serializers.serializeWith(serializer, this)! as Map<String, dynamic>;

static Serializer<PushSubscription> get serializer => _$pushSubscriptionSerializer;

String? get endpoint;

notifications.PushDevice? get pushDevice;
}

final Serializers _serializers = (Serializers().toBuilder()
..add(PushSubscription.serializer)
..add(notifications.PushDevice.serializer)
..addPlugin(StandardJsonPlugin()))
.build();
Loading