/** * POST /video/{videoId}/description-changes * Create a new description change for a video. * * @param string $video_id * @param Request $request * @return Response */ public function postDescriptionChanges($videoId, Request $request) { $this->validateInput($request); $executeAt = null; if ($request->input('execute_at')) { $executeAt = date('Y-m-d H:i:s', strtotime($request->input('execute_at'))); } $change = new DescriptionChange(['user_id' => Auth::user()->id, 'video_id' => $videoId, 'description' => $request->input('description'), 'execute_at' => $executeAt, 'execute_mins_after_publish' => $request->input('execute_mins_after_publish') ? $request->input('execute_mins_after_publish') : null]); $success = $change->save(); return Response::json(['success' => $success, 'change' => $change->fresh()]); }
/** * 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(); }