Skip to content
Draft
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
19 changes: 19 additions & 0 deletions src/QueuedWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ public function prepareInputForAdd($input)
}
$input['sent_try'] = 0;

// Prevent duplicate queued webhooks for the same webhook and the same item/event.
$webhook_fields = ['webhooks_id', 'itemtype', 'items_id', 'event', 'url', 'send_time'];
$criteria = array_intersect_key($input, array_flip($webhook_fields));
if ($criteria !== []) {
$criteria['is_deleted'] = 0;
$criteria['sent_time'] = null;

$iterator = $DB->request([
'SELECT' => ['id'],
'FROM' => $this->getTable(),
'WHERE' => $criteria,
'LIMIT' => 1,
]);

if ($iterator->count() > 0) {
return [];
}
}

return $input;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/functional/QueuedWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,48 @@ public function testQueuedWebhookClean()
$this->assertTrue($queuedWebhook3->getFromDB($queuedWebhook3->getID()));
$this->assertTrue($queuedWebhook4->getFromDB($queuedWebhook4->getID()));
}

public function testPrepareInputForAddDeduplicate()
{
$this->login();

// Create a new webhook
$webhook = new Webhook();
$webhook->add([
'name' => 'dupwebhook',
'sent_try' => 1,
]);

$this->assertGreaterThan(0, $webhook->getID());

// Add a first queued webhook
$queued = new QueuedWebhook();
$send_time1 = date("Y-m-d H:i:s", time() + (1 * HOUR_TIMESTAMP));
$id1 = $queued->add([
'webhooks_id' => $webhook->getID(),
'send_time' => $send_time1,
]);

$this->assertGreaterThan(0, $id1);

// Try to add a second queued webhook with the same webhook and send_time
// This must be rejected !
$queued2 = new QueuedWebhook();
$id2 = $queued2->add([
'webhooks_id' => $webhook->getID(),
'send_time' => $send_time1,
]);

$this->assertFalse($id2);

// Adding with a different send_time must be allowed
$queued3 = new QueuedWebhook();
$send_time2 = date("Y-m-d H:i:s", time() + (2 * HOUR_TIMESTAMP));
$id3 = $queued3->add([
'webhooks_id' => $webhook->getID(),
'send_time' => $send_time2,
]);

$this->assertGreaterThan(0, $id3);
}
}
Loading