Snapshot of main at 9f39a6e1e (2026-11-02)

This commit is contained in:
Ronald Huynen committed 2026-11-02 10:41:47 +01:00
commit 6ccf1792d7
2580 files changed
+1040202

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Observers;
use App\Models\Bank;
class BankObserver
{
/**
* Handle the Bank "created" event.
* Automatically register new Banks as love reacters and reactables.
*/
public function created(Bank $bank): void
{
if (!$bank->isRegisteredAsLoveReacter()) {
$bank->registerAsLoveReacter();
}
if (!$bank->isRegisteredAsLoveReactant()) {
$bank->registerAsLoveReactant();
}
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace App\Observers;
use App\Models\Call;
class CallObserver
{
public function created(Call $call): void
{
if (!$call->isRegisteredAsLoveReactant()) {
$call->registerAsLoveReactant();
}
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace App\Observers;
use Namu\WireChat\Models\Conversation;
class ConversationObserver
{
/**
* Handle the Conversation "created" event.
*/
public function created(Conversation $conversation): void
{
// Automatically set disappearing fields (always enabled to prevent orphaned messages)
$durationInDays = timebank_config('wirechat.disappearing_messages.duration', 30);
$durationInSeconds = $durationInDays * 86400; // Convert days to seconds
$conversation->disappearing_started_at = now();
$conversation->disappearing_duration = $durationInSeconds;
$conversation->save();
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Observers;
use App\Models\Post;
class PostObserver
{
/**
* Handle the Post "created" event.
*/
public function created(Post $post): void
{
// Register as Reactant when Post is created
if (!$post->isRegisteredAsLoveReactant()) {
$post->registerAsLoveReactant();
}
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
// Create this file: app/Observers/ScoutReindexObserver.php
namespace App\Observers;
use Illuminate\Support\Facades\Log;
class ScoutReindexObserver
{
/**
* Handle the model "created" event.
*/
public function created($model)
{
$this->reindexModel($model, 'created');
}
/**
* Handle the model "updated" event.
*/
public function updated($model)
{
$this->reindexModel($model, 'updated');
}
/**
* Handle the model "deleted" event.
*/
public function deleted($model)
{
// For deleted models, remove from search index
if (method_exists($model, 'unsearchable')) {
$model->unsearchable();
Log::info('Observer: Removed from search index', [
'model' => get_class($model),
'id' => $model->id,
'event' => 'deleted'
]);
}
}
/**
* Handle the model "restored" event.
*/
public function restored($model)
{
$this->reindexModel($model, 'restored');
}
/**
* Reindex the model if it uses Scout
*/
private function reindexModel($model, $event)
{
// Check if this model uses Scout
if (method_exists($model, 'searchable') && method_exists($model, 'searchableAs')) {
// Skip reindexing if only timestamp was updated (to avoid infinite loops)
if ($event === 'updated' && $this->onlyTimestampsChanged($model)) {
return;
}
try {
$model->searchable();
Log::info('Observer: Model reindexed', [
'model' => get_class($model),
'id' => $model->id,
'event' => $event
]);
} catch (\Exception $e) {
Log::error('Observer: Failed to reindex model', [
'model' => get_class($model),
'id' => $model->id,
'event' => $event,
'error' => $e->getMessage()
]);
}
}
}
/**
* Check if only timestamps were changed (to avoid infinite reindexing loops)
*/
private function onlyTimestampsChanged($model)
{
if (!$model->isDirty()) {
return false;
}
$dirty = $model->getDirty();
$timestampFields = ['updated_at', 'created_at'];
// Remove timestamp fields from dirty array
$nonTimestampChanges = array_diff_key($dirty, array_flip($timestampFields));
// If only timestamps changed, return true
return empty($nonTimestampChanges);
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace App\Observers;
use App\Models\Transaction;
use App\Services\CallCreditService;
use Illuminate\Support\Facades\Cache;
class TransactionObserver
{
/**
* After a transaction is created, bust balance caches and check credits.
*/
public function created(Transaction $transaction): void
{
$this->bustBalanceCaches($transaction);
$fromAccount = $transaction->accountFrom;
if (!$fromAccount) {
return;
}
$accountable = $fromAccount->accountable;
if (!$accountable) {
return;
}
$profileType = get_class($accountable);
$profileId = $accountable->id;
if (!CallCreditService::profileHasCredits($profileType, $profileId)) {
CallCreditService::pauseAllActiveCalls($profileType, $profileId);
}
}
/**
* Bust account_balance and accounts_info caches for both sides of the transaction.
* This is the SINGLE SOURCE OF TRUTH for cache invalidation.
*/
private function bustBalanceCaches(Transaction $transaction): void
{
$accounts = collect([
$transaction->from_account_id ? $transaction->accountFrom : null,
$transaction->to_account_id ? $transaction->accountTo : null,
])->filter();
$clearedCaches = [];
foreach ($accounts as $account) {
// Bust per-account balance cache
$balanceKey = "account_balance_{$account->id}";
Cache::forget($balanceKey);
$clearedCaches[] = $balanceKey;
// Bust accounts_info cache for the owning profile
$accountable = $account->accountable;
if ($accountable) {
$profileType = get_class($accountable);
$profileId = $accountable->id;
$accountsInfoKey = "accounts_info_{$profileType}_{$profileId}";
Cache::forget($accountsInfoKey);
$clearedCaches[] = $accountsInfoKey;
}
}
\Log::info('[TRANSACTION-OBSERVER] Caches cleared on transaction creation', [
'transactionId' => $transaction->id,
'clearedCaches' => $clearedCaches,
]);
}
}