refactor: forum notification triggers

This commit is contained in:
Roardom
2024-02-13 22:50:57 +00:00
parent 9fe4abd3b1
commit 7490a7a39e
5 changed files with 70 additions and 92 deletions

View File

@@ -28,6 +28,7 @@ use App\Achievements\UserMadeFirstPost;
use App\Models\Post;
use App\Models\Topic;
use App\Models\User;
use App\Notifications\NewPost;
use App\Notifications\NewPostTag;
use App\Repositories\ChatRepository;
use Illuminate\Http\Request;
@@ -104,16 +105,41 @@ class PostController extends Controller
$profileUrl = sprintf('%s/users/%s', $appUrl, $user->username);
if (config('other.staff-forum-notify') && ($forum->id == config('other.staff-forum-id') || $forum->forum_category_id == config('other.staff-forum-id'))) {
$topic->notifyStaffers($user, $topic, $post);
$staffers = User::query()
->where('id', '!=', $user->id)
->whereRelation('group', 'is_modo', '=', true)
->get();
foreach ($staffers as $staffer) {
$staffer->notify(new NewPost('staff', $user, $post));
}
} else {
$this->chatRepository->systemMessage(sprintf('[url=%s]%s[/url] has left a reply on topic [url=%s]%s[/url]', $profileUrl, $user->username, $postUrl, $topic->name));
// Notify All Subscribers Of New Reply
if ($topic->first_post_user_id != $user->id) {
$topic->notifyStarter($user, $topic, $post);
if ($topic->first_post_user_id !== $user->id && $user->acceptsNotification(auth()->user(), $user, 'forum', 'show_forum_topic')) {
$user->notify(new NewPost('topic', $user, $post));
}
$topic->notifySubscribers($user, $topic, $post);
$subscribers = User::query()
->where('id', '!=', $user->id)
->whereRelation('subscriptions', 'topic_id', '=', $topic->id)
->whereRelation('forumPermissions', [
['read_topic', '=', 1],
['forum_id', '=', $topic->forum_id],
])
->where(
fn ($query) => $query
->whereRelation('notification', 'show_subscription_topic', '=', true)
->orDoesntHave('notification')
)
->get();
foreach ($subscribers as $subscriber) {
if ($subscriber->acceptsNotification($user, $subscriber, 'subscription', 'show_subscription_topic')) {
$subscriber->notify(new NewPost('subscription', $user, $post));
}
}
// Achievements
$user->unlock(new UserMadeFirstPost());

View File

@@ -29,6 +29,8 @@ use App\Models\Forum;
use App\Models\Post;
use App\Models\Subscription;
use App\Models\Topic;
use App\Models\User;
use App\Notifications\NewTopic;
use App\Repositories\ChatRepository;
use Illuminate\Http\Request;
use Exception;
@@ -151,10 +153,36 @@ class TopicController extends Controller
$profileUrl = sprintf('%s/users/%s', $appUrl, $user->username);
if (config('other.staff-forum-notify') && ($forum->id == config('other.staff-forum-id') || $forum->forum_category_id == config('other.staff-forum-id'))) {
$forum->notifyStaffers($user, $topic);
$staffers = User::query()
->where('id', '!=', $user->id)
->whereRelation('group', 'is_modo', '=', true)
->get();
foreach ($staffers as $staffer) {
$staffer->notify(new NewTopic('staff', $user, $topic));
}
} else {
$this->chatRepository->systemMessage(sprintf('[url=%s]%s[/url] has created a new topic [url=%s]%s[/url]', $profileUrl, $user->username, $topicUrl, $topic->name));
$forum->notifySubscribers($user, $topic);
$subscribers = User::query()
->where('id', '!=', $topic->first_post_user_id)
->whereRelation('subscriptions', 'forum_id', '=', $topic->forum_id)
->whereRelation('forumPermissions', [
['read_topic', '=', 1],
['forum_id', '=', $topic->forum_id],
])
->where(
fn ($query) => $query
->whereRelation('notification', 'show_subscription_forum', '=', true)
->orDoesntHave('notification')
)
->get();
foreach ($subscribers as $subscriber) {
if ($subscriber->acceptsNotification($user, $subscriber, 'subscription', 'show_subscription_forum')) {
$subscriber->notify(new NewTopic('forum', $user, $topic));
}
}
//Achievements
$user->unlock(new UserMadeFirstPost());

View File

@@ -13,7 +13,6 @@
namespace App\Models;
use App\Notifications\NewTopic;
use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -138,41 +137,6 @@ class Forum extends Model
return $this->belongsToMany(User::class, Subscription::class);
}
/**
* Notify Subscribers Of A Forum When New Topic Is Made.
*/
public function notifySubscribers(User $poster, Topic $topic): void
{
$subscribers = User::selectRaw('distinct(users.id),max(users.username) as username,max(users.group_id) as group_id')->with('group')->where('users.id', '!=', $topic->first_post_user_id)
->join('subscriptions', 'subscriptions.user_id', '=', 'users.id')
->leftJoin('user_notifications', 'user_notifications.user_id', '=', 'users.id')
->where('subscriptions.forum_id', '=', $topic->forum_id)
->whereRaw('(user_notifications.show_subscription_forum = 1 OR user_notifications.show_subscription_forum is null)')
->groupBy('users.id')->get();
foreach ($subscribers as $subscriber) {
if ($subscriber->acceptsNotification($poster, $subscriber, 'subscription', 'show_subscription_forum')) {
$subscriber->notify(new NewTopic('forum', $poster, $topic));
}
}
}
/**
* Notify Staffers When New Staff Topic Is Made.
*/
public function notifyStaffers(User $poster, Topic $topic): void
{
$staffers = User::leftJoin('groups', 'users.group_id', '=', 'groups.id')
->select('users.id')
->where('users.id', '<>', $poster->id)
->where('groups.is_modo', 1)
->get();
foreach ($staffers as $staffer) {
$staffer->notify(new NewTopic('staff', $poster, $topic));
}
}
/**
* Returns The Permission Field.
*/

View File

@@ -13,7 +13,6 @@
namespace App\Models;
use App\Notifications\NewPost;
use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -162,41 +161,6 @@ class Topic extends Model
return $this->belongsTo(User::class, 'last_post_user_id');
}
/**
* Notify Subscribers Of A Topic When New Post Is Made.
*/
public function notifySubscribers(User $poster, Topic $topic, Post $post): void
{
$subscribers = User::selectRaw('distinct(users.id),max(users.username) as username,max(users.group_id) as group_id')->with('group')->where('users.id', '!=', $poster->id)
->join('subscriptions', 'subscriptions.user_id', '=', 'users.id')
->leftJoin('user_notifications', 'user_notifications.user_id', '=', 'users.id')
->where('subscriptions.topic_id', '=', $topic->id)
->whereRaw('(user_notifications.show_subscription_topic = 1 OR user_notifications.show_subscription_topic is null)')
->groupBy('users.id')->get();
foreach ($subscribers as $subscriber) {
if ($subscriber->acceptsNotification($poster, $subscriber, 'subscription', 'show_subscription_topic')) {
$subscriber->notify(new NewPost('subscription', $poster, $post));
}
}
}
/**
* Notify Staffers When New Staff Post Is Made.
*/
public function notifyStaffers(User $poster, Topic $topic, Post $post): void
{
$staffers = User::leftJoin('groups', 'users.group_id', '=', 'groups.id')
->select('users.id')
->where('users.id', '<>', $poster->id)
->where('groups.is_modo', 1)
->get();
foreach ($staffers as $staffer) {
$staffer->notify(new NewPost('staff', $poster, $post));
}
}
/**
* Does User Have Permission To View Topic.
*/
@@ -208,18 +172,4 @@ class Topic extends Model
return $this->forum->getPermission()?->read_topic;
}
/**
* Notify Starter When An Action Is Taken.
*/
public function notifyStarter(User $poster, Topic $topic, Post $post): bool
{
$user = User::find($topic->first_post_user_id);
if ($user->acceptsNotification(auth()->user(), $user, 'forum', 'show_forum_topic')) {
$user->notify(new NewPost('topic', $poster, $post));
}
return true;
}
}

View File

@@ -708,6 +708,16 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->belongsToMany(Topic::class, 'subscriptions');
}
/**
* Has Many Permissions through Group.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany<Permission>
*/
public function forumPermissions(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Permission::class, 'group_id', 'group_id');
}
/**
* Has many free leech tokens.
*