/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $users = User::where(['frequency' => 'new-challenge', 'confirmed' => true, 'unsubscribed' => false])->get();
     $latestChallenge = Thread::all()->sortByDesc('published_at')->first();
     $isNewChallenge = $this->verifyNewChallenge($latestChallenge);
     if (!$isNewChallenge) {
         return;
     }
     Cache::forget('last_challenge_sent');
     Cache::forever('last_challenge_sent', $latestChallenge);
     $latestChallenge->markdown_content = (new CommonMarkConverter())->convertToHtml($latestChallenge->content);
     $latestChallenge->markdown_content = str_replace('<pre>', '<pre style="white-space: pre-wrap;"', $latestChallenge->markdown_content);
     $latestChallengeDifficulty = $latestChallenge->difficulty;
     $users = $users->filter(function ($user) use($latestChallengeDifficulty) {
         return $user->hasDifficulty($latestChallengeDifficulty) || $user->hasDifficulty('all') || collect($user->difficulties)->count() == 0;
     });
     $users->each(function ($user) use($latestChallenge) {
         $this->dispatch(new SendChallengesEmail($user, collect([$latestChallenge]), 'new-challenge'));
     });
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $users = User::where(['frequency' => 'weekly', 'confirmed' => true, 'unsubscribed' => false])->get();
     $threads = Thread::all();
     $currentWeekIndex = Carbon::now()->weekOfYear;
     $currentYear = Carbon::now()->year;
     $currentWeekChallenges = $threads->filter(function ($thread) use($currentWeekIndex, $currentYear) {
         return $thread->published_at->weekOfYear == $currentWeekIndex && $thread->published_at->year == $currentYear;
     });
     if ($currentWeekChallenges->count() == 0) {
         return;
     }
     $markdownConverter = new CommonMarkConverter();
     $currentWeekChallenges->map(function ($challenge) use($markdownConverter) {
         $challenge->markdown_content = $markdownConverter->convertToHtml($challenge->content);
         $challenge->markdown_content = str_replace('<pre>', '<pre style="white-space: pre-wrap;"', $challenge->markdown_content);
     });
     $users->each(function ($user) use($currentWeekChallenges) {
         $challengesForUser = $this->getChallengesForCurrentUser($user, $currentWeekChallenges);
         if ($challengesForUser->count() > 0) {
             $this->dispatch(new SendChallengesEmail($user, $challengesForUser, 'weekly'));
         }
     });
 }
 public function index()
 {
     $threads = Thread::all("title");
     $view = new ThreadsView(compact('threads'));
     $view->render();
 }