<commit_before>﻿using System.Threading.Tasks;
using System;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Models.Data;
using System.Linq;
using System.Collections.Generic;
using Bit.Core.Models.Table;

namespace Bit.Core.Services
{
    public class EventService : IEventService
    {
        private readonly IEventWriteService _eventWriteService;
        private readonly IOrganizationUserRepository _organizationUserRepository;
        private readonly CurrentContext _currentContext;
        private readonly GlobalSettings _globalSettings;

        public EventService(
            IEventWriteService eventWriteService,
            IOrganizationUserRepository organizationUserRepository,
            CurrentContext currentContext,
            GlobalSettings globalSettings)
        {
            _eventWriteService = eventWriteService;
            _organizationUserRepository = organizationUserRepository;
            _currentContext = currentContext;
            _globalSettings = globalSettings;
        }

        public async Task LogUserEventAsync(Guid userId, EventType type)
        {
            var now = DateTime.UtcNow;
            var events = new List<IEvent>
            {
                new EventMessage
                {
                    UserId = userId,
                    Type = type,
                    Date = now
                }
            };

            IEnumerable<IEvent> orgEvents;
            if(_currentContext.UserId.HasValue)
            {
                orgEvents = _currentContext.Organizations.Select(o => new EventMessage
                {
                    OrganizationId = o.Id,
                    UserId = userId,
                    Type = type,
                    Date = DateTime.UtcNow
                });
            }
            else
            {
                var orgs = await _organizationUserRepository.GetManyByUserAsync(userId);
                orgEvents = orgs.Where(o => o.Status == OrganizationUserStatusType.Confirmed)
                    .Select(o => new EventMessage
                    {
                        OrganizationId = o.OrganizationId,
                        UserId = userId,
                        Type = type,
                        Date = DateTime.UtcNow
                    });
            }

            if(orgEvents.Any())
            {
                events.AddRange(orgEvents);
                await _eventWriteService.CreateManyAsync(events);
            }
            else
            {
                await _eventWriteService.CreateAsync(events.First());
            }
        }

        public async Task LogCipherEventAsync(Cipher cipher, EventType type)
        {
            // Only logging organization cipher events for now.
            if(!cipher.OrganizationId.HasValue || (!_currentContext?.UserId.HasValue ?? true))
            {
                return;
            }

            var e = new EventMessage
            {<commit_msg>set acting user for user events<commit_after>﻿using System.Threading.Tasks;
using System;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Models.Data;
using System.Linq;
using System.Collections.Generic;
using Bit.Core.Models.Table;

namespace Bit.Core.Services
{
    public class EventService : IEventService
    {
        private readonly IEventWriteService _eventWriteService;
        private readonly IOrganizationUserRepository _organizationUserRepository;
        private readonly CurrentContext _currentContext;
        private readonly GlobalSettings _globalSettings;

        public EventService(
            IEventWriteService eventWriteService,
            IOrganizationUserRepository organizationUserRepository,
            CurrentContext currentContext,
            GlobalSettings globalSettings)
        {
            _eventWriteService = eventWriteService;
            _organizationUserRepository = organizationUserRepository;
            _currentContext = currentContext;
            _globalSettings = globalSettings;
        }

        public async Task LogUserEventAsync(Guid userId, EventType type)
        {
            var now = DateTime.UtcNow;
            var events = new List<IEvent>
            {
                new EventMessage
                {
                    UserId = userId,
                    ActingUserId = userId,
                    Type = type,
                    Date = now
                }
            };

            IEnumerable<IEvent> orgEvents;
            if(_currentContext.UserId.HasValue)
            {
                orgEvents = _currentContext.Organizations.Select(o => new EventMessage
                {
                    OrganizationId = o.Id,
                    UserId = userId,
                    ActingUserId = userId,
                    Type = type,
                    Date = DateTime.UtcNow
                });
            }
            else
            {
                var orgs = await _organizationUserRepository.GetManyByUserAsync(userId);
                orgEvents = orgs.Where(o => o.Status == OrganizationUserStatusType.Confirmed)
                    .Select(o => new EventMessage
                    {
                        OrganizationId = o.OrganizationId,
                        UserId = userId,
                        ActingUserId = userId,
                        Type = type,
                        Date = DateTime.UtcNow
                    });
            }

            if(orgEvents.Any())
            {
                events.AddRange(orgEvents);
                await _eventWriteService.CreateManyAsync(events);
            }
            else
            {
                await _eventWriteService.CreateAsync(events.First());
            }
        }

        public async Task LogCipherEventAsync(Cipher cipher, EventType type)
        {
            // Only logging organization cipher events for now.
            if(!cipher.OrganizationId.HasValue || (!_currentContext?.UserId.HasValue ?? true))
            {
                return;
            }

            var e = new EventMessage
            {
{'fraction_non_alphanumeric': 0.06187263452361363, 'fraction_numerical': 0.0, 'mean_word_length': 1.432172869147659, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3428122', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1339, 'n_words': 295}