craftcms / webhooks

Plugin for integrating Craft with Zapier and IFTTT.
https://plugins.craftcms.com/webhooks
MIT License
84 stars 12 forks source link

Activity Limit & Entry Type Settings #10

Closed wautersj closed 5 years ago

wautersj commented 5 years ago

First of all, great work on this one. Works like a charm!

I was wondering if there is a way to do the following two things: (Maybe there already is a way, and I'm overlooking something)

1) Limit the amount of activities that are saved. For my project, it will be sending things to slack alot. Which works nicely, but I can imagine when all the actions are kept in the ddb, it might get heavy more quickly that I desire. For example it would be enough to be able to go back like 20 activities.

2) Scope triggers to an entryType I created a Section with 1 ContentType, let's say "FooBar". And I've configured the webhook to trigger on a 'afterSave' event, with Sender-Class 'craft\elements\Entry'. But this way, It will trigger if records of other content-types are saved as well. I'm not using Classes to establish the new content-type, just created it within the CMS.

This would be of such great help. Thanks again!

minyan-gu commented 5 years ago

and also I will add below: 3) Provide a way to delete Activities

4) Restoring the Craft CMS from db dump, then all activities become pending status. Is this a bug? Craft version: 3.2.2

minyan-gu commented 5 years ago

@brandonkelly seems you are v busy on Craft CMS 3.2, but do you have any update there?

I am dumping the entire craft database using mysqldump, so how to delete historic webhook events/activities?

brandonkelly commented 5 years ago

We just released Webhooks 2.1, which introduces a new Event Filters feature:

Event Filters

The default features here should help reduce the number of webhooks sent quite a bit.

As for filtering events by entry type, you can now make this happen by creating a module that registers an additional filter for the entry type:

Your module class:

use yii\base\Event;
use craft\webhooks\Plugin as Webhooks;
use craft\events\RegisterComponentTypesEvent;

Event::on(Webhooks::class, Webhooks::EVENT_REGISTER_FILTER_TYPES, function(RegisterComponentTypesEvent $e) {
    $e->types[] = FooBarFilter::class;
});

EntryTypeFilter.php

use craft\webhooks\filters\FilterInterface;
use craft\elements\Entry;
use yii\base\Event;

class FooBarFilter implements FilterInterface
{
    public static function displayName(): string
    {
        return 'Entry is FooBar';
    }

    public static function show(string $class, string $event): bool
    {
        return $class === Entry::class;
    }

    public static function check(Event $event, bool $value): bool
    {
        /** @var Entry $entry */
        $entry = $event->sender;

        return ($entry->type->handle === 'fooBar') === $value;
    }
}

With that in place, any webhooks with Sender Class set to craft\elements\Entry will start showing your “Entry is FooBar” filter under the Event Filters setting, and you can choose to enable it when needed.