Quellcode durchsuchen

Fix last user visit

Take data from the online table
Visman vor 5 Jahren
Ursprung
Commit
85785bc9df

+ 6 - 6
app/Models/Online/Info.php

@@ -21,20 +21,20 @@ class Info extends Method
         $this->model->maxNum = $this->c->config->st_max_users;
         $this->model->maxTime = $this->c->config->st_max_users_time;
 
-        $info   = [];
+        $info = [];
         if ($this->c->user->g_view_users == '1') {
-            foreach ($this->model->users as $id => $user) {
+            foreach ($this->model->users as $id => $name) {
                 $info[] = [
                     $this->c->Router->link('User', [
                         'id' => $id,
-                        'name' => $user['name'],
+                        'name' => $name,
                     ]),
-                    $user['name'],
+                    $name,
                 ];
             }
         } else {
-            foreach ($this->model->users as $user) {
-                $info[] = $user['name'];
+            foreach ($this->model->users as $name) {
+                $info[] = $name;
             }
         }
         $this->model->numUsers = \count($info);

+ 29 - 34
app/Models/Online/Model.php

@@ -8,6 +8,19 @@ use ForkBB\Models\Page;
 
 class Model extends ParentModel
 {
+    protected $visits = [];
+    protected $online = [];
+
+    public function lastVisit(User $user): ?int
+    {
+        return $this->visits[$user->id] ?? null;
+    }
+
+    public function isOnline(User $user): bool
+    {
+        return isset($this->online[$user->id]);
+    }
+
     /**
      * Обработка данных пользователей онлайн
      * Обновление данных текущего пользователя
@@ -37,12 +50,10 @@ class Model extends ParentModel
         $now     = \time();
         $tOnline = $now - $this->c->config->o_timeout_online;
         $tVisit  = $now - $this->c->config->o_timeout_visit;
-        $online  = [];
         $users   = [];
         $guests  = [];
         $bots    = [];
-        $deleteG = false;
-        $deleteU = false;
+        $needClean = false;
 
         if ($detail) {
             $sql = 'SELECT o.user_id, o.ident, o.logged, o.o_position, o.o_name FROM ::online AS o ORDER BY o.logged';
@@ -52,23 +63,22 @@ class Model extends ParentModel
         $stmt = $this->c->DB->query($sql);
 
         while ($cur = $stmt->fetch()) {
+            $this->visits[$cur['user_id']] = $cur['logged'];
+
             // посетитель уже не онлайн (или почти не онлайн)
             if ($cur['logged'] < $tOnline) {
-                // пользователь
-                if ($cur['user_id'] > 1) {
-                    if ($cur['logged'] < $tVisit) {
-                        $deleteU = true;
+                if ($cur['logged'] < $tVisit) {
+                    $needClean = true;
+
+                    if ($cur['user_id'] > 1) {
                         $this->c->DB->exec('UPDATE ::users SET last_visit=?i:last WHERE id=?i:id', [':last' => $cur['logged'], ':id' => $cur['user_id']]); //????
                     }
-                // гость
-                } else {
-                    $deleteG = true;
                 }
                 continue;
             }
 
             // пользователи онлайн и общее количество
-            $online[$cur['user_id']] = true;
+            $this->online[$cur['user_id']] = true;
             ++$all;
 
             if (! $detail) {
@@ -82,36 +92,22 @@ class Model extends ParentModel
 
             // пользователь
             if ($cur['user_id'] > 1) {
-                $users[$cur['user_id']] = [
-                    'name'   => $cur['ident'],
-                    'logged' => $cur['logged'],
-                ];
+                $users[$cur['user_id']] = $cur['ident'];
             // гость
             } elseif ($cur['o_name'] == '') {
-                $guests[] = [
-                    'name'   => $cur['ident'],
-                    'logged' => $cur['logged'],
-                ];
+                $guests[] = $cur['ident'];
             // бот
             } else {
-                $bots[$cur['o_name']][] = [
-                    'name'   => $cur['ident'],
-                    'logged' => $cur['logged'],
-                ];
+                $bots[$cur['o_name']][] = $cur['ident'];
             }
         }
 
-        // удаление просроченных пользователей
-        if ($deleteU) {
+        // удаление просроченных посетителей
+        if ($needClean) {
             $this->c->DB->exec('DELETE FROM ::online WHERE logged<?i:visit', [':visit' => $tVisit]);
         }
 
-        // удаление просроченных гостей
-        if ($deleteG) {
-            $this->c->DB->exec('DELETE FROM ::online WHERE user_id=1 AND logged<?i:online', [':online' => $tOnline]);
-        }
-
-        // обновление максимального значение пользоватеелй онлайн
+        // обновление максимального значение посетителей онлайн
         if ($this->c->config->st_max_users < $all) {
             $this->c->config->st_max_users      = $all;
             $this->c->config->st_max_users_time = $now;
@@ -121,8 +117,7 @@ class Model extends ParentModel
         $this->all    = $all;
         $this->detail = $detail;
 
-        unset($online[1]);
-        $this->online = $online;
+        unset($this->online[1]);
 
         if ($detail) {
             $this->users  = $users;
@@ -134,7 +129,7 @@ class Model extends ParentModel
     }
 
     /**
-     * Обновление данных текущего пользователя
+     * Обновление данных текущего посетителя
      *
      * @param string $position
      */

+ 4 - 1
app/Models/Pages/Profile/View.php

@@ -24,6 +24,9 @@ class View extends Profile
         $this->canonical  = $this->curUser->link;
         $this->robots     = null;
         $this->crumbs     = $this->crumbs();
+
+        $this->c->Online->calc($this); // для $this->curUser->lastVisit
+
         $this->form       = $this->form();
         $this->actionBtns = $this->btns('view');
 
@@ -209,7 +212,7 @@ class View extends Profile
             $fields['lastvisit'] = [
                 'class'   => 'pline',
                 'type'    => 'str',
-                'value'   => \ForkBB\dt($this->curUser->last_visit, true),
+                'value'   => \ForkBB\dt($this->curUser->lastVisit, true),
                 'caption' => \ForkBB\__('Last visit info'),
             ];
         }

+ 16 - 5
app/Models/User/Model.php

@@ -27,10 +27,11 @@ class Model extends DataModel
         parent::__construct($container);
 
         $this->zDepend = [
-            'group_id' => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'link', 'viewUsers', 'showPostCount', 'searchUsers'],
-            'id' => ['isGuest', 'link', 'avatar', 'online'],
-            'logged' => ['isLogged'],
-            'show_sig' => ['showSignature'],
+            'group_id'     => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'link', 'viewUsers', 'showPostCount', 'searchUsers'],
+            'id'           => ['isGuest', 'link', 'avatar', 'online'],
+            'last_visit'   => ['lastVisit'],
+            'logged'       => ['isLogged'],
+            'show_sig'     => ['showSignature'],
             'show_avatars' => ['showAvatar'],
         ];
     }
@@ -129,6 +130,16 @@ class Model extends DataModel
         return ! empty($attr);
     }
 
+    /**
+     * Время последнего визита
+     *
+     * @return int
+     */
+    protected function getlastVisit(): int
+    {
+        return $this->c->Online->lastVisit($this) ?? $this->last_visit;
+    }
+
     /**
      * Текущий язык пользователя
      *
@@ -246,7 +257,7 @@ class Model extends DataModel
      */
     protected function getonline(): bool
     {
-        return isset($this->c->Online->online[$this->id]);
+        return $this->c->Online->isOnline($this);
     }
 
     /**