diff --git a/Otakians/MainLayout.razor b/Otakians/MainLayout.razor index 2e6c7fd..4f59613 100644 --- a/Otakians/MainLayout.razor +++ b/Otakians/MainLayout.razor @@ -5,7 +5,6 @@ @inject Blazored.LocalStorage.ILocalStorageService localStorage @inject NavigationManager NavManager @inject ISnackbar Snackbar -@inject DataCollectorService DCollector @@ -125,8 +124,7 @@ protected async Task DebugButton() { - ipAddress = (await DCollector.GetBrowserInfo()).ToString(); - Console.WriteLine(ipAddress); + } #region Drawers and overlays @@ -206,6 +204,7 @@ async void Logout() { await localStorage.RemoveItemAsync("User"); + await localStorage.RemoveItemAsync("AcceptedCookies"); RefreshPage(); } @@ -253,10 +252,36 @@ protected override async Task OnInitializedAsync() { - StateHasChanged(); + NavManager.LocationChanged += OnNavigated; + await CheckCookiesCheck(); + CheckLoggedin(); } + async void OnNavigated(object sender, LocationChangedEventArgs e) + { + await CheckCookiesCheck(); + } + + async Task CheckCookiesCheck() + { + string relativeUri = NavManager.ToBaseRelativePath(NavManager.Uri); + relativeUri = (relativeUri == "") ? "Bypassss" : relativeUri; + if (!(new string[] { "Privacy", "Cookies", "TOS" }.Any(relativeUri.Contains))) + { + bool? haveAcceptedCookies = await localStorage.GetItemAsync("AcceptedCookies"); + if (haveAcceptedCookies == null || !(bool)haveAcceptedCookies) + { + NavManager.LocationChanged -= OnNavigated; + NavManager.NavigateTo("/Cookies"); + NavManager.LocationChanged += OnNavigated; + await Task.Delay(500); + if (relativeUri != "Bypassss") + Snackbar.Add("Cookies not accepted, detected!", Severity.Warning); + } + } + } + async void CheckLoggedin() { string? name = await localStorage.GetItemAsStringAsync("User"); @@ -275,7 +300,7 @@ private void RefreshPage() { - NavManager.NavigateTo(NavManager.Uri, forceLoad: true); + NavManager.NavigateTo("/", forceLoad: true); } public class User diff --git a/Otakians/Otakians.csproj b/Otakians/Otakians.csproj index c979e28..b44ada4 100644 --- a/Otakians/Otakians.csproj +++ b/Otakians/Otakians.csproj @@ -7,6 +7,7 @@ + diff --git a/Otakians/Pages/Chatting.razor b/Otakians/Pages/Chatting.razor index d904d0e..50da096 100644 --- a/Otakians/Pages/Chatting.razor +++ b/Otakians/Pages/Chatting.razor @@ -128,7 +128,7 @@ } bool isLooping = false; - private async Task StartLoop() + private async void StartLoop() { if (isLooping) return; @@ -140,7 +140,7 @@ messages = await messageService.GetMessages(); Console.WriteLine("Chat updated!"); - await Task.Delay(10000); + await Task.Delay(20000); } } diff --git a/Otakians/Pages/Cookies.razor b/Otakians/Pages/Cookies.razor new file mode 100644 index 0000000..6361778 --- /dev/null +++ b/Otakians/Pages/Cookies.razor @@ -0,0 +1,39 @@ +@page "/Cookies" +@inject NavigationManager NavManager +@inject Blazored.LocalStorage.ILocalStorageService localStorage + + + + + + Cookies, local storage, and Privacy policy + This site does not use cookies! + This uses local storage, instead. +
+ Do you agree to our privacy policy and accept the use of local storage? +
+ + Privacy policy + + Accept + Decline + +
+
+ +@code{ + protected override async Task OnInitializedAsync() + { + bool? haveAcceptedCookies = await localStorage.GetItemAsync("AcceptedCookies"); + if (haveAcceptedCookies == null) return; + if (!(bool)haveAcceptedCookies) return; + + NavManager.NavigateTo("/"); + } + + async void AcceptedCookies() + { + await localStorage.SetItemAsync("AcceptedCookies", true); + NavManager.NavigateTo("/Posts"); + } +} \ No newline at end of file diff --git a/Otakians/Pages/Index.razor b/Otakians/Pages/Index.razor index 0c70bdd..bf1f11b 100644 --- a/Otakians/Pages/Index.razor +++ b/Otakians/Pages/Index.razor @@ -1,11 +1,13 @@ @page "/" @inject NavigationManager NavManager +@inject DataCollectorService DCollector +

