Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added polling events #10

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Apps.Wordpress/Apps.Wordpress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Product>Wordpress (+ Polylang)</Product>
<Version>1.1.3</Version>
<Version>1.2.0</Version>
<Description>The world’s most popular website builder</Description>
<AssemblyName>Apps.Wordpress</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.6.0" />
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.8.0" />
<PackageReference Include="Blackbird.Applications.SDK.Extensions.FileManagement" Version="1.0.1" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.16" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils.Html" Version="1.0.1" />
Expand Down
8 changes: 8 additions & 0 deletions Apps.Wordpress/Events/Polling/Models/ContentPollingResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Apps.Wordpress.Models.Entities;

namespace Apps.Wordpress.Events.Polling.Models;

public class ContentPollingResult
{
public IEnumerable<WordPressItem> Items { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Apps.Wordpress.Events.Polling.Models.Memory;

public class ContentCreatedPollingMemory
{
public DateTime LastCreationDate { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Apps.Wordpress.Events.Polling.Models.Memory;

public class ContentUpdatedPollingMemory
{
public DateTime LastModificationTime { get; set; }
}
164 changes: 164 additions & 0 deletions Apps.Wordpress/Events/Polling/PollingList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using Apps.Wordpress.Api.RestSharp;
using Apps.Wordpress.Constants;
using Apps.Wordpress.DataSourceHandlers;
using Apps.Wordpress.Events.Polling.Models;
using Apps.Wordpress.Events.Polling.Models.Memory;
using Apps.Wordpress.Models.Dtos;
using Apps.Wordpress.Models.Entities;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.Sdk.Common.Polling;
using Blackbird.Applications.Sdk.Utils.Extensions.String;
using RestSharp;

namespace Apps.Wordpress.Events.Polling;

[PollingEventList]
public class PollingList : BaseInvocable
{
public PollingList(InvocationContext invocationContext) : base(invocationContext)
{
}

[PollingEvent("On post created", "On a new post created")]
public Task<PollingEventResponse<ContentCreatedPollingMemory, ContentPollingResult>> OnPostCreated(
PollingEventRequest<ContentCreatedPollingMemory> request)
=> PollContentCreation(request, new()
{
["after"] = request.Memory?.LastCreationDate.ToString(Formats.ISO8601) ?? string.Empty
}, "posts", () => new()
{
LastCreationDate = DateTime.UtcNow
});


[PollingEvent("On post updated", "On a specific post updated")]
public Task<PollingEventResponse<ContentUpdatedPollingMemory, ContentPollingResult>> OnPostUpdated(
PollingEventRequest<ContentUpdatedPollingMemory> request,
[PollingEventParameter] [Display("Post ID")] [DataSource(typeof(PostDataHandler))]
string? postId)
=> PollContentChanges(request, postId, new()
{
["modified_after"] = request.Memory?.LastModificationTime.ToString(Formats.ISO8601) ?? string.Empty
}, "posts", () => new()
{
LastModificationTime = DateTime.UtcNow
});

[PollingEvent("On page created", "On a new page created")]
public Task<PollingEventResponse<ContentCreatedPollingMemory, ContentPollingResult>> OnPageCreated(
PollingEventRequest<ContentCreatedPollingMemory> request)
=> PollContentCreation(request, new()
{
["after"] = request.Memory?.LastCreationDate.ToString(Formats.ISO8601) ?? string.Empty
}, "pages", () => new()
{
LastCreationDate = DateTime.UtcNow
});


[PollingEvent("On page updated", "On a specific page updated")]
public Task<PollingEventResponse<ContentUpdatedPollingMemory, ContentPollingResult>> OnPageUpdated(
PollingEventRequest<ContentUpdatedPollingMemory> request,
[PollingEventParameter] [Display("Page ID")] [DataSource(typeof(PageDataHandler))]
string? pageId)
=> PollContentChanges(request, pageId, new()
{
["modified_after"] = request.Memory?.LastModificationTime.ToString(Formats.ISO8601) ?? string.Empty
}, "pages", () => new()
{
LastModificationTime = DateTime.UtcNow
});

private async Task<PollingEventResponse<T, ContentPollingResult>> PollContentCreation<T>(
PollingEventRequest<T> request, Dictionary<string, string> query, string endpoint, Func<T> createMemory)
{
if (request.Memory is null)
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

var items = await ListContentItems(endpoint.WithQuery(query));

if (!items.Any())
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

return new()
{
FlyBird = true,
Result = new()
{
Items = items.Select(x => new WordPressItem(x)).ToList()
},
Memory = createMemory()
};
}

private async Task<PollingEventResponse<T, ContentPollingResult>> PollContentChanges<T>(
PollingEventRequest<T> request, string? resourceId, Dictionary<string, string> query, string endpoint,
Func<T> createMemory) where T : ContentUpdatedPollingMemory
{
if (request.Memory is null)
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

var items = string.IsNullOrWhiteSpace(resourceId)
? await ListContentItems(endpoint.WithQuery(query))
: await GetContentItem($"{endpoint}/{resourceId}", request.Memory);

if (!items.Any())
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

return new()
{
FlyBird = true,
Result = new()
{
Items = items.Select(x => new WordPressItem(x)).ToList()
},
Memory = createMemory()
};
}

private Task<List<BaseDto>> ListContentItems(string endpoint)
{
var client = new WordpressRestClient(InvocationContext.AuthenticationCredentialsProviders);
var listRequest =
new WordpressRestRequest(endpoint, Method.Get, InvocationContext.AuthenticationCredentialsProviders);

return client.Paginate<BaseDto>(listRequest);
}

private async Task<List<BaseDto>> GetContentItem(string endpoint, ContentUpdatedPollingMemory memory)
{
var client = new WordpressRestClient(InvocationContext.AuthenticationCredentialsProviders);
var listRequest =
new WordpressRestRequest(endpoint, Method.Get, InvocationContext.AuthenticationCredentialsProviders);

var item = await client.ExecuteWithHandling<BaseDto>(listRequest);

return item.ModifiedGmt > memory.LastModificationTime ? [item] : [];
}
}
2 changes: 1 addition & 1 deletion Apps.Wordpress/Models/Requests/Post/PostRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Apps.Wordpress.Models.Requests.Post
{
public class PostRequest
{
[Display("Post")]
[Display("Post ID")]
[DataSource(typeof(PostDataHandler))]
public string Id { get; set; }
}
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ Our post & page actions can also be extended to deal with more properties like s

Let us know if you're interested!

## Events

- **On post created** triggers when a new post is created.
- **On post updated** triggers when any post is updated.
- **On page created** triggers when a new page is created.
- **On page updated** triggers when any page is updated.

## Feedback

Do you want to use this app or do you have feedback on our implementation? Reach out to us using the [established channels](https://www.blackbird.io/) or create an issue.
Expand Down