Skip to content

Commit

Permalink
Enhancing Gotify tested and working
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroBuffon committed Jul 10, 2024
1 parent 21ad9c1 commit 5edc901
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
79 changes: 52 additions & 27 deletions Gotify/Gotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,77 @@ class Gotify extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;

//protected $login_first = true; // Uncomment if api requests need to be authed first
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST

public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}

public function test()
{
$attrs = [
"headers" => ["Accept" => "application/json"],
];
$test = parent::appTest($this->url("application"), $attrs);
$attrs = $this->getAttrs();
$test = parent::appTest($this->url("health"), $attrs);
echo $test->status;
}

public function livestats()
{
$status = "inactive";
$data = [];
$attrs = [
"headers" => ["Accept" => "application/json"],
];

$messages = json_decode(
parent::execute($this->url("message"), $attrs)->getBody()
);

$data = [];

if ($messages) {
$data["messages"] = count($messages->messages ?? []);
$attrs = $this->getAttrs();

// Fetch applications
$applicationsResponse = parent::execute($this->url("application"), $attrs);
$applications = json_decode($applicationsResponse->getBody());

// Count applications
if ($applications) {
$data["applications"] = count($applications);
} else {
$data["applications"] = 0;
}


// Fetch clients
$clientsResponse = parent::execute($this->url("client"), $attrs);
$clients = json_decode($clientsResponse->getBody());

// Count clients
if ($clients) {
$data["clients"] = count($clients);
} else {
$data["clients"] = 0;
}

// Fetch messages
$messagesResponse = parent::execute($this->url("message"), $attrs);
$messages = json_decode($messagesResponse->getBody());

// Count messages
if ($messages && isset($messages->messages)) {
$data["messages"] = count($messages->messages);
} else {
$data["messages"] = 0;
}

// Determine status based on data
if ($data["applications"] > 0 || $data["clients"] > 0 || $data["messages"] > 0) {
$status = "active";
}

return parent::getLiveStats($status, $data);
}

public function url($endpoint)
{
$api_url =
parent::normaliseurl($this->config->url) .
"application/" .
$endpoint .
"?token=" .
$this->config->apikey;
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
}
private function getAttrs()
{
return [
"headers" => [
"Accept" => "application/json",
"X-Gotify-Key" => $this->config->apikey
],
];
}

}
2 changes: 1 addition & 1 deletion Gotify/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"website": "https://gotify.net",
"license": "MIT License",
"description": "A self-hosted push notification service.",
"enhanced": false,
"enhanced": true,
"tile_background": "dark",
"icon": "gotify.png"
}
4 changes: 0 additions & 4 deletions Gotify/config.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<label>{{ __('app.apps.apikey') }}</label>
{!! Form::text('config[apikey]', isset($item) ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.gotify_token') }}</label>
{!! Form::text('config[token]', isset($item) ? $item->getconfig()->token : null, ['placeholder' => __('app.apps.gotify_token'), 'data-config' => 'token', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
Expand Down
8 changes: 8 additions & 0 deletions Gotify/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<ul class="livestats">
<li>
<span class="title">Apps</span>
<strong>{!! $applications !!}</strong>
</li>
<li>
<span class="title">Clients</span>
<strong>{!! $clients !!}</strong>
</li>
<li>
<span class="title">Messages</span>
<strong>{!! $messages !!}</strong>
Expand Down

0 comments on commit 5edc901

Please sign in to comment.