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;
use App\Models\LibraryItem;
use App\Traits\TwitterTrait;
use Illuminate\Support\Facades\Process;
use App\Traits\NostrTrait;
use Livewire\Component;
use RalphJSmit\Laravel\SEO\Support\SEOData;
use WireUi\Traits\Actions;
@@ -12,62 +11,7 @@ use WireUi\Traits\Actions;
class ArticleOverview extends Component
{
use Actions;
use TwitterTrait;
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()));
}
}
use NostrTrait;
public function nostr($id)
{
@@ -88,20 +32,14 @@ class ArticleOverview extends Component
url()->route('article.view',
['libraryItem' => $libraryItem->slug]),
);
//noscl publish "Good morning!"
$result = Process::timeout(60 * 5)->run('noscl publish "'.$text.'"');
if ($result->successful()) {
$libraryItem->nostr = $result->output();
$libraryItem->save();
$result = $this->publishOnNostr($libraryItem, $text);
if ($result['success']) {
$this->notification()
->success(title: __('Published on Nostr'), description: $result->output());
}
if ($result->failed()) {
} else {
$this->notification()
->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;
use App\Models\BitcoinEvent;
use App\Traits\TwitterTrait;
use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class BitcoinEventObserver
{
use TwitterTrait;
use NostrTrait;
/**
* Handle the BitcoinEvent "created" event.
*/
public function created(BitcoinEvent $bitcoinEvent): void
{
if (config('feeds.services.twitterAccountId')) {
$this->setNewAccessToken(1);
try {
$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->from->asDateTime(),
@@ -24,8 +24,9 @@ class BitcoinEventObserver
$bitcoinEvent->venue->name,
$bitcoinEvent->link,
);
$this->postTweet($text);
$this->publishOnNostr($bitcoinEvent, $text);
} catch (Exception $e) {
Log::error($e->getMessage());
}
}

View File

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

View File

@@ -3,12 +3,13 @@
namespace App\Observers;
use App\Models\MeetupEvent;
use App\Traits\TwitterTrait;
use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class MeetupEventObserver
{
use TwitterTrait;
use NostrTrait;
/**
* Handle the MeetupEvent "created" event.
@@ -16,25 +17,19 @@ class MeetupEventObserver
public function created(MeetupEvent $meetupEvent): void
{
try {
if (config('feeds.services.twitterAccountId')) {
$this->setNewAccessToken(1);
$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);
$meetupName = $meetupEvent->meetup->name;
if ($meetupEvent->meetup->nostr) {
$meetupName .= ' @'.$meetupEvent->meetup->nostr;
}
} 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());
}
}

View File

@@ -3,12 +3,13 @@
namespace App\Observers;
use App\Models\Meetup;
use App\Traits\TwitterTrait;
use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class MeetupObserver
{
use TwitterTrait;
use NostrTrait;
/**
* Handle the Meetup "created" event.
@@ -16,22 +17,16 @@ class MeetupObserver
public function created(Meetup $meetup): void
{
try {
if (config('feeds.services.twitterAccountId')) {
$this->setNewAccessToken(1);
$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);
$meetupName = $meetup->name;
if ($meetup->nostr) {
$meetupName .= ' @'.$meetup->nostr;
}
} 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());
}
}

View File

@@ -3,28 +3,29 @@
namespace App\Observers;
use App\Models\OrangePill;
use App\Traits\TwitterTrait;
use App\Traits\NostrTrait;
use Exception;
use Illuminate\Support\Facades\Log;
class OrangePillObserver
{
use TwitterTrait;
use NostrTrait;
/**
* Handle the OrangePill "created" event.
*/
public function created(OrangePill $orangePill): void
{
// if (config('feeds.services.twitterAccountId')) {
// $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,
// $orangePill->bookCase->address,
// url()->route('bookCases.comment.bookcase', ['country' => 'de', 'bookCase' => $orangePill->bookCase]),
// );
//
// $this->postTweet($text);
// }
try {
$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,
$orangePill->bookCase->address,
url()->route('bookCases.comment.bookcase', ['country' => 'de', 'bookCase' => $orangePill->bookCase]),
);
$this->publishOnNostr($orangePill, $text);
} catch (Exception $e) {
Log::error($e->getMessage());
}
}
/**

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()
];
}
}