diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ad30797f..b50e14a30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ Versioning](https://semver.org/spec/v2.0.0.html). - [PR-412](https://github.com/itk-dev/os2loop/pull/412) - Fixed bug in toc +- Fixed mail notifications not being sent to users whose last notification + time was stored by Drupal 10 (regression since 1.3.0) ## [1.5.2] - 2026-11-18 diff --git a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/Helper.php b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/Helper.php index cba909ab3..70d01b7f5 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/Helper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/Helper.php @@ -153,7 +153,7 @@ public function sendNotifications(DrupalDateTime $now) { // loading until actually needed. $messages = NULL; foreach ($users as $user) { - if ($this->getUserLastNotifiedAt($user) > $startTime) { + if ($this->getUserLastNotifiedAt($user) > $startTime->getTimestamp()) { continue; } if (NULL === $messages) { @@ -204,15 +204,26 @@ public function userInsert(EntityInterface $entity) { * @param \Drupal\user\UserInterface $user * The user. * - * @return \Drupal\Core\Datetime\DrupalDateTime - * The last notification time. + * @return int + * The last notification time as a Unix timestamp (0 if never notified). */ - private function getUserLastNotifiedAt(UserInterface $user) { + private function getUserLastNotifiedAt(UserInterface $user): int { if (NULL === $this->userLastNotifiedAt) { $this->userLastNotifiedAt = $this->userData->get(static::MODULE, NULL, self::USER_LAST_NOTIFIED_AT); } - return $this->userLastNotifiedAt[$user->id()] ?? new DrupalDateTime('@0'); + $value = $this->userLastNotifiedAt[$user->id()] ?? NULL; + + // Older versions stored a serialized DrupalDateTime object. Comparing such + // objects with < or > compares their properties one by one (not the + // actual time), and objects serialized by an older Drupal core (with a + // different set of properties) always compare as "later", i.e. users were + // never notified again. Therefore, we always work on timestamps. + if ($value instanceof DrupalDateTime || $value instanceof \DateTimeInterface) { + return (int) $value->getTimestamp(); + } + + return is_numeric($value) ? (int) $value : 0; } /** @@ -224,7 +235,8 @@ private function getUserLastNotifiedAt(UserInterface $user) { * The notification time. */ private function setUserLastNotifiedAt(UserInterface $user, DrupalDateTime $notifiedAt) { - $this->userData->set(static::MODULE, $user->id(), self::USER_LAST_NOTIFIED_AT, $notifiedAt); + // Store a plain timestamp (cf. getUserLastNotifiedAt()). + $this->userData->set(static::MODULE, $user->id(), self::USER_LAST_NOTIFIED_AT, $notifiedAt->getTimestamp()); } /**