<commit_before>/**
 *  Copyright (C) 2016 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  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
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
(function(){
	"use strict";

	angular.module("3drepo")
		.service("NotificationService", NotificationService);

	NotificationService.$inject = ["ClientConfigService", "$injector", "DialogService"];

	function NotificationService(ClientConfigService, $injector, DialogService){
		

		if(!ClientConfigService.chatHost || !ClientConfigService.chatPath){
			console.error("Chat server settings missing");
			return;
		}

		var socket = io(ClientConfigService.chatHost, {
			path: ClientConfigService.chatPath, 
			transports: ["websocket"],
			reconnection: true,
			reconnectionDelay: 500,
			reconnectionAttempts: ClientConfigService.chatReconnectionAttempts || Infinity
		});

		var joined = [];

		function addSocketIdToHeader(socketId){
			
			var $httpProvider = $injector.get("$http");

			$httpProvider.defaults.headers.post = $httpProvider.defaults.headers.post || {};
			$httpProvider.defaults.headers.put = $httpProvider.defaults.headers.put || {};
			$httpProvider.defaults.headers.delete = $httpProvider.defaults.headers.delete || {};

			$httpProvider.defaults.headers.post["x-socket-id"] = socketId;
			$httpProvider.defaults.headers.put["x-socket-id"] = socketId;
			$httpProvider.defaults.headers.delete["x-socket-id"] = socketId;
		}

		socket.on("connect", function(){
			addSocketIdToHeader(socket.id);
		});

		socket.on("disconnect", function(){
			var content = "Your connection to the 3D Repo's notification service has dropped. " + 
			"3D Repo may not behave as expected when commenting and changing issues. Try refreshing the page" +
			" to reconnect.";

			// Stops it from appearing upon referesh
			setTimeout(function(){
				DialogService.text("Notification Service Disconnected", content, true);
			}, 1500);
			
			console.error("The websocket for the notification service was disconnected");
		});

		socket.on("reconnect", function(){
			console.log("Rejoining all rooms on reconnect");
			addSocketIdToHeader(socket.id);

			var lastJoined = joined.slice(0);
			joined = [];

			lastJoined.forEach(function(room){

				room = room.split("::");

				var account = room[0];
				var model = room[1];<commit_msg>ISSUE #619 - Add debouncing for disconnection notifications<commit_after>/**
 *  Copyright (C) 2016 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  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
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
(function(){
	"use strict";

	angular.module("3drepo")
		.service("NotificationService", NotificationService);

	NotificationService.$inject = ["ClientConfigService", "$injector", "DialogService"];

	function NotificationService(ClientConfigService, $injector, DialogService){
		

		var dialogOpen = false;
		var lastDialogOpen;

		if(!ClientConfigService.chatHost || !ClientConfigService.chatPath){
			console.error("Chat server settings missing");
			return;
		}

		var socket = io(ClientConfigService.chatHost, {
			path: ClientConfigService.chatPath, 
			transports: ["websocket"],
			reconnection: true,
			reconnectionDelay: 500,
			reconnectionAttempts: ClientConfigService.chatReconnectionAttempts || Infinity
		});

		var joined = [];

		function addSocketIdToHeader(socketId){
			
			var $httpProvider = $injector.get("$http");

			$httpProvider.defaults.headers.post = $httpProvider.defaults.headers.post || {};
			$httpProvider.defaults.headers.put = $httpProvider.defaults.headers.put || {};
			$httpProvider.defaults.headers.delete = $httpProvider.defaults.headers.delete || {};

			$httpProvider.defaults.headers.post["x-socket-id"] = socketId;
			$httpProvider.defaults.headers.put["x-socket-id"] = socketId;
			$httpProvider.defaults.headers.delete["x-socket-id"] = socketId;
		}

		socket.on("connect", function(){
			addSocketIdToHeader(socket.id);
		});



		socket.on("disconnect", function(){

			console.error("The websocket for the notification service was disconnected");

			var content = "Your connection to the 3D Repo's notification service has dropped. " + 
			"3D Repo may not behave as expected when commenting and changing issues. Try refreshing the page" +
			" to reconnect.";

			var thirtySeconds = 30 * 1000; /* ms */
			var lessThanThirtySeconds = true;

			if (lastDialogOpen !== undefined) {
				lessThanThirtySeconds = ((new Date) - lastDialogOpen) > thirtySeconds;
				console.debug("Not showing user connection error dialog as it's been less than 30 seconds");
			}
			
			// Stops it from appearing upon referesh
			if (dialogOpen === false && lessThanThirtySeconds) {
				dialogOpen = true;
				lastDialogOpen = Date.now();
				DialogService.text("Notification Service Disconnected", content, true).then(function(){
					dialogOpen = false;
				});
			}
			
			
		});

		socket.on("reconnect", function(){
			console.log("Rejoining all rooms on reconnect");
			addSocketIdToHeader(socket.id);

			var lastJoined = joined.slice(0);
			joined = [];

			lastJoined.forEach(function(room){

				room = room.split("::");

				var account = room[0];
				var model = room[1];
{'fraction_non_alphanumeric': 0.11591667991731595, 'fraction_numerical': 0.007155350612179997, 'mean_word_length': 4.109666937449228, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 6, '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': '3743742', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1730, 'n_words': 527}