Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Webhooks methods to complement Stream and E-Mail notifications. #327

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ table.activitysettings td.small label {
.nav-icon-activity {
background-image: url('../img/activity-dark.svg?v=1');
}

#webhook_settings_form {
display:inline-block;
}

#webhook_url {
width: 260px;
}
28 changes: 27 additions & 1 deletion js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,31 @@ $(document).ready(function() {
'activity', 'enable_email',
$(this).attr('checked') === 'checked' ? 'yes' : 'no'
);
})
});

$('#activity_webhook_enabled').on('change', function() {
OCP.AppConfig.setValue(
'activity', 'enable_webhook',
$(this).attr('checked') === 'checked' ? 'yes' : 'no'
);
});

$('#activity_webhook_ssl_verification').on('change', function() {
OCP.AppConfig.setValue(
'activity', 'webhook_ssl_verification_enabled',
$(this).attr('checked') === 'checked' ? 'yes' : 'no'
);
});

$('#webhook_settings_form').on('submit', function(event) {
event.preventDefault();
OCP.AppConfig.setValue(
'activity', 'webhook_url',
$("#webhook_url").val()
);
OCP.AppConfig.setValue(
'activity', 'webhook_token',
$("#webhook_token").val()
);
});
});
5 changes: 5 additions & 0 deletions lib/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function receive(IEvent $event) {
$selfAction = $event->getAffectedUser() === $event->getAuthor();
$streamSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'stream', $event->getType());
$emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType());
$webhookSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'webhook', $event->getType());
$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false;

// User is not the author or wants to see their own actions
Expand All @@ -76,6 +77,10 @@ public function receive(IEvent $event) {
$this->data->send($event);
}

if($webhookSetting) {
$this->data->sendWebhookRequest($event);
}

// User is not the author or wants to see their own actions
$createEmail = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail');

