2
2
3
3
namespace BNETDocs \Controllers \Server ;
4
4
5
+ use \BNETDocs \Libraries \Discord \Embed as DiscordEmbed ;
6
+ use \BNETDocs \Libraries \Discord \EmbedField as DiscordEmbedField ;
7
+ use \BNETDocs \Libraries \Discord \Webhook as DiscordWebhook ;
5
8
use \BNETDocs \Libraries \Exceptions \ServerNotFoundException ;
6
9
use \BNETDocs \Libraries \Server ;
7
10
16
19
use \DateTimeZone ;
17
20
18
21
class UpdateJob extends Controller {
22
+
23
+ const S_DISABLED = ':no_entry: Disabled ' ;
24
+ const S_ONLINE = ':white_check_mark: Online ' ;
25
+ const S_OFFLINE = ':x: Offline ' ;
26
+
19
27
public function &run ( Router &$ router , ViewLib &$ view , array &$ args ) {
20
28
$ model = new UpdateJobModel ();
21
29
$ action = $ router ->getRequestMethod ();
@@ -36,7 +44,7 @@ public function &run( Router &$router, ViewLib &$view, array &$args ) {
36
44
);
37
45
38
46
$ status = (
39
- isset ( $ q [ 'status ' ]) ? $ q [ 'status ' ] : null
47
+ isset ( $ q [ 'status ' ]) ? ( int ) $ q [ 'status ' ] : null
40
48
);
41
49
42
50
if ( !is_null ( $ server_id )) $ server_id = (int ) $ server_id ;
@@ -47,13 +55,9 @@ public function &run( Router &$router, ViewLib &$view, array &$args ) {
47
55
);
48
56
49
57
if ( !( is_int ( $ server_id ) && is_int ( $ status ))) {
50
-
51
58
$ model ->_responseCode = 400 ;
52
-
53
59
} else if ( !$ authenticated ) {
54
-
55
60
$ model ->_responseCode = 403 ;
56
-
57
61
} else {
58
62
59
63
try {
@@ -63,24 +67,88 @@ public function &run( Router &$router, ViewLib &$view, array &$args ) {
63
67
}
64
68
65
69
if ( !$ model ->server ) {
66
-
67
70
$ model ->_responseCode = 404 ;
68
-
69
71
} else {
70
72
71
73
$ model ->old_status_bitmask = $ model ->server ->getStatusBitmask ();
72
-
73
74
$ model ->server ->setStatusBitmask ( $ status );
74
75
75
76
if ( $ model ->server ->save () ) {
77
+ $ discord = Common::$ config ->discord ->forward_server_updates ;
78
+ if ( $ discord && $ discord ->enabled
79
+ && !in_array ( $ server_id , $ discord ->ignore_server_ids )) {
80
+ self ::dispatchDiscord (
81
+ $ model ->server , $ discord ->webhook ,
82
+ $ model ->old_status_bitmask , $ status
83
+ );
84
+ }
85
+
76
86
$ model ->_responseCode = 200 ;
77
87
}
78
-
79
88
}
80
89
}
81
90
}
82
91
83
92
$ view ->render ( $ model );
84
93
return $ model ;
85
94
}
95
+
96
+ protected static function dispatchDiscord ( $ server , $ webhook , $ old , $ new ) {
97
+ if ($ old === $ new ) return ;
98
+
99
+ if ($ old & Server::STATUS_DISABLED ) {
100
+ $ old_status = self ::S_DISABLED ;
101
+ } else if ($ old & Server::STATUS_ONLINE ) {
102
+ $ old_status = self ::S_ONLINE ;
103
+ } else if (!($ old & Server::STATUS_ONLINE )) {
104
+ $ old_status = self ::S_OFFLINE ;
105
+ } else {
106
+ $ old_status = sprintf ('Unknown (%d) ' , $ old );
107
+ }
108
+
109
+ if ($ new & Server::STATUS_DISABLED ) {
110
+ $ title = 'Server Disabled ' ;
111
+ $ new_status = self ::S_DISABLED ;
112
+ } else if ($ new & Server::STATUS_ONLINE ) {
113
+ $ title = 'Server Online ' ;
114
+ $ new_status = self ::S_ONLINE ;
115
+ } else if (!($ new & Server::STATUS_ONLINE )) {
116
+ $ title = 'Server Offline ' ;
117
+ $ new_status = self ::S_OFFLINE ;
118
+ } else {
119
+ $ title = 'Generic Status Change ' ;
120
+ $ new_status = sprintf ('Unknown (%d) ' , $ new );
121
+ }
122
+
123
+ $ label = $ server ->getLabel ();
124
+
125
+ if (!empty ($ label )) {
126
+ $ title .= ': ' . $ label ;
127
+ }
128
+
129
+ $ webhook = new DiscordWebhook ($ webhook );
130
+ $ embed = new DiscordEmbed ();
131
+
132
+ $ embed ->setUrl ($ server ->getURI ());
133
+ $ embed ->setTitle ($ title );
134
+ $ embed ->setTimestamp (new DateTime ('now ' , new DateTimeZone ('Etc/UTC ' )));
135
+
136
+ $ data = array ();
137
+ $ data ['Type ' ] = $ server ->getType ()->getLabel ();
138
+
139
+ if (!empty ($ label )) {
140
+ $ data ['Label ' ] = $ label ;
141
+ }
142
+
143
+ $ data ['Server ' ] = $ server ->getAddress () . ': ' . $ server ->getPort ();
144
+ $ data ['Status ' ] = $ old_status . ' → ' . $ new_status ;
145
+
146
+ foreach ($ data as $ key => $ value ) {
147
+ $ field = new DiscordEmbedField ($ key , $ value , true );
148
+ $ embed ->addField ($ field );
149
+ }
150
+
151
+ $ webhook ->addEmbed ($ embed );
152
+ $ webhook ->send ();
153
+ }
86
154
}
0 commit comments