protected static function boot() { parent::boot(); static::deleting(function ($userSocialToken) { UserSocialPost::join('social_post', 'social_post.id', '=', 'user_social_post.social_post_id')->where('user_social_post.user_id', '=', $userSocialToken->user_id)->where('social_post.type', '=', $userSocialToken->type)->delete(); UserSocialTokenFilter::where('user_social_token_filter.user_social_token_id', '=', $userSocialToken->id)->delete(); }); }
public function handle() { $facebook = App::make('SammyK\\LaravelFacebookSdk\\LaravelFacebookSdk'); //Check if long term access token is set if (!$this->userFacebookToken->long_lived_token) { //If user has no short term token then abort as cannot extend if (!$this->userFacebookToken->short_lived_token) { abort(422, 'Cannot access posts for user without short lived token!'); } $tokenExtender = new ExtendFacebookShortLiveToken($this->userFacebookToken); $tokenExtender->extend(); } $facebook->setDefaultAccessToken($this->userFacebookToken->long_lived_token); $firstCall = true; $feedQuery = $this->userFacebookToken->entity_id . '/feed?fields=call_to_action,caption,link,message_tags,full_picture,message,description,from{name,picture},icon,name,place,source,story_tags,story,created_time,type,with_tags&limit=100'; if ($lastFacebookPostByUser = UserSocialPost::join('social_post', 'user_social_post.social_post_id', '=', 'social_post.id')->where('user_social_post.user_id', '=', $this->userFacebookToken->user_id)->where('social_post.type', '=', 'facebook')->orderBy('user_social_post.created_at', 'DESC')->first()) { $firstCall = false; $feedQuery = $feedQuery . '&since=' . $lastFacebookPostByUser->created_at; } do { try { $latestPostsRaw = $facebook->get($feedQuery); $latestPostsDecoded = $latestPostsRaw->getDecodedBody(); $latestPostsJSON = $latestPostsDecoded['data']; } catch (Facebook\Exceptions\FacebookResponseException $e) { //Session does not match current stored session! $this->userFacebookToken->expires_at = Carbon::now(); } catch (Facebook\Exceptions\FacebookSDKException $e) { //Session does not match current stored session! $this->userFacebookToken->expires_at = Carbon::now(); } foreach ($latestPostsJSON as $post) { $assignPostToDb = new AssignFacebookPostsToDatabase($this->userFacebookToken->user_id, $post); $assignPostToDb->assign(); } $latestPostsPaging = array(); if (array_key_exists('paging', $latestPostsDecoded)) { $latestPostsPaging = $latestPostsDecoded['paging']; } } while (!$firstCall && array_key_exists('next', $latestPostsPaging) && ($feedQuery = $this->extractMeaningfulPath($latestPostsPaging['next']))); }