<commit_before>﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Core.Repositories;
using Newtonsoft.Json.Linq;
using PushSharp.Google;
using PushSharp.Apple;
using Microsoft.AspNetCore.Hosting;
using PushSharp.Core;
using System.Security.Cryptography.X509Certificates;
using Bit.Core.Domains;
using Bit.Core.Enums;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;

namespace Bit.Core.Services
{
    public class PushService : IPushService
    {
        private readonly IDeviceRepository _deviceRepository;
        private readonly ILogger<IPushService> _logger;
        private GcmServiceBroker _gcmBroker;
        private ApnsServiceBroker _apnsBroker;

        public PushService(
            IDeviceRepository deviceRepository,
            ILogger<IPushService> logger,
            IHostingEnvironment hostingEnvironment,
            GlobalSettings globalSettings)
        {
            _deviceRepository = deviceRepository;
            _logger = logger;

            InitGcmBroker(globalSettings);
            InitApnsBroker(globalSettings, hostingEnvironment);
        }

        public async Task PushSyncCipherCreateAsync(Cipher cipher)
        {
            await PushCipherAsync(cipher, PushType.SyncCipherCreate);
        }

        public async Task PushSyncCipherUpdateAsync(Cipher cipher)
        {
            await PushCipherAsync(cipher, PushType.SyncCipherUpdate);
        }

        public async Task PushSyncCipherDeleteAsync(Cipher cipher)
        {
            switch(cipher.Type)
            {
                case CipherType.Folder:
                    await PushCipherAsync(cipher, PushType.SyncFolderDelete);
                    break;
                case CipherType.Site:
                    await PushCipherAsync(cipher, PushType.SyncSiteDelete);
                    break;
                default:
                    break;
            }
        }

        private async Task PushCipherAsync(Cipher cipher, PushType type)
        {
            var message = new SyncCipherPushNotification
            {
                Type = type,
                Id = cipher.Id,
                UserId = cipher.UserId,
                RevisionDate = cipher.RevisionDate,
                Aps = new PushNotification.AppleData { ContentAvailable = 1 }
            };

            await PushToAllUserDevicesAsync(cipher.UserId, JObject.FromObject(message));
        }

        public async Task PushSyncCiphersAsync(Guid userId)
        {
            var message = new SyncCiphersPushNotification
            {
                Type = PushType.SyncCiphers,
                UserId = userId,
                Date = DateTime.UtcNow,
                Aps = new PushNotification.AppleData { ContentAvailable = 1 }
            };

            await PushToAllUserDevicesAsync(userId, new JObject(message));
        }

        private void InitGcmBroker(GlobalSettings globalSettings)
        {
            if(string.IsNullOrWhiteSpace(globalSettings.Push.GcmSenderId) || string.IsNullOrWhiteSpace(globalSettings.Push.GcmApiKey)
                || string.IsNullOrWhiteSpace(globalSettings.Push.GcmAppPackageName))
            {
                return;
            }
<commit_msg>jobject fox for ciphers push<commit_after>﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Core.Repositories;
using Newtonsoft.Json.Linq;
using PushSharp.Google;
using PushSharp.Apple;
using Microsoft.AspNetCore.Hosting;
using PushSharp.Core;
using System.Security.Cryptography.X509Certificates;
using Bit.Core.Domains;
using Bit.Core.Enums;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;

namespace Bit.Core.Services
{
    public class PushService : IPushService
    {
        private readonly IDeviceRepository _deviceRepository;
        private readonly ILogger<IPushService> _logger;
        private GcmServiceBroker _gcmBroker;
        private ApnsServiceBroker _apnsBroker;

        public PushService(
            IDeviceRepository deviceRepository,
            ILogger<IPushService> logger,
            IHostingEnvironment hostingEnvironment,
            GlobalSettings globalSettings)
        {
            _deviceRepository = deviceRepository;
            _logger = logger;

            InitGcmBroker(globalSettings);
            InitApnsBroker(globalSettings, hostingEnvironment);
        }

        public async Task PushSyncCipherCreateAsync(Cipher cipher)
        {
            await PushCipherAsync(cipher, PushType.SyncCipherCreate);
        }

        public async Task PushSyncCipherUpdateAsync(Cipher cipher)
        {
            await PushCipherAsync(cipher, PushType.SyncCipherUpdate);
        }

        public async Task PushSyncCipherDeleteAsync(Cipher cipher)
        {
            switch(cipher.Type)
            {
                case CipherType.Folder:
                    await PushCipherAsync(cipher, PushType.SyncFolderDelete);
                    break;
                case CipherType.Site:
                    await PushCipherAsync(cipher, PushType.SyncSiteDelete);
                    break;
                default:
                    break;
            }
        }

        private async Task PushCipherAsync(Cipher cipher, PushType type)
        {
            var message = new SyncCipherPushNotification
            {
                Type = type,
                Id = cipher.Id,
                UserId = cipher.UserId,
                RevisionDate = cipher.RevisionDate,
                Aps = new PushNotification.AppleData { ContentAvailable = 1 }
            };

            await PushToAllUserDevicesAsync(cipher.UserId, JObject.FromObject(message));
        }

        public async Task PushSyncCiphersAsync(Guid userId)
        {
            var message = new SyncCiphersPushNotification
            {
                Type = PushType.SyncCiphers,
                UserId = userId,
                Date = DateTime.UtcNow,
                Aps = new PushNotification.AppleData { ContentAvailable = 1 }
            };

            await PushToAllUserDevicesAsync(userId, JObject.FromObject(message));
        }

        private void InitGcmBroker(GlobalSettings globalSettings)
        {
            if(string.IsNullOrWhiteSpace(globalSettings.Push.GcmSenderId) || string.IsNullOrWhiteSpace(globalSettings.Push.GcmApiKey)
                || string.IsNullOrWhiteSpace(globalSettings.Push.GcmAppPackageName))
            {
                return;
            }

{'fraction_non_alphanumeric': 0.06352334748332322, 'fraction_numerical': 0.0015160703456640388, 'mean_word_length': 2.033103448275862, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '500541', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1651, 'n_words': 331}