Removed home.js

Added Discover
This commit is contained in:
Pablo Ferreiro 2022-02-06 00:58:30 +01:00
parent df052dab36
commit 8816f4a1a1
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
25 changed files with 213 additions and 145 deletions

View file

@ -0,0 +1,19 @@
<?php
namespace App\Controllers;
use App\Helpers\ErrorHandler;
use App\Helpers\Misc;
use App\Models\FeedTemplate;
class DiscoverController {
static public function get() {
$api = Misc::api();
$feed = $api->getDiscover();
if ($feed->meta->success) {
$latte = Misc::latte();
$latte->render(Misc::getView('discover'), new FeedTemplate('Discover', $feed));
} else {
ErrorHandler::show($feed->meta);
}
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace App\Controllers;
use App\Helpers\Misc;
/**
* Used to be compatible with HTML forms
*/
class RedirectController {
static public function redirect() {
$endpoint = '';
if (isset($_GET['user'])) {
$endpoint = '/@' . $_GET['user'];
} else if (isset($_GET['tag'])) {
$endpoint = '/tag/' . $_GET['tag'];
} else if (isset($_GET['music'])) {
$endpoint = '/music/' . $_GET['music'];
} else if (isset($_GET['video'])) {
$endpoint = '/video/' . $_GET['video'];
}
$url = Misc::url($endpoint);
header("Location: {$url}");
}
}

View file

@ -18,8 +18,7 @@ class SettingsController {
Cookies::set($proxy_element, $_POST[$proxy_element]);
}
}
http_response_code(302);
$url = Misc::env('APP_URL', '');
$url = Misc::url('/settings');
header("Location: {$url}");
}
@ -49,6 +48,7 @@ class SettingsController {
// Build string
$following_string = implode(',', $following);
Cookies::set('following', $following_string);
header('Location: ../settings');
$url = Misc::url('/settings');
header("Location: {$url}");
}
}

View file

@ -1,10 +1,12 @@
<?php
namespace App\Helpers;
use App\Models\ErrorTemplate;
class ErrorHandler {
static public function show(object $meta) {
http_response_code($meta->http_code);
$latte = Misc::latte();
$latte->render(Misc::getView('error'), ['error' => $meta]);
$latte->render(Misc::getView('error'), new ErrorTemplate($meta));
}
}

View file

@ -1,7 +1,6 @@
<?php
namespace App\Helpers;
use Exception;
use App\Cache\JSONCache;
use App\Cache\RedisCache;
@ -10,11 +9,11 @@ class Misc {
return isset($_GET['cursor']) && is_numeric($_GET['cursor']) ? (int) $_GET['cursor'] : 0;
}
static public function getURL(): string {
return self::env('APP_URL', '');
static public function url(string $endpoint = '') {
return self::env('APP_URL', '') . $endpoint;
}
static public function env(string $key, mixed $default_value): string {
static public function env(string $key, string $default_value): string {
return isset($_ENV[$key]) && !empty($_ENV[$key]) ? $_ENV[$key] : $default_value;
}
@ -45,7 +44,7 @@ class Misc {
break;
case 'redis':
if (!isset($_ENV['REDIS_URL'])) {
throw new Exception('You need to set REDIS_URL to use Redis Cache!');
throw new \Exception('You need to set REDIS_URL to use Redis Cache!');
}
$url = parse_url($_ENV['REDIS_URL']);
@ -64,24 +63,17 @@ class Misc {
* Setup of Latte template engine
*/
static public function latte(): \Latte\Engine {
// Workaround to avoid weird path issues
$url = self::getURL();
$latte = new \Latte\Engine;
$cache_path = self::env('LATTE_CACHE', __DIR__ . '/../../cache/latte');
$latte->setTempDirectory($cache_path);
// -- CUSTOM FUNCTIONS -- //
// Import assets
$latte->addFunction('assets', function (string $name, string $type) use ($url) {
$path = "{$url}/{$type}/{$name}";
return $path;
});
// Get base URL
$latte->addFunction('path', function (string $path = '') use ($url) {
return "{$url}/{$path}";
// Get URL with optional endpoint
$latte->addFunction('path', function (string $endpoint = ''): string {
return self::url($endpoint);
});
// Version being used
$latte->addFunction('version', function () {
$latte->addFunction('version', function (): string {
return \Composer\InstalledVersions::getVersion('pablouser1/proxitok');
});
// https://stackoverflow.com/a/36365553
@ -98,10 +90,6 @@ class Misc {
}
return $x;
});
$latte->addFunction('size', function (string $url) {
$download = new \Sovit\TikTok\Download();
return $download->file_size($url);
});
return $latte;
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace App\Models;
class ErrorTemplate extends BaseTemplate {
public object $error;
function __construct(object $error) {
parent::__construct('Error');
$this->error = $error;
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace App\Models;
/**
* Exclusive for /
*/
class HomeTemplate extends BaseTemplate {
public array $forms = [
[
'title' => 'Search by user',
'input' => 'user',
'placeholder' => 'Type username'
],
[
'title' => 'Search by video ID',
'input' => 'video',
'placeholder' => 'Type video ID'
],
[
'title' => 'Search by tag',
'input' => 'tag',
'placeholder' => 'Type tag'
],
[
'title' => 'Search by music ID',
'input' => 'music',
'placeholder' => 'Type music'
]
];
function __construct() {
parent::__construct('Home');
}
}