Browse Source

Cleanup Webhook Handling

pull/65/head
Sebastian Stehle 9 years ago
parent
commit
9cf7149bf5
  1. 9
      src/Squidex.Read.MongoDb/Schemas/MongoSchemaEntity.cs
  2. 29
      src/Squidex.Read.MongoDb/Schemas/MongoSchemaEntityWebhook.cs
  3. 10
      src/Squidex.Read.MongoDb/Schemas/MongoSchemaRepository_EventHandling.cs
  4. 4
      src/Squidex.Read.MongoDb/Schemas/MongoSchemaWebhookEntity.cs
  5. 14
      src/Squidex.Read.MongoDb/Schemas/MongoSchemaWebhookRepository.cs
  6. 3
      src/Squidex.Read/Schemas/ISchemaEntity.cs
  7. 2
      src/Squidex.Read/Schemas/ISchemaWebhookEntity.cs
  8. 2
      src/Squidex.Read/Schemas/Repositories/ISchemaWebhookRepository.cs
  9. 8
      src/Squidex.Read/Schemas/Services/Implementations/CachingSchemaProvider.cs
  10. 9
      src/Squidex.Read/Schemas/WebhookInvoker.cs
  11. 2
      src/Squidex/Config/Domain/ReadModule.cs
  12. 18
      src/Squidex/Config/Domain/StoreMongoDbModule.cs

9
src/Squidex.Read.MongoDb/Schemas/MongoSchemaEntity.cs

