nostr events added

This commit is contained in:
HolgerHatGarKeineNode
2023-02-24 10:35:07 +01:00
parent 1aaa955567
commit 38a2b63e3f
11 changed files with 167 additions and 212 deletions

View File

@@ -3,8 +3,7 @@
namespace App\Http\Livewire\News; namespace App\Http\Livewire\News;
use App\Models\LibraryItem; use App\Models\LibraryItem;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Illuminate\Support\Facades\Process;
use Livewire\Component; use Livewire\Component;
use RalphJSmit\Laravel\SEO\Support\SEOData; use RalphJSmit\Laravel\SEO\Support\SEOData;
use WireUi\Traits\Actions; use WireUi\Traits\Actions;
@@ -12,62 +11,7 @@ use WireUi\Traits\Actions;
class ArticleOverview extends Component class ArticleOverview extends Component
{ {
use Actions; use Actions;
use TwitterTrait; use NostrTrait;
public function tweet($id)
{
$libraryItem = LibraryItem::query()
->with([
'lecturer',
])
->find($id);
if ($libraryItem->tweet) {
$this->notification()
->error(__('Article already tweeted'));
return;
}
$libraryItem->setStatus('published');
$libraryItemName = $libraryItem->name;
if ($libraryItem->lecturer->twitter_username && $libraryItem->type !== 'markdown_article') {
$libraryItemName .= ' von @'.$libraryItem->lecturer->twitter_username;
}
if (!$libraryItem->lecturer->twitter_username) {
$libraryItemName .= ' von '.$libraryItem->lecturer->name;
}
try {
if (config('feeds.services.twitterAccountId')) {
$this->setNewAccessToken(1);
if (!$libraryItem->approved) {
$this->notification()
->error(__('Article not approved yet'));
return;
}
$text = sprintf("Ein neuer News-Artikel wurde verfasst:\n\n%s\n\n%s\n\n#Bitcoin #News #Einundzwanzig #gesundesgeld",
$libraryItemName,
url()->route('article.view',
['libraryItem' => $libraryItem->slug]),
);
$this->postTweet($text);
$libraryItem->tweet = true;
$libraryItem->save();
$this->notification()
->success(__('Article tweeted'));
$this->emit('$refresh');
}
} catch (\Exception $e) {
$this->notification()
->error(__('Error tweeting article', $e->getMessage()));
}
}
public function nostr($id) public function nostr($id)
{ {
@@ -88,20 +32,14 @@ class ArticleOverview extends Component
url()->route('article.view', url()->route('article.view',
['libraryItem' => $libraryItem->slug]), ['libraryItem' => $libraryItem->slug]),
); );
$result = $this->publishOnNostr($libraryItem, $text);
//noscl publish "Good morning!" if ($result['success']) {
$result = Process::timeout(60 * 5)->run('noscl publish "'.$text.'"');
if ($result->successful()) {
$libraryItem->nostr = $result->output();
$libraryItem->save();
$this->notification() $this->notification()
->success(title: __('Published on Nostr'), description: $result->output()); ->success(title: __('Published on Nostr'), description: $result->output());
} } else {
if ($result->failed()) {
$this->notification() $this->notification()
->error(title: __('Failed'), ->error(title: __('Failed'),
description: 'Exit Code: '.$result->exitCode().' Reason: '.$result->errorOutput()); description: 'Exit Code: '.$result['exitCode'].' Reason: '.$result['errorOutput']);
} }
} }

View File

