Skip to content

Commit

Permalink
refactor: Simplify conditionals in ExtensionController and Helpers.php
Browse files Browse the repository at this point in the history
Simplified the conditionals in ExtensionController and Helpers.php to use boolean values instead of string comparisons. This improves readability and maintainability of the code.
  • Loading branch information
dogukanoksuz committed Oct 17, 2024
1 parent c4646c8 commit 45afe73
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 33 deletions.
8 changes: 1 addition & 7 deletions app/Http/Controllers/API/ExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,7 @@ public function render(Request $request)
$this->checkPermissions(extension());
$this->checkForMissingSettings($dbJson);

if (extension()->status == '0') {
return response()->json([
'message' => 'Eklenti şu anda güncelleniyor, biraz sonra tekrar deneyiniz.',
], Response::HTTP_SERVICE_UNAVAILABLE);
}

if (extension()->require_key == 'true' && server()->key() == null) {
if (extension()->require_key == true && server()->key() == null) {
return response()->json([
'message' => 'Bu eklentiyi kullanabilmek için bir anahtara ihtiyacınız var, lütfen kasa üzerinden bir anahtar ekleyin.',
], Response::HTTP_FORBIDDEN);
Expand Down
19 changes: 4 additions & 15 deletions app/Http/Controllers/API/Settings/ExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ public function upload()
(string) request()->file('extension')->path()
);
}
$list = $this->setupNewExtension($zipFile, $verify);
$error = $list[0];
$new = $list[1];
$old = $list[2] ?? [];

list($error, $new, $old) = $this->setupNewExtension($zipFile);
$old = $old ?? [];

if ($error) {
return $error;
Expand Down Expand Up @@ -284,7 +283,7 @@ public function download()
*
* @throws GuzzleException
*/
private function setupNewExtension($zipFile, $verify = false)
private function setupNewExtension($zipFile)
{
// Initialize Zip Archive Object to use it later.
$zip = new ZipArchive();
Expand Down Expand Up @@ -339,12 +338,6 @@ private function setupNewExtension($zipFile, $verify = false)
];
}

if ($verify) {
$json['issuer'] = explode(' ', (string) $verify, 4)[3];
} else {
$json['issuer'] = '';
}

// Check If Extension Already Exists.
$extension = Extension::where('name', $json['name'])->first();
if ($extension) {
Expand All @@ -369,12 +362,8 @@ private function setupNewExtension($zipFile, $verify = false)
} else {
$new = new Extension();
}
unset($json['issuer']);
unset($json['status']);
unset($json['order']);
$json['display_name'] = json_encode($json['display_name']);
$new->fill($json);
$new->status = '1';
$new->save();

if (array_key_exists('dependencies', $json) && $json['dependencies'] != '') {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/HASync/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function extensionList()
$list[] = [
"id" => $extension->id,
"name" => strtolower($extension->name),
"version_code" => (int) str_replace('.', '', $extension->version),
"version_code" => intval($extension->version_code),
"download_path" => route("ha_download_ext", [
"extension_name" => $extension->name
]),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ function callExtensionFunction(
$params = [],
$target_function = "apiProxy"
) {
if ($extension->require_key == 'true' && $server->key() == null) {
if ($extension->require_key == true && $server->key() == null) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/HighAvailabilitySyncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ private function fetchUpdateInformation($ip)
if (is_file($path . '/db.json')) {
$json = (array) json_decode(file_get_contents($path . '/db.json'));

$version = (int) str_replace('.', '', $json['version']);
$version = $json['version_code'];

if ($version < $extension->version_code) {
if (intval($version) < intval($extension->version_code)) {
$needsToBeUpdated[] = $extension;
continue;
}
Expand Down
9 changes: 2 additions & 7 deletions app/Models/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,16 @@ class Extension extends Model
'display_name',
'name',
'version',
'version_code',
'icon',
'service',
'sslPorts',
'issuer',
'language',
'support',
'displays',
'require_key',
'status',
'license_type',
'ldap_support',
];

protected $casts = [
'displays' => 'array',
'require_key' => 'boolean',
];

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Models/UserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ class UserSettings extends Model
use UsesUuid;

protected $fillable = ['server_id', 'user_id', 'name', 'value'];

protected $hidden = ['value'];
}
42 changes: 42 additions & 0 deletions database/migrations/2024_10_17_150509_alter_table_extensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('extensions', function (Blueprint $table) {
$table->dropColumn('status');
$table->dropColumn('displays');
$table->dropColumn('support');
$table->dropColumn('language');
$table->dropColumn('issuer');
$table->dropColumn('service');
$table->dropColumn('order');
// require_key defaults to false
// first convert existing data to boolean
DB::statement("ALTER TABLE extensions
ALTER COLUMN require_key DROP DEFAULT,
ALTER COLUMN require_key TYPE BOOLEAN USING require_key::BOOLEAN,
ALTER COLUMN require_key SET DEFAULT FALSE;");
// add version_code column
$table->string('version_code')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$extensions = \App\Models\Extension::all();
foreach ($extensions as $extension) {
$extJson = getExtensionJson($extension->name);
if (!$extJson) {
continue;
}
$extension->version_code = $extJson['version_code'];
$extension->save();
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

0 comments on commit 45afe73

Please sign in to comment.