Skip to content
Next Next commit
aggregated an more data in the notifications
Signed-off-by: Artur Neumann <[email protected]>
  • Loading branch information
individual-it committed Oct 11, 2022
commit 32dcf03da9a3e0e7ff94708e52bf7848c8743f68
27 changes: 27 additions & 0 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ public function prepare(INotification $notification, string $languageCode): INot
)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')));
return $notification;
case 'op_notification':
$p = $notification->getSubjectParameters();

// see https://github.com/nextcloud/server/issues/1706 for docs
$richSubjectInstance = [
'type' => 'file',
'id' => 0,
'name' => $p['link'],
'path' => '',
'link' => $p['link'],
];
$message = $p['projectTitle'] . ' ';
foreach ($p['reasons'] as $reason) {
$message .= $reason . ',';
}
$message = rtrim($message, ',');
$notification->setParsedSubject('(' . $p['count']. ') ' . $p['resourceTitle'])
->setParsedMessage('--')
->setLink($p['link'] ?? '')
->setRichMessage(
$message,
[
'instance' => $richSubjectInstance,
]
)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')));
return $notification;

default:
// Unknown subject => Unknown notification => throw
Expand Down
23 changes: 23 additions & 0 deletions lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private function checkNotificationsForUser(string $userId): void {
$newLastNotificationCheck = time();
$openprojectUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url');
$notifications = $this->getNotifications($userId);
$aggregatedNotifications = [];
if (!isset($notifications['error']) && count($notifications) > 0) {
$this->config->setUserValue(
$userId,
Expand All @@ -160,7 +161,29 @@ private function checkNotificationsForUser(string $userId): void {
if ($createdAt->getTimestamp() > $lastNotificationCheck) {
$nbRelevantNotifications++;
}
$wpId = preg_replace('/.*\//','', $n['_links']['resource']['href']);
if (!array_key_exists($wpId, $aggregatedNotifications)) {
$aggregatedNotifications[$wpId] = [
'resourceTitle' => $n['_links']['resource']['title'],
'projectTitle' => $n['_links']['project']['title'],
'count' => 1,
// TODO according to the docs https://github.com/nextcloud/notifications/blob/master/docs/notification-workflow.md#creating-a-new-notification
// links should not be set here
'link' => self::sanitizeUrl(
$openprojectUrl . '/notifications/details/' . $wpId . '/activity/'
)
];
} else {
$aggregatedNotifications[$wpId]['count']++;
}
$aggregatedNotifications[$wpId]['reasons'][] = $n['reason'];
}
foreach ($aggregatedNotifications as $n) {
$n['reasons'] = array_unique($n['reasons']);
// TODO can we use https://github.com/nextcloud/notifications/blob/master/docs/notification-workflow.md#defer-and-flush ?
$this->sendNCNotification($userId, 'op_notification', $n);
}

if ($nbRelevantNotifications > 0) {
$this->sendNCNotification($userId, 'new_open_tickets', [
'nbNotifications' => $nbRelevantNotifications,
Expand Down