Commit e84cfc1f authored by Curtis Adam's avatar Curtis Adam

Add a Backend setting to toggle push notifications

parent 16452aa6
......@@ -2055,7 +2055,12 @@
"visible_invalid": "Das Profil muss bei der Wahl der Rollen öffentlich sein!"
},
"mail-settings": {
"mail-settings": "Versenden von E-Mail-Benachrichtigungen",
"title": "Versenden von E-Mail-Benachrichtigungen",
"enabled": "Aktiviert",
"disabled": "Deaktiviert"
},
"push-settings": {
"title": "Versenden von Push-Benachrichtigungen",
"enabled": "Aktiviert",
"disabled": "Deaktiviert"
}
......
......@@ -2056,7 +2056,12 @@
"visible_invalid": "Das Profil muss bei der Wahl der Rollen öffentlich sein!"
},
"mail-settings": {
"mail-settings": "Versenden von E-Mail-Benachrichtigungen",
"title": "Versenden von E-Mail-Benachrichtigungen",
"enabled": "Aktiviert",
"disabled": "Deaktiviert"
},
"push-settings": {
"title": "Versenden von Push-Benachrichtigungen",
"enabled": "Aktiviert",
"disabled": "Deaktiviert"
}
......
import {Meteor} from "meteor/meteor";
import {Mongo} from "meteor/mongo";
import {check} from "meteor/check";
import {UserPermissions} from "./permissions";
export const AdminSettings = new Mongo.Collection("adminSettings");
......@@ -10,10 +11,10 @@ if (Meteor.isServer) {
});
Meteor.publish('default_db_data', function () {
if (this.userId && !Roles.userIsInRole(this.userId, ["firstLogin", "blocked"])) {
if (Roles.userIsInRole(this.userId, ["admin", "editor"])) {
if (UserPermissions.gotBackendAccess()) {
return AdminSettings.find({});
} else {
return AdminSettings.find({name: "seqSettings"});
return AdminSettings.find({name: {$in: ["seqSettings", 'mailSettings', 'pushSettings']}});
}
}
});
......@@ -50,6 +51,21 @@ Meteor.methods({
}
});
},
updatePushSettings: function (enablePush) {
check(enablePush, Boolean);
if (!UserPermissions.gotBackendAccess()) {
throw new Meteor.Error("not-authorized");
}
AdminSettings.upsert({
name: "pushSettings"
},
{
$set: {
enabled: enablePush
}
});
},
changeNotificationTarget: function (target) {
check(target, String);
......
import {Meteor} from "meteor/meteor";
import {Mongo} from "meteor/mongo";
import webPush from 'web-push';
import {AdminSettings} from "./adminSettings";
export const WebPushSubscriptions = new Mongo.Collection("webPushSubscriptions");
......@@ -12,39 +13,45 @@ export let WebPushNotifications = class WebPushNotifications {
* Meteor-method addWebPushSubscription.
*/
static subscribeForPushNotification () {
try {
if (navigator.serviceWorker !== undefined) {
navigator.serviceWorker.getRegistration()
.then(function (registration) {
return registration.pushManager.getSubscription()
.then(function () {
return registration.pushManager.subscribe({userVisibleOnly: true});
});
})
.then(function (subscription) {
if (subscription) {
let rawKey = subscription.getKey ? subscription.getKey('p256dh') : '';
const key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : '';
let rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : '';
const authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : '';
const endpoint = subscription.endpoint;
const sub = {
endpoint: endpoint,
key: key,
authSecret: authSecret
};
Meteor.call("addWebPushSubscription", sub, function (error) {
if (error) {
throw new Meteor.Error(error.statusCode, 'Error subscription failed');
}
});
}
});
if (this.isPushEnabled()) {
try {
if (navigator.serviceWorker !== undefined) {
navigator.serviceWorker.getRegistration()
.then(function (registration) {
return registration.pushManager.getSubscription()
.then(function () {
return registration.pushManager.subscribe({userVisibleOnly: true});
});
})
.then(function (subscription) {
if (subscription) {
let rawKey = subscription.getKey ? subscription.getKey('p256dh') : '';
const key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : '';
let rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : '';
const authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : '';
const endpoint = subscription.endpoint;
const sub = {
endpoint: endpoint,
key: key,
authSecret: authSecret
};
Meteor.call("addWebPushSubscription", sub, function (error) {
if (error) {
throw new Meteor.Error(error.statusCode, 'Error subscription failed');
}
});
}
});
}
} catch (error) {
console.log(error);
}
} catch (error) {
console.log(error);
}
}
static isPushEnabled () {
return AdminSettings.findOne({name: "pushSettings"}).enabled;
}
};
Meteor.methods({
......
......@@ -405,6 +405,13 @@ Meteor.startup(function () {
});
}
if (!AdminSettings.findOne({name: "pushSettings"})) {
AdminSettings.insert({
name: "pushSettings",
enabled: true
});
}
if (!AdminSettings.findOne({name: "wordcloudPomodoroSettings"})) {
AdminSettings.insert({
name: "wordcloudPomodoroSettings",
......
......@@ -27,7 +27,7 @@
<div class="panel panel-default">
<div class="panel-heading panel-heading-mail">
<h3 class="panel-title">{{_ "admin.mail-settings.mail-settings"}}</h3>
<h3 class="panel-title">{{_ "admin.mail-settings.title"}}</h3>
</div>
<div class="panel-body panel-body-mail">
<div class="row" style="margin-bottom:10px; margin-left: 5px">
......@@ -43,6 +43,26 @@
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading panel-heading-mail">
<h3 class="panel-title">{{_ "admin.push-settings.title"}}</h3>
</div>
<div class="panel-body panel-body-mail">
<div class="row" style="margin-bottom:10px; margin-left: 5px">
<form>
{{#if isPushEnabled}}
<a id="disablePush" class="btn btn-raised btn-success">
<span class="flex-content">{{_ "admin.push-settings.enabled"}}</span></a>
{{else}}
<a id="enablePush" class="btn btn-raised btn-danger">
<span class="flex-content">{{_ "admin.push-settings.disabled"}}</span></a>
{{/if}}
</form>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading panel-heading-test-notifications">
......
......@@ -15,6 +15,12 @@ Template.admin_settings.events({
'click #disableMail': function () {
Meteor.call("updateMailSettings", false);
},
'click #enablePush': function () {
Meteor.call("updatePushSettings", true);
},
'click #disablePush': function () {
Meteor.call("updatePushSettings", false);
},
'click #setNotificationTarget': function (evt, tmpl) {
Meteor.call('changeNotificationTarget', tmpl.find('#notificationTarget').value, function (error, result) {
if (error || result === false) {
......@@ -69,6 +75,9 @@ Template.admin_settings.helpers({
isMailEnabled: function () {
return AdminSettings.findOne({name: "mailSettings"}).enabled;
},
isPushEnabled: function () {
return AdminSettings.findOne({name: "pushSettings"}).enabled;
},
getNotificationTargetText: function () {
let user = Meteor.users.findOne({_id: AdminSettings.findOne({name: "testNotifications"}).target});
if (user === undefined) {
......
......@@ -6,6 +6,7 @@ import {WebNotifier} from "./sendwebpush.js";
import {Bonus} from "../imports/api/bonus";
import {AdminSettings} from "../imports/api/adminSettings.js";
import {LeitnerUtilities} from "../imports/api/leitner";
import {WebPushSubscriptions} from "../imports/api/webPushSubscriptions";
/** Function checks if mail notifications are globally disabled by the admin
* @returns {boolean} - Mail notifications are globally enabled / disabled
......@@ -152,7 +153,7 @@ Meteor.methods({
},
prepareWebpush: function (cardset, user, isNewcomer = false) {
if (Meteor.isServer) {
if ((Bonus.isInBonus(cardset._id, user._id) || user.webNotification) && !isNewcomer) {
if (WebPushSubscriptions.isPushEnabled() && (Bonus.isInBonus(cardset._id, user._id) || user.webNotification) && !isNewcomer) {
try {
let web = new WebNotifier();
if (Meteor.settings.debug.leitner) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment