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);
}
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
@ -148,6 +155,8 @@ namespace Squidex.Domain.Apps.Entities.History
return;
}
try
{
var now = clock.GetCurrentInstant();
var publishedEvents = events
@ -165,37 +174,95 @@ namespace Squidex.Domain.Apps.Entities.History
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
{
Properties = new EventProperties()
@ -215,73 +282,41 @@ namespace Squidex.Domain.Apps.Entities.History
publishRequest.Properties["SquidexUrl"] = url;
}
publishRequest.TemplateCode = @event.HistoryEvent.EventType;
publishRequest.TemplateCode = historyEvent.EventType;
SetUser(payload, publishRequest);
SetTopic(payload, publishRequest, historyEvent);
requests.Add(publishRequest);
}
}
var request = new PublishManyDto
private static void AddMentions(List<PublishDto> requests, CommentCreated comment)
{
Requests = requests
};
await client.Events.PostEventsAsync(options.AppId, request);
}
foreach (var @event in events)
{
switch (@event.AppEvent.Payload)
foreach (var userId in comment.Mentions!)
{
case AppContributorAssigned contributorAssigned:
var publishRequest = new PublishDto
{
var userId = contributorAssigned.ContributorId;
var user = await userResolver.FindByIdAsync(userId);
Topic = $"users/{userId}"
};
if (user != null)
{
await UpsertUserAsync(user);
}
publishRequest.Properties["SquidexApp"] = comment.AppId.Name;
try
publishRequest.Preformatted = new NotificationFormattingDto
{
var request = new AddAllowedTopicDto
Subject =
{
Prefix = GetAppPrefix(contributorAssigned)
["en"] = comment.Text
}
};
await client.Users.PostAllowedTopicAsync(options.AppId, userId, request);
}
catch (NotifoException ex) when (ex.StatusCode == 404)
if (comment.Url?.IsAbsoluteUri == true)
{
break;
}
break;
publishRequest.Preformatted.LinkUrl["en"] = comment.Url.ToString();
}
case AppContributorRemoved contributorRemoved:
{
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;
}
SetUser(comment, publishRequest);
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": {
/*
* The text for the robots.txt file

Loading…
Cancel
Save