Commit 446c6ea5 authored by Curtis Adam's avatar Curtis Adam

Merge branch 'staging' into 'master'

Staging

See merge request arsnova/flashcards!1732
parents 3c554687 b4410e77
import {Meteor} from "meteor/meteor"; import {Meteor} from "meteor/meteor";
import {WebPushNotifications} from "./webPushSubscriptions";
export let CardsetNavigation = class CardsetNavigation { export let CardsetNavigation = class CardsetNavigation {
/**
* Creates a web push subscription for the current device.
* The Browser ask the user for permissions and creates the subscription.
* Afterwards the subscription will be saved for the current user via the
* Meteor-method addWebPushSubscription.
*/
static subscribeForPushNotification () {
try {
navigator.serviceWorker.getRegistration()
.then(function (registration) {
return registration.pushManager.getSubscription()
.then(function (subscription) {
if (!subscription) {
return registration.pushManager.subscribe({userVisibleOnly: true});
}
});
})
.then(function (subscription) {
if (subscription) {
var rawKey = subscription.getKey ? subscription.getKey('p256dh') : '';
const key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : '';
var 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);
}
}
/** /**
* Add the current user to the leitner algorithm. * Add the current user to the leitner algorithm.
*/ */
static addToLeitner (cardset_id) { static addToLeitner (cardset_id) {
this.subscribeForPushNotification(); WebPushNotifications.subscribeForPushNotification();
Meteor.call('addToLeitner', cardset_id); Meteor.call('addToLeitner', cardset_id);
} }
}; };
...@@ -4,6 +4,47 @@ import webPush from 'web-push'; ...@@ -4,6 +4,47 @@ import webPush from 'web-push';
export const WebPushSubscriptions = new Mongo.Collection("webPushSubscriptions"); export const WebPushSubscriptions = new Mongo.Collection("webPushSubscriptions");
export let WebPushNotifications = class WebPushNotifications {
/**
* Creates a web push subscription for the current device.
* The Browser ask the user for permissions and creates the subscription.
* Afterwards the subscription will be saved for the current user via the
* Meteor-method addWebPushSubscription.
*/
static subscribeForPushNotification () {
try {
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);
}
}
};
Meteor.methods({ Meteor.methods({
/** /**
* Stores a web push subscription for the current user in the "webPushSubscriptions" collection. * Stores a web push subscription for the current user in the "webPushSubscriptions" collection.
......
...@@ -8,7 +8,7 @@ import {Route} from "../../api/route.js"; ...@@ -8,7 +8,7 @@ import {Route} from "../../api/route.js";
import {CardEditor} from "../../api/cardEditor"; import {CardEditor} from "../../api/cardEditor";
import {Filter} from "../../api/filter"; import {Filter} from "../../api/filter";
import {MarkdeepEditor} from "../../api/markdeepEditor"; import {MarkdeepEditor} from "../../api/markdeepEditor";
import {CardsetNavigation} from "../../api/cardsetNavigation"; import {WebPushNotifications} from "../../api/webPushSubscriptions";
let loadingScreenTemplate = 'loadingScreen'; let loadingScreenTemplate = 'loadingScreen';
...@@ -834,7 +834,7 @@ Router.route('/admin/settings', { ...@@ -834,7 +834,7 @@ Router.route('/admin/settings', {
layoutTemplate: 'admin_main', layoutTemplate: 'admin_main',
data: function () { data: function () {
Session.set('helpFilter', undefined); Session.set('helpFilter', undefined);
CardsetNavigation.subscribeForPushNotification(); WebPushNotifications.subscribeForPushNotification();
}, },
action: function () { action: function () {
if (this.ready()) { if (this.ready()) {
......
...@@ -4,6 +4,11 @@ import {Leitner} from "../imports/api/learned.js"; ...@@ -4,6 +4,11 @@ import {Leitner} from "../imports/api/learned.js";
import {AdminSettings} from "../imports/api/adminSettings.js"; import {AdminSettings} from "../imports/api/adminSettings.js";
import {Cardsets} from "../imports/api/cardsets.js"; import {Cardsets} from "../imports/api/cardsets.js";
function getDateString(date) {
let dateFormat = "D. MMMM YYYY";
return moment(date).locale(Meteor.settings.mail.language).format(dateFormat);
}
/** /**
* Class used for generating the text of web-push notifications * Class used for generating the text of web-push notifications
*/ */
...@@ -24,9 +29,9 @@ export class WebNotifier { ...@@ -24,9 +29,9 @@ export class WebNotifier {
deadline = new Date(active.currentDate.getTime() + cardset.daysBeforeReset * 86400000); deadline = new Date(active.currentDate.getTime() + cardset.daysBeforeReset * 86400000);
} }
if (deadline.getTime() > cardset.learningEnd.getTime()) { if (deadline.getTime() > cardset.learningEnd.getTime()) {
return (TAPi18n.__('notifications.deadline') + cardset.learningEnd.toLocaleDateString()); return (TAPi18n.__('notifications.deadline', null, Meteor.settings.mail.language) + getDateString(cardset.learningEnd));
} else { } else {
return (TAPi18n.__('notifications.deadline') + deadline.toLocaleDateString() + TAPi18n.__('notifications.warning')); return (TAPi18n.__('notifications.deadline', null, Meteor.settings.mail.language) + getDateString(deadline) + TAPi18n.__('notifications.warning', null, Meteor.settings.mail.language));
} }
} }
} }
...@@ -40,7 +45,7 @@ export class WebNotifier { ...@@ -40,7 +45,7 @@ export class WebNotifier {
throw new Meteor.Error("not-authorized"); throw new Meteor.Error("not-authorized");
} else { } else {
var notifier = new Notifications(); var notifier = new Notifications();
var message = TAPi18n.__('notifications.content') + cardset.name + TAPi18n.__('notifications.cards') + notifier.getActiveCardsCount(cardset._id, user_id) + this.getDeadline(cardset, user_id); var message = TAPi18n.__('notifications.content', null, Meteor.settings.mail.language) + cardset.name + TAPi18n.__('notifications.cards', null, Meteor.settings.mail.language) + notifier.getActiveCardsCount(cardset._id, user_id) + this.getDeadline(cardset, user_id);
Meteor.call("sendPushNotificationsToUser", user_id, message); Meteor.call("sendPushNotificationsToUser", user_id, message);
} }
} }
......
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