From f7413e1929de71c8ca6f4d4c14e8704e5a874203 Mon Sep 17 00:00:00 2001 From: Austin Best Date: Sat, 16 Nov 2024 16:59:11 -0500 Subject: [PATCH] Some mattermost platform adjustments --- .../traits/Notifications/Mattermost.php | 114 ++++++++++++++---- root/app/www/public/functions/telemetry.php | 10 +- 2 files changed, 97 insertions(+), 27 deletions(-) diff --git a/root/app/www/public/classes/traits/Notifications/Mattermost.php b/root/app/www/public/classes/traits/Notifications/Mattermost.php index e467933..e0a53ad 100644 --- a/root/app/www/public/classes/traits/Notifications/Mattermost.php +++ b/root/app/www/public/classes/traits/Notifications/Mattermost.php @@ -64,15 +64,27 @@ public function buildMattermostMessage($payload, $test = false) if ($payload['network']) { $pruned = []; foreach ($payload['network'] as $network) { - $pruned[] = str_replace('_', '\_', $network); + if (!trim($network)) { + continue; + } + + $pruned[] = $network; } - $table[] = '| Networks | ' . implode(', ', $pruned) . ' |'; + if ($pruned) { + $table[] = '| Networks | ' . implode(', ', $pruned) . ' |'; + } } if ($payload['volume']) { $pruned = []; foreach ($payload['volume'] as $volume) { - $pruned[] = truncateMiddle($volume, 20); + if (!trim($volume)) { + continue; + } + + if ($pruned) { + $pruned[] = truncateMiddle($volume, 20); + } } $table[] = '| Volumes | ' . implode(', ', $pruned) . ' |'; @@ -80,7 +92,13 @@ public function buildMattermostMessage($payload, $test = false) if ($payload['image']) { $pruned = []; foreach ($payload['image'] as $image) { - $pruned[] = $image; + if (!trim($image)) { + continue; + } + + if ($pruned) { + $pruned[] = $image; + } } $table[] = '| Images | ' . implode(', ', $pruned) . ' |'; @@ -88,13 +106,27 @@ public function buildMattermostMessage($payload, $test = false) if ($payload['imageList']) { $pruned = []; foreach ($payload['imageList'] as $imageList) { - $pruned[] = $imageList['cr'] . ' (' . byteConversion($imageList['size']) . ')'; + if (!trim($imageList['cr'])) { + continue; + } + + if ($pruned) { + $pruned[] = $imageList['cr'] . ' (' . byteConversion($imageList['size']) . ')'; + } } - $table[] = '| Image List | ' . implode(', ', $pruned) . ' |'; + if ($pruned) { + $table[] = '| Image List | ' . implode(', ', $pruned) . ' |'; + } else { + $table = []; + } } - $message .= implode("\n", $table) . "\n"; + if (count($table) > 2) { + $message .= implode("\n", $table) . "\n"; + } else { + $message = ''; + } break; case 'state': $message .= '##### ' . APP_NAME . ': Container state change' . "\n"; @@ -107,34 +139,53 @@ public function buildMattermostMessage($payload, $test = false) if ($payload['added']) { $state = []; foreach ($payload['added'] as $added) { + if (!trim($added['container'])) { + continue; + } + $state[] = $added['container']; } - $table[] = '| Added | ' . implode(', ', $state) . ' |'; - } else { - $table[] = '| Added | None |'; + + if ($state) { + $table[] = '| Added | ' . implode(', ', $state) . ' |'; + } } if ($payload['removed']) { $state = []; foreach ($payload['removed'] as $removed) { + if (!trim($removed['container'])) { + continue; + } + $state[] = $removed['container']; } - $table[] = '| Removed | ' . implode(', ', $state) . ' |'; - } else { - $table[] = '| Removed | None |'; + + if ($state) { + $table[] = '| Removed | ' . implode(', ', $state) . ' |'; + } } if ($payload['changes']) { $state = []; foreach ($payload['changes'] as $changes) { + if (!trim($changes['container'])) { + continue; + } + $state[] = $changes['container'] . ' [' . $changes['previous'] . ' → ' . $changes['current'] . ']'; } - $table[] = '| Changed | ' . implode("\n", $state) . ' |'; - } else { - $table[] = '| Changed | None |'; + + if (!$state) { + $table[] = '| Changed | ' . implode("\n", $state) . ' |'; + } } - $message .= implode("\n", $table) . "\n"; + if (count($table) > 2) { + $message .= implode("\n", $table) . "\n"; + } else { + $message = ''; + } break; case 'updates': $message .= '##### ' . APP_NAME . ': Updates' . "\n"; @@ -147,23 +198,38 @@ public function buildMattermostMessage($payload, $test = false) if ($payload['available']) { $updates = []; foreach ($payload['available'] as $available) { + if (!trim($available['container'])) { + continue; + } + $updates[] = $available['container']; } - $table[] = '| Available | ' . implode(', ', $updates) . ' |'; - } else { - $table[] = '| Available | None |'; + + if ($updates) { + $table[] = '| Available | ' . implode(', ', $updates) . ' |'; + } } + if ($payload['updated']) { $updates = []; foreach ($payload['updated'] as $updated) { + if (!trim($updated['container'])) { + continue; + } + $updates[] = $updated['container'] . ' [' . $updated['pre'] . ' → ' . $updated['post'] . ']'; } - $table[] = '| Updated | ' . implode("\n", $updates) . ' |'; - } else { - $table[] = '| Updated | None |'; + + if ($updates) { + $table[] = '| Updated | ' . implode("\n", $updates) . ' |'; + } } - $message .= implode("\n", $table) . "\n"; + if (count($table) > 2) { + $message .= implode("\n", $table) . "\n"; + } else { + $message = ''; + } break; case 'usage': $message .= '##### :warning: ' . APP_NAME . ': High usage' . "\n"; diff --git a/root/app/www/public/functions/telemetry.php b/root/app/www/public/functions/telemetry.php index 48306f1..e5e02c9 100644 --- a/root/app/www/public/functions/telemetry.php +++ b/root/app/www/public/functions/telemetry.php @@ -64,9 +64,10 @@ function telemetry($send = false) $telemetry['telemetry']['compose']['total'] = count($existingComposeFolders); - //-- NOTIFICATIONS INFO - $telemetry['telemetry']['notifications']['notifiarr'] = 0; - $telemetry['telemetry']['notifications']['telegram'] = 0; + //-- NOTIFICATION INFO + $telemetry['telemetry']['notifications']['notifiarr'] = 0; + $telemetry['telemetry']['notifications']['telegram'] = 0; + $telemetry['telemetry']['notifications']['mattermost'] = 0; if ($notificationTable) { foreach ($notificationTable as $notificationLink) { @@ -76,6 +77,9 @@ function telemetry($send = false) if ($notificationLink['platform_id'] == NotificationPlatforms::TELEGRAM) { $telemetry['telemetry']['notifications']['telegram']++; } + if ($notificationLink['platform_id'] == NotificationPlatforms::MATTERMOST) { + $telemetry['telemetry']['notifications']['mattermost']++; + } } } } else { //-- TELEMETRY IS DISABLED :(