Browse Source

Improve notifo error logging.

pull/737/head
Sebastian 4 years ago
parent
commit
80cec562fe
  1. 157
      backend/src/Squidex.Domain.Apps.Entities/History/NotifoService.cs
  2. 18
      backend/src/Squidex/appsettings.json

157
backend/src/Squidex.Domain.Apps.Entities/History/NotifoService.cs

@ -131,6 +131,13 @@ namespace Squidex.Domain.Apps.Entities.History
await userResolver.SetClaimAsync(user.Id, SquidexClaimTypes.NotifoKey, response.First().ApiKey, true); await userResolver.SetClaimAsync(user.Id, SquidexClaimTypes.NotifoKey, response.First().ApiKey, true);
} }
catch (NotifoException ex)
{
log.LogError(ex, w => w
.WriteProperty("action", "RegisterToNotifo")
.WriteProperty("status", "Failed")
.WriteProperty("details", ex.ToString()));
}
catch (Exception ex) catch (Exception ex)
{ {
log.LogError(ex, w => w log.LogError(ex, w => w
@ -148,6 +155,8 @@ namespace Squidex.Domain.Apps.Entities.History
return; return;
} }
try
{
var now = clock.GetCurrentInstant(); var now = clock.GetCurrentInstant();
var publishedEvents = events var publishedEvents = events
@ -165,37 +174,95 @@ namespace Squidex.Domain.Apps.Entities.History
if (payload is CommentCreated comment && IsComment(payload)) if (payload is CommentCreated comment && IsComment(payload))
{ {
foreach (var userId in comment.Mentions!) AddMentions(requests, comment);
}
else if (@event.HistoryEvent != null)
{ {
var publishRequest = new PublishDto AddHistoryEvent(requests, @event.HistoryEvent, payload);
}
}
var request = new PublishManyDto
{ {
Topic = $"users/{userId}" Requests = requests
}; };
publishRequest.Properties["SquidexApp"] = comment.AppId.Name; await client.Events.PostEventsAsync(options.AppId, request);
}
publishRequest.Preformatted = new NotificationFormattingDto foreach (var @event in events)
{ {
Subject = switch (@event.AppEvent.Payload)
{ {
["en"] = comment.Text case AppContributorAssigned contributorAssigned:
await AssignContributorAsync(client, contributorAssigned);
break;
case AppContributorRemoved contributorRemoved:
await RemoveContributorAsync(client, contributorRemoved);
break;
}
}
}
catch (NotifoException ex)
{
log.LogError(ex, w => w
.WriteProperty("action", "RegisterToNotifo")
.WriteProperty("status", "Failed")
.WriteProperty("details", ex.ToString()));
}
catch (Exception ex)
{
log.LogError(ex, w => w
.WriteProperty("action", "RegisterToNotifo")
.WriteProperty("status", "Failed"));
}
} }
};
if (comment.Url?.IsAbsoluteUri == true) private async Task AssignContributorAsync(INotifoClient actualClient, AppContributorAssigned contributorAssigned)
{ {
publishRequest.Preformatted.LinkUrl["en"] = comment.Url.ToString(); var userId = contributorAssigned.ContributorId;
var user = await userResolver.FindByIdAsync(userId);
if (user != null)
{
await UpsertUserAsync(user);
} }
SetUser(comment, publishRequest); try
{
var request = new AddAllowedTopicDto
{
Prefix = GetAppPrefix(contributorAssigned)
};
requests.Add(publishRequest); await actualClient.Users.PostAllowedTopicAsync(options.AppId, userId, request);
}
catch (NotifoException ex) when (ex.StatusCode != 404)
{
throw;
} }
} }
else if (@event.HistoryEvent != null)
private async Task RemoveContributorAsync(INotifoClient actualClient, AppContributorRemoved contributorRemoved)
{
var userId = contributorRemoved.ContributorId;
try
{ {
var historyEvent = @event.HistoryEvent; var prefix = GetAppPrefix(contributorRemoved);
await actualClient.Users.DeleteAllowedTopicAsync(options.ApiKey, userId, prefix);
}
catch (NotifoException ex) when (ex.StatusCode != 404)
{
throw;
}
}
private void AddHistoryEvent(List<PublishDto> requests, HistoryEvent historyEvent, AppEvent payload)
{
var publishRequest = new PublishDto var publishRequest = new PublishDto
{ {
Properties = new EventProperties() Properties = new EventProperties()
@ -215,73 +282,41 @@ namespace Squidex.Domain.Apps.Entities.History
publishRequest.Properties["SquidexUrl"] = url; publishRequest.Properties["SquidexUrl"] = url;
} }
publishRequest.TemplateCode = @event.HistoryEvent.EventType; publishRequest.TemplateCode = historyEvent.EventType;
SetUser(payload, publishRequest); SetUser(payload, publishRequest);
SetTopic(payload, publishRequest, historyEvent); SetTopic(payload, publishRequest, historyEvent);
requests.Add(publishRequest); requests.Add(publishRequest);
} }
}
var request = new PublishManyDto private static void AddMentions(List<PublishDto> requests, CommentCreated comment)
{ {
Requests = requests foreach (var userId in comment.Mentions!)
};
await client.Events.PostEventsAsync(options.AppId, request);
}
foreach (var @event in events)
{
switch (@event.AppEvent.Payload)
{ {
case AppContributorAssigned contributorAssigned: var publishRequest = new PublishDto
{ {
var userId = contributorAssigned.ContributorId; Topic = $"users/{userId}"
};
var user = await userResolver.FindByIdAsync(userId);
if (user != null) publishRequest.Properties["SquidexApp"] = comment.AppId.Name;
{
await UpsertUserAsync(user);
}
try publishRequest.Preformatted = new NotificationFormattingDto
{ {
var request = new AddAllowedTopicDto Subject =
{ {
Prefix = GetAppPrefix(contributorAssigned) ["en"] = comment.Text
}
}; };
await client.Users.PostAllowedTopicAsync(options.AppId, userId, request); if (comment.Url?.IsAbsoluteUri == true)
}
catch (NotifoException ex) when (ex.StatusCode == 404)
{ {
break; publishRequest.Preformatted.LinkUrl["en"] = comment.Url.ToString();
}
break;
} }
case AppContributorRemoved contributorRemoved: SetUser(comment, publishRequest);
{
var userId = contributorRemoved.ContributorId;
try
{
var prefix = GetAppPrefix(contributorRemoved);
await client.Users.DeleteAllowedTopicAsync(options.ApiKey, userId, prefix);
}
catch (NotifoException ex) when (ex.StatusCode == 404)
{
break;
}
break; requests.Add(publishRequest);
}
}
} }
} }

18
backend/src/Squidex/appsettings.json

@ -247,6 +247,24 @@
} }
}, },
/*
* Configure notifo if you want to have support for custom notifications.
*/
"notifo": {
/*
* The id of the app in notifo.
*/
"appId": "",
/*
* The API key for your app in notifo.
*/
"apiKey": "",
/*
* The API URL.
*/
"apiUrl": "https://app.notifo.io"
},
"robots": { "robots": {
/* /*
* The text for the robots.txt file * The text for the robots.txt file

Loading…
Cancel
Save