Skip to content

Commit 185cabd

Browse files
authored
Merge pull request #11127 from nanaya/view-count-topic
More relaxed topic view count
2 parents 3a06ab7 + c4283cf commit 185cabd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

app/Http/Controllers/Forum/TopicsController.php

+1
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ public function show($id)
428428

429429
$pollSummary = PollOption::summary($topic, $currentUser);
430430

431+
$topic->incrementViewCount($currentUser, \Request::ip());
431432
$posts->last()->markRead($currentUser);
432433

433434
$coverModel = $topic->cover ?? new TopicCover();

app/Models/Forum/Topic.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class Topic extends Model implements AfterCommit
103103
'topic_title' => 100,
104104
];
105105

106+
const VIEW_COUNT_INTERVAL = 86400; // 1 day
107+
106108
protected $table = 'phpbb_topics';
107109
protected $primaryKey = 'topic_id';
108110

@@ -541,8 +543,6 @@ public function markRead($user, $markTime)
541543

542544
throw $ex;
543545
}
544-
545-
$this->incrementInstance('topic_views');
546546
} elseif ($status->mark_time < $markTime) {
547547
$status->update(['mark_time' => $markTime]);
548548
}
@@ -555,6 +555,18 @@ public function markRead($user, $markTime)
555555
DB::commit();
556556
}
557557

558+
public function incrementViewCount(?User $user, string $ipAddr): void
559+
{
560+
$lockKey = "view:forum_topic:{$this->getKey()}:";
561+
$lockKey .= $user === null
562+
? "guest:{$ipAddr}"
563+
: "user:{$user->getKey()}";
564+
565+
if (\Cache::lock($lockKey, static::VIEW_COUNT_INTERVAL)->get()) {
566+
$this->incrementInstance('topic_views');
567+
}
568+
}
569+
558570
public function isIssue()
559571
{
560572
return in_array($this->forum_id, $GLOBALS['cfg']['osu']['forum']['issue_forum_ids'], true);

0 commit comments

Comments
 (0)