@ -54,15 +54,6 @@ namespace Squidex.Read.MongoDb.Schemas
[BsonElement]
public bool IsDeleted { get; set; }
[BsonRequired]
[BsonElement]
public List<MongoSchemaEntityWebhook> Webhooks { get; set; } = new List<MongoSchemaEntityWebhook>();
IEnumerable<ISchemaWebhookEntity> ISchemaEntity.Webhooks
{
get { return Webhooks; }
}
Schema ISchemaEntity.Schema
{
get { return schema.Value; }

29
src/Squidex.Read.MongoDb/Schemas/MongoSchemaEntityWebhook.cs

@ -1,29 +0,0 @@
// ==========================================================================
// MongoSchemaEntityWebhook.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System;
using MongoDB.Bson.Serialization.Attributes;
using Squidex.Read.Schemas;
namespace Squidex.Read.MongoDb.Schemas
{
public sealed class MongoSchemaEntityWebhook : ISchemaWebhookEntity
{
[BsonRequired]
[BsonElement]
public Guid Id { get; set; }
[BsonRequired]
[BsonElement]
public Uri Url { get; set; }
[BsonRequired]
[BsonElement]
public string SecurityToken { get; set; }
}
}

10
src/Squidex.Read.MongoDb/Schemas/MongoSchemaRepository_EventHandling.cs

@ -98,16 +98,6 @@ namespace Squidex.Read.MongoDb.Schemas
return UpdateSchema(@event, headers, s => SchemaEventDispatcher.Dispatch(@event, s, registry));
}
protected Task On(WebhookAdded @event, EnvelopeHeaders headers)
{
return Collection.UpdateAsync(@event, headers, e => e.Webhooks.Add(SimpleMapper.Map(@event, new MongoSchemaEntityWebhook())));
}
protected Task On(WebhookDeleted @event, EnvelopeHeaders headers)
{
return Collection.UpdateAsync(@event, headers, e => e.Webhooks.RemoveAll(w => w.Id == @event.Id));
}
protected Task On(SchemaDeleted @event, EnvelopeHeaders headers)
{
return Collection.UpdateAsync(@event, headers, e => e.IsDeleted = true);

4
src/Squidex.Read.MongoDb/Schemas/MongoSchemaWebhookEntity.cs

@ -28,6 +28,10 @@ namespace Squidex.Read.MongoDb.Schemas
[BsonElement]
public string SecurityToken { get; set; }
[BsonRequired]
[BsonElement]
public Guid AppId { get; set; }
[BsonRequired]
[BsonElement]
public Guid SchemaId { get; set; }

14
src/Squidex.Read.MongoDb/Schemas/MongoSchemaWebhookRepository.cs

@ -64,9 +64,9 @@ namespace Squidex.Read.MongoDb.Schemas
{
await EnsureWebooksLoadedAsync();
var webhook = SimpleMapper.Map(@event, new MongoSchemaWebhookEntity { SchemaId = @event.SchemaId.Id });
var webhook = SimpleMapper.Map(@event, new MongoSchemaWebhookEntity { AppId = @event.AppId.Id, SchemaId = @event.SchemaId.Id });
inMemoryWebhooks.GetOrAddNew(webhook.SchemaId).Add(webhook);
inMemoryWebhooks.GetOrAddNew(webhook.AppId).Add(webhook);
await Collection.InsertOneAsync(webhook);
}
@ -75,7 +75,7 @@ namespace Squidex.Read.MongoDb.Schemas
{
await EnsureWebooksLoadedAsync();
inMemoryWebhooks.GetOrDefault(@event.SchemaId.Id)?.RemoveAll(w => w.Id == @event.Id);
inMemoryWebhooks.GetOrDefault(@event.AppId.Id)?.RemoveAll(w => w.Id == @event.Id);
await Collection.DeleteManyAsync(x => x.Id == @event.Id);
}
@ -84,16 +84,16 @@ namespace Squidex.Read.MongoDb.Schemas
{
await EnsureWebooksLoadedAsync();
inMemoryWebhooks.Remove(@event.SchemaId.Id);
inMemoryWebhooks.GetOrDefault(@event.AppId.Id)?.RemoveAll(w => w.SchemaId == @event.SchemaId.Id);
await Collection.DeleteManyAsync(x => x.SchemaId == @event.SchemaId.Id);
}
public async Task<IReadOnlyList<ISchemaWebhookEntity>> QueryBySchemaAsync(Guid schemaId)
public async Task<IReadOnlyList<ISchemaWebhookEntity>> QueryByAppAsync(Guid appId)
{
await EnsureWebooksLoadedAsync();
return inMemoryWebhooks.GetOrDefault(schemaId)?.OfType<ISchemaWebhookEntity>()?.ToList() ?? EmptyWebhooks;
return inMemoryWebhooks.GetOrDefault(appId)?.OfType<ISchemaWebhookEntity>()?.ToList() ?? EmptyWebhooks;
}
private async Task EnsureWebooksLoadedAsync()
@ -108,7 +108,7 @@ namespace Squidex.Read.MongoDb.Schemas
{
var webhooks = await Collection.Find(new BsonDocument()).ToListAsync();
inMemoryWebhooks = webhooks.GroupBy(x => x.SchemaId).ToDictionary(x => x.Key, x => x.ToList());
inMemoryWebhooks = webhooks.GroupBy(x => x.AppId).ToDictionary(x => x.Key, x => x.ToList());
}
}
finally

3
src/Squidex.Read/Schemas/ISchemaEntity.cs

@ -7,7 +7,6 @@
// ==========================================================================
using Squidex.Core.Schemas;
using System.Collections.Generic;
namespace Squidex.Read.Schemas
{
@ -20,7 +19,5 @@ namespace Squidex.Read.Schemas
bool IsDeleted { get; }
Schema Schema { get; }
IEnumerable<ISchemaWebhookEntity> Webhooks { get; }
}
}

2
src/Squidex.Read/Schemas/ISchemaWebhookEntity.cs

