Skip to content

A platform for community discussion. Free, open, simple.

FIX: make sure every user instance has correct status tracking counter

Andrei Prigorshnev47917c0be429
diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js
index 213a452de7d..993bc70f380 100644
--- a/app/assets/javascripts/discourse/app/models/user.js
+++ b/app/assets/javascripts/discourse/app/models/user.js
@@ -1178,6 +1178,32 @@ User.reopenClass(Singleton, {
},
});
+User.reopenClass({
+ create(args) {
+ args = args || {};
+ this.deleteStatusTrackingFields(args);
+ return this._super(args);
+ },
+
+ deleteStatusTrackingFields(args) {
+ // every user instance has to have it's own tracking fields
+ // when creating a new user model
+ // its _subscribersCount and _clearStatusTimerId fields
+ // should be equal to 0 and null
+ // here we makes sure that even if these fields
+ // will be passed in args they won't be set anyway
+ //
+ // this is something that could be implemented by making these fields private,
+ // but EmberObject doesn't support private fields
+ if (args.hasOwnProperty("_subscribersCount")) {
+ delete args._subscribersCount;
+ }
+ if (args.hasOwnProperty("_clearStatusTimerId")) {
+ delete args._clearStatusTimerId;
+ }
+ },
+});
+
// user status tracking
User.reopen(Evented, {
_subscribersCount: 0,
diff --git a/app/assets/javascripts/discourse/tests/unit/models/user-test.js b/app/assets/javascripts/discourse/tests/unit/models/user-test.js
index 534a2b4455c..836a1933b03 100644
--- a/app/assets/javascripts/discourse/tests/unit/models/user-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/models/user-test.js
@@ -163,4 +163,20 @@ module("Unit | Model | user", function () {
user2.stopTrackingStatus();
}
});
+
+ test("create() doesn't set internal status tracking fields", function (assert) {
+ const user = User.create({
+ _subscribersCount: 10,
+ _clearStatusTimerId: 100,
+ });
+
+ assert.notOk(
+ user.hasOwnProperty("_subscribersCount"),
+ "_subscribersCount wasn't set"
+ );
+ assert.notOk(
+ user.hasOwnProperty("_clearStatusTimerId"),
+ "_clearStatusTimerId wasn't set"
+ );
+ });
});