@@ -3,20 +3,20 @@
namespace App\Observers; namespace App\Observers;
use App\Models\BitcoinEvent; use App\Models\BitcoinEvent;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class BitcoinEventObserver class BitcoinEventObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the BitcoinEvent "created" event. * Handle the BitcoinEvent "created" event.
*/ */
public function created(BitcoinEvent $bitcoinEvent): void public function created(BitcoinEvent $bitcoinEvent): void
{ {
if (config('feeds.services.twitterAccountId')) { try {
$this->setNewAccessToken(1);
$text = sprintf("Ein neues Event wurde eingestellt:\n\n%s\n\n%s bis %s\n\n%s\n\n%s\n\n#Bitcoin #Event #Einundzwanzig #gesundesgeld", $text = sprintf("Ein neues Event wurde eingestellt:\n\n%s\n\n%s bis %s\n\n%s\n\n%s\n\n#Bitcoin #Event #Einundzwanzig #gesundesgeld",
$bitcoinEvent->title, $bitcoinEvent->title,
$bitcoinEvent->from->asDateTime(), $bitcoinEvent->from->asDateTime(),
@@ -24,8 +24,9 @@ class BitcoinEventObserver
$bitcoinEvent->venue->name, $bitcoinEvent->venue->name,
$bitcoinEvent->link, $bitcoinEvent->link,
); );
$this->publishOnNostr($bitcoinEvent, $text);
$this->postTweet($text); } catch (Exception $e) {
Log::error($e->getMessage());
} }
} }

View File

@@ -3,20 +3,20 @@
namespace App\Observers; namespace App\Observers;
use App\Models\CourseEvent; use App\Models\CourseEvent;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class CourseEventObserver class CourseEventObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the CourseEvent "created" event. * Handle the CourseEvent "created" event.
*/ */
public function created(CourseEvent $courseEvent): void public function created(CourseEvent $courseEvent): void
{ {
if (config('feeds.services.twitterAccountId')) { try {
$this->setNewAccessToken(1);
$text = sprintf("Unser Dozent %s hat einen neuen Kurs-Termin eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig #gesundesgeld", $text = sprintf("Unser Dozent %s hat einen neuen Kurs-Termin eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig #gesundesgeld",
$courseEvent->course->lecturer->name, $courseEvent->course->lecturer->name,
$courseEvent->course->name, $courseEvent->course->name,
@@ -25,7 +25,9 @@ class CourseEventObserver
['country' => 'de', 'lecturer' => $courseEvent->course->lecturer]), ['country' => 'de', 'lecturer' => $courseEvent->course->lecturer]),
); );
$this->postTweet($text); $this->publishOnNostr($courseEvent, $text);
} catch (Exception $e) {
Log::error($e->getMessage());
} }
} }

View File

@@ -3,20 +3,20 @@
namespace App\Observers; namespace App\Observers;
use App\Models\Course; use App\Models\Course;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class CourseObserver class CourseObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the Course "created" event. * Handle the Course "created" event.
*/ */
public function created(Course $course): void public function created(Course $course): void
{ {
if (config('feeds.services.twitterAccountId')) { try {
$this->setNewAccessToken(1);
$text = sprintf("Unser Dozent %s hat einen neuen Kurs eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig #gesundesgeld", $text = sprintf("Unser Dozent %s hat einen neuen Kurs eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Kurs #Education #Einundzwanzig #gesundesgeld",
$course->lecturer->name, $course->lecturer->name,
$course->name, $course->name,
@@ -25,7 +25,9 @@ class CourseObserver
['country' => 'de', 'lecturer' => $course->lecturer]), ['country' => 'de', 'lecturer' => $course->lecturer]),
); );
$this->postTweet($text); $this->publishOnNostr($course, $text);
} catch (Exception $e) {
Log::error($e->getMessage());
} }
} }

View File

@@ -1,48 +0,0 @@
<?php
namespace App\Observers;
use App\Models\CourseEvent;
class EventObserver
{
/**
* Handle the Event "created" event.
*/
public function created(CourseEvent $event): void
{
//
}
/**
* Handle the Event "updated" event.
*/
public function updated(CourseEvent $event): void
{
//
}
/**
* Handle the Event "deleted" event.
*/
public function deleted(CourseEvent $event): void
{
//
}
/**
* Handle the Event "restored" event.
*/
public function restored(CourseEvent $event): void
{
//
}
/**
* Handle the Event "force deleted" event.
*/
public function forceDeleted(CourseEvent $event): void
{
//
}
}

View File