@ -14,6 +14,8 @@ namespace Squidex.Read.Schemas
{
Guid Id { get; }
Guid SchemaId { get; }
Uri Url { get; }
string SecurityToken { get; }

2
src/Squidex.Read/Schemas/Repositories/ISchemaWebhookRepository.cs

@ -14,6 +14,6 @@ namespace Squidex.Read.Schemas.Repositories
{
public interface ISchemaWebhookRepository
{
Task<IReadOnlyList<ISchemaWebhookEntity>> QueryBySchemaAsync(Guid schemaId);
Task<IReadOnlyList<ISchemaWebhookEntity>> QueryByAppAsync(Guid appId);
}
}

8
src/Squidex.Read/Schemas/Services/Implementations/CachingSchemaProvider.cs

@ -125,14 +125,6 @@ namespace Squidex.Read.Schemas.Services.Implementations
{
Remove(schemaUpdatedEvent.AppId, schemaUpdatedEvent.SchemaId);
}
else if (@event.Payload is WebhookAdded webhookAddedEvent)
{
Remove(webhookAddedEvent.AppId, webhookAddedEvent.SchemaId);
}
else if (@event.Payload is WebhookDeleted webhookDeletedEvent)
{
Remove(webhookDeletedEvent.AppId, webhookDeletedEvent.SchemaId);
}
return TaskHelper.Done;
}

9
src/Squidex.Read/Schemas/WebhookInvoker.cs

@ -7,6 +7,7 @@
// ==========================================================================
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
@ -60,13 +61,15 @@ namespace Squidex.Read.Schemas
{
if (@event.Payload is ContentEvent contentEvent)
{
var hooks = await webhookRepository.QueryBySchemaAsync(contentEvent.SchemaId.Id);
var hooks = await webhookRepository.QueryByAppAsync(contentEvent.AppId.Id);
if (hooks.Count > 0)
var schemaHooks = hooks.Where(x => x.SchemaId == contentEvent.SchemaId.Id).ToList();
if (schemaHooks.Count > 0)
{
var payload = CreatePayload(@event);
foreach (var hook in hooks)
foreach (var hook in schemaHooks)
{
DispatchEventAsync(payload, hook).Forget();
}

2
src/Squidex/Config/Domain/ReadModule.cs

@ -11,6 +11,7 @@ using System.Linq;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Squidex.Infrastructure.CQRS.Events;
using Squidex.Read.Apps;
using Squidex.Read.Apps.Services;
using Squidex.Read.Apps.Services.Implementations;
@ -74,6 +75,7 @@ namespace Squidex.Config.Domain
.SingleInstance();
builder.RegisterType<WebhookInvoker>()
.As<IEventConsumer>()
.AsSelf()
.SingleInstance();

18
src/Squidex/Config/Domain/StoreMongoDbModule.cs

@ -30,7 +30,6 @@ using Squidex.Read.MongoDb.History;
using Squidex.Read.MongoDb.Infrastructure;
using Squidex.Read.MongoDb.Schemas;
using Squidex.Read.MongoDb.Users;
using Squidex.Read.Schemas;
using Squidex.Read.Schemas.Repositories;
using Squidex.Read.Schemas.Services.Implementations;
using Squidex.Read.Users;
@ -154,16 +153,17 @@ namespace Squidex.Config.Domain
.AsSelf()
.SingleInstance();
builder.RegisterType<MongoSchemaRepository>()
builder.RegisterType<MongoSchemaWebhookRepository>()
.WithParameter(ResolvedParameter.ForNamed<IMongoDatabase>(MongoDatabaseRegistration))
.As<ISchemaRepository>()
.As<ISchemaWebhookRepository>()
.As<IEventConsumer>()
.As<IExternalSystem>()
.AsSelf()
.SingleInstance();
builder.RegisterType<MongoSchemaWebhookRepository>()
builder.RegisterType<MongoSchemaRepository>()
.WithParameter(ResolvedParameter.ForNamed<IMongoDatabase>(MongoDatabaseRegistration))
.As<ISchemaWebhookRepository>()
.As<ISchemaRepository>()
.As<IExternalSystem>()
.AsSelf()
.SingleInstance();
@ -183,14 +183,6 @@ namespace Squidex.Config.Domain
.As<IEventConsumer>()
.AsSelf()
.SingleInstance();
builder.Register(c =>
new CompoundEventConsumer(
c.Resolve<WebhookInvoker>(),
c.Resolve<MongoSchemaWebhookRepository>()))
.As<IEventConsumer>()
.AsSelf()
.SingleInstance();
}
}
}

Loading…
Cancel
Save