diff --git a/config/sql/se/ProjectReminders.sql b/config/sql/se/ProjectReminders.sql new file mode 100644 index 00000000..db45cf82 --- /dev/null +++ b/config/sql/se/ProjectReminders.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS `se`.`ProjectReminders` ( + `ProjectId` INT UNSIGNED NOT NULL, + `Created` TIMESTAMP NOT NULL, + `Type` ENUM('abandoned', 'stalled') NOT NULL); diff --git a/config/sql/se/Projects.sql b/config/sql/se/Projects.sql index 0defcee2..96e8f37d 100644 --- a/config/sql/se/Projects.sql +++ b/config/sql/se/Projects.sql @@ -14,5 +14,6 @@ CREATE TABLE IF NOT EXISTS `Projects` ( `ReviewerUserId` int(11) NOT NULL, `LastCommitTimestamp` DATETIME NULL DEFAULT NULL, `LastDiscussionTimestamp` DATETIME NULL DEFAULT NULL, + `IsStatusAutomaticallyUpdated` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`ProjectId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; diff --git a/lib/Constants.php b/lib/Constants.php index be3641bc..5563ab56 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -81,6 +81,7 @@ const POSTMARK_WEBHOOK_LOG_FILE_PATH = '/var/log/local/webhooks-postmark.log'; / const ZOHO_WEBHOOK_LOG_FILE_PATH = '/var/log/local/webhooks-zoho.log'; // Must be writable by `www-data` Unix user. const DONATIONS_LOG_FILE_PATH = '/var/log/local/donations.log'; // Must be writable by `www-data` Unix user. const ARTWORK_UPLOADS_LOG_FILE_PATH = '/var/log/local/artwork-uploads.log'; // Must be writable by `www-data` Unix user. +const EMAIL_LOG_FILE_PATH = '/var/log/local/standardebooks.org-email.log'; // Must be writable by `www-data` Unix user. define('PD_YEAR', intval(PD_NOW->format('Y')) - 96); define('PD_STRING', 'January 1, ' . (PD_YEAR + 1)); diff --git a/lib/Email.php b/lib/Email.php index d06307fa..513e6f4b 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -73,10 +73,11 @@ class Email{ } if(SITE_STATUS == SITE_STATUS_DEV){ - Log::WriteErrorLogEntry('Sending mail to ' . $this->To . ' from ' . $this->From); - Log::WriteErrorLogEntry('Subject: ' . $this->Subject); - Log::WriteErrorLogEntry($this->Body); - Log::WriteErrorLogEntry($this->TextBody); + $log = new Log(EMAIL_LOG_FILE_PATH); + $log->Write('Sending mail to ' . $this->To . ' from ' . $this->From); + $log->Write('Subject: ' . $this->Subject); + $log->Write($this->Body); + $log->Write($this->TextBody); } else{ $phpMailer->Send(); diff --git a/lib/Enums/ProjectReminderType.php b/lib/Enums/ProjectReminderType.php new file mode 100644 index 00000000..61e1c6f1 --- /dev/null +++ b/lib/Enums/ProjectReminderType.php @@ -0,0 +1,10 @@ +', '_', '{', '}', '|'], + ['\\\\', '\-', '\#', '\*', '\+', '\`', '\.', '\[', '\]', '\(', '\)', '\!', '\<', '\>', '\_', '\{', '\}', '\|'], + $text); + } + /** * Convert a string of Markdown into HTML. */ diff --git a/lib/Project.php b/lib/Project.php index c7424643..8637444c 100644 --- a/lib/Project.php +++ b/lib/Project.php @@ -12,10 +12,11 @@ use Safe\DateTimeImmutable; /** * @property Ebook $Ebook - * @property User $ManagerUser - * @property User $ReviewerUser + * @property User $Manager + * @property User $Reviewer * @property string $Url * @property DateTimeImmutable $LastActivityTimestamp The timestamp of the latest activity, whether it's a commit, a discussion post, or simply the started timestamp. + * @property array $Reminders */ class Project{ use Traits\Accessor; @@ -36,12 +37,15 @@ class Project{ public int $ReviewerUserId; public ?DateTimeImmutable $LastCommitTimestamp = null; public ?DateTimeImmutable $LastDiscussionTimestamp = null; + public bool $IsStatusAutomaticallyUpdated = true; protected Ebook $_Ebook; - protected User $_ManagerUser; - protected User $_ReviewerUser; + protected User $_Manager; + protected User $_Reviewer; protected string $_Url; protected DateTimeImmutable $_LastActivityTimestamp; + /** @var array $_Reminders */ + protected array $_Reminders; // ******* @@ -72,6 +76,39 @@ class Project{ return $this->_LastActivityTimestamp; } + /** + * @throws Exceptions\UserNotFoundException If the `User` can't be found. + */ + protected function GetManager(): User{ + if(!isset($this->_Manager)){ + $this->_Manager = User::Get($this->ManagerUserId); + } + + return $this->_Manager; + } + + /** + * @throws Exceptions\UserNotFoundException If the `User` can't be found. + */ + protected function GetReviewer(): User{ + if(!isset($this->_Reviewer)){ + $this->_Reviewer = User::Get($this->ReviewerUserId); + } + + return $this->_Reviewer; + } + + /** + * @return array + */ + protected function GetReminders(): array{ + if(!isset($this->_Reminders)){ + $this->_Reminders = Db::Query('SELECT * from ProjectReminders where ProjectId = ? order by Created asc', [$this->ProjectId], ProjectReminder::class); + } + + return $this->_Reminders; + } + // ******* // METHODS @@ -134,7 +171,7 @@ class Project{ } else{ try{ - $this->_ManagerUser = User::Get($this->ManagerUserId); + $this->_Manager = User::Get($this->ManagerUserId); } catch(Exceptions\UserNotFoundException){ $error->Add(new Exceptions\UserNotFoundException('Manager user not found.')); @@ -146,7 +183,7 @@ class Project{ } else{ try{ - $this->_ReviewerUser = User::Get($this->ReviewerUserId); + $this->_Reviewer = User::Get($this->ReviewerUserId); } catch(Exceptions\UserNotFoundException){ $error->Add(new Exceptions\UserNotFoundException('Reviewer user not found.')); @@ -215,7 +252,8 @@ class Project{ ManagerUserId, ReviewerUserId, LastCommitTimestamp, - LastDiscussionTimestamp + LastDiscussionTimestamp, + IsStatusAutomaticallyUpdated ) values ( @@ -232,11 +270,49 @@ class Project{ ?, ?, ?, + ?, ? ) - ', [$this->EbookId, $this->Status, $this->ProducerName, $this->ProducerEmail, $this->DiscussionUrl, $this->VcsUrl, NOW, NOW, $this->Started, $this->Ended, $this->ManagerUserId, $this->ReviewerUserId, $this->LastCommitTimestamp, $this->LastDiscussionTimestamp]); + ', [$this->EbookId, $this->Status, $this->ProducerName, $this->ProducerEmail, $this->DiscussionUrl, $this->VcsUrl, NOW, NOW, $this->Started, $this->Ended, $this->ManagerUserId, $this->ReviewerUserId, $this->LastCommitTimestamp, $this->LastDiscussionTimestamp, $this->IsStatusAutomaticallyUpdated]); $this->ProjectId = Db::GetLastInsertedId(); + + // Notify the manager. + if($this->Status == Enums\ProjectStatusType::InProgress){ + if($this->ManagerUserId == $this->ReviewerUserId){ + if($this->Manager->Email !== null){ + $em = new Email(); + $em->From = ADMIN_EMAIL_ADDRESS; + $em->To = $this->Manager->Email; + $em->Subject = 'New ebook project to manage and review'; + $em->Body = Template::EmailManagerNewProject(['project' => $this, 'role' => 'manage and review']); + $em->TextBody = Template::EmailManagerNewProjectText(['project' => $this, 'role' => 'manage and review']); + $em->Send(); + } + } + else{ + if($this->Manager->Email !== null){ + $em = new Email(); + $em->From = ADMIN_EMAIL_ADDRESS; + $em->To = $this->Manager->Email; + $em->Subject = 'New ebook project to manage'; + $em->Body = Template::EmailManagerNewProject(['project' => $this, 'role' => 'manage']); + $em->TextBody = Template::EmailManagerNewProjectText(['project' => $this, 'role' => 'manage']); + $em->Send(); + } + + // Notify the reviewer. + if($this->Reviewer->Email !== null){ + $em = new Email(); + $em->From = ADMIN_EMAIL_ADDRESS; + $em->To = $this->Reviewer->Email; + $em->Subject = 'New ebook project to review'; + $em->Body = Template::EmailManagerNewProject(['project' => $this, 'role' => 'review']); + $em->TextBody = Template::EmailManagerNewProjectText(['project' => $this, 'role' => 'review']); + $em->Send(); + } + } + } } /** @@ -259,10 +335,11 @@ class Project{ ManagerUserId = ?, ReviewerUserId = ?, LastCommitTimestamp = ?, - LastDiscussionTimestamp = ? + LastDiscussionTimestamp = ?, + IsStatusAutomaticallyUpdated = ? where ProjectId = ? - ', [$this->Status, $this->ProducerName, $this->ProducerEmail, $this->DiscussionUrl, $this->VcsUrl, $this->Started, $this->Ended, $this->ManagerUserId, $this->ReviewerUserId, $this->LastCommitTimestamp, $this->LastDiscussionTimestamp, $this->ProjectId]); + ', [$this->Status, $this->ProducerName, $this->ProducerEmail, $this->DiscussionUrl, $this->VcsUrl, $this->Started, $this->Ended, $this->ManagerUserId, $this->ReviewerUserId, $this->LastCommitTimestamp, $this->LastDiscussionTimestamp, $this->IsStatusAutomaticallyUpdated, $this->ProjectId]); if($this->Status == Enums\ProjectStatusType::Abandoned){ Db::Query(' @@ -286,6 +363,7 @@ class Project{ $this->PropertyFromHttp('Ended'); $this->PropertyFromHttp('ManagerUserId'); $this->PropertyFromHttp('ReviewerUserId'); + $this->PropertyFromHttp('IsStatusAutomaticallyUpdated'); } /** @@ -396,6 +474,47 @@ class Project{ } } + public function GetReminder(Enums\ProjectReminderType $type): ?ProjectReminder{ + foreach($this->Reminders as $reminder){ + if($reminder->Type == $type){ + return $reminder; + } + } + + return null; + } + + public function SendReminder(Enums\ProjectReminderType $type): void{ + if($this->ProducerEmail === null || $this->GetReminder($type) !== null){ + return; + } + + $reminder = new ProjectReminder(); + $reminder->ProjectId = $this->ProjectId; + $reminder->Type = $type; + $reminder->Create(); + + $em = new Email(); + $em->From = EDITOR_IN_CHIEF_EMAIL_ADDRESS; + $em->FromName = EDITOR_IN_CHIEF_NAME; + $em->To = $this->ProducerEmail; + $em->Subject = 'Your Standard Ebooks ebook'; + + switch($type){ + case Enums\ProjectReminderType::Stalled: + $em->Body = Template::EmailProjectStalled(); + $em->TextBody = Template::EmailProjectStalledText(); + break; + + case Enums\ProjectReminderType::Abandoned: + $em->Body = Template::EmailProjectAbandoned(); + $em->TextBody = Template::EmailProjectAbandonedText(); + break; + } + + $em->Send(); + } + // *********** // ORM METHODS diff --git a/lib/ProjectReminder.php b/lib/ProjectReminder.php new file mode 100644 index 00000000..73f461cf --- /dev/null +++ b/lib/ProjectReminder.php @@ -0,0 +1,26 @@ +Created = NOW; + Db::Query(' + INSERT + into ProjectReminders + ( + ProjectId, + Created, + Type + ) + values( + ?, + ?, + ? + ) + ', [$this->ProjectId, $this->Created, $this->Type]); + } +} diff --git a/scripts/update-project-statuses b/scripts/update-project-statuses index 12403a14..e5283c5e 100755 --- a/scripts/update-project-statuses +++ b/scripts/update-project-statuses @@ -9,13 +9,15 @@ use Safe\DateTimeImmutable; * Iterate over all `Project`s that are in progress or stalled and get their latest GitHub commit. If the commit is more than 30 days old, mark the `Project` as stalled. */ +/** @var array $projects */ $projects = array_merge( Project::GetAllByStatus(Enums\ProjectStatusType::InProgress), Project::GetAllByStatus(Enums\ProjectStatusType::Stalled) ); $apiKey = trim(file_get_contents('/standardebooks.org/config/secrets/se-vcs-bot@api.github.com')); -$oldestAllowedTimestamp = new DateTimeImmutable('30 days ago'); +$oldestStalledTimestamp = new DateTimeImmutable('60 days ago'); +$oldestAbandonedTimestamp = new DateTimeImmutable('90 days ago'); foreach($projects as $project){ try{ @@ -33,59 +35,39 @@ foreach($projects as $project){ Log::WriteErrorLogEntry($ex->getMessage()); } - if( - $project->Status == Enums\ProjectStatusType::InProgress - && - ( - ( - $project->LastCommitTimestamp !== null - && - $project->LastDiscussionTimestamp === null - && - $project->LastCommitTimestamp < $oldestAllowedTimestamp - ) - || - ( - $project->LastCommitTimestamp !== null - && - $project->LastDiscussionTimestamp !== null - && - $project->LastCommitTimestamp < $oldestAllowedTimestamp - && - $project->LastDiscussionTimestamp < $oldestAllowedTimestamp - ) - ) - ){ - // An active `Project` has stalled. - $project->Status = Enums\ProjectStatusType::Stalled; - } - elseif( - $project->Status == Enums\ProjectStatusType::Stalled - && - ( - ( - $project->LastCommitTimestamp !== null - && - $project->LastDiscussionTimestamp === null - && - $project->LastCommitTimestamp >= $oldestAllowedTimestamp - ) - || - ( - $project->LastCommitTimestamp !== null - && - $project->LastDiscussionTimestamp !== null - && - ( - $project->LastCommitTimestamp >= $oldestAllowedTimestamp - || - $project->LastDiscussionTimestamp >= $oldestAllowedTimestamp - ) - ) - ) - ){ - // Revive previously-stalled `Project`s. - $project->Status = Enums\ProjectStatusType::InProgress; + if($project->IsStatusAutomaticallyUpdated){ + if( + $project->Status == Enums\ProjectStatusType::InProgress + && + $project->LastActivityTimestamp < $oldestStalledTimestamp + ){ + // An active `Project` has stalled. + $project->Status = Enums\ProjectStatusType::Stalled; + + // Send an email to the producer. + $project->SendReminder(Enums\ProjectReminderType::Stalled); + } + elseif( + $project->Status == Enums\ProjectStatusType::Stalled + && + $project->LastActivityTimestamp >= $oldestStalledTimestamp + ){ + // Revive previously-stalled `Project`s. + $project->Status = Enums\ProjectStatusType::InProgress; + } + elseif( + $project->Status == Enums\ProjectStatusType::Stalled + && + $project->LastActivityTimestamp < $oldestStalledTimestamp + && + $project->GetReminder(Enums\ProjectReminderType::Stalled)?->Created < $oldestAbandonedTimestamp + ){ + // A stalled `Project` is now abandoned. + $project->Status = Enums\ProjectStatusType::Abandoned; + + // Send a notification to the producer. + $project->SendReminder(Enums\ProjectReminderType::Abandoned); + } } $project->Save(); diff --git a/templates/EmailFooter.php b/templates/EmailFooter.php index 22d1d2d8..d648d7dd 100644 --- a/templates/EmailFooter.php +++ b/templates/EmailFooter.php @@ -1,17 +1,24 @@ - diff --git a/templates/EmailHeader.php b/templates/EmailHeader.php index 8c132577..31c133a3 100644 --- a/templates/EmailHeader.php +++ b/templates/EmailHeader.php @@ -1,6 +1,7 @@ @@ -38,30 +39,30 @@ $letterhead = $letterhead ?? false; } - div.body.letterhead{ - background-image: url("https://standardebooks.org/images/logo-email.png"); - background-position: top 2em right 2em; - background-repeat: no-repeat; - background-size: 210px 49px; - padding-top: 5em; - } + div.body.letterhead{ + background-image: url("https://standardebooks.org/images/logo-email.png"); + background-position: top 2em center; + background-repeat: no-repeat; + background-size: 210px 49px; + padding-top: 6em; + } - .preheader{ - display: none !important; - color: #ffffff; - font-size: 1px; - height: 0; - line-height: 1px; - mso-hide: all; - opacity: 0; - overflow: hidden; - position: absolute; - top: -9999px; - visibility: hidden; - width: 0; - } + .preheader{ + display: none !important; + color: #ffffff; + font-size: 1px; + height: 0; + line-height: 1px; + mso-hide: all; + opacity: 0; + overflow: hidden; + position: absolute; + top: -9999px; + visibility: hidden; + width: 0; + } img.logo{ @@ -140,6 +141,11 @@ $letterhead = $letterhead ?? false; max-width: 55px; } + .footer.no-links{ + font-size: 1em; + padding-top: 0; + } + footer{ margin-right: 4em; margin-top: 2em; @@ -184,9 +190,17 @@ $letterhead = $letterhead ?? false; text-align: center; } - .letterhead{ - text-align: right; - } + + table.data-table td:first-child{ + font-weight: bold; + text-align: right; + } + + table.data-table td{ + padding: .25em; + border: none; + } + diff --git a/templates/EmailManagerNewProject.php b/templates/EmailManagerNewProject.php new file mode 100644 index 00000000..d6aee931 --- /dev/null +++ b/templates/EmailManagerNewProject.php @@ -0,0 +1,67 @@ + true, 'letterhead' => true]) ?> +

You’ve been assigned a new ebook project to :

+ + + + + + + + + + + + + + + + + + + + + + + DiscussionUrl !== null){ ?> + + + + + + +
Title:Ebook->Title) ?>
Producer: + ProducerEmail !== null){ ?> + ProducerName) ?> + DiscussionUrl !== null){ ?> + ProducerName) ?> + + ProducerName) ?> + +
Manager: + Manager->DisplayName) ?> +
Reviewer: + Reviewer->DisplayName) ?> +
Repository: + GitHub +
Discussion: + Google Groups +
+

If you’re unable to this ebook project, email the Editor-in-Chief and we’ll reassign it.

+ + false]) ?> diff --git a/templates/EmailManagerNewProjectText.php b/templates/EmailManagerNewProjectText.php new file mode 100644 index 00000000..993040b1 --- /dev/null +++ b/templates/EmailManagerNewProjectText.php @@ -0,0 +1,28 @@ + +You’ve been assigned a new ebook project to ****: + +- Title: [Ebook->Title) ?>](Ebook->Url) ?>) + +- Producer: ProducerEmail !== null){ ?>[ProducerName) ?>](mailto:ProducerEmail) ?>)DiscussionUrl !== null){ ?>[ProducerName) ?>](DiscussionUrl) ?>)ProducerName) ?> + + +- Manager: [Manager->DisplayName) ?>](Manager->Url . '/projects') ?>) + +- Reviewer: [Reviewer->DisplayName) ?>](Reviewer->Url . '/projects') ?>) + +- Repository: [GitHub](VcsUrl) ?>) + +DiscussionUrl !== null){ ?> +- Discussion: [Google Groups](DiscussionUrl) ?>) + + +If you’re unable to this ebook project, [email the Editor-in-Chief](mailto:) and we’ll reassign it. + +- [See all of the ebook projects you’re currently assigned to.](Manager->Url ?>/projects) + +- [See all ebook projects.](/projects) diff --git a/templates/EmailProjectAbandoned.php b/templates/EmailProjectAbandoned.php new file mode 100644 index 00000000..1b921cab --- /dev/null +++ b/templates/EmailProjectAbandoned.php @@ -0,0 +1,11 @@ + + + + + + +

Hi there, I haven't heard from you in a while, and your ebook's Github repo hasn't had any activity for a while either. So, I'm assuming production is either stalled or abandoned, and I'm putting your ebook back on our wanted list so someone else can work on it.

+

If you're still actively making timely progress on your ebook, please let me know and I'm happy to put your ebook back in the "in production" list for as long as you need. Otherwise, there's nothing else you need to do.

+

Have a good one!

+ + diff --git a/templates/EmailProjectAbandonedText.php b/templates/EmailProjectAbandonedText.php new file mode 100644 index 00000000..21787065 --- /dev/null +++ b/templates/EmailProjectAbandonedText.php @@ -0,0 +1,5 @@ +Hi there, I haven't heard from you in a while, and your ebook's Github repo hasn't had any activity for a while either. So, I'm assuming production is either stalled or abandoned, and I'm putting your ebook back on our wanted list so someone else can work on it. + +If you're still actively making timely progress on your ebook, please let me know and I'm happy to put your ebook back in the "in production" list for as long as you need. Otherwise, there's nothing else you need to do. + +Have a good one! diff --git a/templates/EmailProjectStalled.php b/templates/EmailProjectStalled.php new file mode 100644 index 00000000..5acd1615 --- /dev/null +++ b/templates/EmailProjectStalled.php @@ -0,0 +1,11 @@ + + + + + + +

Hi there, just wanted to check in to see how your ebook production was going. I noticed there hasn't been much activity in the Github repo lately. Are you continuing to make progress?

+

The toolset has had several updates recently. You can update it by running `pipx upgrade standardebooks`.

+

Let me know how things are coming along!

+ + diff --git a/templates/EmailProjectStalledText.php b/templates/EmailProjectStalledText.php new file mode 100644 index 00000000..38680836 --- /dev/null +++ b/templates/EmailProjectStalledText.php @@ -0,0 +1,5 @@ +Hi there, just wanted to check in to see how your ebook production was going. I noticed there hasn't been much activity in the Github repo lately. Are you continuing to make progress? + +The toolset has had several updates recently. You can update it by running `pipx upgrade standardebooks`. + +Let me know how things are coming along! diff --git a/templates/ProjectForm.php b/templates/ProjectForm.php index 2f48eaac..7e7ecdc9 100644 --- a/templates/ProjectForm.php +++ b/templates/ProjectForm.php @@ -55,6 +55,16 @@ $reviewers = User::GetAllByCanReviewProjects(); + +