Skip to content

Commit

Permalink
new instant notifications need tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philippejadin committed Nov 22, 2021
1 parent bcdf616 commit 56d4562
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/SendNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getNotificationsToSend()
$notifications = DB::select('
select * from
(select *, date_add(notified_at, interval notification_interval minute) as notify from membership
where notification_interval > 0
where notification_interval > 1
and membership >= :membership) as memberships
where notify < :now or notify is null limit :batch
', ['now' => Carbon::now(), 'membership' => \App\Membership::MEMBER, 'batch' => $this->option('batch')]);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/GroupDiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public function store(Request $request, Group $group)

flash(trans('messages.ressource_created_successfully'));

event(new \App\Events\ContentCreated($discussion));

return redirect()->route('groups.discussions.show', [$group, $discussion]);
}

Expand Down
18 changes: 15 additions & 3 deletions app/Listeners/NotifyInstantly.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Comment;
use App\User;
use App\Discussion;
use Auth;

class NotifyInstantly
{
Expand Down Expand Up @@ -34,12 +35,23 @@ public function handle(ContentCreated $event)
$comment = $event->model;

// get a list of users that must be notified instantly
$users = $comment->discussion->group->users()->where('notification_interval', 1)->get();
$users = $comment->discussion->group->users()->where('notification_interval', 1)->get()->except(Auth::id());

//dd ($users);

foreach ($users as $user) {
// Notification::send($user, new \App\Notifications\MentionedUser($comment, \Auth::user()));
Notification::send($user, new \App\Notifications\CommentCreated($comment, Auth::user()));
}
}

// Comments
if ($event->model instanceof Discussion) {
$discussion = $event->model;

// get a list of users that must be notified instantly
$users = $discussion->group->users()->where('notification_interval', 1)->get()->except(Auth::id());

foreach ($users as $user) {
Notification::send($user, new \App\Notifications\DiscussionCreated($discussion, Auth::user()));
}
}
}
Expand Down
89 changes: 89 additions & 0 deletions app/Notifications/CommentCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Notifications;

use App\Comment;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\HtmlString;

class CommentCreated extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Comment $comment, User $user)
{
$this->comment = $comment;
$this->user = $user;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{

$message = (new MailMessage())
->greeting(' ')
->salutation(' ')
->subject('[' . $this->comment->discussion->group->name . '] ' . $this->comment->discussion->name)
->line(new HtmlString(filter($this->comment->body)))
->line($this->comment->user->name)
->action(trans('messages.reply'), route('groups.discussions.show', [$this->comment->discussion->group, $this->comment->discussion]));
$message->from(config('mail.noreply'), $this->comment->user->name);


// send notification directly from discussion inbox if there is one
if ($this->comment->discussion->inbox()) {
$message->replyTo($this->comment->discussion->inbox(), $this->comment->discussion->group->name);
} else {
$message->line(trans('messages.dont_reply_to_this_email'));
$message->from(config('mail.noreply'), config('mail.from.name'));
}

$message->withSwiftMessage(function ($message) {
$message->getHeaders()->addTextHeader('Message-ID', 'comment-'. $this->comment->id . '@' . config('app.url'));
$message->getHeaders()->addTextHeader('References', 'discussion-'. $this->comment->discussion->id . '@' . config('app.url'));
});

return $message;
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
*
* @return array
*/
public function toArray($notifiable)
{
return [
'comment' => $this->comment->toArray(),
'user' => $this->user->toArray(),
];
}
}
88 changes: 88 additions & 0 deletions app/Notifications/DiscussionCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Notifications;

use App\Discussion;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\HtmlString;

class DiscussionCreated extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Discussion $discussion, User $user)
{
$this->discussion = $discussion;
$this->user = $user;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{

$message = (new MailMessage())
->greeting(' ')
->salutation(' ')
->subject('[' . $this->discussion->group->name . '] ' . $this->discussion->name)
->line(new HtmlString(filter($this->discussion->body)))
->line($this->discussion->user->name)
->action(trans('messages.reply'), route('groups.discussions.show', [$this->discussion->group, $this->discussion]));
$message->from(config('mail.noreply'), $this->discussion->user->name);


// send notification directly from discussion inbox if there is one
if ($this->discussion->inbox()) {
$message->replyTo($this->discussion->inbox(), $this->discussion->group->name);
} else {
$message->line(trans('messages.dont_reply_to_this_email'));
$message->from(config('mail.noreply'), config('mail.from.name'));
}

$message->withSwiftMessage(function ($message) {
$message->getHeaders()->addTextHeader('Message-ID', 'discussion-'. $this->discussion->id . '@' . config('app.url'));
});

return $message;
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
*
* @return array
*/
public function toArray($notifiable)
{
return [
'discussion' => $this->discussion->toArray(),
'user' => $this->user->toArray(),
];
}
}

0 comments on commit 56d4562

Please sign in to comment.