Browse Source

Improve notifo error logging.

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

237
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,140 +155,168 @@ namespace Squidex.Domain.Apps.Entities.History
return; return;
} }
var now = clock.GetCurrentInstant(); try
var publishedEvents = events
.Where(x => x.AppEvent.Headers.Restored() == false)
.Where(x => IsTooOld(x.AppEvent.Headers, now) == false)
.Where(x => IsComment(x.AppEvent.Payload) || x.HistoryEvent != null);
foreach (var batch in publishedEvents.Batch(50))
{ {
var requests = new List<PublishDto>(); var now = clock.GetCurrentInstant();
var publishedEvents = events
.Where(x => x.AppEvent.Headers.Restored() == false)
.Where(x => IsTooOld(x.AppEvent.Headers, now) == false)
.Where(x => IsComment(x.AppEvent.Payload) || x.HistoryEvent != null);
foreach (var @event in batch) foreach (var batch in publishedEvents.Batch(50))
{ {
var payload = @event.AppEvent.Payload; var requests = new List<PublishDto>();
if (payload is CommentCreated comment && IsComment(payload)) foreach (var @event in batch)
{ {
foreach (var userId in comment.Mentions!) var payload = @event.AppEvent.Payload;
{
var publishRequest = new PublishDto
{
Topic = $"users/{userId}"
};
publishRequest.Properties["SquidexApp"] = comment.AppId.Name; if (payload is CommentCreated comment && IsComment(payload))
{
AddMentions(requests, comment);
}
else if (@event.HistoryEvent != null)
{
AddHistoryEvent(requests, @event.HistoryEvent, payload);
}
}
publishRequest.Preformatted = new NotificationFormattingDto var request = new PublishManyDto
{ {
Subject = Requests = requests
{ };
["en"] = comment.Text
}
};
if (comment.Url?.IsAbsoluteUri == true) await client.Events.PostEventsAsync(options.AppId, request);
{ }
publishRequest.Preformatted.LinkUrl["en"] = comment.Url.ToString();
}
SetUser(comment, publishRequest); foreach (var @event in events)
{
switch (@event.AppEvent.Payload)
{
case AppContributorAssigned contributorAssigned:
await AssignContributorAsync(client, contributorAssigned);
break;
requests.Add(publishRequest); case AppContributorRemoved contributorRemoved:
} await RemoveContributorAsync(client, contributorRemoved);
break;
} }
else if (@event.HistoryEvent != null) }
{ }
var historyEvent = @event.HistoryEvent; 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"));
}
}
var publishRequest = new PublishDto private async Task AssignContributorAsync(INotifoClient actualClient, AppContributorAssigned contributorAssigned)
{ {
Properties = new EventProperties() var userId = contributorAssigned.ContributorId;
};
foreach (var (key, value) in historyEvent.Parameters) var user = await userResolver.FindByIdAsync(userId);
{
publishRequest.Properties.Add(key, value);
}
publishRequest.Properties["SquidexApp"] = payload.AppId.Name; if (user != null)
{
await UpsertUserAsync(user);
}
if (payload is ContentEvent c && !(payload is ContentDeleted)) try
{ {
var url = urlGenerator.ContentUI(c.AppId, c.SchemaId, c.ContentId); var request = new AddAllowedTopicDto
{
Prefix = GetAppPrefix(contributorAssigned)
};
publishRequest.Properties["SquidexUrl"] = url; await actualClient.Users.PostAllowedTopicAsync(options.AppId, userId, request);
} }
catch (NotifoException ex) when (ex.StatusCode != 404)
{
throw;
}
}
publishRequest.TemplateCode = @event.HistoryEvent.EventType; private async Task RemoveContributorAsync(INotifoClient actualClient, AppContributorRemoved contributorRemoved)
{
var userId = contributorRemoved.ContributorId;
SetUser(payload, publishRequest); try
SetTopic(payload, publishRequest, historyEvent); {
var prefix = GetAppPrefix(contributorRemoved);
requests.Add(publishRequest); await actualClient.Users.DeleteAllowedTopicAsync(options.ApiKey, userId, prefix);
} }
} catch (NotifoException ex) when (ex.StatusCode != 404)
{
throw;
}
}
var request = new PublishManyDto private void AddHistoryEvent(List<PublishDto> requests, HistoryEvent historyEvent, AppEvent payload)
{ {
Requests = requests var publishRequest = new PublishDto
}; {
Properties = new EventProperties()
};
await client.Events.PostEventsAsync(options.AppId, request); foreach (var (key, value) in historyEvent.Parameters)
{
publishRequest.Properties.Add(key, value);
} }
foreach (var @event in events) publishRequest.Properties["SquidexApp"] = payload.AppId.Name;
{
switch (@event.AppEvent.Payload)
{
case AppContributorAssigned contributorAssigned:
{
var userId = contributorAssigned.ContributorId;
var user = await userResolver.FindByIdAsync(userId); if (payload is ContentEvent c && !(payload is ContentDeleted))
{
var url = urlGenerator.ContentUI(c.AppId, c.SchemaId, c.ContentId);
if (user != null) publishRequest.Properties["SquidexUrl"] = url;
{ }
await UpsertUserAsync(user);
}
try publishRequest.TemplateCode = historyEvent.EventType;
{
var request = new AddAllowedTopicDto
{
Prefix = GetAppPrefix(contributorAssigned)
};
await client.Users.PostAllowedTopicAsync(options.AppId, userId, request); SetUser(payload, publishRequest);
} SetTopic(payload, publishRequest, historyEvent);
catch (NotifoException ex) when (ex.StatusCode == 404)
{
break;
}
break; requests.Add(publishRequest);
} }
case AppContributorRemoved contributorRemoved: private static void AddMentions(List<PublishDto> requests, CommentCreated comment)
{ {
var userId = contributorRemoved.ContributorId; foreach (var userId in comment.Mentions!)
{
var publishRequest = new PublishDto
{
Topic = $"users/{userId}"
};
try publishRequest.Properties["SquidexApp"] = comment.AppId.Name;
{
var prefix = GetAppPrefix(contributorRemoved);
await client.Users.DeleteAllowedTopicAsync(options.ApiKey, userId, prefix); publishRequest.Preformatted = new NotificationFormattingDto
} {
catch (NotifoException ex) when (ex.StatusCode == 404) Subject =
{ {
break; ["en"] = comment.Text
} }
};
break; if (comment.Url?.IsAbsoluteUri == true)
} {
publishRequest.Preformatted.LinkUrl["en"] = comment.Url.ToString();
} }
SetUser(comment, publishRequest);
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