Skip to content

Commit

Permalink
Merge pull request #15 from Relewise/feat/minor-changes-and-improvements
Browse files Browse the repository at this point in the history
feat: upgrade Relewise dependencies, update nullable checks, improve …
  • Loading branch information
mzanoni authored Oct 24, 2022
2 parents d5c0e3b + 4d858fb commit bad23d9
Show file tree
Hide file tree
Showing 28 changed files with 346 additions and 115 deletions.
6 changes: 6 additions & 0 deletions samples/Relewise.Umbraco.Application/Api/CatalogApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Relewise.Client;
using Relewise.Client.DataTypes;
using Relewise.Client.DataTypes.Search.Facets.Enums;
using Relewise.Client.DataTypes.Search.Facets.Queries;
using Relewise.Client.DataTypes.Search.Facets.Result;
using Relewise.Client.DataTypes.Search.Sorting.Enums;
using Relewise.Client.DataTypes.Search.Sorting.Product;
using Relewise.Client.Requests.Filters;
Expand Down Expand Up @@ -69,6 +71,10 @@ private static async Task Search(
}
};

request.Facets ??= new ProductFacetQuery();
request.Filters ??= new FilterCollection();
request.Sorting ??= new ProductSortBySpecification();

request.Facets.AddCategory(CategorySelectionStrategy.ImmediateParent, category2Id != null ? new[] { category2Id } : null);
request.Facets.AddDataString(DataSelectionStrategy.Product, "Country", country != null ? new[] { country } : null);

