mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2026-01-26 05:23:19 +00:00
🗑️ Remove election-related blade files no longer in use
This commit is contained in:
171
app/Livewire/Association/Election/Admin.php
Normal file
171
app/Livewire/Association/Election/Admin.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Association\Election;
|
||||
|
||||
use App\Models\Election;
|
||||
use Livewire\Component;
|
||||
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;
|
||||
|
||||
final class Admin extends Component
|
||||
{
|
||||
public bool $isAllowed = false;
|
||||
|
||||
public ?string $currentPubkey = null;
|
||||
|
||||
public ?array $votes = null;
|
||||
|
||||
public ?array $boardVotes = null;
|
||||
|
||||
public ?array $events = null;
|
||||
|
||||
public ?array $boardEvents = null;
|
||||
|
||||
public ?Election $election = null;
|
||||
|
||||
public string $signThisEvent = '';
|
||||
|
||||
public array $plebs = [];
|
||||
|
||||
public array $electionConfig = [];
|
||||
|
||||
protected $listeners = [
|
||||
'nostrLoggedOut' => 'handleNostrLoggedOut',
|
||||
'nostrLoggedIn' => 'handleNostrLoggedIn',
|
||||
'echo:votes,.newVote' => 'handleNewVote',
|
||||
];
|
||||
|
||||
public function mount(Election $election): void
|
||||
{
|
||||
$this->election = $election;
|
||||
$this->loadEvents();
|
||||
$this->loadBoardEvents();
|
||||
$this->loadVotes();
|
||||
$this->loadBoardVotes();
|
||||
}
|
||||
|
||||
public function handleNostrLoggedOut(): void
|
||||
{
|
||||
$this->currentPubkey = null;
|
||||
$this->currentPleb = null;
|
||||
}
|
||||
|
||||
public function handleNostrLoggedIn($pubkey): void
|
||||
{
|
||||
$this->currentPubkey = $pubkey;
|
||||
$allowedPubkeys = [
|
||||
'0adf67475ccc5ca456fd3022e46f5d526eb0af6284bf85494c0dd7847f3e5033',
|
||||
'430169631f2f0682c60cebb4f902d68f0c71c498fd1711fd982f052cf1fd4279',
|
||||
];
|
||||
if (in_array($this->currentPubkey, $allowedPubkeys, true)) {
|
||||
$this->isAllowed = true;
|
||||
}
|
||||
dd($this->isAllowed);
|
||||
}
|
||||
|
||||
public function handleNewVote(): void
|
||||
{
|
||||
$this->loadEvents();
|
||||
$this->loadBoardEvents();
|
||||
$this->loadVotes();
|
||||
$this->loadBoardVotes();
|
||||
}
|
||||
|
||||
public function loadVotes(): void
|
||||
{
|
||||
$this->votes = collect($this->events)
|
||||
->map(fn ($event) => [
|
||||
'created_at' => $event['created_at'],
|
||||
'pubkey' => $event['pubkey'],
|
||||
'forpubkey' => $this->fetchProfile($event['content']),
|
||||
'type' => str($event['content'])->after(',')->toString(),
|
||||
])
|
||||
->sortByDesc('created_at')
|
||||
->unique(fn ($event) => $event['pubkey'].$event['type'])
|
||||
->values()
|
||||
->groupBy('type')
|
||||
->map(fn ($votes) => [
|
||||
'type' => $votes[0]['type'],
|
||||
'votes' => $votes->groupBy('forpubkey')->map(fn ($group) => ['count' => $group->count()])->toArray(),
|
||||
])
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function loadBoardVotes(): void
|
||||
{
|
||||
$this->boardVotes = collect($this->boardEvents)
|
||||
->map(fn ($event) => [
|
||||
'created_at' => $event['created_at'],
|
||||
'pubkey' => $event['pubkey'],
|
||||
'forpubkey' => $this->fetchProfile($event['content']),
|
||||
'type' => str($event['content'])->after(',')->toString(),
|
||||
])
|
||||
->sortByDesc('created_at')
|
||||
->values()
|
||||
->groupBy('type')
|
||||
->map(fn ($votes) => [
|
||||
'type' => $votes[0]['type'],
|
||||
'votes' => $votes->groupBy('forpubkey')->map(fn ($group) => ['count' => $group->count()])->toArray(),
|
||||
])
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function loadEvents(): void
|
||||
{
|
||||
$this->events = $this->loadNostrEvents([32122]);
|
||||
}
|
||||
|
||||
public function loadBoardEvents(): void
|
||||
{
|
||||
$this->boardEvents = $this->loadNostrEvents([2121]);
|
||||
}
|
||||
|
||||
public function fetchProfile($content): string
|
||||
{
|
||||
$pubkey = str($content)->before(',')->toString();
|
||||
$profile = \App\Models\Profile::query()->where('pubkey', $pubkey)->first();
|
||||
if (! $profile) {
|
||||
\Artisan::call(\App\Console\Commands\Nostr\FetchProfile::class, ['--pubkey' => $pubkey]);
|
||||
$profile = \App\Models\Profile::query()->where('pubkey', $pubkey)->first();
|
||||
}
|
||||
|
||||
return $profile->pubkey;
|
||||
}
|
||||
|
||||
public function loadNostrEvents($kinds): array
|
||||
{
|
||||
$subscription = new Subscription;
|
||||
$subscriptionId = $subscription->setId();
|
||||
$filter = new Filter;
|
||||
$filter->setKinds($kinds);
|
||||
$requestMessage = new RequestMessage($subscriptionId, [$filter]);
|
||||
$relaySet = new RelaySet;
|
||||
$relaySet->setRelays([new Relay(config('services.relay'))]);
|
||||
$request = new Request($relaySet, $requestMessage);
|
||||
$response = $request->send();
|
||||
|
||||
return collect($response[config('services.relay')])
|
||||
->map(function ($event) {
|
||||
if (! isset($event->event)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $event->event->id,
|
||||
'kind' => $event->event->kind,
|
||||
'content' => $event->event->content,
|
||||
'pubkey' => $event->event->pubkey,
|
||||
'tags' => $event->event->tags,
|
||||
'created_at' => $event->event->created_at,
|
||||
];
|
||||
})
|
||||
->filter()
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
71
app/Livewire/Association/Election/Index.php
Normal file
71
app/Livewire/Association/Election/Index.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Association\Election;
|
||||
|
||||
use App\Models\EinundzwanzigPleb;
|
||||
use App\Models\Election;
|
||||
use App\Support\NostrAuth;
|
||||
use Livewire\Component;
|
||||
|
||||
final class Index extends Component
|
||||
{
|
||||
public bool $isAllowed = false;
|
||||
|
||||
public ?string $currentPubkey = null;
|
||||
|
||||
public ?EinundzwanzigPleb $currentPleb = null;
|
||||
|
||||
public array $elections = [];
|
||||
|
||||
protected $listeners = [
|
||||
'nostrLoggedOut' => 'handleNostrLoggedOut',
|
||||
'nostrLoggedIn' => 'handleNostrLoggedIn',
|
||||
];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->elections = Election::query()
|
||||
->get()
|
||||
->toArray();
|
||||
if (NostrAuth::check()) {
|
||||
$this->currentPubkey = NostrAuth::pubkey();
|
||||
$logPubkeys = [
|
||||
'0adf67475ccc5ca456fd3022e46f5d526eb0af6284bf85494c0dd7847f3e5033',
|
||||
'430169631f2f0682c60cebb4f902d68f0c71c498fd1711fd982f052cf1fd4279',
|
||||
];
|
||||
if (in_array($this->currentPubkey, $logPubkeys, true)) {
|
||||
$this->isAllowed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function handleNostrLoggedOut(): void
|
||||
{
|
||||
$this->isAllowed = false;
|
||||
$this->currentPubkey = null;
|
||||
$this->currentPleb = null;
|
||||
}
|
||||
|
||||
public function handleNostrLoggedIn($pubkey): void
|
||||
{
|
||||
NostrAuth::login($pubkey);
|
||||
$this->currentPubkey = $pubkey;
|
||||
$this->currentPleb = \App\Models\EinundzwanzigPleb::query()
|
||||
->where('pubkey', $pubkey)->first();
|
||||
$logPubkeys = [
|
||||
'0adf67475ccc5ca456fd3022e46f5d526eb0af6284bf85494c0dd7847f3e5033',
|
||||
'430169631f2f0682c60cebb4f902d68f0c71c498fd1711fd982f052cf1fd4279',
|
||||
];
|
||||
if (in_array($this->currentPubkey, $logPubkeys, true)) {
|
||||
$this->isAllowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function saveElection($index): void
|
||||
{
|
||||
$election = $this->elections[$index];
|
||||
$electionModel = Election::find($election['id']);
|
||||
$electionModel->candidates = $election['candidates'];
|
||||
$electionModel->save();
|
||||
}
|
||||
}
|
||||
186
app/Livewire/Association/Election/Show.php
Normal file
186
app/Livewire/Association/Election/Show.php
Normal file
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Association\Election;
|
||||
|
||||
use App\Models\EinundzwanzigPleb;
|
||||
use App\Models\Election;
|
||||
use App\Support\NostrAuth;
|
||||
use Livewire\Component;
|
||||
use swentel\nostr\Event\Event as NostrEvent;
|
||||
use swentel\nostr\Filter\Filter;
|
||||
use swentel\nostr\Message\EventMessage;
|
||||
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;
|
||||
|
||||
final class Show extends Component
|
||||
{
|
||||
public bool $isAllowed = false;
|
||||
|
||||
public bool $showLog = false;
|
||||
|
||||
public ?string $currentPubkey = null;
|
||||
|
||||
public ?EinundzwanzigPleb $currentPleb = null;
|
||||
|
||||
public array $events = [];
|
||||
|
||||
public array $boardEvents = [];
|
||||
|
||||
public Election $election;
|
||||
|
||||
public array $plebs = [];
|
||||
|
||||
public string $search = '';
|
||||
|
||||
public string $signThisEvent = '';
|
||||
|
||||
public bool $isNotClosed = true;
|
||||
|
||||
protected $listeners = [
|
||||
'nostrLoggedIn' => 'handleNostrLoggedIn',
|
||||
'nostrLoggedOut' => 'handleNostrLoggedOut',
|
||||
'echo:votes,.newVote' => 'handleNewVote',
|
||||
];
|
||||
|
||||
public function mount(Election $election): void
|
||||
{
|
||||
$this->election = $election;
|
||||
$this->plebs = EinundzwanzigPleb::query()
|
||||
->with(['profile'])
|
||||
->whereIn('association_status', [3, 4])
|
||||
->orderBy('association_status', 'desc')
|
||||
->get()
|
||||
->toArray();
|
||||
$this->loadEvents();
|
||||
$this->loadBoardEvents();
|
||||
if ($this->election->end_time?->isPast() || ! config('services.voting')) {
|
||||
$this->isNotClosed = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedSearch($value): void
|
||||
{
|
||||
$this->plebs = EinundzwanzigPleb::query()
|
||||
->with(['profile'])
|
||||
->whereIn('association_status', [3, 4])
|
||||
->where(fn ($query) => $query
|
||||
->where('pubkey', 'like', "%$value%")
|
||||
->orWhereHas('profile', fn ($query) => $query->where('name', 'ilike', "%$value%")))
|
||||
->orderBy('association_status', 'desc')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function handleNostrLoggedIn($pubkey): void
|
||||
{
|
||||
NostrAuth::login($pubkey);
|
||||
$this->currentPubkey = $pubkey;
|
||||
$this->currentPleb = EinundzwanzigPleb::query()->where('pubkey', $pubkey)->first();
|
||||
$logPubkeys = [
|
||||
'0adf67475ccc5ca456fd3022e46f5d526eb0af6284bf85494c0dd7847f3e5033',
|
||||
'430169631f2f0682c60cebb4f902d68f0c71c498fd1711fd982f052cf1fd4279',
|
||||
];
|
||||
if (in_array($this->currentPubkey, $logPubkeys, true)) {
|
||||
$this->showLog = true;
|
||||
$this->isAllowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function handleNostrLoggedOut(): void
|
||||
{
|
||||
$this->isAllowed = false;
|
||||
$this->currentPubkey = null;
|
||||
$this->currentPleb = null;
|
||||
}
|
||||
|
||||
public function handleNewVote(): void
|
||||
{
|
||||
$this->loadEvents();
|
||||
$this->loadBoardEvents();
|
||||
}
|
||||
|
||||
public function loadEvents(): void
|
||||
{
|
||||
$this->events = $this->loadNostrEvents([32122]);
|
||||
}
|
||||
|
||||
public function loadBoardEvents(): void
|
||||
{
|
||||
$this->boardEvents = $this->loadNostrEvents([2121]);
|
||||
}
|
||||
|
||||
public function loadNostrEvents($kinds): array
|
||||
{
|
||||
$subscription = new Subscription;
|
||||
$subscriptionId = $subscription->setId();
|
||||
$filter = new Filter;
|
||||
$filter->setKinds($kinds);
|
||||
$requestMessage = new RequestMessage($subscriptionId, [$filter]);
|
||||
$relaySet = new RelaySet;
|
||||
$relaySet->setRelays([new Relay(config('services.relay'))]);
|
||||
$request = new Request($relaySet, $requestMessage);
|
||||
$response = $request->send();
|
||||
|
||||
return collect($response[config('services.relay')])
|
||||
->map(function ($event) {
|
||||
if (! isset($event->event)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $event->event->id,
|
||||
'kind' => $event->event->kind,
|
||||
'content' => $event->event->content,
|
||||
'pubkey' => $event->event->pubkey,
|
||||
'tags' => $event->event->tags,
|
||||
'created_at' => $event->event->created_at,
|
||||
];
|
||||
})
|
||||
->filter()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function vote($pubkey, $type, $board = false): void
|
||||
{
|
||||
if ($this->election->end_time?->isPast()) {
|
||||
$this->isNotClosed = false;
|
||||
|
||||
return;
|
||||
}
|
||||
$note = new NostrEvent;
|
||||
$note->setKind($board ? 2121 : 32122);
|
||||
if (! $board) {
|
||||
$dTag = sprintf('%s,%s,%s', $this->currentPleb->pubkey, date('Y'), $type);
|
||||
$note->setTags([['d', $dTag]]);
|
||||
}
|
||||
$note->setContent("$pubkey,$type");
|
||||
$this->signThisEvent = $note->toJson();
|
||||
}
|
||||
|
||||
public function checkElection(): void
|
||||
{
|
||||
if ($this->election->end_time?->isPast()) {
|
||||
$this->isNotClosed = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function signEvent($event): void
|
||||
{
|
||||
$note = new NostrEvent;
|
||||
$note->setId($event['id']);
|
||||
$note->setSignature($event['sig']);
|
||||
$note->setKind($event['kind']);
|
||||
$note->setContent($event['content']);
|
||||
$note->setPublicKey($event['pubkey']);
|
||||
$note->setTags($event['tags']);
|
||||
$note->setCreatedAt($event['created_at']);
|
||||
$eventMessage = new EventMessage($note);
|
||||
$relay = new Relay(config('services.relay'));
|
||||
$relay->setMessage($eventMessage);
|
||||
$relay->send();
|
||||
\App\Support\Broadcast::on('votes')->as('newVote')->sendNow();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user