Skip to content

Node.js based forum software built for the modern web

feat: #8821, allow guest topic views

Barış Soner Uşaklı9e3eb5d41ad2
diff --git a/install/data/defaults.json b/install/data/defaults.json
index 18dd27f612..9cb646a2f8 100644
--- a/install/data/defaults.json
+++ b/install/data/defaults.json
@@ -114,6 +114,8 @@
"hideFullname": 0,
"hideEmail": 0,
"allowGuestHandles": 0,
+ "guestsIncrementTopicViews": 1,
+ "incrementTopicViewsInterval": 60,
"recentMaxTopics": 200,
"disableRecentCategoryFilter": 0,
"maximumRelatedTopics": 0,
diff --git a/public/language/en-GB/admin/settings/guest.json b/public/language/en-GB/admin/settings/guest.json
index a8b9d458f0..3ba81aa778 100644
--- a/public/language/en-GB/admin/settings/guest.json
+++ b/public/language/en-GB/admin/settings/guest.json
@@ -1,5 +1,7 @@
{
"handles": "Guest Handles",
"handles.enabled": "Allow guest handles",
- "handles.enabled-help": "This option exposes a new field that allows guests to pick a name to associate with each post they make. If disabled, they will simply be called \"Guest\""
+ "handles.enabled-help": "This option exposes a new field that allows guests to pick a name to associate with each post they make. If disabled, they will simply be called \"Guest\"",
+ "topic-views": "Topic views",
+ "topic-views.enabled": "Allow guests to increase topic view counts"
}
\ No newline at end of file
diff --git a/public/language/en-GB/admin/settings/post.json b/public/language/en-GB/admin/settings/post.json
index a8d0046355..27493aafbd 100644
--- a/public/language/en-GB/admin/settings/post.json
+++ b/public/language/en-GB/admin/settings/post.json
@@ -34,6 +34,8 @@
"timestamp.cut-off-help": "Dates &amp; times will be shown in a relative manner (e.g. \"3 hours ago\" / \"5 days ago\"), and localised into various\n\t\t\t\t\tlanguages. After a certain point, this text can be switched to display the localised date itself\n\t\t\t\t\t(e.g. 5 Nov 2016 15:30).<br /><em>(Default: <code>30</code>, or one month). Set to 0 to always display dates, leave blank to always display relative times.</em>",
"timestamp.necro-threshold": "Necro Threshold (in days)",
"timestamp.necro-threshold-help": "A message will be shown between posts if the time between them is longer than the necro threshold. (Default: <code>7</code>, or one week). Set to 0 to disable.</em>",
+ "timestamp.topic-views-interval": "Increment topic views interval (in minutes)",
+ "timestamp.topic-views-interval-help": "Topic views will only increment once every X minutes as defined by this setting.",
"teaser": "Teaser Post",
"teaser.last-post": "Last &ndash; Show the latest post, including the original post, if no replies",
"teaser.last-reply": "Last &ndash; Show the latest reply, or a \"No replies\" placeholder if no replies",
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 0387e4b69b..2b40f4595b 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -127,11 +127,14 @@ function calculateStartStop(page, postIndex, settings) {
}
async function incrementViewCount(req, tid) {
- if (req.uid >= 1) {
+ const allow = req.uid > 0 || (meta.config.guestsIncrementTopicViews && req.uid === 0);
+ if (allow) {
req.session.tids_viewed = req.session.tids_viewed || {};
- if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < Date.now() - 3600000) {
+ const now = Date.now();
+ const interval = meta.config.incrementTopicViewsInterval * 60000;
+ if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < now - interval) {
await topics.increaseViewCount(tid);
- req.session.tids_viewed[tid] = Date.now();
+ req.session.tids_viewed[tid] = now;
}
}
}
diff --git a/src/views/admin/settings/guest.tpl b/src/views/admin/settings/guest.tpl
index e6a9672093..368f384b79 100644
--- a/src/views/admin/settings/guest.tpl
+++ b/src/views/admin/settings/guest.tpl
@@ -15,6 +15,17 @@
</p>
</form>
</div>
+ <div class="col-sm-2 col-xs-12 settings-header">[[admin/settings/guest:topic-views]]</div>
+ <div class="col-sm-10 col-xs-12">
+ <form role="form">
+ <div class="checkbox">
+ <label class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
+ <input class="mdl-switch__input" type="checkbox" data-field="guestsIncrementTopicViews">
+ <span class="mdl-switch__label"><strong>[[admin/settings/guest:topic-views.enabled]]</strong></span>
+ </label>
+ </div>
+ </form>
+ </div>
</div>
<!-- IMPORT admin/partials/settings/footer.tpl -->
\ No newline at end of file
diff --git a/src/views/admin/settings/post.tpl b/src/views/admin/settings/post.tpl
index 082cd15973..bc9f5026ee 100644
--- a/src/views/admin/settings/post.tpl
+++ b/src/views/admin/settings/post.tpl
@@ -170,6 +170,13 @@
[[admin/settings/post:timestamp.necro-threshold-help]]
</p>
</div>
+ <div class="form-group">
+ <label for="incrementTopicViewsInterval">[[admin/settings/post:timestamp.topic-views-interval]]</label>
+ <input type="number" class="form-control" id="incrementTopicViewsInterval" data-field="incrementTopicViewsInterval" />
+ <p class="help-block">
+ [[admin/settings/post:timestamp.topic-views-interval-help]]
+ </p>
+ </div>
</form>
</div>
</div>