Expand Down
4 changes: 2 additions & 2 deletions samples/Relewise.Umbraco.Application/Api/SearchApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static async Task Search(
Currency.Undefined,
user,
DisplayedAtLocation,
q,
q ?? string.Empty,
take: 5)
{
Settings = new SearchTermPredictionSettings
Expand All @@ -68,7 +68,7 @@ private static async Task Search(
Currency.Undefined,
user,
DisplayedAtLocation,
q,
q ?? string.Empty,
skip: 0,
take: 5)
{
Expand Down
1 change: 1 addition & 0 deletions samples/Relewise.Umbraco.Application/BlogMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class BlogMapper : IContentTypeMapping
{
public Task<ContentUpdate> Map(ContentMappingContext context, CancellationToken token)
{
context.ContentUpdate.Content.Data ??= new Dictionary<string, DataValue?>();
context.ContentUpdate.Content.Data["Title"] = context.PublishedContent.GetProperty("title")?.GetValue<string>(context.CulturesToPublish.First());

return Task.FromResult(context.ContentUpdate);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace Relewise.Umbraco.Application.Infrastructure.CookieConsent;

public class CookieConsent
{
public readonly string CookieName = "_cookieConsent";

private readonly IHttpContextAccessor _httpContextAccessor;

public CookieConsent(IHttpContextAccessor httpContextAccessor)
Expand All @@ -14,16 +17,16 @@ public CookieConsent(IHttpContextAccessor httpContextAccessor)

public bool HasGivenConsentFor(CookieType type)
{
string? cookie = _httpContextAccessor.HttpContext?.Request.Cookies["_cookieconsent"];
string? cookie = _httpContextAccessor.HttpContext?.Request.Cookies[CookieName];

if (cookie == null)
return false;

CookieData? data = JsonConvert.DeserializeObject<CookieData>(cookie);

if (data == null)
throw new InvalidOperationException("Could not find 3rd party cookieconsent cookie");


throw new InvalidOperationException($"Unable to deserialize cookie '{CookieName}' with data: '{cookie}'.");

return type switch
{
CookieType.Functional => data.Cookies.Functional,
Expand All @@ -33,23 +36,27 @@ public bool HasGivenConsentFor(CookieType type)
};
}

public string? UserId()
public string UserId()
{
string? cookie = _httpContextAccessor.HttpContext?.Request.Cookies["_cookieconsent"];
string? cookie = _httpContextAccessor.HttpContext?.Request.Cookies[CookieName];

if (cookie == null)
return null;
throw new InvalidOperationException("UserId is null as cookie does not exist.");

var data = JsonConvert.DeserializeObject<CookieData>(cookie);

return data?.UserId;
return data?.UserId ?? throw new InvalidOperationException($"Unable to deserialize cookie '{CookieName}' with data: '{cookie}'.");
}

[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Local")]
private class CookieData
{
public string UserId { get; set; } = default!;
public CookieTypes Cookies { get; set; } = default!;
}

[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
private class CookieTypes
{
public bool Functional { get; set; }
Expand Down
8 changes: 5 additions & 3 deletions samples/Relewise.Umbraco.Application/RelewiseUserLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public RelewiseUserLocator(CookieConsent cookieConsent)

public Task<User> GetUser()
{
return Task.FromResult(!_cookieConsent.HasGivenConsentFor(CookieType.Marketing)
? User.Anonymous()
: User.ByTemporaryId(_cookieConsent.UserId()));
bool consentGiven = _cookieConsent.HasGivenConsentFor(CookieType.Marketing);

return Task.FromResult(consentGiven
? User.ByTemporaryId(_cookieConsent.UserId())
: User.Anonymous());
}
}
4 changes: 2 additions & 2 deletions samples/UmbracoV10/Relewise.UmbracoV10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Relewise.Client" Version="1.10.0" />
<PackageReference Include="Relewise.Client.Extensions" Version="1.2.0" />
<PackageReference Include="Relewise.Client" Version="1.35.0" />
<PackageReference Include="Relewise.Client.Extensions" Version="1.3.1" />
<PackageReference Include="Umbraco.Cms" Version="10.0.0" />
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="10.0.0" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions samples/UmbracoV10/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config)
public void ConfigureServices(IServiceCollection services)
{
// This setups the needed configuration for you to be able to interact with our API.
// You need to add you own dataset id and api-key in the appsettings before recommendations and search works
services.AddRelewise(options => options.ReadFromConfiguration(_config));
// You need to add you own dataset id and api-key in the appsettings.json before recommendations and search works
//services.AddRelewise(options => options.ReadFromConfiguration(_config));

services.AddHttpContextAccessor();
services.AddSingleton<CookieConsent>();
Expand Down Expand Up @@ -105,7 +105,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
u.UseBackOffice();
u.UseWebsite();
// Enables tracking of all pageviews to Relewise
// Enables tracking of all page-views to Relewise
u.TrackContentViews();
})
.WithEndpoints(u =>
Expand Down
94 changes: 81 additions & 13 deletions samples/UmbracoV9/App_Plugins/Relewise.Dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,72 @@ <h2>Relewise</h2>
<h3>Settings</h3>
<umb-box>
<umb-box-content>
<p>
These are the settings registered with Relewise.
</p>
<div ng-if="vm.configurationError">
<span class="error">Unexpected error occured. Please check the response on the XHR request or check logs.</span><br />
</div>

<p class="error" ng-if="vm.configurationError">
No options have been configured. Please check your call to the 'services.AddRelewise(options => { /* options goes here */ })'-method in 'Startup.cs'.
</p>
<div ng-if="vm.configuration !== null">
Tracked content types are: <strong>{{vm.configuration.trackedContentTypes.join(', ')}}</strong>
<div style="font-style: italic; color: #666;">(Page views for these content types are being tracked to Relewise, if the automatic page view tracking has been enabled)</div>
<br />
Exported content types are: <strong>{{vm.configuration.exportedContentTypes.join(', ')}}</strong>
<div style="font-style: italic; color: #666;">(These are configured for being exported to Relewise on publish or when the export is triggered manually)</div>

<div ng-if="vm.configuration.factoryFailed">
<span class="error">No settings have been configured. Please check your call to the 'services.AddRelewise(options => { /* options goes here */ })'-method in 'Startup.cs':</span><br />
<pre>
public void ConfigureServices(IServiceCollection services)
{
services.AddRelewise(options => options.ReadFromConfiguration(_config));
...
}</pre>
<span>Which then reads from the following configuration in appsettings.json:</span>
<pre>
"Relewise": {
"DatasetId": "00000000-0000-0000-0000-000000000000",
"ApiKey": "ApiKey",
"Timeout": "00:00:03"
}
</pre>
<span>Error message from server:</span>
<pre>{{vm.configuration.errorMessage}}</pre>
<span>You can read more about configuring Relewise here: <a href="https://github.com/Relewise/relewise-sdk-csharp-extensions" target="_blank">https://github.com/Relewise/relewise-sdk-csharp-extensions</a></span>
</div>

<div ng-if="vm.configuration.trackedContentTypes">
Tracked content types are:
<strong ng-if="vm.configuration.trackedContentTypes.length > 0">{{vm.configuration.trackedContentTypes.join(', ')}}</strong>
<span ng-if="vm.configuration.trackedContentTypes.length === 0" style="font-style: italic;">No tracked content type have been configured</span>
<div style="font-style: italic; color: #666;">(Page views on these content types are automatically being tracked to Relewise by the RelewiseContentMiddleware)</div>
<div ng-if="vm.configuration.trackedContentTypes.length > 0 && !vm.configuration.contentMiddlewareEnabled" style="font-style: italic; color: red;">To have Page views being tracked automatically to Relewise, please ensure a call to the 'TrackContentViews()'-method in 'Startup.cs':</div>
<pre ng-if="vm.configuration.trackedContentTypes.length > 0 && !vm.configuration.contentMiddlewareEnabled">
app.UseUmbraco()
.WithMiddleware(u =>
{
u.UseBackOffice();
u.UseWebsite();
u.TrackContentViews();
})
.WithEndpoints(u =>
{
u.UseInstallerEndpoints();
u.UseBackOfficeEndpoints();
u.UseWebsiteEndpoints();
});</pre>
</div>

<div ng-if="vm.configuration.exportedContentTypes" style="margin-top: 20px;">
Exported content types are:
<strong ng-if="vm.configuration.exportedContentTypes.length > 0">{{vm.configuration.exportedContentTypes.join(', ')}}</strong>
<span ng-if="vm.configuration.exportedContentTypes.length === 0" style="font-style: italic;">No exported content type have been configured</span>
<div style="font-style: italic; color: #666;">(Any publish on these content types will automatically export the data to Relewise)</div>
</div>

</div>
<div>
<hr />
<span>Find more information about the Umbraco integration here: <a href="https://github.com/Relewise/relewise-integrations-umbraco" target="_blank">https://github.com/Relewise/relewise-integrations-umbraco</a></span>
</div>
</umb-box-content>
</umb-box>

<h3 ng-if="vm.configuration !== null">Clients</h3>
<umb-box ng-if="vm.configuration !== null">
<h3 ng-if="vm.configuration !== null && vm.configuration.named">Clients</h3>
<umb-box ng-if="vm.configuration !== null && vm.configuration.named">
<div class="umb-table">
<div class="umb-table-head">
<div class="umb-table-row">
Expand Down Expand Up @@ -81,6 +128,27 @@ <h3 ng-if="vm.configuration !== null">Clients</h3>
<div class="umb-table-cell not-fixed">{{ named.searcher.datasetId }}</div>
<div class="umb-table-cell not-fixed">{{ named.searcher.timeout }}</div>
</div>
<div class="umb-table-row">
<div class="umb-table-cell not-fixed umb-table__name">
<span class="pl-20">Search Administrator</span>
</div>
<div class="umb-table-cell not-fixed">{{ named.searchAdministrator.datasetId }}</div>
<div class="umb-table-cell not-fixed">{{ named.searchAdministrator.timeout }}</div>
</div>
<div class="umb-table-row">
<div class="umb-table-cell not-fixed umb-table__name">
<span class="pl-20">Analyzer</span>
</div>
<div class="umb-table-cell not-fixed">{{ named.analyzer.datasetId }}</div>
<div class="umb-table-cell not-fixed">{{ named.analyzer.timeout }}</div>
</div>
<div class="umb-table-row">
<div class="umb-table-cell not-fixed umb-table__name">
<span class="pl-20">DataAccessor</span>
</div>
<div class="umb-table-cell not-fixed">{{ named.dataAccessor.datasetId }}</div>
<div class="umb-table-cell not-fixed">{{ named.dataAccessor.timeout }}</div>
</div>
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions samples/UmbracoV9/Relewise.UmbracoV9.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Relewise.Client" Version="1.10.0" />
<PackageReference Include="Relewise.Client.Extensions" Version="1.2.0" />
<PackageReference Include="Relewise.Client" Version="1.35.0" />
<PackageReference Include="Relewise.Client.Extensions" Version="1.3.1" />
<PackageReference Include="Umbraco.Cms" Version="9.4.3" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions samples/UmbracoV9/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config)
public void ConfigureServices(IServiceCollection services)
{
// This setups the needed configuration for you to be able to interact with our API.
// You need to add you own dataset id and api-key in the appsettings before recommendations and search works
services.AddRelewise(options => options.ReadFromConfiguration(_config));
// You need to add you own dataset id and api-key in the appsettings.json before recommendations and search works
//services.AddRelewise(options => options.ReadFromConfiguration(_config));

services.AddHttpContextAccessor();
services.AddSingleton<CookieConsent>();
Expand Down Expand Up @@ -107,7 +107,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
u.UseBackOffice();
u.UseWebsite();
// Enables tracking of all pageviews to Relewise
// Enables tracking of all page-views to Relewise
u.TrackContentViews();
})
.WithEndpoints(u =>
Expand Down
12 changes: 10 additions & 2 deletions samples/UmbracoV9/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@
},
//"Relewise": {
// "DatasetId": "<your dataset id here>",
// "ApiKey": "<your api key here>",
// "Timeout": "00:00:05"
// "ApiKey": "<your master api key here>",
// "Timeout": "00:00:05",
// "Named": {
// "Browser": {
// "Tracker": {
// "ApiKey": "<your browser api key here>",
// "Timeout": "00:00:03"
// }
// }
// }
//}
}
Loading

0 comments on commit bad23d9

Please sign in to comment.