topic_reply & hr_reached notification configurable

This commit is contained in:
xiaomlove
2022-11-25 21:04:27 +08:00
parent af67dd868b
commit e9d1f080c0
9 changed files with 130 additions and 61 deletions

View File

@@ -102,6 +102,7 @@ class User extends Authenticatable implements FilamentUser, HasName
'invites' => '邀请',
];
public static array $notificationOptions = ['topic_reply', 'hr_reached'];
public function getClassTextAttribute(): string
{
@@ -555,6 +556,14 @@ class User extends Authenticatable implements FilamentUser, HasName
return false;
}
public function acceptNotification($name): bool
{
if (!isset($this->original['notifs'])) {
throw new \RuntimeException("Not fetch field: notifs");
}
return str_contains($this->notifs, "[{$name}]");
}

View File

@@ -135,7 +135,7 @@ class HitAndRunRepository extends BaseRepository
->with([
'torrent' => function ($query) {$query->select(['id', 'size', 'name', 'category']);},
'snatch',
'user' => function ($query) {$query->select(['id', 'username', 'lang', 'class', 'donoruntil', 'enabled']);},
'user' => function ($query) {$query->select(['id', 'username', 'lang', 'class', 'donoruntil', 'enabled', 'notifs']);},
'user.language',
]);
if (!is_null($uid)) {
@@ -297,8 +297,12 @@ class HitAndRunRepository extends BaseRepository
do_log($hitAndRun->toJson() . ", [$logPrefix], affectedRows != 1, skip!", 'notice');
return false;
}
$message = $this->geReachedMessage($hitAndRun);
Message::query()->insert($message);
if ($hitAndRun->user->acceptNotification('hr_reached')) {
$message = $this->geReachedMessage($hitAndRun);
Message::query()->insert($message);
} else {
do_log($hitAndRun->toJson() . ", [$logPrefix], user do not accept hr_reached notification", 'notice');
}
return true;
}