|
|
@ -20,6 +20,7 @@ using Squidex.Domain.Apps.Events.Contents; |
|
|
using Squidex.Domain.Users; |
|
|
using Squidex.Domain.Users; |
|
|
using Squidex.Infrastructure; |
|
|
using Squidex.Infrastructure; |
|
|
using Squidex.Infrastructure.EventSourcing; |
|
|
using Squidex.Infrastructure.EventSourcing; |
|
|
|
|
|
using Squidex.Log; |
|
|
using Squidex.Shared.Identity; |
|
|
using Squidex.Shared.Identity; |
|
|
using Squidex.Shared.Users; |
|
|
using Squidex.Shared.Users; |
|
|
|
|
|
|
|
|
@ -31,22 +32,28 @@ namespace Squidex.Domain.Apps.Entities.History |
|
|
private readonly NotifoOptions options; |
|
|
private readonly NotifoOptions options; |
|
|
private readonly IUrlGenerator urlGenerator; |
|
|
private readonly IUrlGenerator urlGenerator; |
|
|
private readonly IUserResolver userResolver; |
|
|
private readonly IUserResolver userResolver; |
|
|
|
|
|
private readonly ISemanticLog log; |
|
|
private readonly IClock clock; |
|
|
private readonly IClock clock; |
|
|
private readonly INotifoClient? client; |
|
|
private readonly INotifoClient? client; |
|
|
|
|
|
|
|
|
public NotifoService(IOptions<NotifoOptions> options, IUrlGenerator urlGenerator, IUserResolver userResolver, IClock clock) |
|
|
public NotifoService(IOptions<NotifoOptions> options, |
|
|
|
|
|
IUrlGenerator urlGenerator, |
|
|
|
|
|
IUserResolver userResolver, |
|
|
|
|
|
ISemanticLog log, |
|
|
|
|
|
IClock clock) |
|
|
{ |
|
|
{ |
|
|
Guard.NotNull(options, nameof(options)); |
|
|
Guard.NotNull(options, nameof(options)); |
|
|
Guard.NotNull(urlGenerator, nameof(urlGenerator)); |
|
|
Guard.NotNull(urlGenerator, nameof(urlGenerator)); |
|
|
Guard.NotNull(userResolver, nameof(userResolver)); |
|
|
Guard.NotNull(userResolver, nameof(userResolver)); |
|
|
|
|
|
Guard.NotNull(log, nameof(log)); |
|
|
Guard.NotNull(clock, nameof(clock)); |
|
|
Guard.NotNull(clock, nameof(clock)); |
|
|
|
|
|
|
|
|
this.options = options.Value; |
|
|
this.options = options.Value; |
|
|
|
|
|
|
|
|
this.urlGenerator = urlGenerator; |
|
|
this.urlGenerator = urlGenerator; |
|
|
this.userResolver = userResolver; |
|
|
this.userResolver = userResolver; |
|
|
|
|
|
|
|
|
this.clock = clock; |
|
|
this.clock = clock; |
|
|
|
|
|
this.log = log; |
|
|
|
|
|
|
|
|
if (options.Value.IsConfigured()) |
|
|
if (options.Value.IsConfigured()) |
|
|
{ |
|
|
{ |
|
|
@ -65,10 +72,10 @@ namespace Squidex.Domain.Apps.Entities.History |
|
|
|
|
|
|
|
|
public async Task OnUserCreatedAsync(IUser user) |
|
|
public async Task OnUserCreatedAsync(IUser user) |
|
|
{ |
|
|
{ |
|
|
if (!string.IsNullOrWhiteSpace(user.Email)) |
|
|
if (!string.IsNullOrWhiteSpace(user.Email)) |
|
|
{ |
|
|
{ |
|
|
await UpsertUserAsync(user); |
|
|
await UpsertUserAsync(user); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task OnUserUpdatedAsync(IUser user, IUser previous) |
|
|
public async Task OnUserUpdatedAsync(IUser user, IUser previous) |
|
|
@ -86,44 +93,55 @@ namespace Squidex.Domain.Apps.Entities.History |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var settings = new Dictionary<string, NotificationSettingDto> |
|
|
try |
|
|
{ |
|
|
{ |
|
|
[Providers.WebPush] = new NotificationSettingDto |
|
|
var settings = new Dictionary<string, NotificationSettingDto> |
|
|
{ |
|
|
{ |
|
|
Send = NotificationSend.Send, |
|
|
[Providers.WebPush] = new NotificationSettingDto |
|
|
DelayInSeconds = null |
|
|
{ |
|
|
}, |
|
|
Send = NotificationSend.Send, |
|
|
|
|
|
DelayInSeconds = null |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
[Providers.Email] = new NotificationSettingDto |
|
|
[Providers.Email] = new NotificationSettingDto |
|
|
{ |
|
|
{ |
|
|
Send = NotificationSend.Send, |
|
|
Send = NotificationSend.Send, |
|
|
DelayInSeconds = 5 * 60 |
|
|
DelayInSeconds = 5 * 60 |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var userRequest = new UpsertUserDto |
|
|
var userRequest = new UpsertUserDto |
|
|
{ |
|
|
{ |
|
|
Id = user.Id, |
|
|
Id = user.Id, |
|
|
FullName = user.Claims.DisplayName(), |
|
|
FullName = user.Claims.DisplayName(), |
|
|
PreferredLanguage = "en", |
|
|
PreferredLanguage = "en", |
|
|
PreferredTimezone = null, |
|
|
PreferredTimezone = null, |
|
|
Settings = settings |
|
|
Settings = settings |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
if (user.Email.IsEmail()) |
|
|
|
|
|
{ |
|
|
|
|
|
userRequest.EmailAddress = user.Email; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var response = await client.Users.PostUsersAsync(options.AppId, new UpsertUsersDto |
|
|
if (user.Email.IsEmail()) |
|
|
{ |
|
|
|
|
|
Requests = new List<UpsertUserDto> |
|
|
|
|
|
{ |
|
|
{ |
|
|
userRequest |
|
|
userRequest.EmailAddress = user.Email; |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
await userResolver.SetClaimAsync(user.Id, SquidexClaimTypes.NotifoKey, response.First().ApiKey, true); |
|
|
var response = await client.Users.PostUsersAsync(options.AppId, new UpsertUsersDto |
|
|
|
|
|
{ |
|
|
|
|
|
Requests = new List<UpsertUserDto> |
|
|
|
|
|
{ |
|
|
|
|
|
userRequest |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
var apiKey = response.First().ApiKey; |
|
|
|
|
|
|
|
|
|
|
|
await userResolver.SetClaimAsync(user.Id, SquidexClaimTypes.NotifoKey, response.First().ApiKey, true); |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
log.LogError(ex, w => w |
|
|
|
|
|
.WriteProperty("action", "RegisterToNotifo") |
|
|
|
|
|
.WriteProperty("status", "Failed")); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task HandleEventsAsync(IEnumerable<(Envelope<AppEvent> AppEvent, HistoryEvent? HistoryEvent)> events) |
|
|
public async Task HandleEventsAsync(IEnumerable<(Envelope<AppEvent> AppEvent, HistoryEvent? HistoryEvent)> events) |
|
|
|