Skip to content

Commit

Permalink
Now works without letting chat drop messages due to post rate
Browse files Browse the repository at this point in the history
  • Loading branch information
paul committed Nov 8, 2018
1 parent 1152f65 commit e4135df
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
34 changes: 30 additions & 4 deletions Notification/SynologyChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SynologyChat extends Base implements NotificationInterface
public function notifyUser(array $user, $eventName, array $eventData)
{
$webhook = $this->userMetadataModel->get($user['id'], 'synologychat_webhook_url', $this->configModel->get('synologychat_webhook_url'));
$includeLink = $this->userMetadataModel->get($user['id'], 'synologychat_include_link', $this->configModel->get('synologychat_include_link', false));
$includeLink = $this->userMetadataModel->get($user['id'], 'synologychat_include_link', $this->configModel->get('synologychat_include_link', 0));

if (! empty($webhook)) {
if ($eventName === TaskModel::EVENT_OVERDUE) {
Expand All @@ -52,7 +52,7 @@ public function notifyUser(array $user, $eventName, array $eventData)
public function notifyProject(array $project, $eventName, array $eventData)
{
$webhook = $this->projectMetadataModel->get($project['id'], 'synologychat_webhook_url', $this->configModel->get('synologychat_webhook_url'));
$includeLink = $this->projectMetadataModel->get($project['id'], 'synologychat_include_link', $this->configModel->get('synologychat_include_link', false));
$includeLink = $this->projectMetadataModel->get($project['id'], 'synologychat_include_link', $this->configModel->get('synologychat_include_link', 0));

if (! empty($webhook)) {
$this->sendMessage($webhook, $includeLink, $project, $eventName, $eventData);
Expand Down Expand Up @@ -82,7 +82,7 @@ public function getMessage(array $project, $eventName, array $eventData, $includ
$message .= $title;
$message .= ' ('.$eventData['task']['title'].')';

if ($includeLink && $this->configModel->get('application_url') !== '') {
if ($includeLink == 1 && $this->configModel->get('application_url') !== '') {
$message .= ' - <';
$message .= $this->helper->url->to('TaskViewController', 'show', array('task_id' => $eventData['task']['id'], 'project_id' => $project['id']), '', true);
$message .= '|'.t('view the task on Kanboard').'>';
Expand All @@ -105,8 +105,34 @@ public function getMessage(array $project, $eventName, array $eventData, $includ
*/
protected function sendMessage($webhook, $includeLink, array $project, $eventName, array $eventData)
{
$backoffRetries = 0;
$tryAgain = false;

$payload = $this->getMessage($project, $eventName, $eventData, $includeLink);

$this->httpClient->postFormAsync($webhook, $payload);
do
{
$tryAgain = false;
$body = $this->httpClient->postForm($webhook, $payload);

if ($body !== '')
{
$response = json_decode($body, true);
if (isset($response['success']) && $response['success']==false)
{
// Synology Chat API as of v2.1.0-1228 will only accept a fairly low rate of submission,
// and if it's too fast, it returns
// {"error":{"code":411,"errors":"create post too fast"},"success":false}
if (isset($response['error']) && isset($response['error']['code']) && $response['error']['code'] == 411)
{
$tryAgain = true;
$backoffRetries += 1;

//back off for between 0-0.1 secs, adding 0.1 each retry
usleep(($backoffRetries-1)*100000 + rand(0,100000));
}
}
}
} while ($tryAgain);
}
}
5 changes: 3 additions & 2 deletions Template/config/integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<?= $this->form->label(t('Webhook URL'), 'synologychat_webhook_url') ?>
<?= $this->form->text('synologychat_webhook_url', $values) ?>

<?= $this->form->checkbox('synologychat_include_link', t('Include link to task'), 1, $values['synologychat_include_link'] == 1) ?>
<p class="form-help"><?= t('Not advised in current versions of Synology Chat (version <= 1.0.1-40) since it causes url unfurling showing "Login"') ?></p>
<?= $this->form->hidden('synologychat_include_link', array('synologychat_include_link' => 0)) ?>
<?= $this->form->checkbox('synologychat_include_link', t('Include link to task'), 1, isset($values['synologychat_include_link']) && $values['synologychat_include_link'] == 1) ?>
<p class="form-help"><?= t('Not advised in current versions of Synology Chat (version <= 2.1.0-1228) since it causes url unfurling showing "Login"') ?></p>


<p class="form-help"><a href="https://github.com/Kolossi/plugin-synology-chat#configuration" target="_blank"><?= t('Help on Synology Chat integration') ?></a></p>
Expand Down
5 changes: 3 additions & 2 deletions Template/project/integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<?= $this->form->label(t('Webhook URL'), 'synologychat_webhook_url') ?>
<?= $this->form->text('synologychat_webhook_url', $values) ?>

<?= $this->form->checkbox('synologychat_include_link', t('Include link to task'), 1, $values['synologychat_include_link'] == 1) ?>
<p class="form-help"><?= t('Not advised in current versions of Synology Chat (version <= 1.0.1-40) since it causes url unfurling showing "Login"') ?></p>
<?= $this->form->hidden('synologychat_include_link', array('synologychat_include_link' => 0)) ?>
<?= $this->form->checkbox('synologychat_include_link', t('Include link to task'), 1, isset($values['synologychat_include_link']) && $values['synologychat_include_link'] == 1) ?>
<p class="form-help"><?= t('Not advised in current versions of Synology Chat (version <= 2.1.0-1228) since it causes url unfurling showing "Login"') ?></p>


<p class="form-help"><a href="https://github.com/Kolossi/plugin-synology-chat#configuration" target="_blank"><?= t('Help on Synology Chat integration') ?></a></p>
Expand Down
5 changes: 3 additions & 2 deletions Template/user/integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<?= $this->form->label(t('Webhook URL'), 'synologychat_webhook_url') ?>
<?= $this->form->text('synologychat_webhook_url', $values) ?>

<?= $this->form->checkbox('synologychat_include_link', t('Include link to task'), 1, $values['synologychat_include_link'] == 1) ?>
<p class="form-help"><?= t('Not advised in current versions of Synology Chat (version <= 1.0.1-40) since it causes url unfurling showing "Login"') ?></p>
<?= $this->form->hidden('synologychat_include_link', array('synologychat_include_link' => 0)) ?>
<?= $this->form->checkbox('synologychat_include_link', t('Include link to task'), 1, isset($values['synologychat_include_link']) && $values['synologychat_include_link'] == 1) ?>
<p class="form-help"><?= t('Not advised in current versions of Synology Chat (version <= 2.1.0-1228) since it causes url unfurling showing "Login"') ?></p>


<p class="form-help"><a href="https://github.com/Kolossi/plugin-synology-chat#configuration" target="_blank"><?= t('Help on Synology Chat integration') ?></a></p>
Expand Down

0 comments on commit e4135df

Please sign in to comment.