Skip to content

Commit

Permalink
refactor: forum notification triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Roardom committed Feb 13, 2024
1 parent 9fe4abd commit 7490a7a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 92 deletions.
34 changes: 30 additions & 4 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -104,16 +105,41 @@ public function store(Request $request): \Illuminate\Http\RedirectResponse
$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());
Expand Down
32 changes: 30 additions & 2 deletions app/Http/Controllers/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
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;
Expand Down Expand Up @@ -151,10 +153,36 @@ public function store(Request $request, int $id): \Illuminate\Http\RedirectRespo
$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());
Expand Down
36 changes: 0 additions & 36 deletions app/Models/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,41 +137,6 @@ public function subscribedUsers(): \Illuminate\Database\Eloquent\Relations\Belon
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.
*/
Expand Down
50 changes: 0 additions & 50 deletions app/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,41 +161,6 @@ public function latestPoster(): \Illuminate\Database\Eloquent\Relations\BelongsT
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.
*/
Expand All @@ -208,18 +172,4 @@ public function viewable(): bool

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;
}
}
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,16 @@ public function subscribedTopics(): \Illuminate\Database\Eloquent\Relations\Belo
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.
*
Expand Down

0 comments on commit 7490a7a

Please sign in to comment.