Skip to content

Commit

Permalink
Merge pull request #666 from TheRestartProject/RES-1909_approve_event…
Browse files Browse the repository at this point in the history
…_time

RES-1909 approve event time
  • Loading branch information
edwh authored Dec 5, 2023
2 parents 583c258 + c6f5bdf commit 7734690
Show file tree
Hide file tree
Showing 39 changed files with 170 additions and 58 deletions.
1 change: 1 addition & 0 deletions app/Events/ApproveGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ApproveGroup
use Dispatchable, InteractsWithSockets, SerializesModels;

public $group;
public $data;

/**
* Create a new event instance.
Expand Down
1 change: 1 addition & 0 deletions app/Events/EditEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EditEvent
use Dispatchable, InteractsWithSockets, SerializesModels;

public $party;
public $data;

/**
* Create a new event instance.
Expand Down
1 change: 1 addition & 0 deletions app/Events/EditGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EditGroup
use Dispatchable, InteractsWithSockets, SerializesModels;

public $group;
public $data;

/**
* Create a new event instance.
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/API/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ public function updateEventv2(Request $request, $idEvents)
false
);

if (!Fixometer::userHasEditPartyPermission($idEvents, $user->id)) {
abort(403);
}

// Convert the timezone to UTC, because the timezone is not itself stored in the DB.
$event_start_utc = Carbon::parse($start)->setTimezone('UTC')->toIso8601String();
$event_end_utc = Carbon::parse($end)->setTimezone('UTC')->toIso8601String();
Expand All @@ -645,8 +649,7 @@ public function updateEventv2(Request $request, $idEvents)
$party = Party::findOrFail($idEvents);
$party->update($update);

// REVIEW: In the old code we wouldn't generate EditEvent in the approval case. But I think that was a bug.
if ($request->has('moderate') && $request->input('moderate') == 'approve') {
if ($request->has('moderate') && $request->input('moderate') == 'approve' && Fixometer::userCanApproveEvent($idEvents, $user->id)) {
$party->approve();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/AddUserToDiscourseGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Log;
use Notification;

class AddUserToDiscourseGroup
class AddUserToDiscourseGroup extends BaseEvent
{
/**
* Create the event listener.
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/AnonymiseSoftDeletedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Events\UserDeleted;

class AnonymiseSoftDeletedUser
class AnonymiseSoftDeletedUser extends BaseEvent
{
/**
* @param UserDeleted $event
Expand Down
11 changes: 11 additions & 0 deletions app/Listeners/BaseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Listeners;

use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Bus\Queueable;

abstract class BaseEvent implements ShouldQueue {
use Queueable;
}
2 changes: 1 addition & 1 deletion app/Listeners/ChangeWikiPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Mediawiki\Api\FluentRequest;
use Mediawiki\Api\MediawikiApi;

class ChangeWikiPassword
class ChangeWikiPassword extends BaseEvent
{
/**
* Create the event listener.
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CreateDiscourseGroupForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Lang;

class CreateDiscourseGroupForGroup
class CreateDiscourseGroupForGroup extends BaseEvent
{
/**
* Create the event listener.
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CreateDiscourseThreadForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\User;
use Illuminate\Support\Facades\Log;

class CreateDiscourseThreadForEvent
class CreateDiscourseThreadForEvent extends BaseEvent
{
/**
* Create the event listener.
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CreateWordpressPostForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Support\Facades\Log;
use Notification;

class CreateWordpressPostForEvent
class CreateWordpressPostForEvent extends BaseEvent
{
protected $wpClient;

Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CreateWordpressPostForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Support\Facades\Log;
use Notification;

class CreateWordpressPostForGroup
class CreateWordpressPostForGroup extends BaseEvent
{
/**
* Create the event listener.
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/DeleteEventFromWordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Support\Facades\Log;
use Notification;

class DeleteEventFromWordPress
class DeleteEventFromWordPress extends BaseEvent
{
protected $wpClient;

Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/DeviceUpdatedAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Group;
use App\Party;

class DeviceUpdatedAt
class DeviceUpdatedAt extends BaseEvent
{
/**
* Handle the event.
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/DiscourseUserEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Services\DiscourseService;
use Illuminate\Support\Facades\Log;

class DiscourseUserEventSubscriber
class DiscourseUserEventSubscriber extends BaseEvent
{
private $discourseClient;
private $discourseService;
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/EditWordpressPostForEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Support\Facades\Log;
use Notification;

class EditWordpressPostForEvent
class EditWordpressPostForEvent extends BaseEvent
{
protected $wpClient;

Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/EditWordpressPostForGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Support\Facades\Log;
use Notification;

class EditWordpressPostForGroup
class EditWordpressPostForGroup extends BaseEvent
{
/**
* Create the event listener.
Expand Down
1 change: 1 addition & 0 deletions app/Listeners/LogInToWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\Service\UserCreator;

// Don't extend BaseEvent - we don't want to queue because this needs to happen before we return to the client.
class LogInToWiki
{
// We use the Laravel hashed password as the mediawiki password. This means we can log in to the wiki
Expand Down
1 change: 1 addition & 0 deletions app/Listeners/LogOutOfWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

// Don't extend BaseEvent - we don't want to queue because this needs to happen before we return to the client.
class LogOutOfWiki
{
/**
Expand Down
1 change: 1 addition & 0 deletions app/Listeners/LogSuccessfulLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Auth\Events\Login;
use Illuminate\Http\Request;

// Don't extend BaseEvent - we don't want to queue because this needs to happen before we return to the client.
class LogSuccessfulLogin
{
/**
Expand Down
67 changes: 67 additions & 0 deletions app/Listeners/NotifyApprovedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Listeners;

use App\Events\ApproveEvent;
use App\Group;
use App\Party;
use App\EventsUsers;
use App\User;
use HieuLe\WordpressXmlrpcClient\WordpressClient;
use Illuminate\Support\Facades\Log;
use Notification;
use App\Notifications\NotifyRestartersOfNewEvent;
use App\Notifications\EventConfirmed;

class NotifyApprovedEvent extends BaseEvent
{
/**
* Handle the event.
*
* @param ApproveEvent $event
* @return void
*/
public function handle(ApproveEvent $event)
{
$partyId = $event->party->idevents;

$theParty = Party::find($partyId);

if (empty($theParty)) {
Log::error('Event not found');

return;
}

$group = Group::findOrFail($theParty->group);

// Only send notifications if the event is in the future.
// We don't want to send emails to Restarters about past events being added.
if ($theParty->isUpcoming()) {
$group_restarters = $group->membersRestarters();

// If there are restarters against the group
if ($group_restarters->count()) {
// Send user a notification and email
$users = $group_restarters->get();
Notification::send($users, new NotifyRestartersOfNewEvent([
'event_venue' => $theParty->venue,
'event_url' => url('/party/view/' . $partyId),
'event_group' => $group->name,
]));
}
}

// Notify the person who created it that it has now been approved.
$eu = EventsUsers::where('event', $partyId)->orderBy('idevents_users')->first();

if ($eu) {
$host = User::find($eu->user);

if ($host) {
Notification::send($host, new EventConfirmed($theParty));
}
}
}
}

