Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/Http/Controllers/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Notifications\DatabaseNotification;

class NotificationController extends Controller
Expand All @@ -20,7 +21,7 @@ public function index(Request $request): JsonResponse
);
}

public function markAsRead(Request $request, DatabaseNotification $notification): RedirectResponse
public function markAsRead(Request $request, DatabaseNotification $notification): RedirectResponse|Response
{
abort_unless(
$notification->notifiable()->whereKey($request->user()->getKey())->exists(),
Expand All @@ -29,6 +30,10 @@ public function markAsRead(Request $request, DatabaseNotification $notification)

$notification->markAsRead();

if ($request->wantsJson()) {
return response()->noContent();
}

return back();
}

Expand Down
7 changes: 7 additions & 0 deletions app/Http/Resources/EventIndexResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ class EventIndexResource extends JsonResource
*/
public function toArray(Request $request): array
{
$isDeclined = false;
if ($this->relationLoaded('attendees')) {
$currentUserAttendee = $this->attendees->firstWhere('id', $request->user()?->id);
$isDeclined = $currentUserAttendee?->pivot?->participation_status === 'declined';
}

return [
'id' => $this->id,
'title' => $this->title,
'start_at' => $this->start_at?->toIso8601String(),
'end_at' => $this->end_at?->toIso8601String(),
'has_video' => $this->has_video,
'is_declined' => $isDeclined,
];
}
}
8 changes: 8 additions & 0 deletions app/Http/Resources/EventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function toArray(Request $request): array
'room_name' => $this->room_name,
'has_video' => $this->has_video,
'status' => $this->status,
'user_participation_status' => $this->whenLoaded('attendees', function () use ($request) {
if ($this->organizer_id === $request->user()?->id) {
return 'accepted';
}

return $this->attendees->firstWhere('id', $request->user()?->id)?->pivot?->participation_status;
}),
'organizer' => $this->whenLoaded('organizer', fn () => [
'id' => $this->organizer->id,
'name' => $this->organizer->name,
Expand All @@ -34,6 +41,7 @@ public function toArray(Request $request): array
->map(fn ($attendee) => [
'id' => $attendee->id,
'name' => $attendee->name,
'participation_status' => $attendee->pivot?->participation_status,
])->values()),
];
}
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Resources/NotificationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Http\Resources;

use App\Notifications\EventInvitation;
use App\Notifications\TaskAssigned;
use App\Notifications\TaskCommented;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
Expand All @@ -19,6 +21,8 @@ class NotificationResource extends JsonResource
*/
private const TYPE_ALIASES = [
EventInvitation::class => 'event_invitation',
TaskAssigned::class => 'task_assigned',
TaskCommented::class => 'task_commented',
];

/**
Expand Down
40 changes: 40 additions & 0 deletions app/Jobs/NotifyEventInvitees.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Jobs;

use App\Models\Event;
use App\Models\User;
use App\Notifications\EventInvitation;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\SerializesModels;

class NotifyEventInvitees implements ShouldQueue
{
use Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*/
public int $tries = 3;

/**
* @param array<int> $userIds
*/
public function __construct(
public Event $event,
public array $userIds,
) {}

public function handle(): void
{
if (empty($this->userIds)) {
return;
}

User::query()
->whereIn('id', $this->userIds)
->get()
->each(fn (User $user) => $user->notify(new EventInvitation($this->event)));
}
}
34 changes: 34 additions & 0 deletions app/Jobs/NotifyTaskAssignee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Jobs;

use App\Models\Task;
use App\Models\User;
use App\Notifications\TaskAssigned;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\SerializesModels;

class NotifyTaskAssignee implements ShouldQueue
{
use Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*/
public int $tries = 3;

public function __construct(
public Task $task,
public User $actor,
) {}

public function handle(): void
{
if (empty($this->task->assigned_to) || $this->task->assigned_to === $this->actor->id) {
return;
}

$this->task->assignee?->notify(new TaskAssigned($this->task, $this->actor));
}
}
48 changes: 48 additions & 0 deletions app/Jobs/NotifyTaskCommentStakeholders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Jobs;

use App\Models\Task;
use App\Models\TaskComment;
use App\Models\User;
use App\Notifications\TaskCommented;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\SerializesModels;

class NotifyTaskCommentStakeholders implements ShouldQueue
{
use Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*/
public int $tries = 3;

public function __construct(
public Task $task,
public TaskComment $comment,
public User $actor,
) {}

public function handle(): void
{
$recipientIds = collect([
$this->task->assigned_to,
$this->task->created_by,
$this->comment->parent?->user_id,
])
->filter()
->unique()
->reject(fn (int $id) => $id === $this->actor->id);

if ($recipientIds->isEmpty()) {
return;
}

User::query()
->whereIn('id', $recipientIds)
->get()
->each(fn (User $user) => $user->notify(new TaskCommented($this->task, $this->comment, $this->actor)));
}
}
52 changes: 52 additions & 0 deletions app/Notifications/TaskAssigned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Notifications;

