-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.js
191 lines (164 loc) · 5.22 KB
/
service.js
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
import { alias, notEmpty } from '@ember/object/computed';
import Service, { inject as service } from '@ember/service';
import { cancel, later } from '@ember/runloop';
import { observer } from '@ember/object';
import { getOwner } from '@ember/application';
import Configuration from '@upfluence/ember-upf-utils/configuration';
const notificationMessage = function (message) {
return {
title: `<i class="toast-title__icon upf-icon upf-icon--messages"></i>`,
message: message,
type: 'info'
};
};
const notificationErrorMessage = function (message) {
return {
title: `<i class="fa fa-info-circle" aria-hidden="true"></i>`,
message: message,
type: 'error'
};
};
const notificationMessageWithAvatar = function (avatarUrl, message) {
return {
title: `<img class="toast-title__avatar" src="${avatarUrl}" />
<i class="toast-title__icon upf-icon upf-icon--messages"></i>`,
message: message,
type: 'info'
};
};
export default Service.extend({
ajax: service(),
session: service(),
toast: service(),
_isRunning: false,
_inFetch: false,
_defaultWait: 5,
_from: 0,
_environment: null,
_scope: null,
_timer: null,
start() {
if (this._isRunning || !this.hasToken) {
this.stop();
return;
}
this._environment = getOwner(this).resolveRegistration('config:environment').build_env;
this._scope = Configuration.scope[0];
this._isRunning = true;
this.fetchNotifications();
},
stop() {
this._isRunning = false;
if (this._timer) {
cancel(this._timer);
}
},
fetchNotifications() {
if (!this._isRunning || this._inFetch) {
return;
}
this._inFetch = true;
this.ajax
.request(this.buildUrl(), {
dataType: 'json',
data: this.buildArgs()
})
.then((p) => {
this._from = p.next;
this.displayNotifications(p.notifications);
this._timer = later(this, this.fetchNotifications, this.waitTime());
})
.finally(() => {
this._inFetch = false;
});
},
buildUrl() {
return `${Configuration.activityUrl}notifications`;
},
buildArgs() {
let query = {
from: this._from,
access_token: this.token,
scope: this._scope
};
if (this._environment && this._environment !== 'production') {
query.env = this._environment;
}
return query;
},
waitTime() {
return (Configuration.notificationWait || this._defaultWait) * 1000;
},
displayNotifications(notifications) {
(notifications || []).forEach((n) => {
let d = this.buildNotification(n);
if (!d) {
return;
}
this.renderNotification(d);
});
},
buildNotification(notification) {
let data = notification.data || {};
switch (notification.notification_type) {
case 'mailing_email_received':
return notificationMessageWithAvatar(
data.influencer_avatar,
`Email from <b>${data.influencer_name}</b>
<a href="${data.entity_url}" target="_blank">Reply</a>`
);
case 'conversation_email_received':
return notificationMessageWithAvatar(
data.influencer_avatar,
`Email from <b>${data.influencer_name}</b>
<a href="${data.entity_url}" target="_blank">Reply</a>`
);
case 'direct_message_received':
return notificationMessageWithAvatar(
data.influencer_avatar,
`Message from <b>${data.influencer_name}</b>
<a href="${data.entity_url}" target="_blank">Reply</a>`
);
case 'publishr_application_received':
return notificationMessageWithAvatar(
data.influencer_avatar,
`Application by <b>${data.influencer_name}</b> in <b>${data.campaign_name}</b>
<a href="${data.url}" target="_blank">See application</a>`
);
case 'publishr_draft_created':
return notificationMessageWithAvatar(
data.influencer_avatar,
`Draft by <b>${data.influencer_name}</b> in <b>${data.campaign_name}</b>
<a href="${data.url}" target="_blank">Review</a>`
);
case 'list_recommendation':
return notificationMessage(
`You have <b>${data.count}</b> new recommendations for your <b>${data.list_name}</b> list
<a href="${data.url}" target="_blank">View</a>`
);
case 'thread_failure_summary':
return notificationErrorMessage(
`<b>Mailing error.</b> We ran into a problem with one of your Mailings.
<a href="${data.mailing_url}" target="_blank"><b>View my mailing</b></a>`
);
case 'credential_disconnected':
return notificationErrorMessage(
`<b>Your ${data.integration_name} has been disconnected.</b> Please check your integration
settings and reconnect it to avoid any issues. <a href="${data.integration_url}" target="_blank"><b>Reconnect</b></a>`
);
default:
return null;
}
},
renderNotification(notification) {
this.toast[notification.type](notification.message, notification.title, {
timeOut: 0,
extendedTimeOut: 0
});
},
token: alias('session.data.authenticated.access_token'),
hasToken: notEmpty('token'),
_: observer('hasToken', function () {
this.start();
})
});