Skip to content

Commit

Permalink
Merge pull request #1831 from nextcloud/backport/1803/stable28
Browse files Browse the repository at this point in the history
[stable28] fix: do not send daily digest email to user who is disabled
  • Loading branch information
yemkareems authored Oct 29, 2024
2 parents 038f6c9 + c16b5bb commit c4ead52
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/DigestSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
Expand Down Expand Up @@ -83,17 +84,23 @@ public function sendDigests(int $now): void {
// User got todays digest already
continue;
}
$userObject = $this->userManager->get($user);
if (!$userObject->isEnabled()) {
// User is disabled so do not send the email but update last sent since after enabling avoid flooding
$this->updateLastSentForUser($userObject, $now);
continue;
}

try {
$this->sendDigestForUser($user, $now, $timezone, $language);
$this->sendDigestForUser($userObject, $now, $timezone, $language);
} catch (\Throwable $e) {
$this->logger->error('Exception occurred while sending user digest email', [
'exception' => $e,
]);
}
// We still update the digest time after an failed email,
// so it hopefully works tomorrow
$this->config->setUserValue($user, 'activity', 'digest', $timezoneDigestDay[$timezone]);
$this->config->setUserValue($userObject->getUID(), 'activity', 'digest', $timezoneDigestDay[$timezone]);
}

$this->activityManager->setRequirePNG(false);
Expand All @@ -118,19 +125,29 @@ private function getLastSendActivity(string $user, int $now): int {
return $this->data->getFirstActivitySince($user, $now - (24 * 60 * 60));
}

public function sendDigestForUser(string $uid, int $now, string $timezone, string $language) {
private function updateLastSentForUser(IUser $user, int $now): void {
$uid = $user->getUID();
$lastSend = $this->getLastSendActivity($uid, $now);

['max' => $lastActivityId] = $this->data->getActivitySince($uid, $lastSend, true);
$lastActivityId = (int)$lastActivityId;

$this->config->setUserValue($uid, 'activity', 'activity_digest_last_send', (string)$lastActivityId);
}

public function sendDigestForUser(IUser $user, int $now, string $timezone, string $language) {
$uid = $user->getUID();
$l10n = $this->l10nFactory->get('activity', $language);
$this->groupHelper->setL10n($l10n);
$lastSend = $this->getLastSendActivity($uid, $now);
$user = $this->userManager->get($uid);
if ($lastSend === 0) {
return;
}
$this->activityManager->setCurrentUserId($uid);

['count' => $count, 'max' => $lastActivityId] = $this->data->getActivitySince($uid, $lastSend, true);
$count = (int) $count;
$lastActivityId = (int) $lastActivityId;
$count = (int)$count;
$lastActivityId = (int)$lastActivityId;
if ($count === 0) {
return;
}
Expand Down Expand Up @@ -185,7 +202,7 @@ public function sendDigestForUser(string $uid, int $now, string $timezone, strin
$this->activityManager->setCurrentUserId(null);
try {
$this->mailer->send($message);
$this->config->setUserValue($user->getUID(), 'activity', 'activity_digest_last_send', (string) $lastActivityId);
$this->config->setUserValue($uid, 'activity', 'activity_digest_last_send', (string)$lastActivityId);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return;
Expand All @@ -206,9 +223,9 @@ protected function getHTMLSubject(IEvent $event): string {
$placeholders[] = '{' . $placeholder . '}';

if ($parameter['type'] === 'file') {
$replacement = (string) $parameter['path'];
$replacement = (string)$parameter['path'];
} else {
$replacement = (string) $parameter['name'];
$replacement = (string)$parameter['name'];
}

if (isset($parameter['link'])) {
Expand Down

0 comments on commit c4ead52

Please sign in to comment.