Removed Following (use RSS), allow unix for Redis

This commit is contained in:
Pablo Ferreiro 2022-02-06 14:22:23 +01:00
parent 84023905ff
commit 3772eabd99
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
23 changed files with 144 additions and 205 deletions

View file

@ -27,7 +27,7 @@ class RedisCache {
return null;
}
public function set(string $cache_key, mixed $data, $timeout = 3600) {
public function set(string $cache_key, array $data, $timeout = 3600) {
$this->client->set($cache_key, json_encode($data), $timeout);
}
}

View file

@ -1,15 +0,0 @@
<?php
namespace App\Controllers;
use App\Helpers\Following;
use App\Helpers\Misc;
use App\Models\FollowingTemplate;
class FollowingController {
static public function get() {
$users = Following::getUsers();
$feed = Following::getAll($users);
$latte = Misc::latte();
$latte->render(Misc::getView('following'), new FollowingTemplate($users, $feed));
}
}

View file

@ -3,7 +3,6 @@ namespace App\Controllers;
use App\Helpers\Misc;
use App\Helpers\Cookies;
use App\Helpers\Following;
use App\Models\SettingsTemplate;
class SettingsController {
@ -21,34 +20,4 @@ class SettingsController {
$url = Misc::url('/settings');
header("Location: {$url}");
}
static public function following() {
$following = Following::getUsers();
if (!isset($_POST['mode']) || empty($_POST['mode'])) {
die('You need to send a mode');
}
switch ($_POST['mode']) {
case 'add':
// Add following
array_push($following, $_POST['account']);
break;
case 'remove':
// Remove following
$index = array_search($_POST['account'], $following);
if ($index !== false) {
unset($following[$index]);
}
break;
default:
// Invalid
die('Invalid mode');
}
// Build string
$following_string = implode(',', $following);
Cookies::set('following', $following_string);
$url = Misc::url('/settings');
header("Location: {$url}");
}
}

View file

@ -1,37 +0,0 @@
<?php
namespace App\Helpers;
class Following {
static public function getUsers (): array {
$following_string = Cookies::get('following');
if ($following_string) {
return explode(',', $following_string);
}
return [];
}
static public function getAll (array $users): object {
$allowed_items_total = isset($_GET['max']) && is_numeric($_GET['max']) && $_GET['max'] <= 100 ? $_GET['max'] : 20;
$items = [];
if (count($users) !== 0) {
$api = Misc::api();
$max_items_per_user = $allowed_items_total / count($users);
foreach ($users as $user) {
$user_feed = $api->getUserFeed($user);
if ($user_feed) {
$max = count($user_feed->items) > $max_items_per_user ? $max_items_per_user : count($user_feed->items);
for ($i = 0; $i < $max; $i++) {
$item = $user_feed->items[$i];
array_push($items, $item);
}
}
}
}
$feed = (object) [
'items' => $items,
'hasMore' => false
];
return $feed;
}
};

View file

@ -43,14 +43,20 @@ class Misc {
$cacheEngine = new JSONCache();
break;
case 'redis':
if (!isset($_ENV['REDIS_URL'])) {
throw new \Exception('You need to set REDIS_URL to use Redis Cache!');
if (!(isset($_ENV['REDIS_URL']) || isset($_ENV['REDIS_HOST'], $_ENV['REDIS_PORT']))) {
throw new \Exception('You need to set REDIS_URL or REDIS_HOST and REDIS_PORT to use Redis Cache!');
}
$url = parse_url($_ENV['REDIS_URL']);
$host = $url['host'];
$port = $url['port'];
$password = $url['pass'] ?? null;
if (isset($_ENV['REDIS_URL'])) {
$url = parse_url($_ENV['REDIS_URL']);
$host = $url['host'];
$port = $url['port'];
$password = $url['pass'] ?? null;
} else {
$host = $_ENV['REDIS_HOST'];
$port = (int) $_ENV['REDIS_PORT'];
$password = isset($_ENV['REDIS_PASSWORD']) ? $_ENV['REDIS_PASSWORD'] : null;
}
$cacheEngine = new RedisCache($host, $port, $password);
break;
}
@ -77,7 +83,7 @@ class Misc {
return \Composer\InstalledVersions::getVersion('pablouser1/proxitok');
});
// https://stackoverflow.com/a/36365553
$latte->addFunction('number', function (int $x) {
$latte->addFunction('number', function (float $x) {
if($x > 1000) {
$x_number_format = number_format($x);
$x_array = explode(',', $x_number_format);

View file

@ -1,14 +0,0 @@
<?php
namespace App\Models;
/**
* Exclusive for /following
*/
class FollowingTemplate extends FeedTemplate {
public array $following;
function __construct(array $following, object $feed) {
parent::__construct('Following', $feed);
$this->following = $following;
}
}

View file

@ -2,18 +2,15 @@
namespace App\Models;
use App\Helpers\Cookies;
use App\Helpers\Following;
/**
* Exclusive for /settings
*/
class SettingsTemplate extends BaseTemplate {
public array $proxy_elements = [];
public array $following = [];
function __construct() {
parent::__construct('Settings');
$this->proxy_elements = Cookies::PROXY;
$this->following = Following::getUsers();
}
}