/** * GET /video/{videoId}/description-changes * Get pending description changes for a video. * * @param string $video_id * @param Request $request * @return Response */ public function getDescriptionChanges($videoId) { $descriptionChanges = DescriptionChange::where('video_id', $videoId)->where('user_id', Auth::user()->id)->orderBy('executed_at')->orderBy('execute_at')->orderBy('execute_mins_after_publish')->get(); return view('partials.description-changes', ['descriptionChanges' => $descriptionChanges]); }
/** * Get all the queued changes that are for unpublished videos. * If the video has become published set the correct time to execute based * on the videos' publish time. * * @return null */ public function checkForPublishedVideos() { // First get all videos (one result per video) where its excute time has not yet been set $pendingVideos = DescriptionChange::whereNotNull('execute_mins_after_publish')->whereNull('executed_at')->groupBy('video_id')->get(); $pendingVideoCount = count($pendingVideos); if ($pendingVideoCount === 1) { $this->log->info("1 video needs checking if it's published yet."); } else { $this->log->info($pendingVideoCount . " videos need checking if they're published yet."); } foreach ($pendingVideos as $pendingVideo) { /** @var DescriptionChange $pendingVideo */ $user = $pendingVideo->getUser(); if (!$user) { $this->log->error("User {$pendingVideo->user_id} not found in database."); continue; } $this->log->info("Checking if video {$pendingVideo->video_id} (user {$user->name}) is published yet."); // We need to login as the user that owns this video in order to query it try { /** @var \Google_Client $googleClient */ $googleClient = App::make('Google_Client'); $googleClient->setAccessToken($user->access_token); } catch (Exception $e) { $this->log->error("Exception setting Google access token: " . $e->getMessage()); continue; } $video = $pendingVideo->getVideo(); if ($video->isPublished()) { $this->log->info("Video {$pendingVideo->video_id} was published at " . date('Y-m-d H:i:s', $video->getPublishedTimestamp())); $thisVideoChanges = DescriptionChange::where('video_id', $pendingVideo->video_id)->whereNotNull('execute_mins_after_publish')->whereNull('executed_at')->get(); foreach ($thisVideoChanges as $thisVideoChange) { $publishedAt = $video->getPublishedTimestamp(); $executeAt = $publishedAt + $thisVideoChange->execute_mins_after_publish * 60; $thisVideoChange->execute_at = date('Y-m-d H:i:s', $executeAt); $thisVideoChange->execute_mins_after_publish = null; $thisVideoChange->save(); $this->log->info("Set execute time for change {$thisVideoChange->id} to " . $thisVideoChange->execute_at); } } else { $this->log->notice("Video {$pendingVideo->video_id} is not published."); } } }
/** * Get all DescriptionChanges related to this video. * * @return Illuminate\Database\Eloquent\Collection */ public function getDescriptionChanges() { return DescriptionChange::where('video_id', $this->videoId)->orderBy('executed_at')->orderBy('execute_at')->orderBy('execute_mins_after_publish')->get(); }