<commit_before>/* global Notification */

/**
 * @author <NAME> <<EMAIL>>
 *
 * Mail
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

define(function(require) {
	'use strict';

	var _ = require('underscore');
	var OC = require('OC');
	var Radio = require('radio');

	Radio.ui.on('notification:mail:show', showMailNotification);
	Radio.ui.on('notification:request', requestNotification);

	function requestNotification() {
		if (typeof Notification !== 'undefined') {
			Notification.requestPermission();
		}
	}

	function showNotification(title, body, icon) {
		// notifications not supported -> go away
		if (typeof Notification === 'undefined') {
			return;
		}
		// browser is active -> go away
		var isWindowFocused = document.querySelector(':focus') !== null;
		if (isWindowFocused) {
			return;
		}
		var notification = new Notification(
				title,
				{
					body: body,
					icon: icon
				}
		);
		notification.onclick = function() {
			window.focus();
		};
	}

	/**
	 * @param {array<Message>} messages
	 * @returns {undefined}
	 */
	function showMailNotification(messages) {
		if (messages.length === 0) {
			// Ignore
			return;
		}

		if (Notification.permission !== 'granted') {
			// Don't show a notification
			return;
		}

		// Update favicon to show red dot
		Radio.notification.trigger('favicon:change', OC.filePath('mail', 'img', 'favicon-notification.svg'));

		var from = _.map(messages, function(m) {
			return m.get('from');
		});
		from = _.uniq(from);
		if (from.length > 2) {
			from = from.slice(0, 2);
			from.push('…');
		} else {
			from = from.slice(0, 2);
		}
		// special layout if there is only 1 new message
		var body = '';
		if (messages.length === 1) {<commit_msg>Update favicon regardless of notification permissions<commit_after>/* global Notification */

/**
 * @author <NAME> <<EMAIL>>
 *
 * Mail
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

define(function(require) {
	'use strict';

	var _ = require('underscore');
	var OC = require('OC');
	var Radio = require('radio');

	Radio.ui.on('notification:mail:show', showMailNotification);
	Radio.ui.on('notification:request', requestNotification);

	function requestNotification() {
		if (typeof Notification !== 'undefined') {
			Notification.requestPermission();
		}
	}

	function showNotification(title, body, icon) {
		// notifications not supported -> go away
		if (typeof Notification === 'undefined') {
			return;
		}
		// browser is active -> go away
		var isWindowFocused = document.querySelector(':focus') !== null;
		if (isWindowFocused) {
			return;
		}
		var notification = new Notification(
				title,
				{
					body: body,
					icon: icon
				}
		);
		notification.onclick = function() {
			window.focus();
		};
	}

	/**
	 * @param {array<Message>} messages
	 * @returns {undefined}
	 */
	function showMailNotification(messages) {
		if (messages.length === 0) {
			// Ignore
			return;
		}

		// Update favicon to show red dot
		Radio.notification.trigger('favicon:change', OC.filePath('mail', 'img', 'favicon-notification.svg'));

		if (Notification.permission !== 'granted') {
			// Don't show a notification
			return;
		}

		var from = _.map(messages, function(m) {
			return m.get('from');
		});
		from = _.uniq(from);
		if (from.length > 2) {
			from = from.slice(0, 2);
			from.push('…');
		} else {
			from = from.slice(0, 2);
		}
		// special layout if there is only 1 new message
		var body = '';
		if (messages.length === 1) {
{'fraction_non_alphanumeric': 0.13365259033521984, 'fraction_numerical': 0.0043535045711798, 'mean_word_length': 3.4353281853281854, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2465001', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1342, 'n_words': 451}