-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a job to reanalyze more posts based on date
- Loading branch information
1 parent
3639ce6
commit c91d1b7
Showing
4 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace App\Message; | ||
|
||
use DateTimeInterface; | ||
|
||
final readonly class ReanalyzePostsMessage | ||
{ | ||
public function __construct( | ||
public DateTimeInterface $since, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\MessageHandler; | ||
|
||
use App\Message\AnalyzePostMessage; | ||
use App\Message\ReanalyzePostsMessage; | ||
use DateTimeInterface; | ||
use Rikudou\LemmyApi\Enum\ListingType; | ||
use Rikudou\LemmyApi\Enum\SortType; | ||
use Rikudou\LemmyApi\LemmyApi; | ||
use Rikudou\LemmyApi\Response\View\PostView; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp; | ||
|
||
#[AsMessageHandler] | ||
final readonly class ReanalyzePostsHandler | ||
{ | ||
public function __construct( | ||
private MessageBusInterface $messageBus, | ||
private LemmyApi $api, | ||
) { | ||
} | ||
|
||
public function __invoke(ReanalyzePostsMessage $message): void | ||
{ | ||
foreach ($this->getPosts($message->since) as $post) { | ||
$this->messageBus->dispatch(new AnalyzePostMessage($post->post->id), [ | ||
new DispatchAfterCurrentBusStamp(), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable<PostView> | ||
*/ | ||
private function getPosts(DateTimeInterface $until): iterable | ||
{ | ||
$page = 1; | ||
do { | ||
$posts = $this->api->post()->getPosts(page: $page, sort: SortType::New, listingType: ListingType::All); | ||
foreach ($posts as $post) { | ||
yield $post; | ||
} | ||
} while (isset($post) && $post->post->published > $until); | ||
} | ||
} |