Compare commits

..

No commits in common. "ec09711a6fe7f9ee8c09bb62eb160d07b23b4ba1" and "19cbea9273dffd42422cf7164700680d0bdb2b35" have entirely different histories.

21 changed files with 108 additions and 281 deletions

View file

@ -5,18 +5,4 @@
enum PlatformEnum: string
{
case LEMMY = 'lemmy';
public function channelLabel(): string
{
return match ($this) {
self::LEMMY => 'Community',
};
}
public function channelLabelPlural(): string
{
return match ($this) {
self::LEMMY => 'Communities',
};
}
}

View file

@ -1,18 +0,0 @@
<?php
namespace App\Events;
use App\Enums\LogLevelEnum;
use Illuminate\Foundation\Events\Dispatchable;
class ActionPerformed
{
use Dispatchable;
public function __construct(
public string $message,
public LogLevelEnum $level = LogLevelEnum::INFO,
/** @var array<string, mixed> */
public array $context = [],
) {}
}

View file

@ -2,8 +2,6 @@
namespace App\Jobs;
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
use App\Exceptions\PublishException;
use App\Models\Article;
use App\Models\ArticlePublication;
@ -55,7 +53,7 @@ public function handle(ArticleFetcher $articleFetcher, ArticlePublishingService
return;
}
ActionPerformed::dispatch('Publishing next article from scheduled job', LogLevelEnum::INFO, [
logger()->info('Publishing next article from scheduled job', [
'article_id' => $article->id,
'title' => $article->title,
'url' => $article->url,
@ -68,12 +66,12 @@ public function handle(ArticleFetcher $articleFetcher, ArticlePublishingService
try {
$publishingService->publishToRoutedChannels($article, $extractedData);
ActionPerformed::dispatch('Successfully published article', LogLevelEnum::INFO, [
logger()->info('Successfully published article', [
'article_id' => $article->id,
'title' => $article->title,
]);
} catch (PublishException $e) {
ActionPerformed::dispatch('Failed to publish article', LogLevelEnum::ERROR, [
logger()->error('Failed to publish article', [
'article_id' => $article->id,
'error' => $e->getMessage(),
]);

View file

@ -1,21 +0,0 @@
<?php
namespace App\Listeners;
use App\Events\ActionPerformed;
use App\Services\Log\LogSaver;
use Exception;
class LogActionListener
{
public function __construct(private LogSaver $logSaver) {}
public function handle(ActionPerformed $event): void
{
try {
$this->logSaver->log($event->level, $event->message, context: $event->context);
} catch (Exception $e) {
error_log('Failed to log action to database: '.$e->getMessage());
}
}
}

View file

@ -2,8 +2,6 @@
namespace App\Listeners;
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
use App\Events\ArticleApproved;
use App\Services\Article\ArticleFetcher;
use App\Services\Publishing\ArticlePublishingService;
@ -42,14 +40,14 @@ public function handle(ArticleApproved $event): void
if ($publications->isNotEmpty()) {
$article->update(['publish_status' => 'published']);
ActionPerformed::dispatch('Published approved article', LogLevelEnum::INFO, [
logger()->info('Published approved article', [
'article_id' => $article->id,
'title' => $article->title,
]);
} else {
$article->update(['publish_status' => 'error']);
ActionPerformed::dispatch('No publications created for approved article', LogLevelEnum::WARNING, [
logger()->warning('No publications created for approved article', [
'article_id' => $article->id,
'title' => $article->title,
]);
@ -57,7 +55,7 @@ public function handle(ArticleApproved $event): void
} catch (Exception $e) {
$article->update(['publish_status' => 'error']);
ActionPerformed::dispatch('Failed to publish approved article', LogLevelEnum::ERROR, [
logger()->error('Failed to publish approved article', [
'article_id' => $article->id,
'error' => $e->getMessage(),
]);

View file

@ -2,8 +2,6 @@
namespace App\Listeners;
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
use App\Events\NewArticleFetched;
use App\Models\Setting;
use App\Services\Article\ValidationService;
@ -39,7 +37,7 @@ public function handle(NewArticleFetched $event): void
try {
$article = $this->validationService->validate($article);
} catch (Exception $e) {
ActionPerformed::dispatch('Article validation failed', LogLevelEnum::ERROR, [
logger()->error('Article validation failed', [
'article_id' => $article->id,
'error' => $e->getMessage(),
]);

View file

@ -161,7 +161,7 @@ public function deleteKeyword(int $keywordId): void
public function render(): \Illuminate\Contracts\View\View
{
$routes = Route::with(['feed', 'platformChannel.platformInstance'])
$routes = Route::with(['feed', 'platformChannel'])
->orderBy('priority', 'desc')
->get();
@ -186,7 +186,7 @@ public function render(): \Illuminate\Contracts\View\View
$editingKeywords = collect();
if ($this->editingFeedId && $this->editingChannelId) {
$editingRoute = Route::with(['feed', 'platformChannel.platformInstance'])
$editingRoute = Route::with(['feed', 'platformChannel'])
->where('feed_id', $this->editingFeedId)
->where('platform_channel_id', $this->editingChannelId)
->first();

View file

@ -3,9 +3,7 @@
namespace App\Providers;
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
use App\Events\ExceptionOccurred;
use App\Listeners\LogActionListener;
use App\Listeners\LogExceptionToDatabase;
use Error;
use Illuminate\Contracts\Debug\ExceptionHandler;
@ -20,11 +18,6 @@ public function register(): void {}
public function boot(): void
{
Event::listen(
ActionPerformed::class,
LogActionListener::class,
);
Event::listen(
ExceptionOccurred::class,
LogExceptionToDatabase::class,

View file

@ -43,7 +43,7 @@ public function debug(string $message, ?PlatformChannel $channel = null, array $
/**
* @param array<string, mixed> $context
*/
public function log(LogLevelEnum $level, string $message, ?PlatformChannel $channel = null, array $context = []): void
private function log(LogLevelEnum $level, string $message, ?PlatformChannel $channel = null, array $context = []): void
{
$logContext = $context;

View file

@ -0,0 +1,49 @@
<?php
namespace App\Services\Parsers;
class BelgaHomepageParser
{
/**
* @return array<int, string>
*/
public static function extractArticleUrls(string $html): array
{
// Find all relative article links (most articles use relative paths)
preg_match_all('/<a[^>]+href="(\/[a-z0-9-]+)"/', $html, $matches);
// Blacklist of non-article paths
$blacklistPaths = [
'/',
'/de',
'/feed',
'/search',
'/category',
'/about',
'/contact',
'/privacy',
'/terms',
];
$urls = collect($matches[1])
->unique()
->filter(function ($path) use ($blacklistPaths) {
// Exclude exact matches and paths starting with blacklisted paths
foreach ($blacklistPaths as $blacklistedPath) {
if ($path === $blacklistedPath || str_starts_with($path, $blacklistedPath.'/')) {
return false;
}
}
return true;
})
->map(function ($path) {
// Convert relative paths to absolute URLs
return 'https://www.belganewsagency.eu'.$path;
})
->values()
->toArray();
return $urls;
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace App\Services\Parsers;
use App\Contracts\HomepageParserInterface;
class BelgaHomepageParserAdapter implements HomepageParserInterface
{
public function __construct(
private readonly string $language = 'en',
) {}
public function getLanguage(): string
{
return $this->language;
}
public function canParse(string $url): bool
{
return str_contains($url, 'belganewsagency.eu');
}
public function extractArticleUrls(string $html): array
{
return BelgaHomepageParser::extractArticleUrls($html);
}
public function getHomepageUrl(): string
{
return 'https://www.belganewsagency.eu/';
}
public function getSourceName(): string
{
return 'Belga News Agency';
}
}

View file

@ -33,12 +33,13 @@
'code' => 'belga',
'name' => 'Belga News Agency',
'description' => 'Belgian national news agency',
'type' => 'rss',
'type' => 'website',
'is_active' => true,
'languages' => [
'en' => ['url' => 'https://www.belganewsagency.eu/feed'],
'en' => ['url' => 'https://www.belganewsagency.eu/'],
],
'parsers' => [
'homepage' => \App\Services\Parsers\BelgaHomepageParserAdapter::class,
'article' => \App\Services\Parsers\BelgaArticleParser::class,
'article_page' => \App\Services\Parsers\BelgaArticlePageParser::class,
],

View file

@ -75,8 +75,7 @@ public function belga(): static
{
return $this->state(fn (array $attributes) => [
'provider' => 'belga',
'url' => 'https://www.belganewsagency.eu/feed',
'type' => 'rss',
'url' => 'https://www.belganewsagency.eu/',
]);
}
}

View file

@ -47,7 +47,7 @@ class="inline-flex items-center px-4 py-2 border border-transparent text-sm font
<span>&bull;</span>
<span>Feed: {{ $route->feed?->name }}</span>
<span>&bull;</span>
<span>{{ $route->platformChannel?->platformInstance?->platform?->channelLabel() ?? 'Channel' }}: {{ $route->platformChannel?->display_name ?? $route->platformChannel?->name }}</span>
<span>Channel: {{ $route->platformChannel?->display_name ?? $route->platformChannel?->name }}</span>
<span>&bull;</span>
<span>Created: {{ $route->created_at->format('M d, Y') }}</span>
</div>
@ -246,7 +246,7 @@ class="px-4 py-2 text-sm font-medium text-white bg-blue-600 border border-transp
<strong>Feed:</strong> {{ $editingRoute->feed?->name }}
</p>
<p class="text-sm text-gray-600">
<strong>{{ $editingRoute->platformChannel?->platformInstance?->platform?->channelLabel() ?? 'Channel' }}:</strong> {{ $editingRoute->platformChannel?->display_name ?? $editingRoute->platformChannel?->name }}
<strong>Channel:</strong> {{ $editingRoute->platformChannel?->display_name ?? $editingRoute->platformChannel?->name }}
</p>
</div>

View file

@ -98,16 +98,16 @@ public function test_store_creates_belga_feed_successfully(): void
'message' => 'Feed created successfully!',
'data' => [
'name' => 'Belga Test Feed',
'url' => 'https://www.belganewsagency.eu/feed',
'type' => 'rss',
'url' => 'https://www.belganewsagency.eu/',
'type' => 'website',
'is_active' => true,
],
]);
$this->assertDatabaseHas('feeds', [
'name' => 'Belga Test Feed',
'url' => 'https://www.belganewsagency.eu/feed',
'type' => 'rss',
'url' => 'https://www.belganewsagency.eu/',
'type' => 'website',
]);
}

View file

@ -2,7 +2,6 @@
namespace Tests\Feature;
use App\Events\ActionPerformed;
use App\Events\ArticleApproved;
// use App\Events\ArticleReadyToPublish; // Class no longer exists
use App\Events\ExceptionLogged;
@ -269,9 +268,6 @@ public function test_log_exception_to_database_listener_creates_log(): void
public function test_event_listener_registration_works(): void
{
// Test that events are properly bound to listeners
$listeners = Event::getListeners(ActionPerformed::class);
$this->assertNotEmpty($listeners);
$listeners = Event::getListeners(NewArticleFetched::class);
$this->assertNotEmpty($listeners);

View file

@ -42,8 +42,8 @@ public function test_creates_belga_feed_with_correct_url(): void
$feed = $this->action->execute('Belga News', 'belga', $language->id);
$this->assertEquals('https://www.belganewsagency.eu/feed', $feed->url);
$this->assertEquals('rss', $feed->type);
$this->assertEquals('https://www.belganewsagency.eu/', $feed->url);
$this->assertEquals('website', $feed->type);
$this->assertEquals('belga', $feed->provider);
$this->assertNull($feed->description);
}

View file

@ -86,14 +86,4 @@ public function test_enum_value_is_string_backed(): void
{
$this->assertIsString(PlatformEnum::LEMMY->value);
}
public function test_channel_label_returns_community_for_lemmy(): void
{
$this->assertEquals('Community', PlatformEnum::LEMMY->channelLabel());
}
public function test_channel_label_plural_returns_communities_for_lemmy(): void
{
$this->assertEquals('Communities', PlatformEnum::LEMMY->channelLabelPlural());
}
}

View file

@ -1,48 +0,0 @@
<?php
namespace Tests\Unit\Events;
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
use Illuminate\Foundation\Events\Dispatchable;
use Tests\TestCase;
class ActionPerformedTest extends TestCase
{
public function test_event_can_be_constructed_with_defaults(): void
{
$event = new ActionPerformed('Test message');
$this->assertEquals('Test message', $event->message);
$this->assertEquals(LogLevelEnum::INFO, $event->level);
$this->assertEquals([], $event->context);
}
public function test_event_can_be_constructed_with_custom_level_and_context(): void
{
$context = ['article_id' => 1, 'error' => 'Something failed'];
$event = new ActionPerformed(
'Article validation failed',
LogLevelEnum::ERROR,
$context,
);
$this->assertEquals('Article validation failed', $event->message);
$this->assertEquals(LogLevelEnum::ERROR, $event->level);
$this->assertEquals($context, $event->context);
}
public function test_event_uses_dispatchable_trait(): void
{
$this->assertContains(Dispatchable::class, class_uses(ActionPerformed::class));
}
public function test_event_does_not_use_serializes_models_trait(): void
{
$this->assertNotContains(
\Illuminate\Queue\SerializesModels::class,
class_uses(ActionPerformed::class),
);
}
}

View file

@ -1,85 +0,0 @@
<?php
namespace Tests\Unit\Listeners;
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
use App\Listeners\LogActionListener;
use App\Models\Log;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class LogActionListenerTest extends TestCase
{
use RefreshDatabase;
public function test_listener_creates_log_entry_with_correct_data(): void
{
$event = new ActionPerformed(
'Article published successfully',
LogLevelEnum::INFO,
['article_id' => 42],
);
$listener = app(LogActionListener::class);
$listener->handle($event);
$this->assertDatabaseHas('logs', [
'level' => 'info',
'message' => 'Article published successfully',
]);
$log = Log::where('message', 'Article published successfully')->first();
$this->assertEquals(['article_id' => 42], $log->context);
}
public function test_listener_creates_log_with_warning_level(): void
{
$event = new ActionPerformed(
'No publications created',
LogLevelEnum::WARNING,
['article_id' => 7],
);
$listener = app(LogActionListener::class);
$listener->handle($event);
$this->assertDatabaseHas('logs', [
'level' => 'warning',
'message' => 'No publications created',
]);
}
public function test_listener_creates_log_with_error_level(): void
{
$event = new ActionPerformed(
'Publishing failed',
LogLevelEnum::ERROR,
['error' => 'Connection timeout'],
);
$listener = app(LogActionListener::class);
$listener->handle($event);
$this->assertDatabaseHas('logs', [
'level' => 'error',
'message' => 'Publishing failed',
]);
}
public function test_listener_creates_log_with_empty_context(): void
{
$event = new ActionPerformed('Simple action');
$listener = app(LogActionListener::class);
$listener->handle($event);
$this->assertDatabaseHas('logs', [
'level' => 'info',
'message' => 'Simple action',
]);
$log = Log::where('message', 'Simple action')->first();
$this->assertEquals([], $log->context);
}
}

View file

@ -156,52 +156,6 @@ public function test_get_articles_from_rss_feed_handles_http_failure(): void
$this->assertEmpty($result);
}
public function test_get_articles_from_belga_rss_feed_creates_articles(): void
{
$belgaRss = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Belga News Agency</title>
<link>https://www.belganewsagency.eu</link>
<item>
<title>Belgium announces new climate plan</title>
<link>https://www.belganewsagency.eu/belgium-announces-new-climate-plan</link>
<description>Belgium has unveiled a comprehensive climate strategy.</description>
<pubDate>Sun, 08 Mar 2026 10:00:00 GMT</pubDate>
</item>
<item>
<title>EU summit concludes in Brussels</title>
<link>https://www.belganewsagency.eu/eu-summit-concludes-in-brussels</link>
<description>European leaders reached agreement on key issues.</description>
<pubDate>Sun, 08 Mar 2026 09:00:00 GMT</pubDate>
</item>
</channel>
</rss>
XML;
Http::fake(['*' => Http::response($belgaRss, 200)]);
$feed = Feed::factory()->create([
'type' => 'rss',
'provider' => 'belga',
'url' => 'https://www.belganewsagency.eu/feed',
]);
$fetcher = $this->createArticleFetcher();
$result = $fetcher->getArticlesFromFeed($feed);
$this->assertCount(2, $result);
$this->assertDatabaseHas('articles', [
'url' => 'https://www.belganewsagency.eu/belgium-announces-new-climate-plan',
'feed_id' => $feed->id,
]);
$this->assertDatabaseHas('articles', [
'url' => 'https://www.belganewsagency.eu/eu-summit-concludes-in-brussels',
'feed_id' => $feed->id,
]);
}
protected function tearDown(): void
{
Mockery::close();