Пример #1
0
 public function findNotifications()
 {
     $notifications = [];
     $commentRepository = App::make('CommentRepository');
     // -------------------------------------
     Comment::withTrashed()->withSpot()->get()->each(function ($item) use(&$notifications, &$commentRepository) {
         $spot = $item->spot()->withTrashed()->first();
         if ($spot == NULL) {
             // remove this comment its dead
             $commentRepository->destroy($item->id);
             return;
         }
         if ($spot->user !== null) {
             $from_user_id = $item->user_id;
             $to_user_id = $spot->user_id;
             if ($from_user_id !== $to_user_id) {
                 $notification_event = array('event' => Notification::NOTIFICATION_USER_COMMENTED, 'from_user_id' => $from_user_id, 'to_user_id' => $to_user_id, 'parent' => $item, 'timestamp' => $this->timestamp($item->created_at), 'is_read' => false);
                 array_push($notifications, $notification_event);
             }
         }
     });
     // -------------------------------------
     Visit::withTrashed()->get()->each(function ($item) use(&$notifications) {
         $spot = $item->spot()->withTrashed()->first();
         if ($spot == NULL) {
             // remove this comment its dead
             dd($spot);
             return;
         }
         if ($spot->user !== null) {
             $from_user_id = $item->user_id;
             $to_user_id = $spot->user_id;
             if ($from_user_id !== $to_user_id) {
                 $notification_event = array('event' => Notification::NOTIFICATION_USER_VISITED, 'from_user_id' => $from_user_id, 'to_user_id' => $to_user_id, 'parent' => $item, 'timestamp' => $this->timestamp($item->created_at), 'is_read' => false);
                 array_push($notifications, $notification_event);
             }
         }
     });
     // -------------------------------------
     Illuminate\Support\Collection::make(DB::table('userables')->get())->each(function ($item) use(&$notifications) {
         $type = $item->userable_type;
         $parent = $type::withTrashed()->whereId($item->userable_id)->first();
         $notification_event = array('event' => Notification::NOTIFICATION_USER_ITINERARY_SHARED, 'to_user_id' => $item->user_id, 'from_user_id' => $parent->user->id, 'parent' => $parent, 'timestamp' => $this->timestamp(new Carbon($item->created_at)), 'is_read' => false);
         array_push($notifications, $notification_event);
     });
     // -------------------------------------
     User::all()->each(function ($user) use(&$notifications) {
         // welcome message
         $notification_event = array('event' => Notification::NOTIFICATION_USER_WELCOME, 'to_user_id' => $user->id, 'from_user_id' => null, 'parent' => $user, 'timestamp' => $this->timestamp($user->created_at), 'is_read' => false);
         array_push($notifications, $notification_event);
         // when someone added your spot to favs
         foreach ($user->favorites->getLocations() as $location) {
             if ($location->hasSpot() && $location->spot->user_id !== $user->id) {
                 // send to the owner of the spot
                 $to_user_id = $location->spot->user_id;
                 // who is doing this...?
                 $from_user_id = $user->id;
                 // welcome message
                 $notification_event = array('event' => Notification::NOTIFICATION_USER_FAVORITED, 'to_user_id' => $to_user_id, 'from_user_id' => $from_user_id, 'parent' => $location->spot, 'timestamp' => $this->timestamp($location->pivot->created_at), 'is_read' => false);
                 array_push($notifications, $notification_event);
             }
         }
     });
     foreach ($notifications as $notice) {
         Notification::fireNotification($notice);
     }
     return $notifications;
 }