Skip to content

Commit

Permalink
Merge pull request #72 from tanhongit/main
Browse files Browse the repository at this point in the history
Update setting bot tools
  • Loading branch information
tanhongit authored Jul 30, 2023
2 parents c87866d + 356a152 commit 46d1a2a
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 44 deletions.
16 changes: 8 additions & 8 deletions common/helpers.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use TelegramGithubNotify\App\Helpers\ConfigHelper;
use TelegramGithubNotify\App\Helpers\EventHelper;
use TelegramGithubNotify\App\Helpers\SettingHelper;
use TelegramGithubNotify\App\Models\Event;
use TelegramGithubNotify\App\Models\Setting;

if (!function_exists('config')) {
/**
Expand Down Expand Up @@ -91,19 +91,19 @@ function singularity($word): bool|string
*/
function event_config(): array
{
return (new EventHelper())->getEventConfig();
return (new Event())->getEventConfig();
}
}

if (!function_exists('enable_all_events')) {
if (!function_exists('all_events_notify')) {
/**
* Return enable all events
* Return all events notify status
*
* @return bool
*/
function enable_all_events(): bool
function all_events_notify(): bool
{
return (new SettingHelper())->enableAllEvents();
return (new Setting())->allEventsNotify();
}
}

Expand All @@ -115,6 +115,6 @@ function enable_all_events(): bool
*/
function setting_config(): array
{
return (new SettingHelper())->getSettingConfig();
return (new Setting())->getSettingConfig();
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
"post-install-cmd": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"php -r \"file_exists('storage/tg-event.json') || copy('config/jsons/tg-event.json', 'storage/tg-event.json');\"",
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\""
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\"",
"chmod -R 777 storage"
],
"post-update-cmd": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"php -r \"file_exists('storage/tg-event.json') || copy('config/jsons/tg-event.json', 'storage/tg-event.json');\"",
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\""
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\"",
"chmod -R 777 storage"
]
}
}
2 changes: 1 addition & 1 deletion config/jsons/tg-setting.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"is_notified": true,
"enable_all_event": false
"all_events_notify": false
}
2 changes: 1 addition & 1 deletion resources/tools/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
/settings - Settings github notify.
/menu - To get this menu.

Select a command:
Select a button:
6 changes: 3 additions & 3 deletions src/Http/Actions/SendNotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace TelegramGithubNotify\App\Http\Actions;

use Symfony\Component\HttpFoundation\Request;
use TelegramGithubNotify\App\Services\EventSettingService;
use TelegramGithubNotify\App\Services\EventService;
use TelegramGithubNotify\App\Services\NotificationService;
use TelegramGithubNotify\App\Services\TelegramService;

Expand All @@ -13,7 +13,7 @@ class SendNotifyAction

protected NotificationService $notificationService;

protected EventSettingService $eventSettingService;
protected EventService $eventSettingService;

protected Request $request;

Expand All @@ -24,7 +24,7 @@ public function __construct()
$this->request = Request::createFromGlobals();
$this->telegramService = new TelegramService();
$this->notificationService = new NotificationService();
$this->eventSettingService = new EventSettingService();
$this->eventSettingService = new EventService();

$this->chatIds = config('telegram-bot.notify_chat_ids');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/EventHelper.php → src/Models/Event.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace TelegramGithubNotify\App\Helpers;
namespace TelegramGithubNotify\App\Models;

