Initial commit
This commit is contained in:
23
app/Observers/BankObserver.php
Normal file
23
app/Observers/BankObserver.php
Normal 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
app/Observers/CallObserver.php
Normal file
15
app/Observers/CallObserver.php
Normal 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
app/Observers/ConversationObserver.php
Normal file
22
app/Observers/ConversationObserver.php
Normal 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
app/Observers/PostObserver.php
Normal file
19
app/Observers/PostObserver.php
Normal 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
app/Observers/ScoutReindexObserver.php
Normal file
102
app/Observers/ScoutReindexObserver.php
Normal 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);
|
||||
}
|
||||
}
|
||||
33
app/Observers/TransactionObserver.php
Normal file
33
app/Observers/TransactionObserver.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Transaction;
|
||||
use App\Services\CallCreditService;
|
||||
|
||||
class TransactionObserver
|
||||
{
|
||||
/**
|
||||
* After a transaction is created, check if the spending profile
|
||||
* still has sufficient credits. If not, pause all their active calls.
|
||||
*/
|
||||
public function created(Transaction $transaction): void
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user