first copies from portal

This commit is contained in:
fsociety
2024-09-04 19:37:46 +02:00
parent b38f3f8bed
commit a0ef037b2d
741 changed files with 78623 additions and 387 deletions

View File

@@ -0,0 +1,114 @@
<?php
namespace App\Console\Commands\Nostr;
use App\Models\Event;
use App\Traits\NostrEventRendererTrait;
use Illuminate\Console\Command;
use swentel\nostr\Filter\Filter;
use swentel\nostr\Message\RequestMessage;
use swentel\nostr\Relay\Relay;
use swentel\nostr\Relay\RelaySet;
use swentel\nostr\Request\Request;
use swentel\nostr\Subscription\Subscription;
class FetchEvents extends Command
{
use NostrEventRendererTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fetch:events';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$plebs = \App\Models\EinundzwanzigPleb::query()
->get();
$subscription = new Subscription();
$subscriptionId = $subscription->setId();
$filter1 = new Filter();
$filter1->setKinds([1]); // You can add multiple kind numbers
$filter1->setAuthors($plebs->pluck('pubkey')->toArray()); // You can add multiple authors
$filter1->setLimit(25); // Limit to fetch only a maximum of 25 events
$filters = [$filter1]; // You can add multiple filters.
$requestMessage = new RequestMessage($subscriptionId, $filters);
$relays = [
new Relay('wss://nostr.einundzwanzig.space'),
new Relay('wss://nostr.wine'),
new Relay('wss://nos.lol'),
];
$relaySet = new RelaySet();
$relaySet->setRelays($relays);
$request = new Request($relaySet, $requestMessage);
$response = $request->send();
$uniqueEvents = [];
foreach ($response as $relay => $events) {
foreach ($events as $event) {
if (!isset($uniqueEvents[$event->event->id])) {
$uniqueEvents[$event->event->id] = $event;
}
}
}
foreach ($uniqueEvents as $id => $uniqueEvent) {
$type = $this->isReplyOrRoot($uniqueEvent->event);
$parentEventId = $this->getParentEventId($uniqueEvent->event);
$event = Event::query()->updateOrCreate(
['event_id' => $id],
[
'pubkey' => $uniqueEvent->event->pubkey,
'parent_event_id' => $parentEventId,
'json' => json_encode($uniqueEvent->event, JSON_THROW_ON_ERROR),
'type' => $type,
]
);
$this->renderContentToHtml($event);
}
}
private function getParentEventId($event)
{
foreach ($event->tags as $tag) {
if ($tag[0] === 'e') {
if ((isset($tag[2]) && $tag[2] === '') || (isset($tag[3]) && $tag[3] === 'reply')) {
return $tag[1];
}
}
}
return null;
}
private function isReplyOrRoot($event)
{
foreach ($event->tags as $tag) {
if ($tag[0] === 'e') {
if ((isset($tag[3]) && $tag[3] === 'reply') || (!isset($tag[3]) && isset($tag[2]) && $tag[2] === '')) {
return 'reply';
}
}
}
return 'root';
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Console\Commands\Nostr;
use App\Traits\NostrEventRendererTrait;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Broadcast;
class RenderAllEvents extends Command
{
use NostrEventRendererTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'render';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$events = \App\Models\Event::query()
->get();
foreach ($events as $event) {
$this->renderContentToHtml($event);
}
Broadcast::on('events')
->as('newEvents')
->with([
'test' => 'test',
])
->sendNow();
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Console\Commands\Nostr;
use App\Models\EinundzwanzigPleb;
use App\Traits\NostrFetcherTrait;
use Illuminate\Console\Command;
use swentel\nostr\Subscription\Subscription;
class SyncProfiles extends Command
{
use NostrFetcherTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:profiles';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$plebs = EinundzwanzigPleb::query()
->whereDoesntHave('profile')
->get();
$this->fetchProfile($plebs->pluck('npub')->toArray());
}
}