Skip to content

Commit

Permalink
Feat: Add default community (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Feb 23, 2025
1 parent 4049eee commit 9b96c25
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)%'

Expand Down
8 changes: 4 additions & 4 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/Command/ManuallySyncCommunities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,17 +26,23 @@ protected function configure(): void
{
$this
->addArgument('instance', InputArgument::REQUIRED)
->addOption('sync', mode: InputOption::VALUE_NONE)
->addOption('jwt', mode: InputOption::VALUE_REQUIRED)
;
}

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;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -229,16 +230,23 @@ 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();
if ($default === null) {
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],
Expand Down
33 changes: 25 additions & 8 deletions src/JobHandler/FetchCommunitiesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -18,6 +20,8 @@
public function __construct(
private LemmyApiFactory $apiFactory,
private CacheItemPoolInterface $cache,
#[Autowire('%app.default_communities%')]
private array $defaultCommunities,
) {
}

Expand All @@ -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<CommentView>
* @return iterable<CommunityView>
*/
public function getCommunities(LemmyApi $api): iterable
{
Expand All @@ -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);
Expand Down

0 comments on commit 9b96c25

Please sign in to comment.