From 54f27794e7fee1113115baafe1b86fdbcde8e7c2 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Fri, 23 Dec 2022 20:23:41 +0300
Subject: Display new replies as browser notifications
---
src/main/assets/scripts.js | 41 +++++++-
.../resources/templates/views/settings_main.html | 113 ++++++++++++---------
2 files changed, 103 insertions(+), 51 deletions(-)
diff --git a/src/main/assets/scripts.js b/src/main/assets/scripts.js
index 13f0cb75..73ca80df 100644
--- a/src/main/assets/scripts.js
+++ b/src/main/assets/scripts.js
@@ -57,7 +57,10 @@ const translations = {
'loginDialog.facebook': 'Login with Facebook',
'loginDialog.vk': 'Login with VK',
'loginDialog.email': 'Registration',
- 'error.error': 'Error'
+ 'error.error': 'Error',
+ 'settings.notifications.granted': 'Enabled',
+ 'settings.notifications.denied': 'Denied',
+ 'settings.notifications.request': 'Enable',
},
'ru': {
'message.inReplyTo': 'в ответ на',
@@ -82,7 +85,10 @@ const translations = {
'loginDialog.facebook': 'Войти через Facebook',
'loginDialog.vk': 'Войти через ВКонтакте',
'loginDialog.email': 'Регистрация',
- 'error.error': 'Ошибка'
+ 'error.error': 'Ошибка',
+ 'settings.notifications.granted': 'Подключено',
+ 'settings.notifications.denied': 'Запрещено',
+ 'settings.notifications.request': 'Подключить',
}
};
/* eslint-enable only-ascii/only-ascii */
@@ -189,6 +195,11 @@ function wsIncomingReply(msg) {
document.getElementById('replies').appendChild(li);
updateRepliesCounter();
+ var notify = new Notification(msg.user.uname, {
+ body: `${msg.replyQuote}\n${msg.body}`,
+ icon: msg.user.avatar
+ });
+ notify.onerror = console.log;
}
function newReply(e) {
@@ -531,6 +542,24 @@ function fetchUserUri(dataUri, callback) {
}
}
+function notificationsCheckPermissions(button) {
+ console.log(Notification.permission);
+ switch (Notification.permission.toLowerCase()) {
+ case 'granted':
+ button.innerHTML = `${i18n('settings.notifications.granted')}`;
+ button.disabled = true;
+ break;
+
+ case 'denied':
+ button.innerHTML = `${i18n('settings.notifications.denied')}`;
+ button.disabled = true;
+ break;
+
+ case 'default':
+ button.innerHTML = `${i18n('settings.notifications.request')}`;
+ }
+}
+
/******************************************************************************/
function ready(fn) {
@@ -771,6 +800,12 @@ ready(() => {
es.close();
}
});
-
embedAll();
+ const button = document.getElementById('notifications_toggle');
+ if (button) {
+ button.addEventListener('click', () => {
+ Notification.requestPermission(function(result) { notificationsCheckPermissions(button); });
+ });
+ notificationsCheckPermissions(button);
+ }
});
diff --git a/src/main/resources/templates/views/settings_main.html b/src/main/resources/templates/views/settings_main.html
index 0c0853f5..8f3873a0 100644
--- a/src/main/resources/templates/views/settings_main.html
+++ b/src/main/resources/templates/views/settings_main.html
@@ -1,30 +1,36 @@
{% extends "layouts/default" %}
{% block content %}
Настройки
+ Settings
{%if not visitor.verified %}
Verify your account by adding email or social account
{% endif %}
+
Posts recommendations ("Recommended by @user")
- + checked="checked" {% endif %} /> Posts recommendations ("Recommended by @user") +