/**
  * GET (HTTP). Home.
  *
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
  */
 public function home()
 {
     if (Auth::isSubscriber()) {
         // Fetch data
         $objects['latest'] = Object::authedVisibility()->inMissionControl()->orderBy('created_at', 'desc')->take(10)->get();
         $objects['hot'] = Object::authedVisibility()->inMissionControl()->selectRaw('objects.*, LOG10(greatest(1, count(comments.object_id)) + greatest(1, count(favorites.object_id))) / TIMESTAMPDIFF(HOUR, objects.actioned_at, NOW()) as score')->leftJoin('comments', 'comments.object_id', '=', 'objects.object_id')->leftJoin('favorites', 'favorites.object_id', '=', 'objects.object_id')->groupBy('objects.object_id')->orderBy(DB::raw('score'))->take(10)->get();
         $objects['discussions'] = Object::authedVisibility()->inMissionControl()->where('type', MissionControlType::Text)->join('comments', 'comments.object_id', '=', 'objects.object_id')->orderBy('comments.created_at')->select('objects.*')->take(10)->get();
         $objects['mission'] = Object::authedVisibility()->inMissionControl()->whereHas('Mission', function ($q) {
             $q->future()->take(1);
         })->take(10)->get();
         $objects['random'] = Object::authedVisibility()->inMissionControl()->orderByRaw("RAND()")->take(10)->get();
         // Leaderboards
         $leaderboards['week'] = User::join('awards', 'awards.user_id', '=', 'users.user_id')->selectRaw('users.user_id, users.username, sum(awards.value) as totalDeltaV')->where('awards.created_at', '>=', Carbon::now()->subWeek())->groupBy('users.user_id')->take(10)->get();
         $leaderboards['month'] = User::join('awards', 'awards.user_id', '=', 'users.user_id')->selectRaw('users.user_id, users.username, sum(awards.value) as totalDeltaV')->where('awards.created_at', '>=', Carbon::now()->subMonth())->groupBy('users.user_id')->take(10)->get();
         $leaderboards['year'] = User::join('awards', 'awards.user_id', '=', 'users.user_id')->selectRaw('users.user_id, users.username, sum(awards.value) as totalDeltaV')->where('awards.created_at', '>=', Carbon::now()->subYear())->groupBy('users.user_id')->take(10)->get();
         $leaderboards['alltime'] = User::join('awards', 'awards.user_id', '=', 'users.user_id')->selectRaw('users.user_id, users.username, sum(awards.value) as totalDeltaV')->groupBy('users.user_id')->take(10)->get();
         // Comments
         $comments = Comment::with(['object' => function ($query) {
             $query->select('object_id', 'title');
         }])->with(['user' => function ($query) {
             $query->select('user_id', 'username');
         }])->orderBy('created_at', 'DESC')->take(10)->get();
         // Favorites
         $favorites = Favorite::with(['object' => function ($query) {
             $query->select('object_id', 'title');
         }])->with(['user' => function ($query) {
             $query->select('user_id', 'username');
         }])->orderBy('created_at', 'DESC')->take(10)->get();
         // Downloads
         $downloads = Download::with(['object' => function ($query) {
             $query->select('object_id', 'title');
         }])->with(['user' => function ($query) {
             $query->select('user_id', 'username');
         }])->orderBy('created_at', 'DESC')->take(10)->get();
         return view('missionControl.home', ['upcomingMission' => Mission::future()->first(), 'leaderboards' => $leaderboards, 'objects' => $objects, 'comments' => $comments, 'favorites' => $favorites, 'downloads' => $downloads]);
     } else {
         return redirect()->action('MissionControl\\MissionControlController@about');
     }
 }
 public function download($object_id)
 {
     $object = Object::find($object_id);
     // Only increment the downloads table if the same user has not downloaded it in the last hour (just like views)
     $mostRecentDownload = Download::where('user_id', Auth::id())->where('object_id', $object_id)->first();
     if ($mostRecentDownload === null || $mostRecentDownload->created_at->diffInSeconds(Carbon::now()) > 3600) {
         Download::create(array('user_id' => Auth::id(), 'object_id' => $object_id));
         // Reindex object
         Redis::sadd('objects:toReindex', $object_id);
     }
     if ($object->hasFile()) {
         return response()->json(null, 204);
     } else {
         if ($object->type == MissionControlType::Tweet) {
             $data = $object->tweet_text;
         } else {
             if ($object->type == MissionControlType::Comment) {
                 if ($object->subtype == MissionControlSubtype::RedditComment) {
                     $data = $object->reddit_comment_text;
                 } else {
                     if ($object->subtype == MissionControlSubtype::NSFComment) {
                         $data = $object->nsf_comment_text;
                     }
                 }
             } else {
                 if ($object->type == MissionControlType::Article) {
                     $data = $object->article_text;
                 } else {
                     if ($object->type == MissionControlType::Text) {
                         $data = $object->summary;
                     }
                 }
             }
         }
         return response()->make($data, 200, array('Content-type' => 'text/plain', 'Content-Disposition' => 'attachment;filename="' . str_slug($object->title) . '.txt"'));
     }
 }