From 4e77922be80a1910689857f670277ebb0ac0ec65 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 17 Oct 2023 21:59:49 +0700 Subject: [PATCH] refactor: structures for notifier --- .../{github-event.json => github-events.json} | 0 .../{gitlab-event.json => gitlab-events.json} | 0 .../{tg-setting.json => tgn-settings.json} | 0 config/tg-notifier.php | 14 +++- src/Bot.php | 49 +++++++++---- src/Interfaces/EventInterface.php | 55 +++++++++++++++ src/Interfaces/Structures/EventInterface.php | 68 ------------------- .../Structures/NotificationInterface.php | 2 +- .../Structures/SettingInterface.php | 17 ----- .../{Structures => }/WebhookInterface.php | 2 +- src/{Structures => Models}/Event.php | 27 +++++++- src/{Structures => Models}/Setting.php | 19 +++++- src/Notifier.php | 32 +++++++-- src/Objects/Validator.php | 50 ++++++++++++++ src/Structures/App.php | 2 +- src/Structures/Notification.php | 31 ++++++--- src/Trait/BotSettingTrait.php | 6 +- src/Trait/EventSettingTrait.php | 10 +-- src/Trait/EventTrait.php | 36 ++++++++++ src/Trait/ValidationEventTrait.php | 35 ---------- src/Webhook.php | 2 +- 21 files changed, 290 insertions(+), 167 deletions(-) rename config/jsons/{github-event.json => github-events.json} (100%) rename config/jsons/{gitlab-event.json => gitlab-events.json} (100%) rename config/jsons/{tg-setting.json => tgn-settings.json} (100%) create mode 100644 src/Interfaces/EventInterface.php delete mode 100644 src/Interfaces/Structures/EventInterface.php rename src/Interfaces/{Structures => }/WebhookInterface.php (90%) rename src/{Structures => Models}/Event.php (75%) rename src/{Structures => Models}/Setting.php (87%) create mode 100644 src/Objects/Validator.php create mode 100644 src/Trait/EventTrait.php delete mode 100644 src/Trait/ValidationEventTrait.php diff --git a/config/jsons/github-event.json b/config/jsons/github-events.json similarity index 100% rename from config/jsons/github-event.json rename to config/jsons/github-events.json diff --git a/config/jsons/gitlab-event.json b/config/jsons/gitlab-events.json similarity index 100% rename from config/jsons/gitlab-event.json rename to config/jsons/gitlab-events.json diff --git a/config/jsons/tg-setting.json b/config/jsons/tgn-settings.json similarity index 100% rename from config/jsons/tg-setting.json rename to config/jsons/tgn-settings.json diff --git a/config/tg-notifier.php b/config/tg-notifier.php index 1021753..abf5021 100644 --- a/config/tg-notifier.php +++ b/config/tg-notifier.php @@ -18,12 +18,24 @@ ], 'author' => [ - 'discussion' => $_ENV['TGN_AUTHOR_DISCUSSION'] ?? + 'discussion' => $_ENV['TGN_AUTHOR_DISCUSSION'] ?? 'https://github.com/lbiltech/telegram-git-notifier/discussions', 'source_code' => $_ENV['TGN_AUTHOR_SOURCE_CODE'] ?? 'https://github.com/lbiltech/telegram-git-notifier', ], + 'data_file' => [ + 'setting' => $_ENV['TGN_PATH_SETTING'] ?? + 'storage/json/tgn/tgn-settings.json', + + 'platform' => [ + 'gitlab' => $_ENV['TGN_PATH_PLATFORM_GITLAB'] ?? + 'storage/json/tgn/gitlab-events.json', + 'github' => $_ENV['TGN_PATH_PLATFORM_GITHUB'] ?? + 'storage/json/tgn/github-events.json', + ], + ], + 'view' => [ 'path' => $_ENV['TGN_VIEW_PATH'] ?? 'resources/views', diff --git a/src/Bot.php b/src/Bot.php index 0188f67..dfe9a5b 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -2,33 +2,56 @@ namespace LbilTech\TelegramGitNotifier; +use LbilTech\TelegramGitNotifier\Constants\EventConstant; use LbilTech\TelegramGitNotifier\Interfaces\BotInterface; use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface; -use LbilTech\TelegramGitNotifier\Interfaces\Structures\EventInterface; +use LbilTech\TelegramGitNotifier\Interfaces\EventInterface; use LbilTech\TelegramGitNotifier\Interfaces\Structures\SettingInterface; +use LbilTech\TelegramGitNotifier\Models\Event; +use LbilTech\TelegramGitNotifier\Models\Setting; use LbilTech\TelegramGitNotifier\Structures\App; -use LbilTech\TelegramGitNotifier\Structures\Event; -use LbilTech\TelegramGitNotifier\Structures\Setting; use LbilTech\TelegramGitNotifier\Trait\BotSettingTrait; use LbilTech\TelegramGitNotifier\Trait\EventSettingTrait; -use LbilTech\TelegramGitNotifier\Trait\ValidationEventTrait; -use LbilTech\TelegramGitNotifier\Trait\ActionEventTrait; +use LbilTech\TelegramGitNotifier\Trait\EventTrait; use Telegram; -class Bot implements AppInterface, EventInterface, SettingInterface, BotInterface +class Bot implements AppInterface, BotInterface, EventInterface { use App; - use Event; - use Setting; - use ValidationEventTrait; - use ActionEventTrait; + use EventTrait; use BotSettingTrait; use EventSettingTrait; - public function __construct(Telegram $telegram = null) - { + public Event $event; + + public Setting $setting; + + public function __construct( + Telegram $telegram = null, + ?string $chatBotId = null, + Setting $setting = null, + Event $event = null, + ?string $settingFile = null, + ?string $platform = EventConstant::DEFAULT_PLATFORM, + ?string $platformFile = null, + ) { $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); - $this->setCurrentChatBotId(); + $this->setCurrentChatBotId($chatBotId); + $this->event = $event ?? new Event(); + $this->setPlatFormForEvent($platform, $platformFile); + + $this->setting = $setting ?? new Setting(); + $this->updateSetting($settingFile); + } + + public function updateSetting(?string $settingFile = null): void + { + if ($this->setting->getSettingFile()) { + return; + } + $settingFile = $settingFile ?? config('telegram-git-notifier.data_file.setting'); + $this->setting->setSettingFile($settingFile); + $this->setting->setSettingConfig(); } public function setMyCommands( diff --git a/src/Interfaces/EventInterface.php b/src/Interfaces/EventInterface.php new file mode 100644 index 0000000..571365c --- /dev/null +++ b/src/Interfaces/EventInterface.php @@ -0,0 +1,55 @@ +platformFile; } + /** + * @param string $platformFile + * + * @return void + */ public function setPlatformFile(string $platformFile): void { $this->platformFile = $platformFile; } + /** + * Set event config + * + * @param string $platform + * + * @return void + */ public function setEventConfig( string $platform = EventConstant::DEFAULT_PLATFORM ): void { @@ -31,6 +46,14 @@ public function setEventConfig( $this->eventConfig = json_decode($json, true); } + /** + * Update event config by event and action + * + * @param string $event + * @param string|null $action + * + * @return void + */ public function updateEvent(string $event, string|null $action): void { if (!empty($action)) { diff --git a/src/Structures/Setting.php b/src/Models/Setting.php similarity index 87% rename from src/Structures/Setting.php rename to src/Models/Setting.php index 4de4521..11bc475 100644 --- a/src/Structures/Setting.php +++ b/src/Models/Setting.php @@ -1,12 +1,12 @@ settings) @@ -72,6 +77,14 @@ public function isNotified(): bool return false; } + /** + * Update setting item value and save to file + * + * @param string $settingName + * @param $settingValue + * + * @return bool + */ public function updateSetting( string $settingName, $settingValue = null diff --git a/src/Notifier.php b/src/Notifier.php index 4a6f3bd..37844c0 100644 --- a/src/Notifier.php +++ b/src/Notifier.php @@ -2,24 +2,42 @@ namespace LbilTech\TelegramGitNotifier; +use GuzzleHttp\Client; +use LbilTech\TelegramGitNotifier\Constants\EventConstant; +use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface; +use LbilTech\TelegramGitNotifier\Interfaces\EventInterface; use LbilTech\TelegramGitNotifier\Interfaces\Structures\NotificationInterface; +use LbilTech\TelegramGitNotifier\Models\Event; use LbilTech\TelegramGitNotifier\Structures\App; -use LbilTech\TelegramGitNotifier\Structures\Event; use LbilTech\TelegramGitNotifier\Structures\Notification; -use LbilTech\TelegramGitNotifier\Trait\ActionEventTrait; +use LbilTech\TelegramGitNotifier\Trait\EventTrait; use Telegram; -class Notifier implements NotificationInterface +class Notifier implements AppInterface, NotificationInterface, EventInterface { use App; - use Event; use Notification; - use ActionEventTrait; + + use EventTrait; + + public Event $event; + + public Client $client; public function __construct( - Telegram $telegram = null + Telegram $telegram = null, + ?string $chatBotId = null, + Event $event = null, + ?string $platform = EventConstant::DEFAULT_PLATFORM, + ?string $platformFile = null, + Client $client = null, ) { $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); - $this->setCurrentChatBotId(); + $this->setCurrentChatBotId($chatBotId); + + $this->event = $event ?? new Event(); + $this->setPlatFormForEvent($platform, $platformFile); + + $this->client = $client ?? new Client(); } } diff --git a/src/Objects/Validator.php b/src/Objects/Validator.php new file mode 100644 index 0000000..77969ce --- /dev/null +++ b/src/Objects/Validator.php @@ -0,0 +1,50 @@ +setting = $setting; + $this->event = $event; + } + + public function accessEvent( + string $platform, + string $event, + $payload + ): bool { + if (!$this->setting->isNotified()) { + return false; + } + + if ($this->setting->isAllEventsNotification()) { + return true; + } + $this->event->setEventConfig($platform); + + $eventConfig = $this->event->eventConfig[tgn_convert_event_name($event)] ?? false; + $action = $this->getActionOfEvent($payload); + + if (!empty($action) && isset($eventConfig[$action])) { + $eventConfig = $eventConfig[$action]; + } + + if (!$eventConfig) { + error_log('\n Event config is not found \n'); + } + + return (bool)$eventConfig; + } +} diff --git a/src/Structures/App.php b/src/Structures/App.php index 596b059..f3bf8c1 100644 --- a/src/Structures/App.php +++ b/src/Structures/App.php @@ -43,7 +43,7 @@ public function sendMessage(string $message = '', array $options = []): void unset($options['reply_markup']); } - $content = array_merge($content, $options); + $content = $content + $options; $this->telegram->sendMessage($content); } diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index 2dc6c5b..b9e2508 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -2,8 +2,8 @@ namespace LbilTech\TelegramGitNotifier\Structures; +use GuzzleHttp\Exception\GuzzleException; use LbilTech\TelegramGitNotifier\Constants\EventConstant; -use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException; use Symfony\Component\HttpFoundation\Request; @@ -30,9 +30,9 @@ public function accessDenied( public function setPayload(Request $request, string $event) { - if ($this->platform === 'gitlab') { + if ($this->event->platform === 'gitlab') { $this->payload = json_decode($request->getContent()); - } elseif ($this->platform === EventConstant::DEFAULT_PLATFORM) { + } elseif ($this->event->platform === EventConstant::DEFAULT_PLATFORM) { $this->payload = json_decode($request->request->get('payload')); } $this->setMessage($event); @@ -53,8 +53,8 @@ private function setMessage(string $typeEvent): void $action = $this->getActionOfEvent($this->payload); $viewTemplate = empty($action) - ? "events.{$this->platform}.{$event}.default" - : "events.{$this->platform}.{$event}.{$action}"; + ? "events.{$this->event->platform}.{$event}.default" + : "events.{$this->event->platform}.{$event}.{$action}"; $this->message = view($viewTemplate, [ 'payload' => $this->payload, @@ -68,13 +68,26 @@ public function sendNotify(string $chatId, string $message = null): bool $this->message = $message; } + $queryParams = [ + 'chat_id' => $chatId, + 'disable_web_page_preview' => 1, + 'parse_mode' => 'html', + 'text' => $this->message + ]; + + $url = 'https://api.telegram.org/bot' + . config('telegram-git-notifier.bot.token') . '/sendMessage' + . '?' . http_build_query($queryParams); + try { - $this->sendMessage($this->message, [ - 'chat_id' => $chatId, - ]); + $response = $this->client->request('GET', $url); + + if ($response->getStatusCode() === 200) { + return true; + } throw SendNotificationException::create(); - } catch (MessageIsEmptyException $e) { + } catch (GuzzleException $e) { error_log($e->getMessage()); } diff --git a/src/Trait/BotSettingTrait.php b/src/Trait/BotSettingTrait.php index c5a34a1..3d2593f 100644 --- a/src/Trait/BotSettingTrait.php +++ b/src/Trait/BotSettingTrait.php @@ -19,7 +19,7 @@ public function settingMarkup(): array $markup = [ [ $this->telegram->buildInlineKeyBoardButton( - $this->getSettings()[SettingConstant::T_IS_NOTIFIED] + $this->setting->getSettings()[SettingConstant::T_IS_NOTIFIED] ? '✅ Allow notifications' : 'Allow notifications', '', @@ -28,7 +28,7 @@ public function settingMarkup(): array ], [ $this->telegram->buildInlineKeyBoardButton( - $this->getSettings()[SettingConstant::T_ALL_EVENTS_NOTIFICATION] + $this->setting->getSettings()[SettingConstant::T_ALL_EVENTS_NOTIFICATION] ? '✅ Enable All Events Notify' : 'Enable All Events Notify', '', @@ -52,7 +52,7 @@ public function settingMarkup(): array public function customEventMarkup(array $markup): array { - if (!$this->getSettings()[SettingConstant::T_ALL_EVENTS_NOTIFICATION]) { + if (!$this->setting->getSettings()[SettingConstant::T_ALL_EVENTS_NOTIFICATION]) { $markup[] = [ $this->telegram->buildInlineKeyBoardButton( '🦑 Custom github events', diff --git a/src/Trait/EventSettingTrait.php b/src/Trait/EventSettingTrait.php index dfbbccb..369766d 100644 --- a/src/Trait/EventSettingTrait.php +++ b/src/Trait/EventSettingTrait.php @@ -13,9 +13,9 @@ public function eventMarkup( ): array { $replyMarkup = $replyMarkupItem = []; - $this->setEventConfig($platform); - $events = $parentEvent === null ? $this->eventConfig - : $this->eventConfig[$parentEvent]; + $this->event->setEventConfig($platform); + $events = $parentEvent === null ? $this->event->eventConfig + : $this->event->eventConfig[$parentEvent]; foreach ($events as $key => $value) { if (count($replyMarkupItem) === SettingConstant::BTN_LINE_ITEM_COUNT) { @@ -202,8 +202,8 @@ public function eventUpdateHandle(string $event, string $platform): void { [$event, $action] = explode('.', $event); - $this->setEventConfig($platform); - $this->updateEvent($event, $action); + $this->event->setEventConfig($platform); + $this->event->updateEvent($event, $action); $this->eventHandle( $action ? EventConstant::PLATFORM_EVENT_SEPARATOR[$platform] diff --git a/src/Trait/EventTrait.php b/src/Trait/EventTrait.php new file mode 100644 index 0000000..b29c157 --- /dev/null +++ b/src/Trait/EventTrait.php @@ -0,0 +1,36 @@ +event->getPlatformFile()) { + /** @var array $platformFileDefaults */ + $platformFileDefaults = config('telegram-git-notifier.data_file.platform'); + $this->event->setPlatformFile($platformFile ?? $platformFileDefaults[$platform]); + } + $this->event->setEventConfig($platform); + } + + public function handleEventFromRequest(Request $request): ?string + { + foreach (EventConstant::WEBHOOK_EVENT_HEADER as $platform => $header) { + $event = $request->server->get($header); + if (!is_null($event)) { + $this->event->platform = $platform; + $this->setPlatFormForEvent($platform); + + return $event; + } + } + + return null; + } +} diff --git a/src/Trait/ValidationEventTrait.php b/src/Trait/ValidationEventTrait.php deleted file mode 100644 index 3628820..0000000 --- a/src/Trait/ValidationEventTrait.php +++ /dev/null @@ -1,35 +0,0 @@ -isNotified()) { - return false; - } - - if ($this->isAllEventsNotification()) { - return true; - } - - $this->setEventConfig($platform); - - $eventConfig = $this->eventConfig[tgn_convert_event_name($event)] ?? false; - $action = $this->getActionOfEvent($payload); - - if (!empty($action) && isset($eventConfig[$action])) { - $eventConfig = $eventConfig[$action]; - } - - if (!$eventConfig) { - error_log('\n Event config is not found \n'); - } - - return (bool)$eventConfig; - } -} diff --git a/src/Webhook.php b/src/Webhook.php index 4ab0230..1782adc 100644 --- a/src/Webhook.php +++ b/src/Webhook.php @@ -2,7 +2,7 @@ namespace LbilTech\TelegramGitNotifier; -use LbilTech\TelegramGitNotifier\Interfaces\Structures\WebhookInterface; +use LbilTech\TelegramGitNotifier\Interfaces\WebhookInterface; class Webhook implements WebhookInterface {