From f905220923c63ddf3cca28d0d88736533da1db74 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 30 Nov 2022 22:34:50 +0000 Subject: [PATCH] New field to show the day of the last activity --- database.sql | 1 + doc/database/db_user.md | 1 + src/Model/Contact/Relation.php | 13 ++++++++++++- src/Model/User.php | 16 ++++++++++++++++ src/Security/Authentication.php | 9 +++++++-- src/Security/OAuth.php | 11 +++++++++++ src/Worker/UpdateAllSuggestions.php | 2 +- static/dbstructure.config.php | 1 + 8 files changed, 50 insertions(+), 4 deletions(-) diff --git a/database.sql b/database.sql index cf0793772..f41defe42 100644 --- a/database.sql +++ b/database.sql @@ -59,6 +59,7 @@ CREATE TABLE IF NOT EXISTS `user` ( `language` varchar(32) NOT NULL DEFAULT 'en' COMMENT 'default language', `register_date` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'timestamp of registration', `login_date` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'timestamp of last login', + `last-activity` date COMMENT 'Day of the last activity', `default-location` varchar(255) NOT NULL DEFAULT '' COMMENT 'Default for item.location', `allow_location` boolean NOT NULL DEFAULT '0' COMMENT '1 allows to display the location', `theme` varchar(255) NOT NULL DEFAULT '' COMMENT 'user theme preference', diff --git a/doc/database/db_user.md b/doc/database/db_user.md index 85fe895e9..7f58ce58f 100644 --- a/doc/database/db_user.md +++ b/doc/database/db_user.md @@ -21,6 +21,7 @@ Fields | language | default language | varchar(32) | NO | | en | | | register_date | timestamp of registration | datetime | NO | | 0001-01-01 00:00:00 | | | login_date | timestamp of last login | datetime | NO | | 0001-01-01 00:00:00 | | +| last-activity | Day of the last activity | date | YES | | NULL | | | default-location | Default for item.location | varchar(255) | NO | | | | | allow_location | 1 allows to display the location | boolean | NO | | 0 | | | theme | user theme preference | varchar(255) | NO | | | | diff --git a/src/Model/Contact/Relation.php b/src/Model/Contact/Relation.php index 5a85ebebc..bae59f48c 100644 --- a/src/Model/Contact/Relation.php +++ b/src/Model/Contact/Relation.php @@ -260,6 +260,17 @@ class Relation return true; } + /** + * Check if the cached suggestion is outdated + * + * @param integer $uid + * @return boolean + */ + static public function areSuggestionsOutdated(int $uid): bool + { + return DI::pConfig()->get($uid, 'suggestion', 'last_update') + 3600 < time(); + } + /** * Update contact suggestions for a given user * @@ -268,7 +279,7 @@ class Relation */ static public function updateCachedSuggestions(int $uid) { - if (DI::pConfig()->get($uid, 'suggestion', 'last_update') + 3600 > time()) { + if (!self::areSuggestionsOutdated($uid)) { return; } diff --git a/src/Model/User.php b/src/Model/User.php index 73c380561..6d7efafb1 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -665,6 +665,22 @@ class User return $user; } + /** + * Update the day of the last activity of the given user + * + * @param integer $uid + * @return void + */ + public static function updateLastActivity(int $uid) + { + $user = User::getById($uid, ['last-activity']); + $current_day = date('Y-m-d'); + + if ($user['last-activity'] != $current_day) { + User::update(['last-activity' => $current_day], $uid); + } + } + /** * Generates a human-readable random password * diff --git a/src/Security/Authentication.php b/src/Security/Authentication.php index 61ac4fae0..62318bedb 100644 --- a/src/Security/Authentication.php +++ b/src/Security/Authentication.php @@ -39,6 +39,7 @@ use Friendica\Util\Network; use LightOpenID; use Friendica\Core\L10n; use Friendica\Core\Worker; +use Friendica\Model\Contact; use Psr\Log\LoggerInterface; /** @@ -358,8 +359,12 @@ class Authentication $this->dba->update('user', ['login_date' => DateTimeFormat::utcNow()], ['parent-uid' => $user_record['uid'], 'account_removed' => false]); - // Update suggestions upon login - Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $user_record['uid']); + User::updateLastActivity($user_record['uid']); + + // Regularly update suggestions + if (Contact\Relation::areSuggestionsOutdated($user_record['uid'])) { + Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $user_record['uid']); + } } if ($login_initial) { diff --git a/src/Security/OAuth.php b/src/Security/OAuth.php index 3eaa022c5..27a3dfa11 100644 --- a/src/Security/OAuth.php +++ b/src/Security/OAuth.php @@ -22,8 +22,11 @@ namespace Friendica\Security; use Friendica\Core\Logger; +use Friendica\Core\Worker; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\Model\Contact; +use Friendica\Model\User; use Friendica\Module\BaseApi; use Friendica\Util\DateTimeFormat; @@ -100,6 +103,14 @@ class OAuth return []; } Logger::debug('Token found', $token); + + User::updateLastActivity($token['uid']); + + // Regularly update suggestions + if (Contact\Relation::areSuggestionsOutdated($token['uid'])) { + Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $token['uid']); + } + return $token; } diff --git a/src/Worker/UpdateAllSuggestions.php b/src/Worker/UpdateAllSuggestions.php index 5bc647614..478ba89a9 100644 --- a/src/Worker/UpdateAllSuggestions.php +++ b/src/Worker/UpdateAllSuggestions.php @@ -32,7 +32,7 @@ class UpdateAllSuggestions { public static function execute() { - $users = DBA::select('user', ['uid'], ["`login_date` > ?", DateTimeFormat::utc('now - 7 days')]); + $users = DBA::select('user', ['uid'], ["`last-activity` > ?", DateTimeFormat::utc('now - 3 days', 'Y-m-d')]); while ($user = DBA::fetch($users)) { Contact\Relation::updateCachedSuggestions($user['uid']); } diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 55c900916..2979ca80d 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -115,6 +115,7 @@ return [ "language" => ["type" => "varchar(32)", "not null" => "1", "default" => "en", "comment" => "default language"], "register_date" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "timestamp of registration"], "login_date" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "timestamp of last login"], + "last-activity" => ["type" => "date", "comment" => "Day of the last activity"], "default-location" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Default for item.location"], "allow_location" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "1 allows to display the location"], "theme" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "user theme preference"],