BloomFeed Upgrade v0.21
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -11,11 +12,15 @@ use Illuminate\View\View;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
private function registrationsAllowed(): bool
|
||||
{
|
||||
return ! User::query()->exists() || Setting::registrationsOpen();
|
||||
}
|
||||
|
||||
public function showRegister(): View|RedirectResponse
|
||||
{
|
||||
if (User::query()->exists()) {
|
||||
return redirect()->route('login')
|
||||
->with('status', 'Les inscriptions sont fermées sur cette instance. Connectez-vous.');
|
||||
if (! $this->registrationsAllowed()) {
|
||||
return redirect()->route('login')->with('status', __('app.registrations_closed'));
|
||||
}
|
||||
|
||||
return view('auth.register');
|
||||
@@ -23,9 +28,8 @@ class AuthController extends Controller
|
||||
|
||||
public function register(Request $request): RedirectResponse
|
||||
{
|
||||
if (User::query()->exists()) {
|
||||
return redirect()->route('login')
|
||||
->with('status', 'Les inscriptions sont fermées sur cette instance. Connectez-vous.');
|
||||
if (! $this->registrationsAllowed()) {
|
||||
return redirect()->route('login')->with('status', __('app.registrations_closed'));
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
@@ -38,6 +42,8 @@ class AuthController extends Controller
|
||||
'name' => $validated['name'],
|
||||
'email' => $validated['email'],
|
||||
'password' => Hash::make($validated['password']),
|
||||
'is_owner' => ! User::query()->exists(),
|
||||
'locale' => session('locale'),
|
||||
]);
|
||||
|
||||
Auth::login($user);
|
||||
@@ -60,7 +66,7 @@ class AuthController extends Controller
|
||||
|
||||
if (! Auth::attempt($credentials, $request->boolean('remember'))) {
|
||||
return back()->withErrors([
|
||||
'email' => 'Ces identifiants ne correspondent à aucun compte.',
|
||||
'email' => __('app.invalid_credentials'),
|
||||
])->onlyInput('email');
|
||||
}
|
||||
|
||||
|
||||
@@ -32,10 +32,10 @@ class FeedController extends Controller
|
||||
$result = $service->fetchFeed($feed);
|
||||
|
||||
if ($result['error'] !== null) {
|
||||
return back()->with('status', "Flux ajouté, mais la première récupération a échoué : {$result['error']}");
|
||||
return back()->with('status', __('app.feed_added_error', ['error' => $result['error']]));
|
||||
}
|
||||
|
||||
return back()->with('status', "Flux ajouté avec {$result['new']} article(s).");
|
||||
return back()->with('status', __('app.feed_added', ['count' => $result['new']]));
|
||||
}
|
||||
|
||||
public function destroy(Feed $feed): RedirectResponse
|
||||
@@ -44,7 +44,7 @@ class FeedController extends Controller
|
||||
|
||||
$feed->delete();
|
||||
|
||||
return back()->with('status', 'Flux supprimé.');
|
||||
return back()->with('status', __('app.feed_deleted'));
|
||||
}
|
||||
|
||||
public function refresh(Feed $feed, FeedFetcherService $service): RedirectResponse
|
||||
@@ -54,10 +54,10 @@ class FeedController extends Controller
|
||||
$result = $service->fetchFeed($feed);
|
||||
|
||||
if ($result['error'] !== null) {
|
||||
return back()->with('status', "Échec de la récupération : {$result['error']}");
|
||||
return back()->with('status', __('app.refresh_failed', ['error' => $result['error']]));
|
||||
}
|
||||
|
||||
return back()->with('status', "{$result['new']} nouvel(aux) article(s).");
|
||||
return back()->with('status', __('app.new_articles', ['count' => $result['new']]));
|
||||
}
|
||||
|
||||
public function refreshAll(Request $request, FeedFetcherService $service): RedirectResponse
|
||||
@@ -69,6 +69,6 @@ class FeedController extends Controller
|
||||
$totalNew += $service->fetchFeed($feed)['new'];
|
||||
}
|
||||
|
||||
return back()->with('status', "{$totalNew} nouvel(aux) article(s) sur {$feeds->count()} flux.");
|
||||
return back()->with('status', __('app.new_articles_across', ['count' => $totalNew, 'feeds' => $feeds->count()]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Services\FluxCatalogService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
private function ensureOwner(Request $request): void
|
||||
{
|
||||
abort_unless($request->user()->is_owner, 403);
|
||||
}
|
||||
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$this->ensureOwner($request);
|
||||
|
||||
return view('settings', [
|
||||
'registrationsOpen' => Setting::registrationsOpen(),
|
||||
'users' => User::query()->orderBy('created_at')->get(),
|
||||
'catalogSources' => Setting::catalogSources(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function addCatalogSource(Request $request, FluxCatalogService $catalog): RedirectResponse
|
||||
{
|
||||
$this->ensureOwner($request);
|
||||
|
||||
$validated = $request->validate([
|
||||
'source_url' => ['required', 'url', 'max:2048'],
|
||||
]);
|
||||
|
||||
$sources = Setting::catalogSources();
|
||||
|
||||
if (in_array($validated['source_url'], $sources, true)) {
|
||||
return redirect()->route('settings')->with('status', __('app.source_already_added'));
|
||||
}
|
||||
|
||||
$count = $catalog->validateSource($validated['source_url']);
|
||||
|
||||
if ($count === null) {
|
||||
return redirect()->route('settings')->withErrors(['source_url' => __('app.source_invalid')]);
|
||||
}
|
||||
|
||||
$sources[] = $validated['source_url'];
|
||||
Setting::setCatalogSources($sources);
|
||||
|
||||
return redirect()->route('settings')->with('status', __('app.source_added', ['count' => $count]));
|
||||
}
|
||||
|
||||
public function removeCatalogSource(Request $request): RedirectResponse
|
||||
{
|
||||
$this->ensureOwner($request);
|
||||
|
||||
$validated = $request->validate([
|
||||
'source_url' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
Setting::setCatalogSources(array_filter(
|
||||
Setting::catalogSources(),
|
||||
fn (string $url) => $url !== $validated['source_url']
|
||||
));
|
||||
|
||||
return redirect()->route('settings')->with('status', __('app.source_removed'));
|
||||
}
|
||||
|
||||
public function toggleRegistrations(Request $request): RedirectResponse
|
||||
{
|
||||
$this->ensureOwner($request);
|
||||
|
||||
$open = ! Setting::registrationsOpen();
|
||||
Setting::set('registrations_open', $open ? '1' : '0');
|
||||
|
||||
return redirect()->route('settings')
|
||||
->with('status', $open ? __('app.registrations_now_open') : __('app.registrations_now_closed'));
|
||||
}
|
||||
|
||||
public function createUser(Request $request): RedirectResponse
|
||||
{
|
||||
$this->ensureOwner($request);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],
|
||||
'password' => ['required', 'string', 'min:8'],
|
||||
]);
|
||||
|
||||
User::create([
|
||||
'name' => $validated['name'],
|
||||
'email' => $validated['email'],
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
return redirect()->route('settings')->with('status', __('app.user_created'));
|
||||
}
|
||||
|
||||
public function updateLocale(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'locale' => ['required', 'in:fr,en,es,de'],
|
||||
]);
|
||||
|
||||
session(['locale' => $validated['locale']]);
|
||||
|
||||
if ($request->user()) {
|
||||
$request->user()->update(['locale' => $validated['locale']]);
|
||||
}
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SetLocale
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public const SUPPORTED = ['fr', 'en', 'es', 'de'];
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$locale = $request->user()?->locale
|
||||
?? $request->session()->get('locale')
|
||||
?? config('app.locale');
|
||||
|
||||
if (in_array($locale, self::SUPPORTED, true)) {
|
||||
app()->setLocale($locale);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable(['key', 'value'])]
|
||||
class Setting extends Model
|
||||
{
|
||||
protected $primaryKey = 'key';
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
public static function get(string $key, ?string $default = null): ?string
|
||||
{
|
||||
return static::query()->find($key)?->value ?? $default;
|
||||
}
|
||||
|
||||
public static function set(string $key, ?string $value): void
|
||||
{
|
||||
static::query()->updateOrCreate(['key' => $key], ['value' => $value]);
|
||||
}
|
||||
|
||||
public static function registrationsOpen(): bool
|
||||
{
|
||||
return static::get('registrations_open', '0') === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function catalogSources(): array
|
||||
{
|
||||
$sources = json_decode(static::get('catalog_sources', '[]'), true);
|
||||
|
||||
return is_array($sources) ? array_values(array_filter($sources, 'is_string')) : [];
|
||||
}
|
||||
|
||||
public static function setCatalogSources(array $sources): void
|
||||
{
|
||||
static::set('catalog_sources', json_encode(array_values($sources)));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Fillable(['name', 'email', 'password', 'is_owner', 'locale'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@@ -33,6 +33,7 @@ class User extends Authenticatable
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'is_owner' => 'boolean',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,28 +2,129 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class FluxCatalogService
|
||||
{
|
||||
/**
|
||||
* Retourne le catalogue de flux embarqué (resources/data/bloomflux.json).
|
||||
* La liste est versionnée avec le code : la mettre à jour = éditer le fichier et commit.
|
||||
* Retourne le catalogue complet : le fichier embarqué (resources/data/bloomflux.json)
|
||||
* fusionné avec les sources externes configurées (URLs de fichiers bloomflux.json
|
||||
* hébergés dans d'autres dépôts). Dédoublonné par URL de flux, l'embarqué gagne.
|
||||
*
|
||||
* @return list<array{title: string, url: string, category: string, lang: string, description: ?string}>
|
||||
*/
|
||||
public function getCatalog(): array
|
||||
{
|
||||
$feeds = $this->loadEmbedded();
|
||||
|
||||
foreach (Setting::catalogSources() as $sourceUrl) {
|
||||
$feeds = array_merge($feeds, $this->loadRemoteSource($sourceUrl));
|
||||
}
|
||||
|
||||
$seen = [];
|
||||
$unique = [];
|
||||
|
||||
foreach ($feeds as $feed) {
|
||||
if (! isset($seen[$feed['url']])) {
|
||||
$seen[$feed['url']] = true;
|
||||
$unique[] = $feed;
|
||||
}
|
||||
}
|
||||
|
||||
return $unique;
|
||||
}
|
||||
|
||||
/**
|
||||
* Valide une source distante et retourne le nombre de flux qu'elle contient,
|
||||
* ou null si elle est injoignable ou invalide.
|
||||
*/
|
||||
public function validateSource(string $sourceUrl): ?int
|
||||
{
|
||||
$feeds = $this->fetchSource($sourceUrl);
|
||||
|
||||
return $feeds === null ? null : count($feeds);
|
||||
}
|
||||
|
||||
private function loadEmbedded(): array
|
||||
{
|
||||
try {
|
||||
$raw = file_get_contents(resource_path('data/bloomflux.json'));
|
||||
$data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
return $data['feeds'] ?? [];
|
||||
return $this->normalize($data['feeds'] ?? []);
|
||||
} catch (Throwable $e) {
|
||||
Log::warning('Catalogue bloomflux.json illisible', ['error' => $e->getMessage()]);
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge une source externe : réseau si joignable (et met le cache à jour),
|
||||
* sinon dernière copie en cache.
|
||||
*/
|
||||
private function loadRemoteSource(string $sourceUrl): array
|
||||
{
|
||||
$cacheKey = 'bloomflux.source.'.md5($sourceUrl);
|
||||
|
||||
$feeds = $this->fetchSource($sourceUrl);
|
||||
|
||||
if ($feeds !== null) {
|
||||
Cache::forever($cacheKey, $feeds);
|
||||
|
||||
return $feeds;
|
||||
}
|
||||
|
||||
Log::warning('Source de catalogue injoignable, utilisation du cache', ['source' => $sourceUrl]);
|
||||
|
||||
return Cache::get($cacheKey, []);
|
||||
}
|
||||
|
||||
private function fetchSource(string $sourceUrl): ?array
|
||||
{
|
||||
try {
|
||||
$response = Http::timeout(6)
|
||||
->withHeaders(['User-Agent' => 'BloomFeed/1.0'])
|
||||
->get($sourceUrl);
|
||||
|
||||
if ($response->failed()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($response->body(), true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
if (! isset($data['feeds']) || ! is_array($data['feeds'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->normalize($data['feeds']);
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function normalize(array $feeds): array
|
||||
{
|
||||
$clean = [];
|
||||
|
||||
foreach ($feeds as $feed) {
|
||||
if (! is_array($feed) || empty($feed['title']) || empty($feed['url'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$clean[] = [
|
||||
'title' => (string) $feed['title'],
|
||||
'url' => (string) $feed['url'],
|
||||
'category' => (string) ($feed['category'] ?? 'Divers'),
|
||||
'lang' => strtolower((string) ($feed['lang'] ?? 'en')),
|
||||
'description' => isset($feed['description']) ? (string) $feed['description'] : null,
|
||||
];
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user