Skip to content

Commit

Permalink
add event service
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Jul 11, 2019
1 parent a240a4a commit 40d68b1
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Core/Abstractions/IApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ Task PostShareCipherAttachmentAsync(string id, string attachmentId, MultipartFor
Task<List<BreachAccountResponse>> GetHibpBreachAsync(string username);
Task PostTwoFactorEmailAsync(TwoFactorEmailRequest request);
Task PutDeviceTokenAsync(string identifier, DeviceTokenRequest request);
Task PostEventsCollectAsync(EventRequest request);
Task PostEventsCollectAsync(IEnumerable<EventRequest> request);
}
}
12 changes: 12 additions & 0 deletions src/Core/Abstractions/IEventService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;
using Bit.Core.Enums;

namespace Bit.Core.Abstractions
{
public interface IEventService
{
Task ClearEventsAsync();
Task CollectAsync(EventType eventType, string cipherId = null, bool uploadImmediately = false);
Task UploadEventsAsync();
}
}
1 change: 1 addition & 0 deletions src/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static class Constants
public static string MigratedFromV1 = "migratedFromV1";
public static string MigratedFromV1AutofillPromptShown = "migratedV1AutofillPromptShown";
public static string TriedV1Resync = "triedV1Resync";
public static string EventCollectionKey = "eventCollection";
public const int SelectFileRequestCode = 42;
public const int SelectFilePermissionRequestCode = 43;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Models/Data/EventData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Bit.Core.Enums;
using System;

namespace Bit.Core.Models.Data
{
public class EventData : Data
{
public EventType Type { get; set; }
public string CipherId { get; set; }
public DateTime Date { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/Core/Models/Request/EventRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Bit.Core.Enums;
using Bit.Core.Models.Domain;
using System;

namespace Bit.Core.Models.Request
{
public class EventRequest
{
public EventType Type { get; set; }
public string CipherId { get; set; }
public DateTime Date { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Core/Services/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public Task PutDeviceTokenAsync(string identifier, DeviceTokenRequest request)

#region Event APIs

public async Task PostEventsCollectAsync(EventRequest request)
public async Task PostEventsCollectAsync(IEnumerable<EventRequest> request)
{
using(var requestMessage = new HttpRequestMessage())
{
Expand Down
106 changes: 106 additions & 0 deletions src/Core/Services/EventService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Bit.Core.Abstractions;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Data;
using Bit.Core.Models.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Bit.Core.Services
{
public class EventService : IEventService
{
private readonly IStorageService _storageService;
private readonly IApiService _apiService;
private readonly IUserService _userService;
private readonly ICipherService _cipherService;

public EventService(
IStorageService storageService,
IApiService apiService,
IUserService userService,
ICipherService cipherService)
{
_storageService = storageService;
_apiService = apiService;
_userService = userService;
_cipherService = cipherService;
}

public async Task CollectAsync(EventType eventType, string cipherId = null, bool uploadImmediately = false)
{
var authed = await _userService.IsAuthenticatedAsync();
if(!authed)
{
return;
}
var organizations = await _userService.GetAllOrganizationAsync();
if(organizations == null)
{
return;
}
var orgIds = new HashSet<string>(organizations.Where(o => o.UseEvents).Select(o => o.Id));
if(!orgIds.Any())
{
return;
}
if(cipherId != null)
{
var cipher = await _cipherService.GetAsync(cipherId);
if(cipher?.OrganizationId == null || !orgIds.Contains(cipher.OrganizationId))
{
return;
}
}
var eventCollection = await _storageService.GetAsync<List<EventData>>(Constants.EventCollectionKey);
if(eventCollection == null)
{
eventCollection = new List<EventData>();
}
eventCollection.Add(new EventData
{
Type = eventType,
CipherId = cipherId,
Date = DateTime.UtcNow
});
await _storageService.SaveAsync(Constants.EventCollectionKey, eventCollection);
if(uploadImmediately)
{
await UploadEventsAsync();
}
}

public async Task UploadEventsAsync()
{
var authed = await _userService.IsAuthenticatedAsync();
if(!authed)
{
return;
}
var eventCollection = await _storageService.GetAsync<List<EventData>>(Constants.EventCollectionKey);
if(eventCollection == null || !eventCollection.Any())
{
return;
}
var request = eventCollection.Select(e => new EventRequest
{
Type = e.Type,
CipherId = e.CipherId,
Date = e.Date
});
try
{
await _apiService.PostEventsCollectAsync(request);
await ClearEventsAsync();
}
catch(ApiException) { }
}

public async Task ClearEventsAsync()
{
await _storageService.RemoveAsync(Constants.EventCollectionKey);
}
}
}

0 comments on commit 40d68b1

Please sign in to comment.