diff --git a/app/Console/Commands/SendNotifications.php b/app/Console/Commands/SendNotifications.php index f0f4ad67..ff9850a6 100644 --- a/app/Console/Commands/SendNotifications.php +++ b/app/Console/Commands/SendNotifications.php @@ -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')]); diff --git a/app/Http/Controllers/GroupDiscussionController.php b/app/Http/Controllers/GroupDiscussionController.php index 8bc0f8bc..cd1e0ae0 100644 --- a/app/Http/Controllers/GroupDiscussionController.php +++ b/app/Http/Controllers/GroupDiscussionController.php @@ -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]); } diff --git a/app/Listeners/NotifyInstantly.php b/app/Listeners/NotifyInstantly.php index 33d161bd..9fa71b42 100644 --- a/app/Listeners/NotifyInstantly.php +++ b/app/Listeners/NotifyInstantly.php @@ -7,6 +7,7 @@ use App\Comment; use App\User; use App\Discussion; +use Auth; class NotifyInstantly { @@ -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())); } } } diff --git a/app/Notifications/CommentCreated.php b/app/Notifications/CommentCreated.php new file mode 100644 index 00000000..a36ef411 --- /dev/null +++ b/app/Notifications/CommentCreated.php @@ -0,0 +1,89 @@ +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(), + ]; + } +} diff --git a/app/Notifications/DiscussionCreated.php b/app/Notifications/DiscussionCreated.php new file mode 100644 index 00000000..c7267d25 --- /dev/null +++ b/app/Notifications/DiscussionCreated.php @@ -0,0 +1,88 @@ +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(), + ]; + } +}