/** * Manage threads that have expired. * * @param App\Board $board * @param Carbon\Carbon $time Optional Carbon that will be the xed_at timestamps. Defaults to now. * @return void */ protected function handleThreadEphemeral(Board $board, Carbon $time = null) { if (is_null($time)) { $time = Carbon::now(); } // Get important settings. $threadsPerPage = (int) $board->getConfig('postsPerPage', 10); // Collect a list of threads which have been modified. $threadsToSave = []; // There are two groups of autoprune settings. // x on day since last reply $sageOnDay = (int) $board->getConfig('epheSageThreadDays', false); $lockOnDay = (int) $board->getConfig('epheLockThreadDays', false); $deleteOnDay = (int) $board->getConfig('epheDeleteThreadDays', false); // x on page (meaning the thread has fallen to this page) $sageOnPage = (int) $board->getConfig('epheSageThreadPage', false); $lockOnPage = (int) $board->getConfig('epheLockThreadPage', false); $deleteOnPage = (int) $board->getConfig('epheDeleteThreadPage', false); // Don't do anything unless we have to. if ($sageOnDay || $lockOnDay || $deleteOnDay || $sageOnPage || $lockOnPage || $deleteOnPage) { $this->comment(" Pruning /{$board->board_uri}/..."); // Modify threads based on these settings. foreach ($board->threads as $threadIndex => $thread) { $threadPage = (int) (floor($threadIndex / $threadsPerPage) + 1); $modified = false; $replyLast = clone $thread->reply_last; // x on day since last reply // This is asking if: // 1) The setting is set ($x > 0) // 2) the last reply date + the number of days permitted by each setting is < now. if (!$thread->isBumplocked() && ($sageOnDay > 0 && $replyLast->addDays($sageOnDay)->isPast() || $sageOnPage > 0 && $sageOnPage <= $threadPage)) { $this->comment(" Bumplocking #{$thread->board_id}"); $modified = true; $thread->bumplocked_at = $time; } if (!$thread->isLocked() && ($lockOnDay > 0 && $replyLast->addDays($lockOnDay)->isPast() || $lockOnPage > 0 && $lockOnPage <= $threadPage)) { $this->comment(" Locking #{$thread->board_id}"); $modified = true; $thread->locked_at = $time; } if (!$thread->isDeleted() && ($deleteOnDay > 0 && $replyLast->addDays($sageOnDay)->isPast() || $deleteOnPage > 0 && $deleteOnPage <= $threadPage)) { $this->comment(" Deleting #{$thread->board_id}"); $modified = true; $thread->deleted_at = $time; } if ($modified) { $threadsToSave[] = $thread; } } if (count($threadsToSave)) { // Save all at once. $board->threads()->saveMany($threadsToSave); } else { $this->comment(" Nothing to do."); } } }