@@ -2,50 +2,41 @@
namespace App\Observers; namespace App\Observers;
use App\Enums\LibraryItemType;
use App\Models\LibraryItem; use App\Models\LibraryItem;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class LibraryItemObserver class LibraryItemObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the LibraryItem "created" event. * Handle the LibraryItem "created" event.
*/ */
public function created(LibraryItem $libraryItem): void public function created(LibraryItem $libraryItem): void
{ {
// todo: we can change this later
try { try {
$libraryItem->setStatus('published'); $libraryItem->setStatus('published');
$libraryItemName = $libraryItem->name; $libraryItemName = $libraryItem->name;
if ($libraryItem->lecturer->twitter_username && $libraryItem->type !== 'markdown_article') { $libraryItemName .= ' von '.$libraryItem->lecturer->name;
$libraryItemName .= ' von @'.$libraryItem->lecturer->twitter_username;
}
if (! $libraryItem->lecturer->twitter_username) {
$libraryItemName .= ' von '.$libraryItem->lecturer->name;
}
if (config('feeds.services.twitterAccountId')) { if ($libraryItem->type !== LibraryItemType::MarkdownArticle()) {
$this->setNewAccessToken(1); if ($libraryItem->whereDoesntHave('libraries',
fn($query) => $query->where('libraries.is_public', false))
// http://localhost/de/library/library-item?l=de&table[filters][id]=2 ->exists()) {
$text = sprintf("Es gibt was Neues zum Lesen oder Anhören:\n\n%s\n\n%s\n\n#Bitcoin #Wissen #Einundzwanzig #gesundesgeld",
if ($libraryItem->type !== 'markdown_article') { $libraryItemName,
if ($libraryItem->whereDoesntHave('libraries', url()->route('article.view',
fn ($query) => $query->where('libraries.is_public', false)) ['libraryItem' => $libraryItem->slug]),
->exists()) { );
$text = sprintf("Es gibt was Neues zum Lesen oder Anhören:\n\n%s\n\n%s\n\n#Bitcoin #Wissen #Einundzwanzig #gesundesgeld", $this->publishOnNostr($libraryItem, $text);
$libraryItemName,
url()->route('article.view',
['libraryItem' => $libraryItem->slug]),
);
$this->postTweet($text);
}
} }
} }
} catch (\Exception $e) { } catch (Exception $e) {
// todo: log this Log::error($e->getMessage());
} }
} }

View File

@@ -3,12 +3,13 @@
namespace App\Observers; namespace App\Observers;
use App\Models\MeetupEvent; use App\Models\MeetupEvent;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class MeetupEventObserver class MeetupEventObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the MeetupEvent "created" event. * Handle the MeetupEvent "created" event.
@@ -16,25 +17,19 @@ class MeetupEventObserver
public function created(MeetupEvent $meetupEvent): void public function created(MeetupEvent $meetupEvent): void
{ {
try { try {
if (config('feeds.services.twitterAccountId')) { $meetupName = $meetupEvent->meetup->name;
$this->setNewAccessToken(1); if ($meetupEvent->meetup->nostr) {
$meetupName .= ' @'.$meetupEvent->meetup->nostr;
$meetupName = $meetupEvent->meetup->name;
if ($meetupEvent->meetup->twitter_username) {
$meetupName .= ' @'.$meetupEvent->meetup->twitter_username;
}
$text = sprintf("%s hat einen neuen Termin eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Meetup #Einundzwanzig #gesundesgeld",
$meetupName,
$meetupEvent->start->asDateTime(),
$meetupEvent->location,
url()->route('meetup.event.landing',
['country' => 'de', 'meetupEvent' => $meetupEvent->id]),
);
$this->postTweet($text);
} }
} catch (\Exception $e) { $text = sprintf("%s hat einen neuen Termin eingestellt:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Meetup #Einundzwanzig #gesundesgeld",
$meetupName,
$meetupEvent->start->asDateTime(),
$meetupEvent->location,
url()->route('meetup.event.landing',
['country' => 'de', 'meetupEvent' => $meetupEvent->id]),
);
$this->publishOnNostr($meetupEvent, $text);
} catch (Exception $e) {
Log::error($e->getMessage()); Log::error($e->getMessage());
} }
} }

View File

