<commit_before>﻿using System;
using System.Threading.Tasks;
using Exceptionless.Core.Component;
using Exceptionless.Core.Extensions;
using Microsoft.AspNet.SignalR;

namespace Exceptionless.Api.Hubs {
    public class MessageBusConnection : PersistentConnection {
        private readonly ConnectionMapping _userIdConnections;

        public MessageBusConnection(ConnectionMapping userIdConnections) {
            _userIdConnections = userIdConnections;
        }
        
        protected override Task OnConnected(IRequest request, string connectionId) {
            foreach (string organizationId in request.User.GetOrganizationIds())
                Groups.Add(connectionId, organizationId);

            _userIdConnections.Add(request.User.GetUserId(), connectionId);

            return TaskHelper.Completed();
        }

        protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled) {
            _userIdConnections.Remove(request.User.GetUserId(), connectionId);

            return TaskHelper.Completed();
        }

        protected override Task OnReconnected(IRequest request, string connectionId) {
            foreach (string organizationId in request.User.GetOrganizationIds())
                Groups.Add(connectionId, organizationId);

            if (!_userIdConnections.GetConnections(request.User.GetUserId()).Contains(connectionId))
                _userIdConnections.Add(request.User.GetUserId(), connectionId);

            return TaskHelper.Completed();
        }

        protected override bool AuthorizeRequest(IRequest request) {
            return request.User.Identity.IsAuthenticated;
        }
    }
}<commit_msg>Revert "Remove all awaits in the MessageBusConnection"<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Exceptionless.Core.Component;
using Exceptionless.Core.Extensions;
using Foundatio.Logging;
using Microsoft.AspNet.SignalR;

namespace Exceptionless.Api.Hubs {
    public class MessageBusConnection : PersistentConnection {
        private readonly ConnectionMapping _userIdConnections;

        public MessageBusConnection(ConnectionMapping userIdConnections) {
            _userIdConnections = userIdConnections;
        }
        
        protected override async Task OnConnected(IRequest request, string connectionId) {
            try {
                await Task.WhenAll(request.User.GetOrganizationIds().Select(id => Groups.Add(connectionId, id))).AnyContext();
                _userIdConnections.Add(request.User.GetUserId(), connectionId);
            } catch (Exception ex) {
                Logger.Error().Exception(ex).Message($"OnConnected Error: {ex.Message}").Tag("SignalR").Write();
                throw;
            }
        }

        protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled) {
            try {
                _userIdConnections.Remove(request.User.GetUserId(), connectionId);
            } catch (Exception ex) {
                Logger.Error().Exception(ex).Message($"OnDisconnected Error: {ex.Message}").Tag("SignalR").Write();
                throw;
            }

            return TaskHelper.Completed();
        }

        protected override async Task OnReconnected(IRequest request, string connectionId) {
            try {
                await Task.WhenAll(request.User.GetOrganizationIds().Select(id => Groups.Add(connectionId, id))).AnyContext();
                if (!_userIdConnections.GetConnections(request.User.GetUserId()).Contains(connectionId))
                    _userIdConnections.Add(request.User.GetUserId(), connectionId);
            } catch (Exception ex) {
                Logger.Error().Exception(ex).Message($"OnReconnected Error: {ex.Message}").Tag("SignalR").Write();
                throw;
            }
        }

        protected override bool AuthorizeRequest(IRequest request) {
            return request.User.Identity.IsAuthenticated;
        }
    }
}
{'fraction_non_alphanumeric': 0.09261576971214018, 'fraction_numerical': 0.0, 'mean_word_length': 2.752112676056338, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4138069', 'n_tokens_mistral': 966, 'n_tokens_neox': 962, 'n_words': 192}