<commit_before>/* global $ */

import swPromise from './sw.js';
import {sendSubscriptionToServer} from './api.js';
import {notify, warn, error} from './notify';

export default function pushNotifications() {
	const pushBanner = $('#emoji__push');
	if (pushBanner) {
		pushBanner.style.display = 'none';
		pushBanner.on('click', subscribe);
	}
	function subscribe() {
		notify('Subscribing');
		pushBanner.classList.add('working');

		swPromise.then(serviceWorkerRegistration => {
			serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true})
			.then(function(subscription) {
				pushBanner.style.display = 'none';
				return sendSubscriptionToServer(subscription);
				notify('Success!');
			})
			.catch(function(e) {
				if (Notification.permission === 'denied') {
					pushBanner.style.display = '';
					warn('Permission for Notifications was denied');
				} else {

					// A problem occurred with the subscription; common reasons
					// include network errors, and lacking gcm_sender_id and/or
					// gcm_user_visible_only in the manifest.
					error('Unable to subscribe to push.');
					console.log(e);
				}
			})
			.then(() => {
				pushBanner.classList.remove('working');
			});
		});
	}

	swPromise
	.then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription())
	.then(subscription => {
		if (!subscription) {

			if (pushBanner) {

				// Not subscribed: show subscribe button
				pushBanner.style.display = '';
			}
		} else {

			// Update server with correct info.
			return sendSubscriptionToServer(subscription);
		}
	})
	.catch(e => {
<commit_msg>Streamline push notification<commit_after>/* global $ */

import swPromise from './sw.js';
import {sendSubscriptionToServer} from './api.js';
import {notify, warn, error} from './notify';

export default function pushNotifications() {
	const pushBanner = $('#emoji__push');
	if (pushBanner) {
		pushBanner.style.display = 'none';
		pushBanner.on('click', subscribe);
	}
	function subscribe() {
		notify('Subscribing');
		pushBanner.classList.add('working');

		swPromise.then(
			serviceWorkerRegistration => serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true})
		)
		.then(function(subscription) {
			pushBanner.style.display = 'none';
			return sendSubscriptionToServer(subscription);
		})
		.catch(function(e) {
			if (Notification.permission === 'denied') {
				pushBanner.style.display = '';
				warn('Permission for Notifications was denied');
			} else {

				// A problem occurred with the subscription; common reasons
				// include network errors, and lacking gcm_sender_id and/or
				// gcm_user_visible_only in the manifest.
				error('Unable to subscribe to push.');
				console.log(e);
			}
		})
		.then(() => {
			pushBanner.classList.remove('working');
		});
	}

	swPromise
	.then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription())
	.then(subscription => {
		if (!subscription) {

			if (pushBanner) {

				// Not subscribed: show subscribe button
				pushBanner.style.display = '';
			}
		} else {

			// Update server with correct info.
			return sendSubscriptionToServer(subscription);
		}
	})
	.catch(e => {

{'fraction_non_alphanumeric': 0.13856740694401, 'fraction_numerical': 0.0, 'mean_word_length': 3.867579908675799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5094703', 'n_tokens_mistral': 1096, 'n_tokens_neox': 949, 'n_words': 208}