2nd loading... Please wait

@code { - protected override void OnInitialized() + protected override async Task OnInitializedAsync() { - NavManager.NavigateTo("/Posts"); + //await DCollector.PushDataToAnalytics(); } } \ No newline at end of file diff --git a/Otakians/Program.cs b/Otakians/Program.cs index 614c2b0..7a5f769 100644 --- a/Otakians/Program.cs +++ b/Otakians/Program.cs @@ -8,6 +8,7 @@ using System.Security.Cryptography; using System.Text; using MudBlazor; using Otakians.Services; +using Blazor.Analytics; internal class Program { @@ -25,6 +26,8 @@ internal class Program builder.Services.AddBlazoredLocalStorage(config => config.JsonSerializerOptions.WriteIndented = true); // local storage + builder.Services.AddGoogleAnalytics("GT-TB78HCS"); + builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Otakians/Services/DataCollectorService.cs b/Otakians/Services/DataCollectorService.cs index ce4120b..14e6c04 100644 --- a/Otakians/Services/DataCollectorService.cs +++ b/Otakians/Services/DataCollectorService.cs @@ -1,8 +1,11 @@ -using DataStructs; +using Otakians.DataStructs; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System.Threading.Tasks; using System; +using System.Drawing; +using System.Text.Json; +using System.Text; namespace Otakians.Services { @@ -10,11 +13,13 @@ namespace Otakians.Services { private readonly IJSRuntime jsRuntime; private readonly NavigationManager navigationManager; + private readonly HttpClient httpClient; - public DataCollectorService(IJSRuntime jsRuntime, NavigationManager navigation) + public DataCollectorService(IJSRuntime jsRuntime, NavigationManager navigation, HttpClient httpClient) { this.jsRuntime = jsRuntime; this.navigationManager = navigation; + this.httpClient = httpClient; } public async Task GetIPAddress() @@ -27,10 +32,43 @@ namespace Otakians.Services var browserInfo = await jsRuntime.InvokeAsync("getBrowserInfo"); return browserInfo; } + + private async Task GetGeolocation() + { + var position = await jsRuntime.InvokeAsync("getCurrentPosition"); + return position; + } + + public async Task PushDataToAnalytics() + { + if (string.IsNullOrEmpty(Program.LoggedInName)) + { + await Console.Out.WriteLineAsync("User not logged in. Canceling data push"); + return; + } + + var currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + var analyticsUrl = "https://globalchat-4a89f-default-rtdb.europe-west1.firebasedatabase.app/Database/Analytics.json"; + + var dataObject = new + { + Username = Program.LoggedInName, + Time = currentTime, + Data = new + { + ipAddress = await GetIPAddress(), + browserInfo = await GetBrowserInfo() + } + }; + + var jsonContent = new StringContent(JsonSerializer.Serialize(dataObject), Encoding.UTF8, "application/json"); + + var response = await httpClient.PostAsync(analyticsUrl, jsonContent); + } } } -namespace DataStructs +namespace Otakians.DataStructs { public class BrowserInfo { @@ -56,4 +94,11 @@ namespace DataStructs } } + public class Geolocation + { + public double? latitude { get; set; } + public double? longitude { get; set; } + } + + } diff --git a/Otakians/wwwroot/images/CookiesImage.jpg b/Otakians/wwwroot/images/CookiesImage.jpg new file mode 100644 index 0000000..90b164b Binary files /dev/null and b/Otakians/wwwroot/images/CookiesImage.jpg differ diff --git a/Otakians/wwwroot/index.html b/Otakians/wwwroot/index.html index 52e5cce..042f8ab 100644 --- a/Otakians/wwwroot/index.html +++ b/Otakians/wwwroot/index.html @@ -30,6 +30,18 @@ + + + + + + diff --git a/Otakians/wwwroot/jsFunctions.js b/Otakians/wwwroot/jsFunctions.js index fe7465b..0dbd3a8 100644 --- a/Otakians/wwwroot/jsFunctions.js +++ b/Otakians/wwwroot/jsFunctions.js @@ -18,4 +18,20 @@ function getBrowserInfo() { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, userAgent: navigator.userAgent }; -} \ No newline at end of file +} + +window.getCurrentPosition = () => { + return new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition( + position => { + resolve({ + latitude: position.coords.latitude, + longitude: position.coords.longitude + }); + }, + error => { + reject(error); + } + ); + }); +};