-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivity-watcher.ts
200 lines (178 loc) · 5.65 KB
/
activity-watcher.ts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import Service from '@ember/service';
import { inject as service } from '@ember/service';
import { Observable } from '@upfluence/hyperevents/helpers/observable';
import EventsService, { ResourceEvent, prefixPath } from '@upfluence/hyperevents/services/events-service';
import ToastService, { ToastOptions } from '@upfluence/oss-components/services/toast';
import { IntlService } from 'ember-intl';
type NotificationEvent = {
resource: string;
payload: {
notification_type: string;
timestamp: number;
data: any;
};
};
type RenderedNotification = {
title: string;
message: string;
type: string;
avatarUrl?: string;
};
function renderNotificationMessage(message: string, title: string): RenderedNotification {
return {
title,
message,
type: 'info'
};
}
function renderNotificationErrorMessage(message: string, title: string): RenderedNotification {
return {
title,
message,
type: 'error'
};
}
function renderNotificationMessageWithAvatar(message: string, title: string, avatarUrl: string): RenderedNotification {
return {
title,
message,
type: 'info',
avatarUrl: avatarUrl
};
}
type NotificationRendererMap = {
[key: string]: (data: any, intl?: IntlService) => RenderedNotification;
};
const renderersByNotificationType: NotificationRendererMap = {
mailing_email_received: (data: any, intl: IntlService) => {
return renderNotificationMessageWithAvatar(
intl.t('notifications.mailing_email_received.description', {
influencer_name: data.influencer_name,
entity_url: data.entity_url
}),
intl.t('notifications.mailing_email_received.title'),
data.influencer_avatar
);
},
conversation_email_received: (data: any, intl: IntlService) => {
return renderNotificationMessageWithAvatar(
intl.t('notifications.conversation_email_received.description', {
influencer_name: data.influencer_name,
entity_url: data.entity_url
}),
intl.t('notifications.conversation_email_received.title'),
data.influencer_avatar
);
},
direct_message_received: (data: any, intl: IntlService) => {
return renderNotificationMessageWithAvatar(
intl.t('notifications.direct_message_received.description', {
influencer_name: data.influencer_name,
entity_url: data.entity_url
}),
intl.t('notifications.direct_message_received.title'),
data.influencer_avatar
);
},
publishr_application_received: (data: any, intl: IntlService) => {
return renderNotificationMessageWithAvatar(
intl.t('notifications.publishr_application_received.description', {
influencer_name: data.influencer_name,
campaign_name: data.campaign_name,
url: data.url
}),
intl.t('notifications.publishr_application_received.title'),
data.influencer_avatar
);
},
publishr_draft_created: (data: any, intl: IntlService) => {
return renderNotificationMessageWithAvatar(
intl.t('notifications.publishr_draft_created.description', {
influencer_name: data.influencer_name,
campaign_name: data.campaign_name,
url: data.url
}),
intl.t('notifications.publishr_draft_created.title'),
data.influencer_avatar
);
},
list_recommendation: (data: any, intl: IntlService) => {
return renderNotificationMessage(
intl.t('notifications.list_recommendation.description', {
count: data.count,
list_name: data.list_name,
url: data.url
}),
intl.t('notifications.list_recommendation.title')
);
},
thread_failure_summary: (data: any, intl: IntlService) => {
return renderNotificationErrorMessage(
intl.t('notifications.thread_failure_summary.description', {
mailing_url: data.mailing_url
}),
intl.t('notifications.thread_failure_summary.title')
);
},
credential_disconnected: (data: any, intl: IntlService) => {
return renderNotificationErrorMessage(
intl.t('notifications.credential_disconnected.description', {
integration_name: data.integration_name,
integration_url: data.integration_url
}),
intl.t('notifications.credential_disconnected.title')
);
}
};
export default class ActivityWatcher extends Service {
@service declare eventsService: EventsService;
@service declare toast: ToastService;
@service declare intl: IntlService;
private declare _observer: Observable<ResourceEvent> | null;
watch(): void {
if (this._observer) {
return;
}
this._observer = this.eventsService.watch(prefixPath('/notification'));
this._observer.subscribe((evt: NotificationEvent) => {
this._processEvent(evt);
});
}
unwatch(): void {
if (!this._observer) {
return;
}
this._observer.unsubscribe();
this._observer = null;
}
private _processEvent(evt: NotificationEvent): void {
const notif = this._buildNotification(evt);
if (!notif) {
return;
}
let toastOpts: ToastOptions | undefined = undefined;
if (notif.avatarUrl) {
toastOpts = { badge: { image: notif.avatarUrl } };
}
switch (notif.type) {
case 'info':
this.toast.info(notif.message, notif.title, toastOpts);
break;
case 'error':
this.toast.error(notif.message, notif.title, toastOpts);
break;
}
}
private _buildNotification(evt: NotificationEvent): RenderedNotification | null {
const renderer = renderersByNotificationType[evt.payload.notification_type];
if (!renderer) {
return null;
}
return renderer(evt.payload.data, this.intl || {});
}
}
declare module '@ember/service' {
interface Registry {
'activity-watcher': ActivityWatcher;
}
}