@@ -3,12 +3,13 @@
namespace App\Observers; namespace App\Observers;
use App\Models\Meetup; use App\Models\Meetup;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class MeetupObserver class MeetupObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the Meetup "created" event. * Handle the Meetup "created" event.
@@ -16,22 +17,16 @@ class MeetupObserver
public function created(Meetup $meetup): void public function created(Meetup $meetup): void
{ {
try { try {
if (config('feeds.services.twitterAccountId')) { $meetupName = $meetup->name;
$this->setNewAccessToken(1); if ($meetup->nostr) {
$meetupName .= ' @'.$meetup->nostr;
$meetupName = $meetup->name;
if ($meetup->twitter_username) {
$meetupName .= ' @'.$meetup->twitter_username;
}
$text = sprintf("Eine neue Meetup Gruppe wurde hinzugefügt:\n\n%s\n\n%s\n\n#Bitcoin #Meetup #Einundzwanzig #gesundesgeld",
$meetupName,
url()->route('meetup.landing', ['country' => $meetup->city->country->code, 'meetup' => $meetup])
);
$this->postTweet($text);
} }
} catch (\Exception $e) { $text = sprintf("Eine neue Meetup Gruppe wurde hinzugefügt:\n\n%s\n\n%s\n\n#Bitcoin #Meetup #Einundzwanzig #gesundesgeld",
$meetupName,
url()->route('meetup.landing', ['country' => $meetup->city->country->code, 'meetup' => $meetup])
);
$this->publishOnNostr($meetup, $text);
} catch (Exception $e) {
Log::error($e->getMessage()); Log::error($e->getMessage());
} }
} }

View File

@@ -3,28 +3,29 @@
namespace App\Observers; namespace App\Observers;
use App\Models\OrangePill; use App\Models\OrangePill;
use App\Traits\TwitterTrait; use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class OrangePillObserver class OrangePillObserver
{ {
use TwitterTrait; use NostrTrait;
/** /**
* Handle the OrangePill "created" event. * Handle the OrangePill "created" event.
*/ */
public function created(OrangePill $orangePill): void public function created(OrangePill $orangePill): void
{ {
// if (config('feeds.services.twitterAccountId')) { try {
// $this->setNewAccessToken(1); $text = sprintf("Ein neues Bitcoin-Buch liegt nun in diesem öffentlichen Bücherschrank:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Education #Einundzwanzig #gesundesgeld",
// $orangePill->bookCase->title,
// $text = sprintf("Ein neues Bitcoin-Buch liegt nun in diesem öffentlichen Bücherschrank:\n\n%s\n\n%s\n\n%s\n\n#Bitcoin #Education #Einundzwanzig #gesundesgeld", $orangePill->bookCase->address,
// $orangePill->bookCase->title, url()->route('bookCases.comment.bookcase', ['country' => 'de', 'bookCase' => $orangePill->bookCase]),
// $orangePill->bookCase->address, );
// url()->route('bookCases.comment.bookcase', ['country' => 'de', 'bookCase' => $orangePill->bookCase]), $this->publishOnNostr($orangePill, $text);
// ); } catch (Exception $e) {
// Log::error($e->getMessage());
// $this->postTweet($text); }
// }
} }
/** /**

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

@@ -0,0 +1,27 @@
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Process;
trait NostrTrait
{
public function publishOnNostr($model, $text): array
{
//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()
];
}
}

View File

@@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('library_items', function (Blueprint $table) {
$table->renameColumn('nostr', 'nostr_status');
});
Schema::table('bitcoin_events', function (Blueprint $table) {
$table->text('nostr_status')
->nullable();
});
Schema::table('course_events', function (Blueprint $table) {
$table->text('nostr_status')
->nullable();
});
Schema::table('courses', function (Blueprint $table) {
$table->text('nostr_status')
->nullable();
});
Schema::table('meetup_events', function (Blueprint $table) {
$table->text('nostr_status')
->nullable();
});
Schema::table('meetups', function (Blueprint $table) {
$table->text('nostr_status')
->nullable();
});
Schema::table('orange_pills', function (Blueprint $table) {
$table->text('nostr_status')
->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('tables', function (Blueprint $table) {
//
});
}
};