Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Oct 19, 2017
1 parent 82e4bdc commit af7ca5a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/embed/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const serviceWorkerName = '/sw.js';
let isSubscribed = false;
let swRegistration = null;

(function() {
(function() {
Notification.requestPermission().then(function (status) {
if (status === 'granted') {
initialiseServiceWorker();
Expand Down Expand Up @@ -66,7 +66,7 @@ function initialiseState(reg) {
function subscribe() {
navigator.serviceWorker.ready.then(function (reg) {
const subscribeParams = {userVisibleOnly: true};

//Setting the public key of our VAPID key pair.
const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
subscribeParams.applicationServerKey = applicationServerKey;
Expand Down Expand Up @@ -112,13 +112,13 @@ function unsubscribe() {
function sendSubscriptionToServer(endpoint, key, auth) {
const encodedKey = btoa(String.fromCharCode.apply(null, new Uint8Array(key)));
const encodedAuth = btoa(String.fromCharCode.apply(null, new Uint8Array(auth)));

fetch('/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({publicKey: encodedKey, auth: encodedAuth, endpoint})
}).then((res) => {
console.log('Subscribed successfully! ' + JSON.stringify(res));
console.log('Subscribed successfully! ' + JSON.stringify(res));
})
}

Expand All @@ -128,7 +128,7 @@ function removeSubscriptionFromServer(endpoint) {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({publicKey: encodedKey, auth: encodedAuth, endpoint})
}).then((res) => {
console.log('Unsubscribed successfully! ' + JSON.stringify(res));
console.log('Unsubscribed successfully! ' + JSON.stringify(res));
})
}

Expand Down
35 changes: 27 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ if (!schnack_url.hostname) {
}
const schnack_domain = schnack_url.hostname.split('.').slice(1).join('.');

function send_file(file, admin_only) {
return send_string(fs.readFileSync(file, 'utf-8'), admin_only);
}

function send_string(body, admin_only) {
return function(request, reply, next) {
if (admin_only) {
const user = getUser(request);
if (!user.admin) return next();
}
if (request.baseUrl.endsWith('.js')) {
reply.header("Content-Type", "application/javascript");
}
reply.send(body);
};
}

function run(db) {
app.use(cors({
credentials: true,
Expand All @@ -46,8 +63,11 @@ function run(db) {
// init session + passport middleware and auth routes
auth.init(app, db, schnack_domain);

// serve /build
app.use(express.static(path.join('build')));
// serve static js files
app.use('/embed.js', send_file('build/embed.js'));
app.use('/sw.js', send_file('src/embed/sw.js', true));
app.use('/push.js', send_string(fs.readFileSync('src/embed/push.js', 'utf-8')
.replace('%VAPID_PUBLIC_KEY%', config.notify.webpush.vapid_public_key), true));

app.get('/comments/:slug', (request, reply) => {
const { slug } = request.params;
Expand Down Expand Up @@ -163,16 +183,16 @@ function run(db) {
// push notifications
app.post('/subscribe', (request, reply) => {
const { endpoint, publicKey, auth } = request.body;

db.run(queries.subscribe, endpoint, publicKey, auth, (err) => {
if (error(err, request, reply)) return;
reply.send({ status: 'ok' });
});
});

app.post('/unsubscribe', (request, reply) => {
const { endpoint } = request.body;

db.run(queries.unsubscribe, endpoint, (err) => {
if (error(err, request, reply)) return;
reply.send({ status: 'ok' });
Expand All @@ -199,7 +219,6 @@ function run(db) {
config.notify.webpush.vapid_private_key
);


notifier.push((msg, callback) => {
db.each(queries.get_subscriptions, (err, row) => {
if (error(err)) return;
Expand All @@ -211,9 +230,9 @@ function run(db) {
auth: row.auth
}
};
webpush.sendNotification(subscription, JSON.stringify({title: 'schnack', message: msg.message, clickTarget: msg.url}))
webpush.sendNotification(subscription, JSON.stringify({title: 'schnack', message: msg.message, clickTarget: msg.url}));
}, callback);
})
});
}

// check for new comments in need of moderation
Expand Down

0 comments on commit af7ca5a

Please sign in to comment.