From 9b96c25c14835cedda6e82639372e2c658319106 Mon Sep 17 00:00:00 2001 From: Rikudou_Sage Date: Sun, 23 Feb 2025 19:53:41 +0100 Subject: [PATCH] Feat: Add default community (#39) --- .env | 1 + README.md | 1 + config/services.yaml | 1 + shell.nix | 8 +++--- src/Command/ManuallySyncCommunities.php | 12 ++++++-- src/Controller/PostController.php | 10 ++++++- src/JobHandler/FetchCommunitiesHandler.php | 33 ++++++++++++++++------ 7 files changed, 51 insertions(+), 15 deletions(-) diff --git a/.env b/.env index 7a457cb..2bc8f40 100644 --- a/.env +++ b/.env @@ -50,6 +50,7 @@ NEW_VERSION_CHECK=1 SOURCE_URL="https://github.com/RikudouSage/LemmySchedule" # used for new version checks and footer link CATBOX_USER_HASH= CATBOX_ALLOW_ANONYMOUS=0 +DEFAULT_COMMUNITIES= # Feature flags FLAG_COMMUNITY_GROUPS=0 diff --git a/README.md b/README.md index f05c260..2d3710a 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ in `.env.local` file. | `FLAG_COMMUNITY_GROUPS` | whether community groups, an experimental feature, should be enabled | 0 | no | | `SOURCE_URL` | the URL of the source code, used for new version checks and for a footer link - if you plan on maintaining a fork, you can change it to your fork URL | https://github.com/RikudouSage/LemmySchedule | **yes** | | `NEW_VERSION_CHECK` | set to 1 or 0 to enable or disable checks for a new version | 1 | **yes** | +| `DEFAULT_COMMUNITIES` | a comma separated list of communities that will be preselected when going to the schedule page | | no | #### Job transports diff --git a/config/services.yaml b/config/services.yaml index 2d2869e..aa74ded 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -7,6 +7,7 @@ parameters: app.default_instance: '%env(DEFAULT_INSTANCE)%' app.single_instance: '%env(bool:SINGLE_INSTANCE_MODE)%' app.default_post_language: '%env(int:DEFAULT_POST_LANGUAGE)%' + app.default_communities: '%env(csv:DEFAULT_COMMUNITIES)%' app.source_url: '%env(SOURCE_URL)%' app.version_check.enabled: '%env(bool:NEW_VERSION_CHECK)%' diff --git a/shell.nix b/shell.nix index 569c9bc..6768c65 100644 --- a/shell.nix +++ b/shell.nix @@ -2,7 +2,7 @@ pkgs.mkShell { nativeBuildInputs = with pkgs.buildPackages; let - php82 = pkgs.php82.buildEnv { + php83 = pkgs.php83.buildEnv { extensions = ({ enabled, all }: enabled ++ (with all; [ xdebug redis @@ -17,9 +17,9 @@ pkgs.mkShell { [ nodejs_18 nodePackages.serverless - php82 - php82.packages.composer - php82Extensions.redis + php83 + php83.packages.composer + php83Extensions.redis redis symfony-cli yarn diff --git a/src/Command/ManuallySyncCommunities.php b/src/Command/ManuallySyncCommunities.php index 37759f6..bd3cced 100644 --- a/src/Command/ManuallySyncCommunities.php +++ b/src/Command/ManuallySyncCommunities.php @@ -7,9 +7,11 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Messenger\MessageBusInterface; +use Symfony\Component\Messenger\Stamp\TransportNamesStamp; #[AsCommand('app:sync:manual')] final class ManuallySyncCommunities extends Command @@ -24,6 +26,8 @@ protected function configure(): void { $this ->addArgument('instance', InputArgument::REQUIRED) + ->addOption('sync', mode: InputOption::VALUE_NONE) + ->addOption('jwt', mode: InputOption::VALUE_REQUIRED) ; } @@ -31,10 +35,14 @@ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); + $stamps = $input->getOption('sync') ? [new TransportNamesStamp('sync')] : []; + + $jwt = $input->getOption('jwt') ?? $io->askHidden('JWT'); + $this->messageBus->dispatch(new FetchCommunitiesJob( instance: $input->getArgument('instance'), - jwt: $io->askHidden('JWT') - )); + jwt: $jwt, + ), $stamps); return self::SUCCESS; } diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index a79041f..62e1b4d 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Authentication\User; +use App\Dto\CommunityGroup; use App\Enum\DayType; use App\Enum\PinType; use App\Enum\ScheduleType; @@ -229,6 +230,8 @@ public function createPost( #[Autowire('%app.default_post_language%')] int $defaultLanguage, CommunityGroupManager $groupManager, + #[Autowire('%app.default_communities%')] + array $defaultCommunities, ): Response { $fileProviders = [...$fileProviders]; $default = (array_values(array_filter($fileProviders, static fn (FileProvider $fileProvider) => $fileProvider->isDefault()))[0] ?? null)?->getId(); @@ -236,9 +239,14 @@ public function createPost( throw new LogicException('No default file provider specified'); } + $defaultCommunities = array_map( + fn (string $community) => str_starts_with($community, '!') ? $community : '!' . $community, + $defaultCommunities, + ); + return $this->render('post/create.html.twig', [ 'communities' => $this->getCommunities(), - 'selectedCommunities' => [], + 'selectedCommunities' => $defaultCommunities, 'languages' => Language::cases(), 'selectedLanguage' => Language::tryFrom($defaultLanguage) ?? Language::Undetermined, 'fileProviders' => [...$fileProviders], diff --git a/src/JobHandler/FetchCommunitiesHandler.php b/src/JobHandler/FetchCommunitiesHandler.php index 7cb4298..95a5495 100644 --- a/src/JobHandler/FetchCommunitiesHandler.php +++ b/src/JobHandler/FetchCommunitiesHandler.php @@ -7,9 +7,11 @@ use Psr\Cache\CacheItemPoolInterface; use Rikudou\LemmyApi\Enum\ListingType; use Rikudou\LemmyApi\Enum\SortType; +use Rikudou\LemmyApi\Exception\HttpApiException; use Rikudou\LemmyApi\LemmyApi; use Rikudou\LemmyApi\Response\View\CommentView; use Rikudou\LemmyApi\Response\View\CommunityView; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Messenger\Attribute\AsMessageHandler; #[AsMessageHandler] @@ -18,6 +20,8 @@ public function __construct( private LemmyApiFactory $apiFactory, private CacheItemPoolInterface $cache, + #[Autowire('%app.default_communities%')] + private array $defaultCommunities, ) { } @@ -30,13 +34,17 @@ public function __invoke(FetchCommunitiesJob $job): void static fn (CommunityView $community) => '!' . $community->community->name . '@' . parse_url($community->community->actorId, PHP_URL_HOST), [...$communities], ); + foreach ($this->defaultCommunities as $defaultCommunity) { + $communities[] = str_starts_with($defaultCommunity, '!') ? $defaultCommunity : '!' . $defaultCommunity; + } + $communities = array_unique($communities); $cacheItem->set($communities); $this->cache->save($cacheItem); } /** - * @return iterable + * @return iterable */ public function getCommunities(LemmyApi $api): iterable { @@ -47,13 +55,22 @@ public function getCommunities(LemmyApi $api): iterable if ($i > $totalLimit) { break; } - $communities = $api->community()->list( - limit: 20, - page: $page, - sort: SortType::TopAll, - listingType: ListingType::All, - showNsfw: true, - ); + fetch: + try { + $communities = $api->community()->list( + limit: 20, + page: $page, + sort: SortType::TopAll, + listingType: ListingType::All, + showNsfw: true, + ); + } catch (HttpApiException $e) { + if (!str_contains($e->getMessage(), 'rate_limit_error')) { + throw $e; + } + sleep(5); + goto fetch; + } yield from $communities; $i += count($communities);