-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUpdateJob.php
More file actions
130 lines (108 loc) · 3.91 KB
/
Copy pathUpdateJob.php
File metadata and controls
130 lines (108 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace BNETDocs\Controllers\Server;
use \BNETDocs\Libraries\Core\DateTimeImmutable;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\Discord\Embed as DiscordEmbed;
use \BNETDocs\Libraries\Discord\EmbedField as DiscordEmbedField;
use \BNETDocs\Libraries\Discord\Webhook as DiscordWebhook;
use \BNETDocs\Libraries\Server\Server;
use \DateTimeZone;
class UpdateJob extends \BNETDocs\Controllers\Base
{
public const S_DISABLED = ':no_entry: Disabled';
public const S_ONLINE = ':white_check_mark: Online';
public const S_OFFLINE = ':x: Offline';
public function __construct()
{
$this->model = new \BNETDocs\Models\Server\UpdateJob();
}
public function invoke(?array $args): bool
{
if (Router::requestMethod() !== Router::METHOD_POST)
{
$this->model->_responseCode = HttpCode::HTTP_METHOD_NOT_ALLOWED;
$this->model->_responseHeaders['Allow'] = Router::METHOD_POST;
return true;
}
$q = Router::query();
$server_id = isset($q['id']) ? (int) $q['id'] : null;
$job_token = isset($q['job_token']) ? $q['job_token'] : null;
$status = isset($q['status']) ? (int) $q['status'] : null;
if ($job_token !== \BNETDocs\Libraries\Core\Config::get('bnetdocs.server_update_job_token'))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
return true;
}
if (!is_int($server_id) || !is_int($status))
{
$this->model->_responseCode = HttpCode::HTTP_BAD_REQUEST;
return true;
}
try { $this->model->server = new Server($server_id); }
catch (\UnexpectedValueException) { $this->model->server = null; }
if (!$this->model->server)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
return true;
}
$this->model->old_status_bitmask = $this->model->server->getStatusBitmask();
$this->model->server->setStatusBitmask($status);
if (!$this->model->server->commit()) return true;
$discord = $c->discord->forward_server_updates ?? null;
if ($discord && $discord->enabled && !in_array($server_id, $discord->ignore_server_ids))
{
self::dispatchDiscord(
$this->model->server, $discord->webhook, $this->model->old_status_bitmask, $status
);
}
$this->model->_responseCode = HttpCode::HTTP_OK;
return true;
}
protected static function dispatchDiscord(Server $server, string $webhook, int $old, int $new): void
{
if ($old === $new) return;
if ($old & Server::STATUS_DISABLED) $old_status = self::S_DISABLED;
else if ($old & Server::STATUS_ONLINE) $old_status = self::S_ONLINE;
else if (!($old & Server::STATUS_ONLINE)) $old_status = self::S_OFFLINE;
else $old_status = sprintf('Unknown (%d)', $old);
if ($new & Server::STATUS_DISABLED)
{
$title = 'Server Disabled';
$new_status = self::S_DISABLED;
}
else if ($new & Server::STATUS_ONLINE)
{
$title = 'Server Online';
$new_status = self::S_ONLINE;
}
else if (!($new & Server::STATUS_ONLINE))
{
$title = 'Server Offline';
$new_status = self::S_OFFLINE;
}
else
{
$title = 'Generic Status Change';
$new_status = sprintf('Unknown (%d)', $new);
}
$label = $server->getLabel();
if (!empty($label)) $title .= ': ' . $label;
$webhook = new DiscordWebhook($webhook);
$embed = new DiscordEmbed();
$embed->setUrl($server->getURI());
$embed->setTitle($title);
$embed->setTimestamp(new DateTimeImmutable('now', new DateTimeZone('Etc/UTC')));
$data = [];
$data['Type'] = $server->getType()->getLabel();
if (!empty($label)) $data['Label'] = $label;
$data['Server'] = $server->getAddress() . ':' . $server->getPort();
$data['Status'] = $old_status . ' → ' . $new_status;
foreach ($data as $key => $value)
{
$embed->addField(new DiscordEmbedField($key, $value, true));
}
$webhook->addEmbed($embed);
$webhook->send();
}
}