183 lines
7.1 KiB
PHP
183 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace Kanboard\Plugin\Discord\Notification;
|
|
|
|
use Kanboard\Core\Base;
|
|
use Kanboard\Core\Notification\NotificationInterface;
|
|
use Kanboard\Model\TaskModel;
|
|
|
|
/**
|
|
* Discord Notification
|
|
*
|
|
* @package notification
|
|
* @author Ryonez Coruscare
|
|
*/
|
|
class Discord extends Base implements NotificationInterface
|
|
{
|
|
/**
|
|
* Send notification to a user
|
|
*
|
|
* @access public
|
|
* @param array $user
|
|
* @param string $eventName
|
|
* @param array $eventData
|
|
*/
|
|
public function notifyUser(array $user, $eventName, array $eventData)
|
|
{
|
|
$webhook = $this->userMetadataModel->get($user['id'], 'discord_webhook_url', $this->configModel->get('discord_webhook_url'));
|
|
$forward_attachments = $this->userMetadataModel->get($user['id'], 'forward_attachments', $this->configModel->get('forward_attachments'));
|
|
|
|
if (! empty($webhook)) {
|
|
if ($eventName === TaskModel::EVENT_OVERDUE) {
|
|
foreach ($eventData['tasks'] as $task) {
|
|
$project = $this->projectModel->getById($task['project_id']);
|
|
$eventData['task'] = $task;
|
|
$this->sendMessage($webhook, $forward_attachments, $project, $eventName, $eventData);
|
|
}
|
|
} else {
|
|
$project = $this->projectModel->getById($eventData['task']['project_id']);
|
|
$this->sendMessage($webhook, $forward_attachments, $project, $eventName, $eventData);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send notification to a project
|
|
*
|
|
* @access public
|
|
* @param array $project
|
|
* @param string $eventName
|
|
* @param array $eventData
|
|
*/
|
|
public function notifyProject(array $project, $eventName, array $eventData)
|
|
{
|
|
$webhook = $this->projectMetadataModel->get($project['id'], 'discord_webhook_url', $this->configModel->get('discord_webhook_url'));
|
|
$forward_attachments = $this->userMetadataModel->get($user['id'], 'forward_attachments', $this->configModel->get('forward_attachments'));
|
|
|
|
if (! empty($webhook)) {
|
|
$this->sendMessage($webhook, $forward_attachments, $project, $eventName, $eventData);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get message to send
|
|
*
|
|
* @access public
|
|
* @param array $project
|
|
* @param string $eventName
|
|
* @param array $eventData
|
|
* @return array
|
|
*/
|
|
public function getMessage(array $project, $forward_attachments, $eventName, array $eventData)
|
|
{
|
|
if ($this->userSession->isLogged()) {
|
|
$author = $this->helper->user->getFullname();
|
|
$title = $this->notificationModel->getTitleWithAuthor($author, $eventName, $eventData);
|
|
} else {
|
|
$title = $this->notificationModel->getTitleWithoutAuthor($eventName, $eventData);
|
|
}
|
|
|
|
$proj_name = isset($eventData['project_name']) ? $eventData['project_name'] : $eventData['task']['project_name'];
|
|
$task_title = $eventData['task']['title'];
|
|
$task_url = $this->helper->url->to('TaskViewController', 'show', array('task_id' => $eventData['task']['id'], 'project_id' => $project['id']), '', true);
|
|
|
|
$attachment = '';
|
|
|
|
//Build Message
|
|
|
|
$message = "[".htmlspecialchars($proj_name, ENT_NOQUOTES | ENT_IGNORE)."]\n";
|
|
$message .= htmlspecialchars($title, ENT_NOQUOTES | ENT_IGNORE)."\n";
|
|
|
|
if ($this->configModel->get('application_url') !== '')
|
|
{
|
|
$message .= '📝 <a href="'.$task_url.'">'.htmlspecialchars($task_title, ENT_NOQUOTES | ENT_IGNORE).'</a>';
|
|
}
|
|
else
|
|
{
|
|
$message .= htmlspecialchars($task_title, ENT_NOQUOTES | ENT_IGNORE);
|
|
}
|
|
|
|
// Add additional informations
|
|
|
|
$description_events = array(TaskModel::EVENT_CREATE, TaskModel::EVENT_UPDATE, TaskModel::EVENT_USER_MENTION);
|
|
$subtask_events = array(SubtaskModel::EVENT_CREATE, SubtaskModel::EVENT_UPDATE, SubtaskModel::EVENT_DELETE);
|
|
$comment_events = array(CommentModel::EVENT_UPDATE, CommentModel::EVENT_CREATE, CommentModel::EVENT_DELETE, CommentModel::EVENT_USER_MENTION);
|
|
|
|
if (in_array($eventName, $subtask_events)) // For subtask events
|
|
{
|
|
$subtask_status = $eventData['subtask']['status'];
|
|
$subtask_symbol = '';
|
|
|
|
if ($subtask_status == SubtaskModel::STATUS_DONE)
|
|
{
|
|
$subtask_symbol = '❌ ';
|
|
}
|
|
elseif ($subtask_status == SubtaskModel::STATUS_TODO)
|
|
{
|
|
$subtask_symbol = '';
|
|
}
|
|
elseif ($subtask_status == SubtaskModel::STATUS_INPROGRESS)
|
|
{
|
|
$subtask_symbol = '🕘 ';
|
|
}
|
|
|
|
$message .= "\n<b> ↳ ".$subtask_symbol.'</b> <em>"'.htmlspecialchars($eventData['subtask']['title'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
|
|
}
|
|
|
|
elseif (in_array($eventName, $description_events)) // If description available
|
|
{
|
|
if ($eventData['task']['description'] != '')
|
|
{
|
|
$message .= "\n✏️ ".'<em>"'.htmlspecialchars($eventData['task']['description'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
|
|
}
|
|
}
|
|
|
|
elseif (in_array($eventName, $comment_events)) // If comment available
|
|
{
|
|
$message .= "\n💬 ".'<em>"'.htmlspecialchars($eventData['comment']['comment'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
|
|
}
|
|
|
|
elseif ($eventName === TaskFileModel::EVENT_CREATE and $forward_attachments) // If attachment available
|
|
{
|
|
$file_path = getcwd()."/data/files/".$eventData['file']['path'];
|
|
$file_name = $eventData['file']['name'];
|
|
$is_image = $eventData['file']['is_image'];
|
|
|
|
mkdir(sys_get_temp_dir()."/kanboard_discord_plugin");
|
|
$attachment = sys_get_temp_dir()."/kanboard_discord_plugin/".clean($file_name);
|
|
file_put_contents($attachment, file_get_contents($file_path));
|
|
}
|
|
|
|
|
|
if ($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').'>';
|
|
}
|
|
|
|
return array(
|
|
'text' => $message,
|
|
'attachment' => $attachment,
|
|
'username' => 'Kanboard',
|
|
'icon_url' => 'https://raw.githubusercontent.com/kanboard/kanboard/master/assets/img/favicon.png',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Send message to Discord
|
|
*
|
|
* @access protected
|
|
* @param string $webhook
|
|
* @param string $channel
|
|
* @param array $project
|
|
* @param string $eventName
|
|
* @param array $eventData
|
|
*/
|
|
protected function sendMessage($webhook, $forward_attachments, array $project, $eventName, array $eventData)
|
|
{
|
|
$payload = $this->getMessage($project, $forward_attachments, $eventName, $eventData);
|
|
|
|
$this->httpClient->postJsonAsync($webhook, $payload);
|
|
}
|
|
}
|