Skip to content

Commit 46d1a2a

Browse files
authored
Merge pull request #72 from tanhongit/main
Update setting bot tools
2 parents c87866d + 356a152 commit 46d1a2a

File tree

11 files changed

+111
-44
lines changed

11 files changed

+111
-44
lines changed

common/helpers.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use TelegramGithubNotify\App\Helpers\ConfigHelper;
4-
use TelegramGithubNotify\App\Helpers\EventHelper;
5-
use TelegramGithubNotify\App\Helpers\SettingHelper;
4+
use TelegramGithubNotify\App\Models\Event;
5+
use TelegramGithubNotify\App\Models\Setting;
66

77
if (!function_exists('config')) {
88
/**
@@ -91,19 +91,19 @@ function singularity($word): bool|string
9191
*/
9292
function event_config(): array
9393
{
94-
return (new EventHelper())->getEventConfig();
94+
return (new Event())->getEventConfig();
9595
}
9696
}
9797

98-
if (!function_exists('enable_all_events')) {
98+
if (!function_exists('all_events_notify')) {
9999
/**
100-
* Return enable all events
100+
* Return all events notify status
101101
*
102102
* @return bool
103103
*/
104-
function enable_all_events(): bool
104+
function all_events_notify(): bool
105105
{
106-
return (new SettingHelper())->enableAllEvents();
106+
return (new Setting())->allEventsNotify();
107107
}
108108
}
109109

@@ -115,6 +115,6 @@ function enable_all_events(): bool
115115
*/
116116
function setting_config(): array
117117
{
118-
return (new SettingHelper())->getSettingConfig();
118+
return (new Setting())->getSettingConfig();
119119
}
120120
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@
5050
"post-install-cmd": [
5151
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
5252
"php -r \"file_exists('storage/tg-event.json') || copy('config/jsons/tg-event.json', 'storage/tg-event.json');\"",
53-
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\""
53+
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\"",
54+
"chmod -R 777 storage"
5455
],
5556
"post-update-cmd": [
5657
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
5758
"php -r \"file_exists('storage/tg-event.json') || copy('config/jsons/tg-event.json', 'storage/tg-event.json');\"",
58-
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\""
59+
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\"",
60+
"chmod -R 777 storage"
5961
]
6062
}
6163
}

config/jsons/tg-setting.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"is_notified": true,
3-
"enable_all_event": false
3+
"all_events_notify": false
44
}

resources/tools/menu.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
/settings - Settings github notify.
88
/menu - To get this menu.
99

10-
Select a command:
10+
Select a button:

src/Http/Actions/SendNotifyAction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace TelegramGithubNotify\App\Http\Actions;
44

55
use Symfony\Component\HttpFoundation\Request;
6-
use TelegramGithubNotify\App\Services\EventSettingService;
6+
use TelegramGithubNotify\App\Services\EventService;
77
use TelegramGithubNotify\App\Services\NotificationService;
88
use TelegramGithubNotify\App\Services\TelegramService;
99

@@ -13,7 +13,7 @@ class SendNotifyAction
1313

1414
protected NotificationService $notificationService;
1515

16-
protected EventSettingService $eventSettingService;
16+
protected EventService $eventSettingService;
1717

1818
protected Request $request;
1919

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

2929
$this->chatIds = config('telegram-bot.notify_chat_ids');
3030
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace TelegramGithubNotify\App\Helpers;
3+
namespace TelegramGithubNotify\App\Models;
44