use App\Models\Task;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class TaskAssigned extends Notification
{
use Queueable;

public function __construct(public Task $task, public User $actor) {}

public function via(object $notifiable): array
{
return ['database', 'mail'];
}

public function toDatabase(object $notifiable): array
{
return [
'task_id' => $this->task->id,
'task_title' => $this->task->title,
'assigner' => $this->actor->name,
'due_date' => $this->task->due_date?->toISOString(),
];
}

public function toArray(object $notifiable): array
{
return $this->toDatabase($notifiable);
}

public function toMail(object $notifiable): MailMessage
{
$mail = (new MailMessage)
->subject('You have been assigned: '.$this->task->title)
->greeting('Hello '.$notifiable->name.',')
->line($this->actor->name.' assigned you to a task on Taskly.')
->line($this->task->title)
->action('View task', url('/tasks?id='.$this->task->id));

if ($this->task->due_date) {
$mail->line('Due: '.$this->task->due_date->format('l, F j, Y'));
}

return $mail->line('Open the task to see all the details.');
}
}
59 changes: 59 additions & 0 deletions app/Notifications/TaskCommented.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Notifications;

use App\Models\Task;
use App\Models\TaskComment;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;

class TaskCommented extends Notification
{
use Queueable;

public function __construct(public Task $task, public TaskComment $comment, public User $actor) {}

public function via(object $notifiable): array
{
return ['database', 'mail'];
}

public function toDatabase(object $notifiable): array
{
return [
'task_id' => $this->task->id,
'task_title' => $this->task->title,
'commenter' => $this->actor->name,
'comment' => $this->excerpt(),
'is_reply' => $this->comment->parent_id !== null,
];
}

public function toArray(object $notifiable): array
{
return $this->toDatabase($notifiable);
}

public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('New comment on: '.$this->task->title)
->greeting('Hello '.$notifiable->name.',')
->line($this->actor->name.' commented on a task you are following on Taskly.')
->line($this->task->title)
->line('"'.$this->excerpt().'"')
->action('View comment', url('/tasks?id='.$this->task->id))
->line('Open the task to read the full conversation.');
}

/**
* A short plain-text version of the comment for previews.
*/
private function excerpt(): string
{
return Str::limit(strip_tags($this->comment->body), 140);
}
}
3 changes: 1 addition & 2 deletions app/Policies/EventPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function update(User $user, Event $event): bool
*/
public function delete(User $user, Event $event): bool
{
return $event->calendar?->team_id !== null
&& $user->canAccessTeam($event->calendar->team_id);
return $user->id === $event->organizer_id;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions app/Services/CalendarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use App\Jobs\NotifyEventInvitees;
use App\Models\Calendar;
use App\Models\Event;
use App\Models\User;
Expand Down Expand Up @@ -45,12 +46,13 @@ public function indexData(User $user, ?string $start = null, ?string $end = null
->where(fn ($query) => $query->where('organizer_id', $user->id)
->orWhereHas('attendees', fn ($attendees) => $attendees
->where(self::ATTENDEE_TABLE.'.user_id', $user->id)
->where(self::ATTENDEE_TABLE.'.participation_status', 'accepted')))
->whereIn(self::ATTENDEE_TABLE.'.participation_status', ['accepted', 'declined'])))
->whereDoesntHave('attendees', fn ($attendees) => $attendees
->where(self::ATTENDEE_TABLE.'.user_id', $user->id)
->whereNotNull(self::ATTENDEE_TABLE.'.hidden_at'))
->where('start_at', '<=', $end)
->where('end_at', '>=', $start)
->with(['attendees' => fn ($query) => $query->where(self::ATTENDEE_TABLE.'.user_id', $user->id)])
->orderBy('start_at')
->get();

Expand Down Expand Up @@ -105,7 +107,7 @@ public function create(array $data, User $user): Event
);

$event->load('attendees');
$event->attendees->each->notify(new EventInvitation($event));
NotifyEventInvitees::dispatch($event, $event->attendees->pluck('id')->all());

return $event;
}
Expand Down Expand Up @@ -144,8 +146,7 @@ public function update(Event $event, array $data): void
$addedIds = $attendeeIds->diff($existing->keys());

if ($addedIds->isNotEmpty()) {
$this->teamAttendees($event->calendar->team_id, $addedIds)
->each->notify(new EventInvitation($event));
NotifyEventInvitees::dispatch($event, $addedIds->all());
}

$this->deleteInvitationNotifications($event, $existing->keys()->diff($attendeeIds));
Expand Down
Loading
Loading