Skip to content

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

DEV: Keep server shapes in optimistic topic updates (#45064)

Sérgio Saquetimc3843596ef9b
diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.js
index b76b4ec7731..98881ecfc2f 100644
--- a/frontend/discourse/app/controllers/topic.js
+++ b/frontend/discourse/app/controllers/topic.js
@@ -1422,7 +1422,7 @@ export default class TopicController extends Controller {
// optimistic update
topic.setProperties({
- pinned_at: value ? moment() : null,
+ pinned_at: value ? moment().toISOString() : null,
pinned_globally: false,
pinned_until: value ? until : null,
});
@@ -1437,7 +1437,7 @@ export default class TopicController extends Controller {
// optimistic update
topic.setProperties({
- pinned_at: moment(),
+ pinned_at: moment().toISOString(),
pinned_globally: true,
pinned_until: until,
});
diff --git a/frontend/discourse/app/models/post-stream.js b/frontend/discourse/app/models/post-stream.js
index 87e57941b38..b85801196f6 100644
--- a/frontend/discourse/app/models/post-stream.js
+++ b/frontend/discourse/app/models/post-stream.js
@@ -623,7 +623,7 @@ export default class PostStream extends RestModel {
this.topic.setProperties({
posts_count: (this.topic.posts_count || 0) + 1,
- last_posted_at: new Date(),
+ last_posted_at: new Date().toISOString(),
"details.last_poster": user,
highest_post_number: (this.topic.highest_post_number || 0) + 1,
});
diff --git a/frontend/discourse/app/models/topic.js b/frontend/discourse/app/models/topic.js
index 0279a212794..31d964f962d 100644
--- a/frontend/discourse/app/models/topic.js
+++ b/frontend/discourse/app/models/topic.js
@@ -872,7 +872,7 @@ export default class Topic extends RestModel {
return bookmark.id !== id;
})
);
- this.set("bookmarked", this.bookmarks.length);
+ this.set("bookmarked", this.bookmarks.length > 0);
this.incrementProperty("bookmarksWereChanged");
}
@@ -922,7 +922,7 @@ export default class Topic extends RestModel {
})
.then(() => {
this.setProperties({
- deleted_at: new Date(),
+ deleted_at: new Date().toISOString(),
deleted_by,
"details.can_delete": false,
"details.can_recover": true,
diff --git a/frontend/discourse/tests/unit/controllers/topic-test.js b/frontend/discourse/tests/unit/controllers/topic-test.js
index 828ea70232e..4aedbbcbc36 100644
--- a/frontend/discourse/tests/unit/controllers/topic-test.js
+++ b/frontend/discourse/tests/unit/controllers/topic-test.js
@@ -857,4 +857,38 @@ module("Unit | Controller | topic", function (hooks) {
"regular user's post is fetched into the stream"
);
});
+
+ test("pinning keeps the server's shape for pinned_at", async function (assert) {
+ const controller = getOwner(this).lookup("controller:topic");
+ const model = this.store.createRecord("topic", { id: 1 });
+ sinon.stub(model, "saveStatus").resolves();
+ controller.setProperties({ model });
+
+ await controller.togglePinned();
+ assert.strictEqual(
+ typeof model.pinned_at,
+ "string",
+ "pinning in the category stores a string, like the server sends"
+ );
+ assert.strictEqual(
+ new Date(model.pinned_at).toISOString(),
+ model.pinned_at,
+ "pinning in the category stores an ISO 8601 timestamp"
+ );
+
+ await controller.togglePinned();
+ assert.strictEqual(model.pinned_at, null, "unpinning clears it");
+
+ await controller.pinGlobally();
+ assert.strictEqual(
+ typeof model.pinned_at,
+ "string",
+ "pinning globally stores a string, like the server sends"
+ );
+ assert.strictEqual(
+ new Date(model.pinned_at).toISOString(),
+ model.pinned_at,
+ "pinning globally stores an ISO 8601 timestamp"
+ );
+ });
});
diff --git a/frontend/discourse/tests/unit/models/post-stream-test.js b/frontend/discourse/tests/unit/models/post-stream-test.js
index 15438469510..05e349c4016 100644
--- a/frontend/discourse/tests/unit/models/post-stream-test.js
+++ b/frontend/discourse/tests/unit/models/post-stream-test.js
@@ -1427,4 +1427,32 @@ module("Unit | Model | post-stream", function (hooks) {
(error) => error.jqXHR && error.jqXHR.status === 500
);
});
+
+ test("stagePost keeps the server's shape for last_posted_at", function (assert) {
+ const postStream = buildStream.call(this, 10101, [1]);
+ const store = getOwner(this).lookup("service:store");
+ postStream.appendPost(
+ store.createRecord("post", { id: 1, post_number: 1, topic_id: 10101 })
+ );
+ const user = store.createRecord("user", { username: "eviltrout", id: 321 });
+ const stagedPost = store.createRecord("post", {
+ raw: "hello world this is my new post",
+ topic_id: 10101,
+ });
+ postStream.topic.setProperties({ posts_count: 1, highest_post_number: 1 });
+
+ postStream.stagePost(stagedPost, user);
+
+ const lastPostedAt = postStream.topic.last_posted_at;
+ assert.strictEqual(
+ typeof lastPostedAt,
+ "string",
+ "last_posted_at is a string, like the server sends"
+ );
+ assert.strictEqual(
+ new Date(lastPostedAt).toISOString(),
+ lastPostedAt,
+ "last_posted_at is an ISO 8601 timestamp"
+ );
+ });
});
diff --git a/frontend/discourse/tests/unit/models/topic-test.js b/frontend/discourse/tests/unit/models/topic-test.js
index 6ee8a1edca7..545735464ba 100644
--- a/frontend/discourse/tests/unit/models/topic-test.js
+++ b/frontend/discourse/tests/unit/models/topic-test.js
@@ -3,9 +3,11 @@ import { setupTest } from "ember-qunit";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
import { module, test } from "qunit";
import { withPluginApi } from "discourse/lib/plugin-api";
+import Bookmark from "discourse/models/bookmark";
import Category from "discourse/models/category";
import Topic from "discourse/models/topic";
import TopicDetails from "discourse/models/topic-details";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
module("Unit | Model | topic", function (hooks) {
setupTest(hooks);
@@ -308,4 +310,45 @@ module("Unit | Model | topic", function (hooks) {
"other plugin tracked property is updated via updateFromJson"
);
});
+
+ test("destroy keeps the server's shape for deleted_at", async function (assert) {
+ pretender.delete("/t/1234", () => response(200, {}));
+ const topic = this.store.createRecord("topic", { id: 1234 });
+
+ await topic.destroy({ admin: true, staff: true, visibleGroups: [] });
+
+ assert.strictEqual(
+ typeof topic.deleted_at,
+ "string",
+ "deleted_at is a string, like the server sends"
+ );
+ assert.strictEqual(
+ new Date(topic.deleted_at).toISOString(),
+ topic.deleted_at,
+ "deleted_at is an ISO 8601 timestamp"
+ );
+ assert.true(topic.deleted);
+ });
+
+ test("removeBookmark keeps the server's shape for bookmarked", function (assert) {
+ const topic = this.store.createRecord("topic", { id: 1234 });
+ topic.set("bookmarks", [
+ Bookmark.create({
+ id: 1,
+ bookmarkable_type: "Post",
+ bookmarkable_id: 10,
+ }),
+ Bookmark.create({
+ id: 2,
+ bookmarkable_type: "Post",
+ bookmarkable_id: 11,
+ }),
+ ]);
+
+ topic.removeBookmark(1);
+ assert.true(topic.bookmarked, "a bookmark remains");
+
+ topic.removeBookmark(2);
+ assert.false(topic.bookmarked, "no bookmarks remain");
+ });
});