Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add daily activity digest option #483

Merged
merged 4 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/BackgroundJob/DigestMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class DigestMail extends TimedJob {

/** @var DigestSender */
protected $digestSender;
/** @var ITimeFactory */
protected $timeFactory;

public function __construct(DigestSender $digestSender, ITimeFactory $timeFactory) {
// run daily
$this->setInterval(24 * 60 * 60);
// run hourly
$this->setInterval(60 * 60);

$this->digestSender = $digestSender;
$this->timeFactory = $timeFactory;
Expand Down
3 changes: 2 additions & 1 deletion lib/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ public function getFirstActivitySince(string $user, int $timestamp): int {
public function getActivitySince(string $user, int $since, bool $byOthers) {
$query = $this->connection->getQueryBuilder();
$nameParam = $query->createNamedParameter($user);
$query->select($query->func()->count('activity_id'), $query->func()->max('activity_id'))
$query->select($query->func()->count('activity_id', 'count'))
->selectAlias($query->func()->max('activity_id'), 'max')
->from('activity')
->where($query->expr()->eq('affecteduser', $nameParam))
->andWhere($query->expr()->gt('activity_id', $query->createNamedParameter($since, IQueryBuilder::PARAM_INT)));
Expand Down
43 changes: 33 additions & 10 deletions lib/DigestSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace OCA\Activity;

use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Defaults;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
Expand All @@ -33,9 +32,10 @@
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Util;
use Psr\Log\LoggerInterface;

class DigestSender {
const ACTIVITY_LIMIT = 20;
public const ACTIVITY_LIMIT = 20;

private $config;
private $data;
Expand All @@ -46,8 +46,8 @@ class DigestSender {
private $urlGenerator;
private $defaults;
private $l10nFactory;
private $activityManager;
private $dateFormatter;
private $logger;

public function __construct(
IConfig $config,
Expand All @@ -59,8 +59,8 @@ public function __construct(
IURLGenerator $urlGenerator,
Defaults $defaults,
IFactory $l10nFactory,
IManager $activityManager,
IDateTimeFormatter $dateTimeFormatter
IDateTimeFormatter $dateTimeFormatter,
LoggerInterface $logger
) {
$this->config = $config;
$this->data = $data;
Expand All @@ -71,21 +71,41 @@ public function __construct(
$this->urlGenerator = $urlGenerator;
$this->defaults = $defaults;
$this->l10nFactory = $l10nFactory;
$this->activityManager = $activityManager;
$this->dateFormatter = $dateTimeFormatter;
$this->logger = $logger;
}

public function sendDigests(int $now) {
public function sendDigests(int $now): void {
$users = $this->getDigestUsers();
$userLanguages = $this->config->getUserValueForUsers('core', 'lang', $users);
$userTimezones = $this->config->getUserValueForUsers('core', 'timezone', $users);
$digestDate = $this->config->getUserValueForUsers('activity', 'digest', $users);
$defaultLanguage = $this->config->getSystemValue('default_language', 'en');
$defaultTimeZone = date_default_timezone_get();
$timezoneDigestDay = [];

foreach ($users as $user) {
$language = (!empty($userLanguages[$user])) ? $userLanguages[$user] : $defaultLanguage;
$timezone = (!empty($userTimezones[$user])) ? $userTimezones[$user] : $defaultTimeZone;

// Check if the user's timezone is after 6am already
if (!isset($timezoneDigestDay[$timezone])) {
$timezoneDate = new \DateTime('now', new \DateTimeZone($timezone));
if ($timezoneDate->format('H') < 6) {
// Still before 6am, so dont send yet.
$timezoneDate->sub(new \DateInterval('P1D'));
}
$timezoneDigestDay[$timezone] = $timezoneDate->format('Y.m.d');
}

$userDigestDate = $digestDate[$user] ?? '';
if ($userDigestDate === $timezoneDigestDay[$timezone]) {
// User got todays digits already
nickvergessen marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

$this->sendDigestForUser($user, $now, $timezone, $language);
$this->config->setUserValue($user, 'activity', 'digest', $timezoneDigestDay[$timezone]);
}
}

Expand All @@ -104,6 +124,7 @@ private function getLastSendActivity(string $user, int $now): int {
return $lastSend;
}

// Don't flood on first email with old news, just consider the last 24h
return $this->data->getFirstActivitySince($user, $now - (24 * 60 * 60));
}

Expand All @@ -115,8 +136,10 @@ public function sendDigestForUser(string $uid, int $now, string $timezone, strin
return;
}

['count' => $count, 'max' => $lastActivityId] = $this->data->getActivitySince($uid, $lastSend, true);
if ($count == 0) {
['count' => $count, 'max' => $lastActivityId] = $this->data->getActivitySince($uid, $lastSend, false);
$count = (int) $count;
$lastActivityId = (int) $lastActivityId;
if ($count === 0) {
return;
}

Expand Down Expand Up @@ -171,7 +194,7 @@ public function sendDigestForUser(string $uid, int $now, string $timezone, strin
$this->mailer->send($message);
$this->config->setUserValue($user->getUID(), 'activity', 'activity_digest_last_send', $lastActivityId);
} catch (\Exception $e) {
var_dump($e->getMessage());
$this->logger->error($e->getMessage());
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/settings/personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@

<input id="activity_email_enabled" name="activity_digest" type="checkbox" class="checkbox"
value="1" <?php if ($_['activity_digest_enabled']) { print_unescaped('checked="checked"'); } ?> />
<label for="activity_email_enabled"><?php p($l->t('Send daily activity summary')); ?></label>
<label for="activity_email_enabled"><?php p($l->t('Send daily activity summary in the morning')); ?></label>
</form>