Add Nostr publishing and login keys cleanup commands

This commit is contained in:
HolgerHatGarKeineNode
2025-11-21 16:34:38 +01:00
parent efe44cf344
commit 7adefecfbb
4 changed files with 197 additions and 5 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Console\Commands\Database;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CleanupLoginKeys extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'loginkeys:cleanup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): int
{
DB::table('login_keys')
->where('created_at', '<', now()->subDays(1))
->delete();
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Console\Commands\Nostr;
use App\Traits\NostrTrait;
use Illuminate\Console\Command;
class PublishUnpublishedItems extends Command
{
use NostrTrait;
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'nostr:publish {--model=}';
/**
* The console command description.
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): void
{
config(['app.user-timezone' => 'Europe/Berlin']);
$modelName = $this->option('model');
$className = '\\App\Models\\' . $modelName;
$model = $className::query()
->whereNull('nostr_status')
->when($modelName === 'BitcoinEvent', fn($q) => $q->where('from', '>', now()))
->when($modelName === 'CourseEvent', fn($q) => $q->where('from', '>', now()))
->when($modelName === 'MeetupEvent', fn($q) => $q->where('start', '>', now()))
->when($modelName === 'LibraryItem', fn($q) => $q
->where('type', '<>', 'markdown_article')
->where('type', '<>', 'bindle')
)
->orderByDesc('created_at')
->first();
if ($model) {
$this->publishOnNostr($model, $this->getText($model));
}
}
}

104
app/Traits/NostrTrait.php Normal file
View File

@@ -0,0 +1,104 @@
<?php
namespace App\Traits;
use App\Models\Course;
use App\Models\CourseEvent;
use App\Models\Meetup;
use App\Models\MeetupEvent;
use Illuminate\Support\Facades\Process;
trait NostrTrait
{
public function publishOnNostr($model, $text): array
{
if (app()->environment('local')) {
return [
'success' => true,
'output' => 'local',
'exitCode' => 0,
'errorOutput' => '',
];
}
//noscl publish "Good morning!"
$result = Process::timeout(60 * 5)
->run('noscl publish "'.$text.'"');
if ($result->successful()) {
$model->nostr_status = $result->output();
$model->save();
}
return [
'success' => $result->successful(),
'output' => $result->output(),
'exitCode' => $result->exitCode(),
'errorOutput' => $result->errorOutput(),
];
}
public function getText($model)
{
$from = '';
if ($model instanceof CourseEvent) {
if ($model->course->lecturer->nostr) {
$from .= '@'.$model->course->lecturer->nostr;
} else {
$from .= $model->course->lecturer->name;
}
return sprintf("Unser Dozent %s hat einen neuen Kurs-Termin eingestellt:\n%s\n%s\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig #gesundesgeld #einundzwanzig_portal_lecturer_%s",
$from,
$model->course->name,
str($model->course->description)->toString(),
url()->route('courses.landingpage',
['country' => 'de', 'course' => $model->course]),
str($model->course->lecturer->slug)->replace('-', '_'),
);
}
if ($model instanceof MeetupEvent) {
$from = $model->meetup->name;
if ($model->meetup->nostr) {
$from .= ' @'.$model->meetup->nostr;
}
return sprintf("%s hat einen neuen Termin eingestellt:\n%s\n%s\n%s\n\n#Bitcoin #Meetup #Einundzwanzig #gesundesgeld #einundzwanzig_portal_%s",
$from,
$model->start->asDateTime(),
$model->location,
url()->route('meetups.landingpage-event',
['country' => 'de', 'meetup' => $model, 'event' => $model]),
str($model->meetup->slug)->replace('-', '_'),
);
}
if ($model instanceof Meetup) {
$from = $model->name;
if ($model->nostr) {
$from .= ' @'.$model->nostr;
}
return sprintf("Eine neue Meetup Gruppe wurde hinzugefügt:\n%s\n%s\n\n#Bitcoin #Meetup #Einundzwanzig #gesundesgeld #einundzwanzig_portal_%s",
$from,
url()->route('meetups.landingpage', ['country' => $model->city->country->code, 'meetup' => $model]),
str($model->slug)->replace('-', '_'),
);
}
if ($model instanceof Course) {
if ($model->lecturer->nostr) {
$from .= '@'.$model->lecturer->nostr;
} else {
$from .= $model->lecturer->name;
}
return sprintf("Unser Dozent %s hat einen neuen Kurs eingestellt:\n%s\n%s\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig #gesundesgeld #einundzwanzig_portal_lecturer_%s",
$from,
$model->name,
str($model->description)->toString(),
url()->route('courses.landingpage',
['country' => 'de', 'course' => $model]),
str($model->lecturer->slug)->replace('-', '_'),
);
}
}
}

View File

@@ -1,8 +1,14 @@
<?php <?php
use Illuminate\Foundation\Inspiring; use App\Console\Commands\Database\CleanupLoginKeys;
use Illuminate\Support\Facades\Artisan; use App\Console\Commands\Nostr\PublishUnpublishedItems;
Artisan::command('inspire', function () { Schedule::command(CleanupLoginKeys::class)->everyFifteenMinutes();
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote'); Schedule::command(PublishUnpublishedItems::class, [
'--model' => 'MeetupEvent',
])->dailyAt('17:00');
Schedule::command(PublishUnpublishedItems::class, [
'--model' => 'Meetup',
])->dailyAt('18:00');