From 0a44b6bd9b886df60125d5ada85f9fdb9fa3849d Mon Sep 17 00:00:00 2001 From: xlcrr Date: Sun, 15 Sep 2024 20:05:43 +0100 Subject: [PATCH] fix double events --- app/Events/NewCityAdded.php | 2 - app/Helpers/Post/UploadHelper.php | 25 --------- app/Http/Controllers/ApiPhotosController.php | 27 +++++++++- app/Http/Controllers/PhotosController.php | 54 ++++++++----------- .../Uploads/UploadPhotoController.php | 47 ++++++---------- resources/js/views/general/Upload.vue | 2 +- 6 files changed, 66 insertions(+), 91 deletions(-) diff --git a/app/Events/NewCityAdded.php b/app/Events/NewCityAdded.php index d8240f95c..fca997e96 100644 --- a/app/Events/NewCityAdded.php +++ b/app/Events/NewCityAdded.php @@ -4,8 +4,6 @@ use Illuminate\Broadcasting\Channel; use Illuminate\Queue\SerializesModels; -use Illuminate\Broadcasting\PrivateChannel; -use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; diff --git a/app/Helpers/Post/UploadHelper.php b/app/Helpers/Post/UploadHelper.php index ced6862d3..9aaf5635b 100644 --- a/app/Helpers/Post/UploadHelper.php +++ b/app/Helpers/Post/UploadHelper.php @@ -30,11 +30,6 @@ public function getCountryFromAddressArray (array $addressArray) ['country' => $addressArray["country"] ?? '', 'created_by' => auth()->id()] ); - if ($country->wasRecentlyCreated) { - // Broadcast an event to anyone viewing the Global Map - event(new NewCountryAdded($country->country, $countryCode, now())); - } - return $country; } @@ -62,12 +57,6 @@ public function getStateFromAddressArray (Country $country, array $addressArray) ['created_by' => auth()->id()] ); - if ($state->wasRecentlyCreated) - { - // Broadcast an event to anyone viewing the Global Map - event(new NewStateAdded($stateName, $country->country, now())); - } - return $state; } @@ -96,20 +85,6 @@ public function getCityFromAddressArray (Country $country, State $state, $addres ['created_by' => auth()->id()] ); - if ($city->wasRecentlyCreated) - { - // Broadcast an event to anyone viewing the Global Map - event(new NewCityAdded( - $cityName, - $state->state, - $country->country, - now(), - $city->id, - $lat, - $lon - )); - } - return $city; } diff --git a/app/Http/Controllers/ApiPhotosController.php b/app/Http/Controllers/ApiPhotosController.php index 8c4506727..06b512a7e 100644 --- a/app/Http/Controllers/ApiPhotosController.php +++ b/app/Http/Controllers/ApiPhotosController.php @@ -2,6 +2,9 @@ namespace App\Http\Controllers; +use App\Events\NewCityAdded; +use App\Events\NewCountryAdded; +use App\Events\NewStateAdded; use GeoHash; use Carbon\Carbon; use App\Models\Photo; @@ -196,7 +199,29 @@ protected function storePhoto (Request $request): Photo $city )); - // Move this to redis + // Broadcast an event to anyone viewing the Global Map + // Sends Notification to Twitter & Slack + if ($country->wasRecentlyCreated) { + event(new NewCountryAdded($country->country, $country->shortcode, now())); + } + + if ($state->wasRecentlyCreated) { + event(new NewStateAdded($state->state, $country->country, now())); + } + + if ($city->wasRecentlyCreated) { + event(new NewCityAdded( + $city->city, + $state->state, + $country->country, + now(), + $city->id, + $lat, + $lon, + $photo->id + )); + } + event(new IncrementPhotoMonth( $country->id, $state->id, diff --git a/app/Http/Controllers/PhotosController.php b/app/Http/Controllers/PhotosController.php index 1a446e931..bd733edd8 100644 --- a/app/Http/Controllers/PhotosController.php +++ b/app/Http/Controllers/PhotosController.php @@ -2,42 +2,35 @@ namespace App\Http\Controllers; -use App\Actions\Photos\AddCustomTagsToPhotoAction; -use App\Actions\Photos\AddTagsToPhotoAction; -use App\Actions\Photos\DeletePhotoAction; -use App\Actions\Photos\GetPreviousCustomTagsAction; -use App\Actions\Locations\UpdateLeaderboardsForLocationAction; -use App\Events\ImageDeleted; -use App\Http\Requests\AddTagsRequest; +use App\Models\Photo; use App\Models\User\User; -use App\Models\Photo; -use Illuminate\Http\Request; +use App\Events\ImageDeleted; use App\Events\TagsVerifiedByAdmin; use App\Helpers\Post\UploadHelper; +use App\Http\Requests\AddTagsRequest; +use App\Actions\Photos\DeletePhotoAction; +use App\Actions\Photos\AddTagsToPhotoAction; +use App\Actions\Photos\AddCustomTagsToPhotoAction; +use App\Actions\Photos\GetPreviousCustomTagsAction; +use App\Actions\Locations\UpdateLeaderboardsForLocationAction; + +use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class PhotosController extends Controller { - /** @var UploadHelper */ - protected $uploadHelper; - /** @var AddTagsToPhotoAction */ - private $addTagsAction; - /** @var UpdateLeaderboardsForLocationAction */ - private $updateLeaderboardsAction; - /** @var DeletePhotoAction */ - private $deletePhotoAction; + protected UploadHelper $uploadHelper; + private AddTagsToPhotoAction $addTagsAction; + private DeletePhotoAction $deletePhotoAction; + private UpdateLeaderboardsForLocationAction $updateLeaderboardsAction; /** * PhotosController constructor * Apply middleware to all of these routes - * - * @param UploadHelper $uploadHelper - * @param AddTagsToPhotoAction $addTagsAction - * @param UpdateLeaderboardsForLocationAction $updateLeaderboardsAction - * @param DeletePhotoAction $deletePhotoAction */ public function __construct( UploadHelper $uploadHelper, @@ -57,11 +50,9 @@ public function __construct( /** * Delete an image */ - public function deleteImage(Request $request) + public function deleteImage (Request $request) { - /** @var User $user */ $user = Auth::user(); - /** @var Photo $photo */ $photo = Photo::findOrFail($request->photoid); if ($user->id !== $photo->user_id) { @@ -86,7 +77,7 @@ public function deleteImage(Request $request) $photo->team_id )); - return ['message' => 'Photo deleted successfully!']; + return response()->json(['message' => 'Photo deleted successfully!']); } /** @@ -138,18 +129,17 @@ public function addTags (AddTagsRequest $request, AddCustomTagsToPhotoAction $cu $photo->save(); - return [ + return response()->json([ 'success' => true, 'msg' => 'success' - ]; + ]); } /** * Get unverified photos for tagging */ - public function unverified (GetPreviousCustomTagsAction $previousTagsAction) + public function unverified (GetPreviousCustomTagsAction $previousTagsAction): JsonResponse { - /** @var User $user */ $user = Auth::user(); $query = Photo::where([ @@ -168,11 +158,11 @@ public function unverified (GetPreviousCustomTagsAction $previousTagsAction) $total = Photo::where('user_id', $user->id)->count(); - return [ + return response()->json([ 'photos' => $photos, 'remaining' => $remaining, 'total' => $total, 'custom_tags' => $previousTagsAction->run($user) - ]; + ]); } } diff --git a/app/Http/Controllers/Uploads/UploadPhotoController.php b/app/Http/Controllers/Uploads/UploadPhotoController.php index 2b33b08f6..b66049f5c 100644 --- a/app/Http/Controllers/Uploads/UploadPhotoController.php +++ b/app/Http/Controllers/Uploads/UploadPhotoController.php @@ -2,48 +2,37 @@ namespace App\Http\Controllers\Uploads; -use App\Exceptions\InvalidCoordinates; +use Carbon\Carbon; use Geohash\GeoHash; -use App\Actions\Locations\UpdateLeaderboardsForLocationAction; -use App\Actions\Photos\MakeImageAction; -use App\Actions\Photos\UploadPhotoAction; + use App\Events\NewCityAdded; use App\Events\NewCountryAdded; use App\Events\NewStateAdded; use App\Helpers\Post\UploadHelper; -use Carbon\Carbon; use App\Models\Photo; use App\Models\User\User; use App\Events\ImageUploaded; use App\Events\Photo\IncrementPhotoMonth; use App\Http\Requests\UploadPhotoRequest; +use App\Exceptions\InvalidCoordinates; + +use App\Actions\Photos\MakeImageAction; +use App\Actions\Photos\UploadPhotoAction; use App\Actions\Locations\ReverseGeocodeLocationAction; +use App\Actions\Locations\UpdateLeaderboardsForLocationAction; use App\Http\Controllers\Controller; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; class UploadPhotoController extends Controller { - /** @var MakeImageAction */ - private $makeImageAction; - - /** @var UploadPhotoAction */ - private $uploadPhotoAction; - - /** @var UploadHelper */ - protected $uploadHelper; - - /** @var UpdateLeaderboardsForLocationAction */ - private $updateLeaderboardsAction; + protected UploadHelper $uploadHelper; + private MakeImageAction $makeImageAction; + private UploadPhotoAction $uploadPhotoAction; + private UpdateLeaderboardsForLocationAction $updateLeaderboardsAction; - /** - * Initialise Helper Actions - * - * @param MakeImageAction $makeImageAction - * @param UploadPhotoAction $uploadPhotoAction - * @param UploadHelper $uploadHelper - */ public function __construct ( MakeImageAction $makeImageAction, UploadPhotoAction $uploadPhotoAction, @@ -67,11 +56,10 @@ public function __construct ( * then persist new record to photos table * * @param UploadPhotoRequest $request - * @return array + * @return JsonResponse */ - public function __invoke (UploadPhotoRequest $request): array + public function __invoke (UploadPhotoRequest $request): JsonResponse { - /** @var User $user */ $user = Auth::user(); \Log::channel('photos')->info([ @@ -105,8 +93,7 @@ public function __invoke (UploadPhotoRequest $request): array if ($exif["GPSLatitude"][0] === "0/0" && $exif["GPSLongitude"][0] === "0/0") { abort(500, - "Sorry, Your Images have GeoTags, - but they have values of Zero. + "Sorry, Your Images have GeoTags, but they have values of Zero. You may have lost the geotags when transferring images across devices." ); } @@ -306,9 +293,9 @@ public function __invoke (UploadPhotoRequest $request): array $dateTime )); - return [ + return response()->json([ 'success' => true - ]; + ]); } /** diff --git a/resources/js/views/general/Upload.vue b/resources/js/views/general/Upload.vue index ceda9a149..e2eb1a741 100644 --- a/resources/js/views/general/Upload.vue +++ b/resources/js/views/general/Upload.vue @@ -44,8 +44,8 @@