Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:at_client/src/exception/at_client_exception.dart';
4 : import 'package:at_client/src/response/at_notification.dart';
5 : import 'package:at_commons/at_commons.dart';
6 :
7 : abstract class NotificationService {
8 : // Gives back stream of notifications from the server to the subscribing client.
9 : // Optionally pass a regex to filter notification keys matching the regex.
10 : Stream<AtNotification> subscribe({String? regex});
11 :
12 : /// Sends notification to [notificationParams.atKey.sharedWith] atSign.
13 : ///
14 : /// Returns [NotificationResult] when await on the method .
15 : /// When run asynchronously, register to onSuccess and onError callbacks to get [NotificationResult].
16 : ///
17 : /// OnSuccess is called when the notification has been delivered to the recipient successfully.
18 : ///
19 : /// onError is called when the notification could not delivered
20 : ///
21 : /// Following exceptions are encapsulated in [NotificationResult.atClientException]
22 : ///* [AtKeyException] when invalid [NotificationParams.atKey.key] is formed or when
23 : ///invalid metadata is provided.
24 : ///* [InvalidAtSignException] on invalid [NotificationParams.atKey.sharedWith] or [NotificationParams.atKey.sharedBy]
25 : ///* [AtClientException] when keys to encrypt the data are not found.
26 : ///* [AtClientException] when [notificationParams.notifier] is null when [notificationParams.strategy] is set to latest.
27 : ///* [AtClientException] when fails to connect to cloud secondary server.
28 : ///
29 : /// Usage
30 : ///
31 : /// ```dart
32 : /// var currentAtSign = '@alice'
33 : /// ```
34 : ///
35 : /// 1. To notify update of a key to @bob.
36 : ///```dart
37 : /// var key = AtKey()
38 : /// ..key = 'phone'
39 : /// ..sharedWith = '@bob';
40 : ///
41 : /// var notification = NotificationServiceImpl(atClient!);
42 : /// await notification.notify(NotificationParams.forUpdate(key));
43 : ///```
44 : ///2. To notify and cache a key to @bob
45 : ///```dart
46 : /// var metaData = Metadata()..ttr = '600000';
47 : /// var key = AtKey()
48 : /// ..key = 'phone'
49 : /// ..sharedWith = '@bob'
50 : /// ..metadata = metaData;
51 : ///
52 : /// var value = '+1 998 999 4940'
53 : ///
54 : /// var notification = NotificationServiceImpl(atClient!);
55 : /// await notification.notify(NotificationParams.forUpdate(key, value: value));
56 : ///```
57 : ///3. To notify deletion of a key to @bob.
58 : ///```dart
59 : /// var key = AtKey()
60 : /// ..key = 'phone'
61 : /// ..sharedWith = '@bob';
62 : ///
63 : /// var notification = NotificationServiceImpl(atClient!);
64 : /// await notification.notify(NotificationParams.forDelete(key));
65 : ///```
66 : ///4. To notify a text message to @bob
67 : /// await notification.notify(NotificationParams.forText(<Text to Notify>,<Whom to Notify>));
68 : ///
69 : ///```dart
70 : /// var notification = NotificationServiceImpl(atClient!);
71 : /// await notification.notify(NotificationParams.forText('Hello','@bob'));
72 : ///```
73 : Future<NotificationResult> notify(NotificationParams notificationParams,
74 : {Function? onSuccess, Function? onError});
75 :
76 : /// Stops all subscriptions on the current instance
77 : void stopAllSubscriptions();
78 : }
79 :
80 : /// [NotificationParams] represents a notification input params.
81 : class NotificationParams {
82 : late AtKey _atKey;
83 : String? _value;
84 : late OperationEnum _operation;
85 : late MessageTypeEnum _messageType;
86 : late PriorityEnum _priority;
87 : late StrategyEnum _strategy;
88 : final int _latestN = 1;
89 : final String _notifier = SYSTEM;
90 :
91 0 : AtKey get atKey => _atKey;
92 :
93 0 : String? get value => _value;
94 :
95 0 : OperationEnum get operation => _operation;
96 :
97 0 : MessageTypeEnum get messageType => _messageType;
98 :
99 0 : PriorityEnum get priority => _priority;
100 :
101 0 : StrategyEnum get strategy => _strategy;
102 :
103 0 : String get notifier => _notifier;
104 :
105 0 : int get latestN => _latestN;
106 :
107 : /// Returns [NotificationParams] to send an update notification.
108 0 : static NotificationParams forUpdate(AtKey atKey, {String? value}) {
109 0 : return NotificationParams()
110 0 : .._atKey = atKey
111 0 : .._value = value
112 0 : .._operation = OperationEnum.update
113 0 : .._messageType = MessageTypeEnum.key
114 0 : .._priority = PriorityEnum.low
115 0 : .._strategy = StrategyEnum.all;
116 : }
117 :
118 : /// Returns [NotificationParams] to send a delete notification.
119 0 : static NotificationParams forDelete(AtKey atKey) {
120 0 : return NotificationParams()
121 0 : .._atKey = atKey
122 0 : .._operation = OperationEnum.delete
123 0 : .._messageType = MessageTypeEnum.key
124 0 : .._priority = PriorityEnum.low
125 0 : .._strategy = StrategyEnum.all;
126 : }
127 :
128 : /// Returns [NotificationParams] to send a text message to another atSign.
129 0 : static NotificationParams forText(String text, String whomToNotify) {
130 0 : var atKey = AtKey()
131 0 : ..key = text
132 0 : ..sharedWith = whomToNotify;
133 0 : return NotificationParams()
134 0 : .._atKey = atKey
135 0 : .._operation = OperationEnum.update
136 0 : .._messageType = MessageTypeEnum.text
137 0 : .._priority = PriorityEnum.low
138 0 : .._strategy = StrategyEnum.all;
139 : }
140 : }
141 :
142 : /// [NotificationResult] encapsulates the notification response
143 : class NotificationResult {
144 : String? notificationID;
145 : late AtKey atKey;
146 : NotificationStatusEnum notificationStatusEnum =
147 : NotificationStatusEnum.undelivered;
148 :
149 : AtClientException? atClientException;
150 :
151 0 : @override
152 : String toString() {
153 0 : return 'key: ${atKey.key} sharedWith: ${atKey.sharedWith} status: $notificationStatusEnum';
154 : }
155 : }
156 :
157 7 : enum NotificationStatusEnum { delivered, undelivered }
|