Loheide-eu-mitarbeiterplatt.../app/Digest/Digest.php
Friederich Loheide f9922f7308
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m19s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m29s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m39s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m38s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m25s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 7m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m4s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 46s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m9s
Ideenbox-Moderation und Wochenüberblick per E-Mail (0.36.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 15:17:47 +00:00

120 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Digest;
use App\Mail\WeeklyDigest;
use App\Models\User;
use App\Plugins\PluginAccess;
use App\Support\Settings;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Mail;
use Throwable;
/**
* Wochenüberblick per E-Mail. Plugins steuern Abschnitte bei:
*
* app(Digest::class)->add('news', 'Neuigkeiten', 10, fn (User $user, CarbonImmutable $since) => [
* ['title' => '…', 'url' => '…', 'meta' => '…', 'text' => '…'], …
* ]);
*
* Es gilt, was die Person im Portal sieht. Leere Abschnitte fallen weg; ist alles leer,
* geht keine E-Mail raus.
*/
class Digest
{
public const WEEKDAYS = [1 => 'Montag', 2 => 'Dienstag', 3 => 'Mittwoch', 4 => 'Donnerstag', 5 => 'Freitag', 6 => 'Samstag', 7 => 'Sonntag'];
/** @var array<string, array{label: string, sort: int, items: callable}> */
private array $sections = [];
public function __construct(private readonly Settings $settings) {}
public function add(string $key, string $label, int $sort, callable $items): void
{
$this->sections[$key] = ['label' => $label, 'sort' => $sort, 'items' => $items];
}
public function enabled(): bool
{
return (bool) $this->settings->get('digest.enabled', true);
}
public function weekday(): int
{
return (int) $this->settings->get('digest.weekday', 1);
}
public function hour(): int
{
return (int) $this->settings->get('digest.hour', 7);
}
/** „montags ab 7 Uhr“ */
public function scheduleLabel(): string
{
return mb_strtolower(self::WEEKDAYS[$this->weekday()]).'s ab '.$this->hour().' Uhr';
}
/**
* @return list<array{key: string, label: string, items: list<array<string, mixed>>}>
*/
public function build(User $user, CarbonImmutable $since): array
{
$access = app(PluginAccess::class);
return collect($this->sections)
->filter(fn ($section, $key) => $access->allows($key.'.digest', $user))
->sortBy('sort')
->map(function (array $section, string $key) use ($user, $since) {
try {
$items = array_values(($section['items'])($user, $since));
} catch (Throwable $e) {
report($e);
$items = [];
}
return ['key' => $key, 'label' => $section['label'], 'items' => $items];
})
->filter(fn (array $section) => $section['items'] !== [])
->values()->all();
}
/**
* Stündlich aufgerufen: am eingestellten Tag ab der eingestellten Stunde an alle,
* die ihn eingeschaltet haben – je Person höchstens einmal in sechs Tagen.
*/
public function sendDue(): int
{
$now = CarbonImmutable::now(config('app.timezone'));
if (! $this->enabled() || $now->dayOfWeekIso !== $this->weekday() || $now->hour < $this->hour()) {
return 0;
}
$sent = 0;
User::query()->active()->where('weekly_digest', true)->whereNotNull('email')
->where(fn ($q) => $q->whereNull('digest_sent_at')->orWhere('digest_sent_at', '<', $now->subDays(6)))
->chunkById(100, function ($users) use ($now, &$sent) {
foreach ($users as $user) {
$user->forceFill(['digest_sent_at' => $now])->saveQuietly();
$sent += $this->sendTo($user, $now->subDays(7)) ? 1 : 0;
}
});
return $sent;
}
public function sendTo(User $user, CarbonImmutable $since): bool
{
$sections = $this->build($user, $since);
if ($sections === []) {
return false;
}
Mail::to($user)->queue(new WeeklyDigest($user, $sections, $since));
return true;
}
}