Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ return array(

'shortenTimeout' => 15 // If after this amount of seconds no url shortener has come up with a short URL the normal URL will be used. (Not in effect when there are no shorteners listening.)

// or

'filter' => null // Any valid filter implementing Phergie\Irc\Plugin\React\EventFilter\FilterInterface to filter which messages should be handled

)),

)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"phergie/phergie-irc-bot-react": "~1.0",
"nojimage/twitter-text-php": "1.1.1",
"phergie/phergie-irc-plugin-http": "^3.0.0-alpha1",
"react/promise": "~1.0|~2.0"
"react/promise": "~1.0|~2.0",
"phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0"
},
"require-dev": {
"phergie/phergie-irc-bot-react-development": "^1.0.2|^1.1"
Expand Down
48 changes: 46 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Phergie\Irc\Bot\React\EventQueue;
use Phergie\Irc\Client\React\LoopAwareInterface;
use Phergie\Irc\Event\UserEvent;
use Phergie\Irc\Plugin\React\EventFilter\FilterInterface;
use React\Promise\Deferred;
use Phergie\Plugin\Http\Request;

Expand All @@ -27,7 +28,8 @@
*/
class Plugin extends AbstractPlugin implements LoopAwareInterface
{
const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface';
const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface';
const EVENT_FILTER_INTERFACE = 'Phergie\Irc\Plugin\React\EventFilter\FilterInterface';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for using a constant versus just referencing FilterInterface::class where needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force of 5.4 habbit, I'll update both of them


/**
* @var UrlHandlerInterface
Expand All @@ -42,6 +44,11 @@ class Plugin extends AbstractPlugin implements LoopAwareInterface
*/
protected $hostUrlEmitsOnly = false;

/**
* @var FilterInterface
*/
protected $filter = null;

/**
* @var LoopInterface
*/
Expand Down Expand Up @@ -73,6 +80,12 @@ public function __construct(array $config = [])
if (isset($config['hostUrlEmitsOnly'])) {
$this->hostUrlEmitsOnly = boolval($config['hostUrlEmitsOnly']);
}
if (
isset($config['filter']) &&
in_array(static::EVENT_FILTER_INTERFACE, class_implements($config['filter']))
) {
$this->filter = $config['filter'];
}
}

/**
Expand Down Expand Up @@ -113,6 +126,12 @@ public function logDebug($message)

public function handleIrcReceived(UserEvent $event, EventQueue $queue)
{
if (
$this->filter !== null &&
$this->filter->filter($event) !== false
) {
return;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original impetus for filing #11 was to facilitate filtering out URLs by hostname. If the event filter doesn't have access to the parsed URLs or has to parse them a second time, it seems to defeat that point. It's already possible to use this plugin with the EventFilter plugin via configuration without any changes to this plugin (see usage documentation). What I want is filtering specific to URLs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, currently writing a set of filters that lets user filter based on hostname, path, protocol, and port.

$params = $event->getParams();
$extractor = new \Twitter_Extractor($params['text']);
$urls = $extractor->extractURLs();
Expand Down
35 changes: 35 additions & 0 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use GuzzleHttp\Message\Response;
use Phake;
use Phergie\Irc\Plugin\React\EventFilter\NotFilter;
use Phergie\Irc\Plugin\React\Url\Plugin;
use React\Promise\FulfilledPromise;

Expand Down Expand Up @@ -351,4 +352,38 @@ public function testHandleUrlRequest() {
))
);
}

public function testFiltered()
{
$event = Phake::mock('Phergie\Irc\Event\UserEvent');
Phake::when($event)->getParams()->thenReturn([
'text' => '',
]);
$queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue');
$filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface');
Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(false);

(new Plugin([
'filter' => new NotFilter($filter),
]))->handleIrcReceived($event, $queue);

Phake::verify($event, Phake::never())->getParams();
}

public function testNotFiltered()
{
$event = Phake::mock('Phergie\Irc\Event\UserEvent');
Phake::when($event)->getParams()->thenReturn([
'text' => '',
]);
$queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue');
$filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface');
Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(true);

(new Plugin([
'filter' => new NotFilter($filter),
]))->handleIrcReceived($event, $queue);

Phake::verify($event)->getParams();
}
}