5-
class EventHelper
5+
class Event
66
{
77
public const EVENT_FILE = __DIR__ . '/../../storage/tg-event.json';
88

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace TelegramGithubNotify\App\Helpers;
3+
namespace TelegramGithubNotify\App\Models;
44

5-
class SettingHelper
5+
class Setting
66
{
77
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';
88

@@ -39,9 +39,9 @@ public function getSettingConfig(): array
3939
/**
4040
* @return bool
4141
*/
42-
public function enableAllEvents(): bool
42+
public function allEventsNotify(): bool
4343
{
44-
if (!empty($this->settings) && $this->settings['enable_all_event'] === true) {
44+
if (!empty($this->settings) && $this->settings['all_events_notify'] === true) {
4545
return true;
4646
}
4747

src/Services/AppService.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ public function sendMessage(string $message = '', array $options = [], string $s
3939
}
4040

4141
if (!empty($options) && isset($options['reply_markup'])) {
42-
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard(
43-
$options['reply_markup']
44-
);
42+
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
4543
}
4644

4745
$this->telegram->{'send' . $sendType}($content);
4846
} catch (Exception $e) {
49-
$content['text'] = $e->getMessage();
50-
$this->telegram->sendMessage($content);
47+
error_log($e->getMessage());
5148
}
5249
}
5350
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Symfony\Component\HttpFoundation\Request;
66

7-
class EventSettingService
7+
class EventService
88
{
99
/**
1010
* Validate access event before send notify
@@ -15,17 +15,12 @@ class EventSettingService
1515
*/
1616
public function validateAccessEvent(Request $request, $payload): bool
1717
{
18-
if (enable_all_events()) {
18+
if (all_events_notify()) {
1919
return true;
2020
}
2121

2222
$eventConfig = event_config();
2323

24-
if (empty($eventConfig)) {
25-
error_log('\n Event config is empty \n');
26-
return false;
27-
}
28-
2924
$event = singularity($request->server->get('HTTP_X_GITHUB_EVENT'));
3025
$eventConfig = $eventConfig[$event] ?? false;
3126

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

4035
return (bool)$eventConfig;
4136
}
37+
38+
public function eventHandle()
39+
{
40+
}
4241
}

src/Services/SettingService.php

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,108 @@
22

33
namespace TelegramGithubNotify\App\Services;
44

5+
use TelegramGithubNotify\App\Models\Setting;
6+
57
class SettingService extends AppService
68
{
9+
public Setting $setting;
10+
11+
public array $settingConfig = [];
12+
13+
public function __construct()
14+
{
15+
parent::__construct();
16+
$this->setting = new Setting();
17+
$this->settingConfig = $this->setting->getSettingConfig();
18+
}
719
/**
820
* @return void
921
*/
1022
public function settingHandle(): void
1123
{
12-
$settings = setting_config();
13-
14-
if ($settings['is_notified']) {
15-
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable Notification', '', 'setting.disable_notification');
24+
if ($this->settingConfig['is_notified']) {
25+
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('❌ Notification', '', 'setting.is_notified');
1626
} else {
17-
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable Notification', '', 'setting.enable_notification');
27+
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('Notification', '', 'setting.is_notified');
1828
}
1929

20-
if ($settings['enable_all_event']) {
21-
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable All Events', '', 'setting.disable_all_events');
30+
if ($this->settingConfig['all_events_notify']) {
31+
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 All Events Notify', '', 'setting.all_events_notify');
2232
} else {
23-
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable All Events', '', 'setting.enable_all_events');
33+
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 All Events Notify', '', 'setting.all_events_notify');
2434
}
2535

2636
$keyboard = [
2737
[
2838
$notificationSetting,
2939
], [
3040
$eventSetting,
31-
$this->telegram->buildInlineKeyBoardButton('Check Events', '', 'setting.check_events'),
32-
],
41+
$this->telegram->buildInlineKeyBoardButton('Custom individual events', '', 'setting.custom_events'),
42+
], [
43+
$this->telegram->buildInlineKeyBoardButton('🔙 Back', '', 'back.menu'),
44+
]
3345
];
3446

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

38-
public function settingCallbackHandler(string $callback)
50+
/**
51+
* @param string $callback
52+
* @return void
53+
*/
54+
public function settingCallbackHandler(string $callback): void
3955
{
56+
if ($callback === 'setting.custom_events') {
57+
(new EventService())->eventHandle();
58+
return;
59+
}
60+
61+
$callback = str_replace('setting.', '', $callback);
62+
63+
$this->updateSetting($callback, !$this->settingConfig[$callback]);
64+
$this->settingHandle();
65+
}
66+
67+
/**
68+
* @param string $settingName
69+
* @param $settingValue
70+
* @return bool
71+
*/
72+
public function updateSetting(string $settingName, $settingValue = null): bool
73+
{
74+
$keys = explode('.', $settingName);
75+
$lastKey = array_pop($keys);
76+
$nestedSettings = &$this->settingConfig;
77+
78+
foreach ($keys as $key) {
79+
if (!isset($nestedSettings[$key]) || !is_array($nestedSettings[$key])) {
80+
return false;
81+
}
82+
$nestedSettings = &$nestedSettings[$key];
83+
}
84+
85+
if (isset($nestedSettings[$lastKey])) {
86+
$nestedSettings[$lastKey] = $settingValue ?? !$nestedSettings[$lastKey];
87+
if ($this->saveSettingsToFile()) {
88+
return true;
89+
}
90+
}
91+
92+
return false;
93+
}
94+
95+
/**
96+
* @return bool
97+
*/
98+
private function saveSettingsToFile(): bool
99+
{
100+
$json = json_encode($this->settingConfig, JSON_PRETTY_PRINT);
101+
if (file_exists(Setting::SETTING_FILE)) {
102+
file_put_contents(Setting::SETTING_FILE, $json, LOCK_EX);
103+
104+
return true;
105+
}
106+
107+
return false;
40108
}
41109
}

0 commit comments

Comments
 (0)