class EventHelper
class Event
{
public const EVENT_FILE = __DIR__ . '/../../storage/tg-event.json';

Expand Down
8 changes: 4 additions & 4 deletions src/Helpers/SettingHelper.php → src/Models/Setting.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace TelegramGithubNotify\App\Helpers;
namespace TelegramGithubNotify\App\Models;

class SettingHelper
class Setting
{
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';

Expand Down Expand Up @@ -39,9 +39,9 @@ public function getSettingConfig(): array
/**
* @return bool
*/
public function enableAllEvents(): bool
public function allEventsNotify(): bool
{
if (!empty($this->settings) && $this->settings['enable_all_event'] === true) {
if (!empty($this->settings) && $this->settings['all_events_notify'] === true) {
return true;
}

Expand Down
7 changes: 2 additions & 5 deletions src/Services/AppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ public function sendMessage(string $message = '', array $options = [], string $s
}

if (!empty($options) && isset($options['reply_markup'])) {
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard(
$options['reply_markup']
);
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
}

$this->telegram->{'send' . $sendType}($content);
} catch (Exception $e) {
$content['text'] = $e->getMessage();
$this->telegram->sendMessage($content);
error_log($e->getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Component\HttpFoundation\Request;

class EventSettingService
class EventService
{
/**
* Validate access event before send notify
Expand All @@ -15,17 +15,12 @@ class EventSettingService
*/
public function validateAccessEvent(Request $request, $payload): bool
{
if (enable_all_events()) {
if (all_events_notify()) {
return true;
}

$eventConfig = event_config();

if (empty($eventConfig)) {
error_log('\n Event config is empty \n');
return false;
}

$event = singularity($request->server->get('HTTP_X_GITHUB_EVENT'));
$eventConfig = $eventConfig[$event] ?? false;

Expand All @@ -39,4 +34,8 @@ public function validateAccessEvent(Request $request, $payload): bool

return (bool)$eventConfig;
}

public function eventHandle()
{
}
}
90 changes: 79 additions & 11 deletions src/Services/SettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,108 @@

namespace TelegramGithubNotify\App\Services;

use TelegramGithubNotify\App\Models\Setting;

class SettingService extends AppService
{
public Setting $setting;

public array $settingConfig = [];

public function __construct()
{
parent::__construct();
$this->setting = new Setting();
$this->settingConfig = $this->setting->getSettingConfig();
}
/**
* @return void
*/
public function settingHandle(): void
{
$settings = setting_config();

if ($settings['is_notified']) {
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable Notification', '', 'setting.disable_notification');
if ($this->settingConfig['is_notified']) {
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('❌ Notification', '', 'setting.is_notified');
} else {
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable Notification', '', 'setting.enable_notification');
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('Notification', '', 'setting.is_notified');
}

if ($settings['enable_all_event']) {
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable All Events', '', 'setting.disable_all_events');
if ($this->settingConfig['all_events_notify']) {
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 All Events Notify', '', 'setting.all_events_notify');
} else {
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable All Events', '', 'setting.enable_all_events');
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 All Events Notify', '', 'setting.all_events_notify');
}

$keyboard = [
[
$notificationSetting,
], [
$eventSetting,
$this->telegram->buildInlineKeyBoardButton('Check Events', '', 'setting.check_events'),
],
$this->telegram->buildInlineKeyBoardButton('Custom individual events', '', 'setting.custom_events'),
], [
$this->telegram->buildInlineKeyBoardButton('🔙 Back', '', 'back.menu'),
]
];

$this->sendMessage(view('tools.settings'), ['reply_markup' => $keyboard]);
}

public function settingCallbackHandler(string $callback)
/**
* @param string $callback
* @return void
*/
public function settingCallbackHandler(string $callback): void
{
if ($callback === 'setting.custom_events') {
(new EventService())->eventHandle();
return;
}

$callback = str_replace('setting.', '', $callback);

$this->updateSetting($callback, !$this->settingConfig[$callback]);
$this->settingHandle();
}

/**
* @param string $settingName
* @param $settingValue
* @return bool
*/
public function updateSetting(string $settingName, $settingValue = null): bool
{
$keys = explode('.', $settingName);
$lastKey = array_pop($keys);
$nestedSettings = &$this->settingConfig;

foreach ($keys as $key) {
if (!isset($nestedSettings[$key]) || !is_array($nestedSettings[$key])) {
return false;
}
$nestedSettings = &$nestedSettings[$key];
}

if (isset($nestedSettings[$lastKey])) {
$nestedSettings[$lastKey] = $settingValue ?? !$nestedSettings[$lastKey];
if ($this->saveSettingsToFile()) {
return true;
}
}

return false;
}

/**
* @return bool
*/
private function saveSettingsToFile(): bool
{
$json = json_encode($this->settingConfig, JSON_PRETTY_PRINT);
if (file_exists(Setting::SETTING_FILE)) {
file_put_contents(Setting::SETTING_FILE, $json, LOCK_EX);

return true;
}

return false;
}
}
1 change: 1 addition & 0 deletions storage/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deny from all

0 comments on commit 46d1a2a

Please sign in to comment.