/**
* Update the day of the last activity of the given user
*
- * @param integer $uid
+ * @param array $user
+ * @param bool $refresh_login
* @return void
*/
- public static function updateLastActivity(int $uid)
+ public static function updateLastActivity(array $user, bool $refresh_login)
{
- if (!$uid) {
+ $current_day = DateTimeFormat::utcNow('Y-m-d');
+ if (($user['last-activity'] == $current_day) && (!$refresh_login || DateTimeFormat::utc($user['login_date'], 'z-H') == date('z-H'))) {
return;
}
- $user = self::getById($uid, ['last-activity']);
- if (empty($user)) {
- return;
+ $fields = ['last-activity' => $current_day];
+ if ($refresh_login) {
+ $fields['login_date'] = DateTimeFormat::utcNow();
}
- $current_day = DateTimeFormat::utcNow('Y-m-d');
-
- if ($user['last-activity'] != $current_day) {
- 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]);
- }
+ 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]);
}
/**
$this->baseUrl->redirect();
}
- // Make sure to refresh the last login time for the user if the 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);
+ $this->setForUser($a, $user);
}
}
}
// if we haven't failed up this point, log them in.
$this->session->set('remember', $remember);
- $this->session->set('last_login_date', DateTimeFormat::utcNow());
$openid_identity = $this->session->get('openid_identity');
$openid_server = $this->session->get('openid_server');
* @param array $user_record The current "user" record
* @param bool $login_initial
* @param bool $interactive
- * @param bool $login_refresh
+ * @param bool $refresh_login
*
* @throws HTTPException\FoundException
* @throws HTTPException\MovedPermanentlyException
* @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'];
$this->setXAccMgmtStatusHeader($user_record);
- if ($login_initial || $login_refresh) {
- $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]);
+ User::updateLastActivity($user_record, $refresh_login);
+ if ($login_initial) {
// Regularly update suggestions
if (Contact\Relation::areSuggestionsOutdated($user_record['uid'])) {
Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $user_record['uid']);
throw new UnauthorizedException("This API requires login");
}
- // Don't refresh the login date more often than twice a day to spare database writes
- $login_refresh = strcmp(DateTimeFormat::utc('now - 12 hours'), $record['login_date']) > 0;
-
- DI::auth()->setForUser($a, $record, false, false, $login_refresh);
+ DI::auth()->setForUser($a, $record, false, false, false);
Hook::callAll('logged_in', $record);