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,95 @@
<?php
namespace App\Traits;
use App\Models\Event;
use App\Models\Profile;
use App\Models\RenderedEvent;
use swentel\nostr\Key\Key;
trait NostrEventRendererTrait
{
public function renderContentToHtml(Event $event): void
{
$content = json_decode($event->json, true, 512, JSON_THROW_ON_ERROR)['content'];
$profile = Profile::query()->where('pubkey', $event->pubkey)->first();
if ($profile && $profile->name) {
$name = $profile->name;
} elseif ($profile && !empty($profile->display_name)) {
$name = $profile->display_name;
} else {
$name = 'Anonymous';
}
$content = $this->nprofile1($content);
$content = $this->images($content);
$content = $this->youtube($content);
$content = $this->npub1($content);
RenderedEvent::query()->updateOrCreate([
'event_id' => $event->event_id,
], [
'html' => $content,
'profile_image' => $profile && $profile->picture !== '' ? $profile->picture : 'https://robohash.org/' . $profile->pubkey,
'profile_name' => $name,
]);
}
protected function images($content): string
{
// we need to find all image urls by looking for the extension
// and replace them with the img tag
$pattern = '/(https?:\/\/.*\.(?:png|jpg|jpeg|gif|webp))/';
$replacement = '<div class="w-96 group aspect-h-7 aspect-w-10"><img class="pointer-events-none object-cover" src="$1" alt="image" /></div>';
return preg_replace($pattern, $replacement, $content);
}
protected function npub1($content): string
{
// Pattern to match nostr:npub1 elements, optionally followed by a non-alphanumeric character
$pattern = '/(nostr:npub1[a-zA-Z0-9]+)(\W?)/';
// find all matches of the pattern
preg_match_all($pattern, $content, $matches);
// loop through all matches
foreach ($matches[1] as $match) {
$pubkey = (new Key)->convertToHex(str($match)->after('nostr:'));
$profile = Profile::query()->where('pubkey', $pubkey)->first();
if ($profile && $profile->name) {
$name = $profile->name;
} elseif ($profile && !empty($profile->display_name)) {
$name = $profile->display_name;
} else {
$name = 'Anonymous';
}
// replace the match with the profile name
$content = str_replace($match, $name, $content);
}
return $content;
}
protected function nprofile1($content): string
{
// todo: implement this
return $content;
}
protected function youtube($content): string
{
// Pattern to match YouTube short URLs like https://youtu.be/ddvHagjmRJY?feature=shared
$pattern1 = '/https:\/\/youtu.be\/([a-zA-Z0-9-_]+)\??.*/';
$replacement1 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
// Pattern to match YouTube long URLs like https://www.youtube.com/watch?v=tiNZoDBGhdo
$pattern2 = '/https:\/\/www.youtube.com\/watch\?v=([a-zA-Z0-9-_]+)\??.*/';
$replacement2 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
// Replace both patterns in the content
$content = preg_replace($pattern1, $replacement1, $content);
$content = preg_replace($pattern2, $replacement2, $content);
return $content;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Traits;
use App\Models\Profile;
use swentel\nostr\Filter\Filter;
use swentel\nostr\Key\Key;
use swentel\nostr\Message\RequestMessage;
use swentel\nostr\Relay\Relay;
use swentel\nostr\Request\Request;
use swentel\nostr\Subscription\Subscription;
trait NostrFetcherTrait
{
public function fetchProfile($npubs)
{
$hex = collect([]);
foreach ($npubs as $item) {
$hex->push([
'hex' => (new Key)->convertToHex($item),
'npub' => $item,
]);
}
$subscription = new Subscription();
$subscriptionId = $subscription->setId();
$filter1 = new Filter();
$filter1->setKinds([0]); // You can add multiple kind numbers
$filter1->setAuthors($hex->pluck('hex')->toArray()); // You can add multiple author ids
$filters = [$filter1]; // You can add multiple filters.
$requestMessage = new RequestMessage($subscriptionId, $filters);
$relayUrl = 'wss://relay.nostr.band/';
$relay = new Relay($relayUrl);
$relay->setMessage($requestMessage);
$request = new Request($relay, $requestMessage);
$response = $request->send();
foreach ($response['wss://relay.nostr.band/'] as $item) {
try {
$result = json_decode($item->event->content, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException('Error decoding JSON: ' . $e->getMessage());
}
Profile::query()->updateOrCreate(
['pubkey' => $item->event->pubkey],
[
'name' => $result['name'] ?? null,
'display_name' => $result['display_name'] ?? null,
'picture' => $result['picture'] ?? null,
'banner' => $result['banner'] ?? null,
'website' => $result['website'] ?? null,
'about' => $result['about'] ?? null,
'nip05' => $result['nip05'] ?? null,
'lud16' => $result['lud16'] ?? null,
'lud06' => $result['lud06'] ?? null,
'deleted' => $result['deleted'] ?? false,
]
);
}
}
}