2021-08-11 13:45:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-11-26 05:58:50 +00:00
|
|
|
use Carbon\Carbon;
|
2022-01-01 05:59:35 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2021-08-11 13:45:30 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2021-11-26 05:58:50 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-01-01 05:59:35 +00:00
|
|
|
|
|
|
|
use App\Traits\ScopeActive;
|
2021-08-11 13:45:30 +00:00
|
|
|
|
|
|
|
class Echoarea extends Model
|
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
use SoftDeletes,ScopeActive;
|
2021-08-11 13:45:30 +00:00
|
|
|
|
2021-11-26 05:58:50 +00:00
|
|
|
private const CACHE_TIME = 3600;
|
|
|
|
|
2021-08-11 13:45:30 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
|
|
|
public function addresses()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Address::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function domain()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Domain::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function echomail()
|
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
return $this->hasMany(Echomail::class)
|
|
|
|
->orderBy('datetime','ASC');
|
2021-08-11 13:45:30 +00:00
|
|
|
}
|
2021-11-26 05:58:50 +00:00
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
|
|
|
public function getLastMessageAttribute(): ?Carbon
|
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
return $this->echomail?->last()->datetime;
|
2021-11-26 05:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
public function messages_count(int $period): int
|
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
$key = sprintf('%s_%d_%d','echo_messages_count',$this->id,$period);
|
2021-11-26 05:58:50 +00:00
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
return Cache::remember($key,self::CACHE_TIME,function() use ($period) {
|
2021-11-26 05:58:50 +00:00
|
|
|
switch ($period) {
|
|
|
|
case 1: // day
|
|
|
|
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subDay())->count();
|
|
|
|
case 7: // week
|
|
|
|
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subWeek())->count();
|
|
|
|
case 30: // month
|
|
|
|
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subMonth())->count();
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-01 05:59:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of messages waiting for address
|
|
|
|
*
|
|
|
|
* @param Address $ao
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function waiting(Address $ao): Collection
|
|
|
|
{
|
|
|
|
return $this->echomail()
|
|
|
|
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
|
|
|
|
->whereNull('sent_at')
|
|
|
|
->whereNotNull('export_at')
|
|
|
|
->where('address_id',$ao->id)
|
|
|
|
->get();
|
|
|
|
}
|
2021-08-11 13:45:30 +00:00
|
|
|
}
|