Prechádzať zdrojové kódy

Fix HTTP cache of user queries (#6718)

fix https://github.com/FreshRSS/FreshRSS/issues/6717
Alexandre Alapetite 1 rok pred
rodič
commit
c480e57161
4 zmenil súbory, kde vykonal 31 pridanie a 10 odobranie
  1. 21 2
      app/Models/UserDAO.php
  2. 1 5
      lib/lib_rss.php
  3. 6 1
      p/api/query.php
  4. 3 2
      p/i/index.php

+ 21 - 2
app/Models/UserDAO.php

@@ -45,14 +45,33 @@ class FreshRSS_UserDAO extends Minz_ModelPdo {
 		return is_dir(USERS_PATH . '/' . $username);
 	}
 
+	/** Update time of the last modification action by the user (e.g., mark an article as read) */
 	public static function touch(string $username = ''): bool {
-		if (!FreshRSS_user_Controller::checkUsername($username)) {
+		if ($username === '') {
 			$username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
+		} elseif (!FreshRSS_user_Controller::checkUsername($username)) {
+			return false;
 		}
 		return touch(USERS_PATH . '/' . $username . '/config.php');
 	}
 
+	/** Time of the last modification action by the user (e.g., mark an article as read) */
 	public static function mtime(string $username): int {
-		return @(int)filemtime(USERS_PATH . '/' . $username . '/config.php');
+		return @filemtime(USERS_PATH . '/' . $username . '/config.php') ?: 0;
+	}
+
+	/** Update time of the last new content automatically received by the user (e.g., cron job, WebSub) */
+	public static function ctouch(string $username = ''): bool {
+		if ($username === '') {
+			$username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
+		} elseif (!FreshRSS_user_Controller::checkUsername($username)) {
+			return false;
+		}
+		return touch(USERS_PATH . '/' . $username . '/' . LOG_FILENAME);
+	}
+
+	/** Time of the last new content automatically received by the user (e.g., cron job, WebSub) */
+	public static function ctime(string $username): int {
+		return @filemtime(USERS_PATH . '/' . $username . '/' . LOG_FILENAME) ?: 0;
 	}
 }

+ 1 - 5
lib/lib_rss.php

@@ -631,11 +631,7 @@ function invalidateHttpCache(string $username = ''): bool {
 		Minz_Session::_param('touch', uTimeString());
 		$username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
 	}
-	$ok = @touch(DATA_PATH . '/users/' . $username . '/' . LOG_FILENAME);
-	//if (!$ok) {
-		//TODO: Display notification error on front-end
-	//}
-	return $ok;
+	return FreshRSS_UserDAO::ctouch($username);
 }
 
 /**

+ 6 - 1
p/api/query.php

@@ -47,8 +47,13 @@ if (!FreshRSS_Context::hasUserConf() || !FreshRSS_Context::userConf()->enabled)
 
 if (!file_exists(DATA_PATH . '/no-cache.txt')) {
 	require(LIB_PATH . '/http-conditional.php');
+	$dateLastModification = max(
+		FreshRSS_UserDAO::ctime($user),
+		FreshRSS_UserDAO::mtime($user),
+		@filemtime(DATA_PATH . '/config.php') ?: 0
+	);
 	// TODO: Consider taking advantage of $feedMode, only for monotonous queries {all, categories, feeds} and not dynamic ones {read/unread, favourites, user labels}
-	if (httpConditional(FreshRSS_UserDAO::mtime($user) ?: time(), 0, 0, false, PHP_COMPRESSION, false)) {
+	if (httpConditional($dateLastModification ?: time(), 0, 0, false, PHP_COMPRESSION, false)) {
 		exit();	//No need to send anything
 	}
 }

+ 3 - 2
p/i/index.php

@@ -37,10 +37,11 @@ if (!file_exists($applied_migrations_path)) {
 		require(LIB_PATH . '/http-conditional.php');
 		$currentUser = Minz_User::name();
 		$dateLastModification = $currentUser === null ? time() : max(
-			@filemtime(USERS_PATH . '/' . $currentUser . '/' . LOG_FILENAME) ?: 0,
+			FreshRSS_UserDAO::ctime($currentUser),
+			FreshRSS_UserDAO::mtime($currentUser),
 			@filemtime(DATA_PATH . '/config.php') ?: 0
 		);
-		if (httpConditional($dateLastModification, 0, 0, false, PHP_COMPRESSION, true)) {
+		if (httpConditional($dateLastModification ?: time(), 0, 0, false, PHP_COMPRESSION, true)) {
 			Minz_Session::init('FreshRSS');
 			Minz_Session::_param('keepAlive', 1);	//To prevent the PHP session from expiring
 			exit();	//No need to send anything