Merge pull request #13965 from annando/last-activity

Improved assigning of "last-activity" and "login_date"
This commit is contained in:
Hypolite Petovan 2024-03-05 10:38:41 -05:00 committed by GitHub
commit 3ad4ab2940
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 44 deletions

View File

@ -826,27 +826,26 @@ class User
/** /**
* Update the day of the last activity of the given user * Update the day of the last activity of the given user
* *
* @param integer $uid * @param array $user
* @param bool $refresh_login
* @return void * @return void
*/ */
public static function updateLastActivity(int $uid) public static function updateLastActivity(array $user, bool $refresh_login)
{ {
if (!$uid) {
return;
}
$user = self::getById($uid, ['last-activity']);
if (empty($user)) {
return;
}
$current_day = DateTimeFormat::utcNow('Y-m-d'); $current_day = DateTimeFormat::utcNow('Y-m-d');
if (($user['last-activity'] == $current_day) && (!$refresh_login || DateTimeFormat::utc($user['login_date'], 'z-H') == DateTimeFormat::utcNow('z-H'))) {
if ($user['last-activity'] != $current_day) { return;
self::update(['last-activity' => $current_day], $uid);
// Set the last activity for all identities of the user
DBA::update('user', ['last-activity' => $current_day], ['parent-uid' => $uid, 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false]);
} }
$fields = ['last-activity' => $current_day];
if ($refresh_login) {
$fields['login_date'] = DateTimeFormat::utcNow();
}
Logger::debug('Set last activity for user', ['uid' => $user['uid'], 'fields' => $fields]);
self::update($fields, $user['uid']);
// Set the last activity for all identities of the user
DBA::update('user', $fields, ['parent-uid' => $user['uid'], 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false]);
} }
/** /**

View File

@ -194,18 +194,7 @@ class Authentication
$this->baseUrl->redirect(); $this->baseUrl->redirect();
} }
// Make sure to refresh the last login time for the user if the user $this->setForUser($a, $user);
// stays logged in for a long time, e.g. with "Remember Me"
$login_refresh = false;
if (!$this->session->get('last_login_date')) {
$this->session->set('last_login_date', DateTimeFormat::utcNow());
}
if (strcmp(DateTimeFormat::utc('now - 12 hours'), $this->session->get('last_login_date')) > 0) {
$this->session->set('last_login_date', DateTimeFormat::utcNow());
$login_refresh = true;
}
$this->setForUser($a, $user, false, false, $login_refresh);
} }
} }
} }
@ -283,7 +272,6 @@ class Authentication
// if we haven't failed up this point, log them in. // if we haven't failed up this point, log them in.
$this->session->set('remember', $remember); $this->session->set('remember', $remember);
$this->session->set('last_login_date', DateTimeFormat::utcNow());
$openid_identity = $this->session->get('openid_identity'); $openid_identity = $this->session->get('openid_identity');
$openid_server = $this->session->get('openid_server'); $openid_server = $this->session->get('openid_server');
@ -311,7 +299,7 @@ class Authentication
* @param array $user_record The current "user" record * @param array $user_record The current "user" record
* @param bool $login_initial * @param bool $login_initial
* @param bool $interactive * @param bool $interactive
* @param bool $login_refresh * @param bool $refresh_login
* *
* @throws HTTPException\FoundException * @throws HTTPException\FoundException
* @throws HTTPException\MovedPermanentlyException * @throws HTTPException\MovedPermanentlyException
@ -321,7 +309,7 @@ class Authentication
* @throws HTTPException\InternalServerErrorException In case of Friendica specific exceptions * @throws HTTPException\InternalServerErrorException In case of Friendica specific exceptions
* *
*/ */
public function setForUser(App $a, array $user_record, bool $login_initial = false, bool $interactive = false, bool $login_refresh = false) public function setForUser(App $a, array $user_record, bool $login_initial = false, bool $interactive = false, bool $refresh_login = true)
{ {
$my_url = $this->baseUrl . '/profile/' . $user_record['nickname']; $my_url = $this->baseUrl . '/profile/' . $user_record['nickname'];
@ -354,13 +342,9 @@ class Authentication
$this->setXAccMgmtStatusHeader($user_record); $this->setXAccMgmtStatusHeader($user_record);
if ($login_initial || $login_refresh) { User::updateLastActivity($user_record, $refresh_login);
$this->dba->update('user', ['last-activity' => DateTimeFormat::utcNow('Y-m-d'), 'login_date' => DateTimeFormat::utcNow()], ['uid' => $user_record['uid']]);
// Set the login date for all identities of the user
$this->dba->update('user', ['last-activity' => DateTimeFormat::utcNow('Y-m-d'), 'login_date' => DateTimeFormat::utcNow()],
['parent-uid' => $user_record['uid'], 'account_removed' => false]);
if ($login_initial) {
// Regularly update suggestions // Regularly update suggestions
if (Contact\Relation::areSuggestionsOutdated($user_record['uid'])) { if (Contact\Relation::areSuggestionsOutdated($user_record['uid'])) {
Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $user_record['uid']); Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $user_record['uid']);

View File

@ -183,10 +183,7 @@ class BasicAuth
throw new UnauthorizedException("This API requires login"); throw new UnauthorizedException("This API requires login");
} }
// Don't refresh the login date more often than twice a day to spare database writes DI::auth()->setForUser($a, $record, false, false, false);
$login_refresh = strcmp(DateTimeFormat::utc('now - 12 hours'), $record['login_date']) > 0;
DI::auth()->setForUser($a, $record, false, false, $login_refresh);
Hook::callAll('logged_in', $record); Hook::callAll('logged_in', $record);

View File

@ -104,7 +104,10 @@ class OAuth
} }
Logger::debug('Token found', $token); Logger::debug('Token found', $token);
User::updateLastActivity($token['uid']); $user = User::getById($token['uid'], ['uid', 'last-activity', 'login_date']);
if (!empty($user)) {
User::updateLastActivity($user, false);
}
// Regularly update suggestions // Regularly update suggestions
if (Contact\Relation::areSuggestionsOutdated($token['uid'])) { if (Contact\Relation::areSuggestionsOutdated($token['uid'])) {

View File

@ -215,7 +215,7 @@ return [
'id' => 48, 'id' => 48,
'uid' => 0, 'uid' => 0,
'uri-id' => 42, 'uri-id' => 42,
'name' => 'Self contact', 'name' => 'Test user',
'nick' => 'selfcontact', 'nick' => 'selfcontact',
'self' => 0, 'self' => 0,
'nurl' => 'http://friendica.local/profile/selfcontact', 'nurl' => 'http://friendica.local/profile/selfcontact',
@ -928,6 +928,7 @@ return [
[ [
'id' => 1, 'id' => 1,
'uid' => 42, 'uid' => 42,
'locality' => 'DFRN',
], ],
], ],
'group' => [ 'group' => [

View File

@ -43,7 +43,7 @@ abstract class ApiTest extends FixtureTest
// User data that the test database is populated with // User data that the test database is populated with
const SELF_USER = [ const SELF_USER = [
'id' => 42, 'id' => 42,
'name' => 'Self contact', 'name' => 'Test user',
'nick' => 'selfcontact', 'nick' => 'selfcontact',
'nurl' => 'http://localhost/profile/selfcontact' 'nurl' => 'http://localhost/profile/selfcontact'
]; ];