2018-05-19 15:46:47 +03:00
|
|
|
import IntlMessageFormat from 'intl-messageformat';
|
|
|
|
import locales from './web_push_locales';
|
2018-05-21 17:00:56 +03:00
|
|
|
import { unescape } from 'lodash';
|
2018-05-19 15:46:47 +03:00
|
|
|
|
2017-07-28 06:06:22 +03:00
|
|
|
const MAX_NOTIFICATIONS = 5;
|
|
|
|
const GROUP_TAG = 'tag';
|
|
|
|
|
|
|
|
const notify = options =>
|
|
|
|
self.registration.getNotifications().then(notifications => {
|
2018-05-19 15:46:47 +03:00
|
|
|
if (notifications.length >= MAX_NOTIFICATIONS) { // Reached the maximum number of notifications, proceed with grouping
|
2017-07-28 06:06:22 +03:00
|
|
|
const group = {
|
2018-05-19 15:46:47 +03:00
|
|
|
title: formatMessage('notifications.group', options.data.preferred_locale, { count: notifications.length + 1 }),
|
|
|
|
body: notifications.sort((n1, n2) => n1.timestamp < n2.timestamp).map(notification => notification.title).join('\n'),
|
2017-07-28 06:06:22 +03:00
|
|
|
badge: '/badge.png',
|
|
|
|
icon: '/android-chrome-192x192.png',
|
|
|
|
tag: GROUP_TAG,
|
|
|
|
data: {
|
|
|
|
url: (new URL('/web/notifications', self.location)).href,
|
|
|
|
count: notifications.length + 1,
|
2018-05-19 15:46:47 +03:00
|
|
|
preferred_locale: options.data.preferred_locale,
|
2017-07-28 06:06:22 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
notifications.forEach(notification => notification.close());
|
|
|
|
|
|
|
|
return self.registration.showNotification(group.title, group);
|
2018-05-19 15:46:47 +03:00
|
|
|
} else if (notifications.length === 1 && notifications[0].tag === GROUP_TAG) { // Already grouped, proceed with appending the notification to the group
|
2018-05-27 05:55:19 +03:00
|
|
|
const group = cloneNotification(notifications[0]);
|
2017-07-28 06:06:22 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
group.title = formatMessage('notifications.group', options.data.preferred_locale, { count: group.data.count + 1 });
|
2017-09-01 14:35:23 +03:00
|
|
|
group.body = `${options.title}\n${group.body}`;
|
|
|
|
group.data = { ...group.data, count: group.data.count + 1 };
|
2017-07-28 06:06:22 +03:00
|
|
|
|
|
|
|
return self.registration.showNotification(group.title, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.registration.showNotification(options.title, options);
|
|
|
|
});
|
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const fetchFromApi = (path, method, accessToken) => {
|
|
|
|
const url = (new URL(path, self.location)).href;
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
return fetch(url, {
|
|
|
|
headers: {
|
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
method: method,
|
|
|
|
credentials: 'include',
|
|
|
|
}).then(res => {
|
|
|
|
if (res.ok) {
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
throw new Error(res.status);
|
|
|
|
}
|
|
|
|
}).then(res => res.json());
|
2017-07-13 23:15:32 +03:00
|
|
|
};
|
|
|
|
|
2018-05-27 05:55:19 +03:00
|
|
|
const cloneNotification = notification => {
|
|
|
|
const clone = {};
|
|
|
|
let k;
|
|
|
|
|
|
|
|
// Object.assign() does not work with notifications
|
|
|
|
for(k in notification) {
|
|
|
|
clone[k] = notification[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const formatMessage = (messageId, locale, values = {}) =>
|
|
|
|
(new IntlMessageFormat(locales[locale][messageId], locale)).format(values);
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2018-05-21 04:35:37 +03:00
|
|
|
const htmlToPlainText = html =>
|
2018-05-21 17:00:56 +03:00
|
|
|
unescape(html.replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n').replace(/<[^>]*>/g, ''));
|
2018-05-21 04:35:37 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const handlePush = (event) => {
|
|
|
|
const { access_token, notification_id, preferred_locale, title, body, icon } = event.data.json();
|
|
|
|
|
|
|
|
// Placeholder until more information can be loaded
|
|
|
|
event.waitUntil(
|
2018-08-23 22:44:27 +03:00
|
|
|
fetchFromApi(`/api/v1/notifications/${notification_id}`, 'get', access_token).then(notification => {
|
2018-05-19 15:46:47 +03:00
|
|
|
const options = {};
|
|
|
|
|
|
|
|
options.title = formatMessage(`notification.${notification.type}`, preferred_locale, { name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
|
2018-05-21 04:35:37 +03:00
|
|
|
options.body = notification.status && htmlToPlainText(notification.status.content);
|
2018-05-19 15:46:47 +03:00
|
|
|
options.icon = notification.account.avatar_static;
|
|
|
|
options.timestamp = notification.created_at && new Date(notification.created_at);
|
|
|
|
options.tag = notification.id;
|
|
|
|
options.badge = '/badge.png';
|
|
|
|
options.image = notification.status && notification.status.media_attachments.length > 0 && notification.status.media_attachments[0].preview_url || undefined;
|
2021-09-26 06:46:13 +03:00
|
|
|
options.data = { access_token, preferred_locale, id: notification.status ? notification.status.id : notification.account.id, url: notification.status ? `/web/@${notification.account.acct}/${notification.status.id}` : `/web/@${notification.account.acct}` };
|
2018-05-19 15:46:47 +03:00
|
|
|
|
2019-01-11 09:29:17 +02:00
|
|
|
if (notification.status && notification.status.spoiler_text || notification.status.sensitive) {
|
2018-05-21 04:35:37 +03:00
|
|
|
options.data.hiddenBody = htmlToPlainText(notification.status.content);
|
2018-05-19 15:46:47 +03:00
|
|
|
options.data.hiddenImage = notification.status.media_attachments.length > 0 && notification.status.media_attachments[0].preview_url;
|
|
|
|
|
2019-01-11 09:29:17 +02:00
|
|
|
if (notification.status.spoiler_text) {
|
|
|
|
options.body = notification.status.spoiler_text;
|
|
|
|
}
|
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
options.image = undefined;
|
|
|
|
options.actions = [actionExpand(preferred_locale)];
|
2018-05-27 05:55:19 +03:00
|
|
|
} else if (notification.type === 'mention') {
|
2018-05-19 15:46:47 +03:00
|
|
|
options.actions = [actionReblog(preferred_locale), actionFavourite(preferred_locale)];
|
|
|
|
}
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
return notify(options);
|
2018-08-23 22:44:27 +03:00
|
|
|
}).catch(() => {
|
|
|
|
return notify({
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
icon,
|
|
|
|
tag: notification_id,
|
|
|
|
timestamp: new Date(),
|
|
|
|
badge: '/badge.png',
|
|
|
|
data: { access_token, preferred_locale, url: '/web/notifications' },
|
|
|
|
});
|
2020-03-08 17:02:36 +02:00
|
|
|
}),
|
2018-05-19 15:46:47 +03:00
|
|
|
);
|
2017-07-13 23:15:32 +03:00
|
|
|
};
|
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const actionExpand = preferred_locale => ({
|
|
|
|
action: 'expand',
|
|
|
|
icon: '/web-push-icon_expand.png',
|
|
|
|
title: formatMessage('status.show_more', preferred_locale),
|
|
|
|
});
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const actionReblog = preferred_locale => ({
|
|
|
|
action: 'reblog',
|
|
|
|
icon: '/web-push-icon_reblog.png',
|
|
|
|
title: formatMessage('status.reblog', preferred_locale),
|
|
|
|
});
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const actionFavourite = preferred_locale => ({
|
|
|
|
action: 'favourite',
|
|
|
|
icon: '/web-push-icon_favourite.png',
|
|
|
|
title: formatMessage('status.favourite', preferred_locale),
|
|
|
|
});
|
2017-07-13 23:15:32 +03:00
|
|
|
|
2017-07-28 06:06:01 +03:00
|
|
|
const findBestClient = clients => {
|
|
|
|
const focusedClient = clients.find(client => client.focused);
|
|
|
|
const visibleClient = clients.find(client => client.visibilityState === 'visible');
|
|
|
|
|
|
|
|
return focusedClient || visibleClient || clients[0];
|
|
|
|
};
|
|
|
|
|
2018-05-19 15:46:47 +03:00
|
|
|
const expandNotification = notification => {
|
2018-05-27 05:55:19 +03:00
|
|
|
const newNotification = cloneNotification(notification);
|
2018-05-19 15:46:47 +03:00
|
|
|
|
|
|
|
newNotification.body = newNotification.data.hiddenBody;
|
|
|
|
newNotification.image = newNotification.data.hiddenImage;
|
|
|
|
newNotification.actions = [actionReblog(notification.data.preferred_locale), actionFavourite(notification.data.preferred_locale)];
|
|
|
|
|
|
|
|
return self.registration.showNotification(newNotification.title, newNotification);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeActionFromNotification = (notification, action) => {
|
2018-05-27 05:55:19 +03:00
|
|
|
const newNotification = cloneNotification(notification);
|
2018-05-19 15:46:47 +03:00
|
|
|
|
|
|
|
newNotification.actions = newNotification.actions.filter(item => item.action !== action);
|
|
|
|
|
|
|
|
return self.registration.showNotification(newNotification.title, newNotification);
|
|
|
|
};
|
|
|
|
|
2017-07-18 01:19:17 +03:00
|
|
|
const openUrl = url =>
|
|
|
|
self.clients.matchAll({ type: 'window' }).then(clientList => {
|
2017-07-28 06:06:01 +03:00
|
|
|
if (clientList.length !== 0) {
|
|
|
|
const webClients = clientList.filter(client => /\/web\//.test(client.url));
|
2017-07-18 01:19:17 +03:00
|
|
|
|
2017-07-28 06:06:01 +03:00
|
|
|
if (webClients.length !== 0) {
|
2017-09-01 14:35:23 +03:00
|
|
|
const client = findBestClient(webClients);
|
2018-09-24 06:45:58 +03:00
|
|
|
const { pathname } = new URL(url, self.location);
|
2017-07-18 01:19:17 +03:00
|
|
|
|
2017-07-28 06:06:01 +03:00
|
|
|
if (pathname.startsWith('/web/')) {
|
|
|
|
return client.focus().then(client => client.postMessage({
|
|
|
|
type: 'navigate',
|
|
|
|
path: pathname.slice('/web/'.length - 1),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else if ('navigate' in clientList[0]) { // Chrome 42-48 does not support navigate
|
|
|
|
const client = findBestClient(clientList);
|
|
|
|
|
|
|
|
return client.navigate(url).then(client => client.focus());
|
|
|
|
}
|
2017-07-18 01:19:17 +03:00
|
|
|
}
|
2017-07-28 06:06:01 +03:00
|
|
|
|
|
|
|
return self.clients.openWindow(url);
|
2017-07-18 01:19:17 +03:00
|
|
|
});
|
|
|
|
|
2017-07-13 23:15:32 +03:00
|
|
|
const handleNotificationClick = (event) => {
|
|
|
|
const reactToNotificationClick = new Promise((resolve, reject) => {
|
|
|
|
if (event.action) {
|
2018-05-19 15:46:47 +03:00
|
|
|
if (event.action === 'expand') {
|
2017-07-13 23:15:32 +03:00
|
|
|
resolve(expandNotification(event.notification));
|
2018-05-19 15:46:47 +03:00
|
|
|
} else if (event.action === 'reblog') {
|
|
|
|
const { data } = event.notification;
|
|
|
|
resolve(fetchFromApi(`/api/v1/statuses/${data.id}/reblog`, 'post', data.access_token).then(() => removeActionFromNotification(event.notification, 'reblog')));
|
|
|
|
} else if (event.action === 'favourite') {
|
|
|
|
const { data } = event.notification;
|
|
|
|
resolve(fetchFromApi(`/api/v1/statuses/${data.id}/favourite`, 'post', data.access_token).then(() => removeActionFromNotification(event.notification, 'favourite')));
|
2017-07-13 23:15:32 +03:00
|
|
|
} else {
|
2018-05-19 15:46:47 +03:00
|
|
|
reject(`Unknown action: ${event.action}`);
|
2017-07-13 23:15:32 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.notification.close();
|
2017-07-18 01:19:17 +03:00
|
|
|
resolve(openUrl(event.notification.data.url));
|
2017-07-13 23:15:32 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
event.waitUntil(reactToNotificationClick);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.addEventListener('push', handlePush);
|
|
|
|
self.addEventListener('notificationclick', handleNotificationClick);
|