Expand Down
18 changes: 18 additions & 0 deletions lib/Controller/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function personal(
$notify_setting_selfemail = false) {

$settings = $this->manager->getSettings();
$webhookEnabled = $this->config->getAppValue('activity', 'enable_webhook', 'no') === 'yes';
foreach ($settings as $setting) {
if ($setting->canChangeStream()) {
$this->config->setUserValue(
Expand All @@ -121,6 +122,14 @@ public function personal(
(int) $this->request->getParam($setting->getIdentifier() . '_email', false)
);
}

if ($webhookEnabled) {
$this->config->setUserValue(
$this->user, 'activity',
'notify_webhook_' . $setting->getIdentifier(),
(int) $this->request->getParam($setting->getIdentifier() . '_webhook', false)
);
}
}

$email_batch_time = 3600;
Expand Down Expand Up @@ -167,6 +176,7 @@ public function admin(
$notify_setting_selfemail = false) {

$settings = $this->manager->getSettings();
$webhookEnabled = $this->config->getAppValue('activity', 'enable_webhook', 'no') === 'yes';
foreach ($settings as $setting) {
if ($setting->canChangeStream()) {
$this->config->setAppValue(
Expand All @@ -183,6 +193,14 @@ public function admin(
(int) $this->request->getParam($setting->getIdentifier() . '_email', false)
);
}

if ($webhookEnabled) {
$this->config->setAppValue(
'activity',
'notify_webhook_' . $setting->getIdentifier(),
(int) $this->request->getParam($setting->getIdentifier() . '_webhook', false)
);
}
}

$email_batch_time = 3600;
Expand Down
32 changes: 31 additions & 1 deletion lib/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ class Data {
/** @var IDBConnection */
protected $connection;

/** @var WebhookHelper */
protected $webhookHelper;

/**
* @param IManager $activityManager
* @param IDBConnection $connection
*/
public function __construct(IManager $activityManager, IDBConnection $connection) {
public function __construct(IManager $activityManager, IDBConnection $connection, WebhookHelper $webhookHelper) {
$this->activityManager = $activityManager;
$this->connection = $connection;
$this->webhookHelper = $webhookHelper;
}

/**
Expand Down Expand Up @@ -102,6 +106,32 @@ public function send(IEvent $event) {
return true;
}

/**
* Send an http(s) request for the event to the webhook url
*
* @param IEvent $event
* @return bool
*/
public function sendWebhookRequest(IEvent $event) {
$content = [
'app' => $event->getApp(),
'type' => $event->getType(),
'affecteduser' => $event->getAffectedUser(),
'user' => $event->getAuthor(),
'timestamp' => (int) $event->getTimestamp(),
'subject' => $event->getSubject(),
'subjectparams' => $event->getSubjectParameters(),
'message' => $event->getMessage(),
'messageparams' => $event->getMessageParameters(),
'object_type' => $event->getObjectType(),
'object_id' => (int) $event->getObjectId(),
'object_name' => $event->getObjectName(),
'link' => $event->getLink(),
];
$this->webhookHelper->sendWebhookRequest($content);
return true;
}

/**
* Send an event as email
*
Expand Down
37 changes: 28 additions & 9 deletions lib/FilesHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ protected function addNotificationsForFileAction($filePath, $activityType, $subj
$fileId, $path, true,
!empty($filteredStreamUsers[$user]),
$filteredEmailUsers[$user] ?? false,
$this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'webhook', $activityType),
$activityType
);
}
Expand Down Expand Up @@ -405,6 +406,7 @@ protected function fileRenaming($oldPath, $newPath) {
$fileId, $path . '/' . $fileName, true,
!empty($filteredStreamUsers[$user]),
$filteredEmailUsers[$user] ?? false,
$this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'webhook', Files::TYPE_SHARE_CHANGED),
Files::TYPE_SHARE_CHANGED
);
}
Expand Down Expand Up @@ -505,6 +507,7 @@ protected function generateDeleteActivities($users, $pathMap, $fileId, $oldFileN
$fileId, $path . '/' . $oldFileName, true,
!empty($filteredStreamUsers[$user]),
$filteredEmailUsers[$user] ?? false,
$this->userSettings->filterUsersBySetting($users, 'webhook', Files::TYPE_SHARE_DELETED),
Files::TYPE_SHARE_DELETED
);
}
Expand Down Expand Up @@ -544,6 +547,7 @@ protected function generateAddActivities($users, $pathMap, $fileId, $fileName) {
$fileId, $path . '/' . $fileName, true,
!empty($filteredStreamUsers[$user]),
$filteredEmailUsers[$user] ?? false,
$this->userSettings->filterUsersBySetting($users, 'webhook', Files::TYPE_SHARE_CREATED),
Files::TYPE_SHARE_CREATED
);
}
Expand Down Expand Up @@ -590,6 +594,7 @@ protected function generateMoveActivities($users, $beforePathMap, $afterPathMap,
$fileId, $afterPathMap[$user] . '/' . $fileName, true,
!empty($filteredStreamUsers[$user]),
$filteredEmailUsers[$user] ?? false,
$this->userSettings->filterUsersBySetting($users, 'webhook', Files::TYPE_SHARE_CHANGED),
Files::TYPE_SHARE_CHANGED
);
}
Expand Down Expand Up @@ -703,7 +708,8 @@ protected function shareWithUser($shareWith, $fileSource, $itemType, $fileTarget
$shareWith, 'shared_with_by', [[$fileSource => $fileTarget], $this->currentUser->getUserIdentifier()],
(int) $fileSource, $fileTarget, $itemType === 'file',
$this->userSettings->getUserSetting($shareWith, 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($shareWith, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($shareWith, 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($shareWith, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($shareWith, 'setting', 'batchtime') : false,
$this->userSettings->getUserSetting($shareWith, 'webhook', Files_Sharing::TYPE_SHARED)
);
}

Expand Down Expand Up @@ -760,7 +766,8 @@ protected function shareByLink($fileSource, $itemType, $linkOwner) {
$linkOwner, 'shared_link_self', [[$fileSource => $path]],
(int) $fileSource, $path, $itemType === 'file',
$this->userSettings->getUserSetting($linkOwner, 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($linkOwner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($linkOwner, 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($linkOwner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($linkOwner, 'setting', 'batchtime') : false,
$this->userSettings->getUserSetting($linkOwner, 'webhook', Files_Sharing::TYPE_SHARED)
);
}

Expand Down Expand Up @@ -806,7 +813,8 @@ protected function unshareFromUser(IShare $share) {
$share->getSharedWith(), 'unshared_by', [[$share->getNodeId() => $share->getTarget()], $this->currentUser->getUserIdentifier()],
$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
$this->userSettings->getUserSetting($share->getSharedWith(), 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($share->getSharedWith(), 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($share->getSharedWith(), 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($share->getSharedWith(), 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($share->getSharedWith(), 'setting', 'batchtime') : false,
$this->userSettings->getUserSetting($share->getSharedWith(), 'webhook', Files_Sharing::TYPE_SHARED)
);
}

Expand Down Expand Up @@ -875,7 +883,9 @@ protected function unshareLink(IShare $share) {
$owner, $actionSharer, [[$share->getNodeId() => $share->getTarget()]],
$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false,

$this->userSettings->getUserSetting($owner, 'webhook', Files_Sharing::TYPE_SHARED)
);

if ($share->getSharedBy() !== $share->getShareOwner()) {
Expand All @@ -884,7 +894,8 @@ protected function unshareLink(IShare $share) {
$owner, $actionOwner, [[$share->getNodeId() => $share->getTarget()], $share->getSharedBy()],
$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false,
$this->userSettings->getUserSetting($owner, 'webhook', Files_Sharing::TYPE_SHARED)
);
}
}
Expand Down Expand Up @@ -925,7 +936,8 @@ protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUse
$user, $actionUser, [[$fileSource => $path], $this->currentUser->getUserIdentifier()],
$fileSource, $path, ($itemType === 'file'),
!empty($filteredStreamUsersInGroup[$user]),
$filteredEmailUsersInGroup[$user] ?? false
$filteredEmailUsersInGroup[$user] ?? false,
$this->userSettings->filterUsersBySetting($userIds, 'webhook', Files_Sharing::TYPE_SHARED)
);
}
}
Expand Down Expand Up @@ -979,7 +991,8 @@ protected function shareNotificationForSharer($subject, $shareWith, $fileSource,
$sharer, $subject, [[$fileSource => $path], $shareWith],
$fileSource, $path, ($itemType === 'file'),
$this->userSettings->getUserSetting($sharer, 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($sharer, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($sharer, 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($sharer, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($sharer, 'setting', 'batchtime') : false,
$this->userSettings->getUserSetting($sharer, 'webhook', Files_Sharing::TYPE_SHARED)
);
}

Expand All @@ -1005,7 +1018,8 @@ protected function reshareNotificationForSharer($owner, $subject, $shareWith, $f
$owner, $subject, [[$fileSource => $path], $this->currentUser->getUserIdentifier(), $shareWith],
$fileSource, $path, ($itemType === 'file'),
$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false
$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : false,
$this->userSettings->getUserSetting($owner, 'webhook', Files_Sharing::TYPE_SHARED)
);
}

Expand Down Expand Up @@ -1070,9 +1084,10 @@ protected function shareNotificationForOriginalOwners($currentOwner, $subject, $
* @param bool $isFile If the item is a file, we link to the parent directory
* @param bool $streamSetting
* @param int $emailSetting
* @param bool $webhookSetting
* @param string $type
*/
protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) {
protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $webhookSetting, $type = Files_Sharing::TYPE_SHARED) {
if (!$streamSetting && !$emailSetting) {
return;
}
Expand Down Expand Up @@ -1114,5 +1129,9 @@ protected function addNotificationsForUser($user, $subject, $subjectParams, $fil
$latestSend = time() + $emailSetting;
$this->activityData->storeMail($event, $latestSend);
}

if($webhookSetting) {
$this->activityData->sendWebhookRequest($event);
}
}
}
7 changes: 7 additions & 0 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ public function getForm() {
if ($setting->canChangeMail()) {
$methods[] = IExtension::METHOD_MAIL;
}
$methods[] = 'webhook';

$identifier = $setting->getIdentifier();

$activities[$identifier] = array(
'desc' => $setting->getName(),
IExtension::METHOD_MAIL => $this->userSettings->getConfigSetting('email', $identifier),
IExtension::METHOD_STREAM => $this->userSettings->getConfigSetting('stream', $identifier),
'webhook' => $this->userSettings->getConfigSetting('webhook', $identifier),
'methods' => $methods,
);
}
Expand All @@ -110,6 +112,10 @@ public function getForm() {
'activities' => $activities,
'is_email_set' => true,
'email_enabled' => $this->config->getAppValue('activity', 'enable_email', 'yes') === 'yes',
'webhook_enabled' => $this->config->getAppValue('activity', 'enable_webhook', 'no') === 'yes',
'webhook_url' => $this->config->getAppValue('activity', 'webhook_url', ''),
'webhook_token' => $this->config->getAppValue('activity', 'webhook_token', ''),
'webhook_ssl_verification_enabled' => $this->config->getAppValue('activity', 'webhook_ssl_verification_enabled', 'yes') === 'yes',

'setting_batchtime' => $settingBatchTime,

Expand All @@ -119,6 +125,7 @@ public function getForm() {
'methods' => [
IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
'webhook' => $this->l10n->t('Webhook'),
],
], 'blank');
}
Expand Down
10 changes: 9 additions & 1 deletion lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public function getForm() {
return $a->getPriority() > $b->getPriority();
});

$webhookEnabled = $this->config->getAppValue('activity', 'enable_webhook', 'no') === 'yes';
$activities = [];
foreach ($settings as $setting) {
if (!$setting->canChangeStream() && !$setting->canChangeMail()) {
if (!$setting->canChangeStream() && !$setting->canChangeMail() && !$webhookEnabled) {
// No setting can be changed => don't display
continue;
}
Expand All @@ -97,13 +98,17 @@ public function getForm() {
if ($setting->canChangeMail()) {
$methods[] = IExtension::METHOD_MAIL;
}
if ($webhookEnabled) {
$methods[] = 'webhook';
}

$identifier = $setting->getIdentifier();

$activities[$identifier] = array(
'desc' => $setting->getName(),
IExtension::METHOD_MAIL => $this->userSettings->getUserSetting($this->user, 'email', $identifier),
IExtension::METHOD_STREAM => $this->userSettings->getUserSetting($this->user, 'stream', $identifier),
'webhook' => $this->userSettings->getUserSetting($this->user, 'webhook', $identifier),
'methods' => $methods,
);
}
Expand All @@ -129,6 +134,9 @@ public function getForm() {
IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
];
}
if ($webhookEnabled) {
$methods['webhook'] = $this->l10n->t('Webhook');
}

return new TemplateResponse('activity', 'settings/personal', [
'setting' => 'personal',
Expand Down
Loading