clrghouz/app/Models/Echoarea.php

120 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2021-08-11 13:45:30 +00:00
<?php
namespace App\Models;
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;
2022-01-01 05:59:35 +00:00
2023-07-29 03:17:36 +00:00
use App\Traits\{AreaSecurity,ScopeActive};
/**
* Echomail echoareas
*
* Security thoughts:
* + ZCs/RCs/NCs/HUBs carry all echos
* + echos for HUBs only (not NODES/POINTs, thus Hub/NC/RC/ZC)
* + echos for NCs only (NC/RC/ZC)
* + echos for RCs only (RC/ZC)
* YYRRRWWW
*
* Thus YY:
* + 0 - not exported
* + 1 - Sent to RCs (RW)
* + 2 - Sent to NCs as well (RW)
* + 3 - Sent to Hubs as well (RW)
*
* Thus RRR: (Read)
* + 0-7
* = 0 no read access
* = 1-7 minimum access required to perform
* Thus WWW: (Write)
* + 0-7
* = 0 no write access
* = 1-7 minimum access required to perform
*
* - If a node has 0, or an echoarea has 0, then no access to the function
* - So if node has 1, and echoarea has 2, no access to function
*
* @note change "public" to "bot posts"?
*/
2021-08-11 13:45:30 +00:00
class Echoarea extends Model
{
use SoftDeletes,ScopeActive,AreaSecurity;
2021-08-11 13:45:30 +00:00
private const CACHE_TIME = 3600;
protected $casts = [
'first_message' => 'datetime:Y-m-d H:i:s',
'last_message' => 'datetime:Y-m-d H:i:s',
];
2021-08-11 13:45:30 +00:00
/* RELATIONS */
public function addresses()
{
return $this->belongsToMany(Address::class);
}
public function addresses_active()
{
return $this->belongsToMany(Address::class);
}
2021-08-11 13:45:30 +00:00
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
}
/* METHODS */
public function messages_count(int $period=NULL): int
{
$eo = Echomail::where('echoarea_id',$this->id);
$dt = Carbon::now()->startOfday();
switch ($period) {
case 1: // day
$eo->where('datetime','>=',$dt->subDay());
break;
case 7: // week
$eo->where('datetime','>=',$dt->subWeek());
break;
case 30: // month
$eo->where('datetime','>=',$dt->subMonth());
break;
case NULL: // all
break;
default:
return 0;
}
return $eo->count();
}
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
}