A platform for community discussion. Free, open, simple.
DEV: Use the Store to create User records (#23584)
Jarek Radosz4a1621c677f2
diff --git a/app/assets/javascripts/discourse/app/models/topic-details.js b/app/assets/javascripts/discourse/app/models/topic-details.jsindex fdb43c38967..8b0f8e0521e 100644--- a/app/assets/javascripts/discourse/app/models/topic-details.js+++ b/app/assets/javascripts/discourse/app/models/topic-details.js@@ -1,7 +1,7 @@-import EmberObject from "@ember/object";import RestModel from "discourse/models/rest";-import User from "discourse/models/user";+import EmberObject from "@ember/object";import { ajax } from "discourse/lib/ajax";+import { inject as service } from "@ember/service";/**A model representing a Topic's details that aren't always present, such as a list of participants.@@ -9,19 +9,21 @@ import { ajax } from "discourse/lib/ajax";**/const TopicDetails = RestModel.extend({+ store: service(),+loaded: false,updateFromJson(details) {const topic = this.topic;if (details.allowed_users) {- details.allowed_users = details.allowed_users.map(function (u) {- return User.create(u);- });+ details.allowed_users = details.allowed_users.map((u) =>+ this.store.createRecord("user", u)+ );}if (details.participants) {- details.participants = details.participants.map(function (p) {+ details.participants = details.participants.map((p) => {p.topic = topic;return EmberObject.create(p);});diff --git a/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js b/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.jsindex 4411032e8dc..493d5c014c6 100644--- a/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js+++ b/app/assets/javascripts/discourse/tests/integration/components/invite-panel-test.js@@ -5,7 +5,7 @@ import { exists, query } from "discourse/tests/helpers/qunit-helpers";import { hbs } from "ember-cli-htmlbars";import pretender, { response } from "discourse/tests/helpers/create-pretender";import selectKit from "discourse/tests/helpers/select-kit-helper";-import User from "discourse/models/user";+import { getOwner } from "@ember/application";module("Integration | Component | invite-panel", function (hooks) {setupRenderingTest(hooks);@@ -19,8 +19,11 @@ module("Integration | Component | invite-panel", function (hooks) {}));- this.currentUser.set("details", { can_invite_via_email: true });- this.set("inviteModel", User.create(this.currentUser));+ const store = getOwner(this).lookup("service:store");+ const user = store.createRecord("user", {+ details: { can_invite_via_email: true },+ });+ this.set("inviteModel", user);await render(hbs`<InvitePanel @inviteModel={{this.inviteModel}} />`);diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.jsindex 3ad97ae96c9..0a0023a809b 100644--- a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js+++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js@@ -10,7 +10,6 @@ import {import { hbs } from "ember-cli-htmlbars";import EmberObject from "@ember/object";import I18n from "I18n";-import User from "discourse/models/user";import { getOwner } from "discourse-common/lib/get-owner";module("Integration | Component | Widget | post", function (hooks) {@@ -936,7 +935,8 @@ module("Integration | Component | Widget | post", function (hooks) {emoji: "tooth",description: "off to dentist",};- const user = User.create({ status });+ const store = getOwner(this).lookup("service:store");+ const user = store.createRecord("user", { status });this.set("args", { user });await render(hbs`<MountWidget @widget="post" @args={{this.args}} />`);@@ -950,7 +950,8 @@ module("Integration | Component | Widget | post", function (hooks) {emoji: "tooth",description: "off to dentist",};- const user = User.create({ status });+ const store = getOwner(this).lookup("service:store");+ const user = store.createRecord("user", { status });this.set("args", { user });await render(hbs`<MountWidget @widget="post" @args={{this.args}} />`);diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.jsindex 95f7e33dd2b..98dae53abcd 100644--- a/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js+++ b/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js@@ -1,6 +1,5 @@import { module, test } from "qunit";import { setupTest } from "ember-qunit";-import User from "discourse/models/user";module("Unit | Controller | preferences/profile", function (hooks) {setupTest(hooks);@@ -14,8 +13,9 @@ module("Unit | Controller | preferences/profile", function (hooks) {]);const controller = this.owner.lookup("controller:preferences/profile");+ const store = this.owner.lookup("service:store");controller.setProperties({- model: User.create({+ model: store.createRecord("user", {id: 70,second_factor_enabled: true,is_anonymous: true,diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.jsindex 940bc252d0d..49f73a52e67 100644--- a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js+++ b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js@@ -4,7 +4,6 @@ import { settled } from "@ember/test-helpers";import pretender, { response } from "discourse/tests/helpers/create-pretender";import EmberObject from "@ember/object";import { Placeholder } from "discourse/lib/posts-with-placeholders";-import User from "discourse/models/user";import { next } from "@ember/runloop";import { getOwner } from "discourse-common/lib/get-owner";import sinon from "sinon";@@ -260,7 +259,7 @@ module("Unit | Controller | topic", function (hooks) {});test("canDeleteSelected", function (assert) {- const currentUser = User.create({ admin: false });+ const currentUser = this.store.createRecord("user", { admin: false });const model = topicWithStream.call(this, {posts: [{ id: 1, can_delete: false },@@ -353,7 +352,7 @@ module("Unit | Controller | topic", function (hooks) {});test("canChangeOwner", function (assert) {- const currentUser = User.create({ admin: false });+ const currentUser = this.store.createRecord("user", { admin: false });const model = topicWithStream.call(this, {posts: [{ id: 1, username: "gary" },@@ -385,7 +384,7 @@ module("Unit | Controller | topic", function (hooks) {});test("modCanChangeOwner", function (assert) {- const currentUser = User.create({ moderator: false });+ const currentUser = this.store.createRecord("user", { moderator: false });const model = topicWithStream.call(this, {posts: [{ id: 1, username: "gary" },diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/user-notifications-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/user-notifications-test.jsindex f408d41af5c..29d0f832921 100644--- a/app/assets/javascripts/discourse/tests/unit/controllers/user-notifications-test.js+++ b/app/assets/javascripts/discourse/tests/unit/controllers/user-notifications-test.js@@ -3,7 +3,6 @@ import { setupTest } from "ember-qunit";import sinon from "sinon";import pretender, { response } from "discourse/tests/helpers/create-pretender";import EmberObject from "@ember/object";-import User from "discourse/models/user";module("Unit | Controller | user-notifications", function (hooks) {setupTest(hooks);@@ -44,7 +43,10 @@ module("Unit | Controller | user-notifications", function (hooks) {test("Marks all notifications read when no high priority notifications", function (assert) {let markRead = false;- const currentUser = User.create({ unread_high_priority_notifications: 0 });+ const store = this.owner.lookup("service:store");+ const currentUser = store.createRecord("user", {+ unread_high_priority_notifications: 0,+ });const controller = this.owner.lookup("controller:user-notifications");controller.setProperties({model: [],diff --git a/app/assets/javascripts/discourse/tests/unit/lib/uploads-test.js b/app/assets/javascripts/discourse/tests/unit/lib/uploads-test.jsindex 97311cdd835..12bb89a044a 100644--- a/app/assets/javascripts/discourse/tests/unit/lib/uploads-test.js+++ b/app/assets/javascripts/discourse/tests/unit/lib/uploads-test.js@@ -9,7 +9,6 @@ import {validateUploadedFiles,} from "discourse/lib/uploads";import I18n from "I18n";-import User from "discourse/models/user";import sinon from "sinon";import { module, test } from "qunit";import { setupTest } from "ember-qunit";@@ -20,6 +19,7 @@ module("Unit | Utility | uploads", function (hooks) {hooks.beforeEach(function () {this.siteSettings = getOwner(this).lookup("service:site-settings");+ this.store = getOwner(this).lookup("service:store");});test("validateUploadedFiles", function (assert) {@@ -52,7 +52,7 @@ module("Unit | Utility | uploads", function (hooks) {assert.notOk(validateUploadedFiles([{ name: "image.png" }], {- user: User.create(),+ user: this.store.createRecord("user"),siteSettings: this.siteSettings,}),"the upload is not valid"@@ -72,7 +72,7 @@ module("Unit | Utility | uploads", function (hooks) {assert.ok(validateUploadedFiles([{ name: "image.png" }], {- user: User.create(),+ user: this.store.createRecord("user"),siteSettings: this.siteSettings,}));@@ -84,7 +84,7 @@ module("Unit | Utility | uploads", function (hooks) {assert.ok(validateUploadedFiles([{ name: "image.png" }], {- user: User.create({ trust_level: 1 }),+ user: this.store.createRecord("user", { trust_level: 1 }),siteSettings: this.siteSettings,}));@@ -96,7 +96,7 @@ module("Unit | Utility | uploads", function (hooks) {assert.notOk(validateUploadedFiles([{ name: "roman.txt" }], {- user: User.create(),+ user: this.store.createRecord("user"),siteSettings: this.siteSettings,}));@@ -151,7 +151,7 @@ module("Unit | Utility | uploads", function (hooks) {sinon.stub(dialog, "alert");assert.notOk(validateUploadedFiles([{ name: "test.jpg" }], {- user: User.create(),+ user: this.store.createRecord("user"),siteSettings: this.siteSettings,}));@@ -167,7 +167,7 @@ module("Unit | Utility | uploads", function (hooks) {sinon.stub(dialog, "alert");assert.notOk(validateUploadedFiles([{ name: "test.jpg" }], {- user: User.create({ staff: true }),+ user: this.store.createRecord("user", { staff: true }),siteSettings: this.siteSettings,}));@@ -181,7 +181,7 @@ module("Unit | Utility | uploads", function (hooks) {this.siteSettings.authorized_extensions = "jpeg";sinon.stub(dialog, "alert");- let user = User.create({ moderator: true });+ let user = this.store.createRecord("user", { moderator: true });assert.notOk(validateUploadedFiles(files, { user, siteSettings: this.siteSettings }));@@ -215,7 +215,7 @@ module("Unit | Utility | uploads", function (hooks) {test("allows valid uploads to go through", function (assert) {sinon.stub(dialog, "alert");- let user = User.create({ trust_level: 1 });+ let user = this.store.createRecord("user", { trust_level: 1 });// imagelet image = { name: "image.png", size: imageSize };diff --git a/app/assets/javascripts/discourse/tests/unit/models/user-stream-test.js b/app/assets/javascripts/discourse/tests/unit/models/user-stream-test.jsindex fba153094d6..959e808b9c9 100644--- a/app/assets/javascripts/discourse/tests/unit/models/user-stream-test.js+++ b/app/assets/javascripts/discourse/tests/unit/models/user-stream-test.js@@ -1,13 +1,14 @@import { module, test } from "qunit";-import User from "discourse/models/user";import UserAction from "discourse/models/user-action";import { setupTest } from "ember-qunit";+import { getOwner } from "discourse-common/lib/get-owner";module("Unit | Model | user-stream", function (hooks) {setupTest(hooks);test("basics", function (assert) {- const user = User.create({ id: 1, username: "eviltrout" });+ const store = getOwner(this).lookup("service:store");+ const user = store.createRecord("user", { id: 1, username: "eviltrout" });const stream = user.stream;assert.present(stream, "a user has a stream by default");assert.strictEqual(stream.user, user, "the stream points back to the user");@@ -20,7 +21,8 @@ module("Unit | Model | user-stream", function (hooks) {});test("filterParam", function (assert) {- const user = User.create({ id: 1, username: "eviltrout" });+ const store = getOwner(this).lookup("service:store");+ const user = store.createRecord("user", { id: 1, username: "eviltrout" });const stream = user.stream;// defaults to posts/topicsdiff --git a/app/assets/javascripts/discourse/tests/unit/routes/review-index-test.js b/app/assets/javascripts/discourse/tests/unit/routes/review-index-test.jsindex d1be337a19d..04b02eacc69 100644--- a/app/assets/javascripts/discourse/tests/unit/routes/review-index-test.js+++ b/app/assets/javascripts/discourse/tests/unit/routes/review-index-test.js@@ -1,4 +1,3 @@-import User from "discourse/models/user";import { setupTest } from "ember-qunit";import { module, test } from "qunit";@@ -6,9 +5,8 @@ module("Unit | Route | review-index", function (hooks) {setupTest(hooks);test("subscribes and unsubscribes /reviewable_counts(with id) when user menu enabled", function (assert) {- const currentUser = User.create({- id: "the-id",- });+ const store = this.owner.lookup("service:store");+ const currentUser = store.createRecord("user", { id: 654 });this.owner.unregister("service:current-user");this.owner.register("service:current-user", currentUser, {instantiate: false,@@ -18,19 +16,19 @@ module("Unit | Route | review-index", function (hooks) {const messageBus = this.owner.lookup("service:message-bus");let channels = messageBus.callbacks.map((c) => c.channel);- assert.false(channels.includes("/reviewable_counts/the-id"));+ assert.false(channels.includes("/reviewable_counts/654"));assert.false(channels.includes("/reviewable_claimed"));reviewIndexRoute.activate();channels = messageBus.callbacks.map((c) => c.channel);- assert.true(channels.includes("/reviewable_counts/the-id"));+ assert.true(channels.includes("/reviewable_counts/654"));assert.true(channels.includes("/reviewable_claimed"));reviewIndexRoute.deactivate();channels = messageBus.callbacks.map((c) => c.channel);- assert.false(channels.includes("/reviewable_counts/the-id"));+ assert.false(channels.includes("/reviewable_counts/654"));assert.false(channels.includes("/reviewable_claimed"));});});diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer.js b/plugins/chat/assets/javascripts/discourse/components/chat-composer.jsindex 3e3268a4877..1db161084a5 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer.js+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer.js@@ -17,7 +17,6 @@ import { translations } from "pretty-text/emoji/data";import { setupHashtagAutocomplete } from "discourse/lib/hashtag-autocomplete";import { isPresent } from "@ember/utils";import { Promise } from "rsvp";-import User from "discourse/models/user";import ChatMessageInteractor from "discourse/plugins/chat/discourse/lib/chat-message-interactor";import {destroyUserStatuses,@@ -31,6 +30,7 @@ export default class ChatComposer extends Component {@service capabilities;@service site;@service siteSettings;+ @service store;@service chat;@service chatComposerPresenceManager;@service chatComposerWarningsTracker;@@ -397,7 +397,7 @@ export default class ChatComposer extends Component {}#addMentionedUser(userData) {- const user = User.create(userData);+ const user = this.store.createRecord("user", userData);this.currentMessage.mentionedUsers.set(user.id, user);}