-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathindex.ts
More file actions
168 lines (151 loc) · 5.19 KB
/
index.ts
File metadata and controls
168 lines (151 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import EventEmitter2 from 'eventemitter2';
import type {
MarkAsReadNotificationsParam,
NotificationServicesControllerEnableNotificationsOptions,
} from '@metamask/notification-services-controller/notification-services';
import Engine from '../../../core/Engine';
import { isNotificationsFeatureEnabled } from '../../../util/notifications';
const previewTokenEventEmitter = new EventEmitter2();
const PREVIEW_TOKEN_UPDATE_EVENT = 'previewTokenUpdate';
let previewToken: string | undefined;
export function setContentPreviewToken(newPreviewToken?: string | null) {
if (typeof newPreviewToken === 'string') {
previewToken = newPreviewToken;
previewTokenEventEmitter.emit(PREVIEW_TOKEN_UPDATE_EVENT, previewToken);
}
}
export function getContentPreviewToken() {
return previewToken;
}
export function subscribeToContentPreviewToken(
callback: (token: string) => void,
) {
const sub = previewTokenEventEmitter.on(PREVIEW_TOKEN_UPDATE_EVENT, callback);
return () => sub.off(PREVIEW_TOKEN_UPDATE_EVENT, callback);
}
export const assertIsFeatureEnabled = () => {
if (!isNotificationsFeatureEnabled()) {
throw new Error(
'Notifications Feature is not yet enabled, you should not have been able to access this yet!',
);
}
};
/**
* Enable Notifications Switch
* - This is used during onboarding and for the notifications settings toggle
* - Enables wallet notifications, feature announcements, and push notifications
*/
export const enableNotifications = async (
options?: NotificationServicesControllerEnableNotificationsOptions,
) => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.enableMetamaskNotifications(
options,
);
};
/**
* Checks whether the authenticated user storage already has notification preferences.
* A missing AUS row is returned as `null` by the service.
*/
export const hasNotificationPreferences = async () => {
assertIsFeatureEnabled();
const preferences = await Engine.controllerMessenger.call(
'AuthenticatedUserStorageService:getNotificationPreferences',
);
return preferences != null;
};
/**
* Disable Notifications Switch
* - Disables wallet notifications, feature announcements, and push notifications
*/
export const disableNotifications = async () => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.disableNotificationServices();
};
/**
* Push Notifications Switch
* - Allows us to enable push notifications
* @throws if fails to enable push notifications
*/
export const enablePushNotifications = async () => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.enablePushNotifications();
};
/**
* Push Notifications Switch
* - Allows us to disable push notifications
*/
export const disablePushNotifications = async () => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.disablePushNotifications();
};
/**
* Feature Announcement Switch
* - Enables/Disables Feature Announcements
* @param featureAnnouncementsEnabled boolean to toggle on/off
*/
export const toggleFeatureAnnouncements = async (
featureAnnouncementsEnabled: boolean,
) => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.setFeatureAnnouncementsEnabled(
featureAnnouncementsEnabled,
);
};
/**
* Account Notification Settings.
* - Informs us which accounts have notifications enabled.
* @param accounts - accounts to check
* @returns Record of Address <> Boolean (for which accounts are enabled/disabled)
*/
export const fetchAccountNotificationSettings = async (accounts: string[]) => {
assertIsFeatureEnabled();
const accountsStatus =
await Engine.context.NotificationServicesController.checkAccountsPresence(
accounts,
);
return accountsStatus;
};
/**
* Account Notification Settings.
* - Allows us to delete notifications for accounts
* @param accounts - accounts to disable notifications for
*/
export const disableAccounts = async (accounts: string[]) => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.disableAccounts(accounts);
};
/**
* Account Notification Settings
* - Allows us to enable notifications for accounts
* @param accounts - accounts to enable notifications for
*/
export const enableAccounts = async (accounts: string[]) => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.enableAccounts(accounts);
};
/**
* Fetch Notifications
* - Use to invoke the series of fetch calls to get notifications
* - Use Selectors to grab pending state and data
* @throws Error if fails to fetch notifications
*/
export const fetchNotifications = async () => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.fetchAndUpdateMetamaskNotifications(
getContentPreviewToken(),
);
};
/**
* Mark Notification as Read
* - Fire this in the background to notify our services that a notification was read.
* @param notifications - notifications to mark as read
*/
export const markNotificationsAsRead = async (
notifications: MarkAsReadNotificationsParam,
) => {
assertIsFeatureEnabled();
await Engine.context.NotificationServicesController.markMetamaskNotificationsAsRead(
notifications,
);
};