2 changes: 1 addition & 1 deletion app/Listeners/RemoveSoftDeletedUserFromAllGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Events\UserDeleted;
use App\UserGroups;

class RemoveSoftDeletedUserFromAllGroups
class RemoveSoftDeletedUserFromAllGroups extends BaseEvent
{
/**
* @param UserDeleted $event
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendAdminModerateEventPhotosNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;

class SendAdminModerateEventPhotosNotification
class SendAdminModerateEventPhotosNotification extends BaseEvent
{
/**
* Ensure uploads within this timespan (in minutes) will only result in one notification being sent
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/SendAdminUserDeletedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Notifications\AdminUserDeleted;
use Illuminate\Support\Facades\Notification;

class SendAdminUserDeletedNotification
class SendAdminUserDeletedNotification extends BaseEvent
{
/**
* @param UserDeleted $event
Expand Down
2 changes: 2 additions & 0 deletions app/Listeners/SyncUserProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Events\UserLanguageUpdated;
use App\Events\UserUpdated;

// Don't extend BaseEvent - we don't want to queue because this breaks tests in a way that isn't obvious and isn't
// worth fixing given how frequently this occurs.
class SyncUserProperties
{
/**
Expand Down
33 changes: 0 additions & 33 deletions app/Party.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Events\ApproveEvent;
use App\EventUsers;
use App\Helpers\Fixometer;
use App\Notifications\NotifyRestartersOfNewEvent;
use App\Notifications\EventConfirmed;
use Auth;
use Carbon\Carbon;
use DB;
Expand Down Expand Up @@ -794,35 +790,6 @@ public function canDelete()

public function approve()
{
$group = Group::findOrFail($this->group);

// Only send notifications if the event is in the future.
// We don't want to send emails to Restarters about past events being added.
if ($this->isUpcoming()) {
$group_restarters = $group->membersRestarters();

// If there are restarters against the group
if ($group_restarters->count()) {
// Send user a notification and email
Notification::send($group_restarters->get(), new NotifyRestartersOfNewEvent([
'event_venue' => $this->venue,
'event_url' => url('/party/view/'.$this->idevents),
'event_group' => $group->name,
]));
}
}

// Notify the person who created it that it has now been approved.
$eu = EventsUsers::where('event', $this->idevents)->orderBy('idevents_users')->first();

if ($eu) {
$host = User::find($eu->user);

if ($host) {
Notification::send($host, new EventConfirmed($this));
}
}

event(new ApproveEvent($this));
}

Expand Down
1 change: 1 addition & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class EventServiceProvider extends ServiceProvider
\App\Events\ApproveEvent::class => [
\App\Listeners\CreateWordpressPostForEvent::class,
\App\Listeners\CreateDiscourseThreadForEvent::class,
\App\Listeners\NotifyApprovedEvent::class
],

\App\Events\EditEvent::class => [
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Admin/Users/UserDeletedNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function a_notification_is_sent_to_admins_when_a_user_is_deleted()
$restarter = User::factory()->restarter()->create();
$restarter->delete();

$this->artisan("queue:work --stop-when-empty");

Notification::assertSentTo(
[$admins], AdminUserDeleted::class
);
Expand Down
7 changes: 5 additions & 2 deletions tests/Feature/Dashboard/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testUpcomingEvents() {
$host = User::factory()->restarter()->create();

// Create an event.
$this->loginAsTestUser(Role::ADMINISTRATOR);
$admin = $this->loginAsTestUser(Role::ADMINISTRATOR);

$event = Party::factory()->create([
'group' => $this->idgroups,
Expand Down Expand Up @@ -117,7 +117,10 @@ public function testUpcomingEvents() {
$eventData = $event->getAttributes();
$eventData['id'] = $event->idevents;
$eventData['moderate'] = 'approve';
$response1a = $this->patch('/api/v2/events/'.$event->idevents, $this->eventAttributesToAPI($eventData));
$response1a = $this->patch('/api/v2/events/'.$event->idevents . '?api_token=' . $admin->api_key, $this->eventAttributesToAPI($eventData));
$response1a->assertSuccessful();
$this->artisan("queue:work --stop-when-empty");
$this->artisan("queue:work --stop-when-empty");

// Should now show as an upcoming event, both on dashboard page and events page.
$this->actingAs($host);
Expand Down
Loading

0 comments on commit 7734690

Please sign in to comment.