DEV: Use the Store to create User records (#23584)

This commit is contained in:
Jarek Radosz 2023-09-14 23:26:51 +02:00 committed by GitHub
parent c75b379d6f
commit 4a1621c677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 48 additions and 41 deletions

View File

@ -1,7 +1,7 @@
import EmberObject from "@ember/object";
import RestModel from "discourse/models/rest"; import RestModel from "discourse/models/rest";
import User from "discourse/models/user"; import EmberObject from "@ember/object";
import { ajax } from "discourse/lib/ajax"; 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. 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({ const TopicDetails = RestModel.extend({
store: service(),
loaded: false, loaded: false,
updateFromJson(details) { updateFromJson(details) {
const topic = this.topic; const topic = this.topic;
if (details.allowed_users) { if (details.allowed_users) {
details.allowed_users = details.allowed_users.map(function (u) { details.allowed_users = details.allowed_users.map((u) =>
return User.create(u); this.store.createRecord("user", u)
}); );
} }
if (details.participants) { if (details.participants) {
details.participants = details.participants.map(function (p) { details.participants = details.participants.map((p) => {
p.topic = topic; p.topic = topic;
return EmberObject.create(p); return EmberObject.create(p);
}); });

View File

@ -5,7 +5,7 @@ import { exists, query } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
import pretender, { response } from "discourse/tests/helpers/create-pretender"; import pretender, { response } from "discourse/tests/helpers/create-pretender";
import selectKit from "discourse/tests/helpers/select-kit-helper"; 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) { module("Integration | Component | invite-panel", function (hooks) {
setupRenderingTest(hooks); setupRenderingTest(hooks);
@ -19,8 +19,11 @@ module("Integration | Component | invite-panel", function (hooks) {
}) })
); );
this.currentUser.set("details", { can_invite_via_email: true }); const store = getOwner(this).lookup("service:store");
this.set("inviteModel", User.create(this.currentUser)); const user = store.createRecord("user", {
details: { can_invite_via_email: true },
});
this.set("inviteModel", user);
await render(hbs`<InvitePanel @inviteModel={{this.inviteModel}} />`); await render(hbs`<InvitePanel @inviteModel={{this.inviteModel}} />`);

View File

@ -10,7 +10,6 @@ import {
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import I18n from "I18n"; import I18n from "I18n";
import User from "discourse/models/user";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
module("Integration | Component | Widget | post", function (hooks) { module("Integration | Component | Widget | post", function (hooks) {
@ -936,7 +935,8 @@ module("Integration | Component | Widget | post", function (hooks) {
emoji: "tooth", emoji: "tooth",
description: "off to dentist", 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 }); this.set("args", { user });
await render(hbs`<MountWidget @widget="post" @args={{this.args}} />`); await render(hbs`<MountWidget @widget="post" @args={{this.args}} />`);
@ -950,7 +950,8 @@ module("Integration | Component | Widget | post", function (hooks) {
emoji: "tooth", emoji: "tooth",
description: "off to dentist", 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 }); this.set("args", { user });
await render(hbs`<MountWidget @widget="post" @args={{this.args}} />`); await render(hbs`<MountWidget @widget="post" @args={{this.args}} />`);

View File

@ -1,6 +1,5 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import User from "discourse/models/user";
module("Unit | Controller | preferences/profile", function (hooks) { module("Unit | Controller | preferences/profile", function (hooks) {
setupTest(hooks); setupTest(hooks);
@ -14,8 +13,9 @@ module("Unit | Controller | preferences/profile", function (hooks) {
]); ]);
const controller = this.owner.lookup("controller:preferences/profile"); const controller = this.owner.lookup("controller:preferences/profile");
const store = this.owner.lookup("service:store");
controller.setProperties({ controller.setProperties({
model: User.create({ model: store.createRecord("user", {
id: 70, id: 70,
second_factor_enabled: true, second_factor_enabled: true,
is_anonymous: true, is_anonymous: true,

View File

@ -4,7 +4,6 @@ import { settled } from "@ember/test-helpers";
import pretender, { response } from "discourse/tests/helpers/create-pretender"; import pretender, { response } from "discourse/tests/helpers/create-pretender";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import { Placeholder } from "discourse/lib/posts-with-placeholders"; import { Placeholder } from "discourse/lib/posts-with-placeholders";
import User from "discourse/models/user";
import { next } from "@ember/runloop"; import { next } from "@ember/runloop";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import sinon from "sinon"; import sinon from "sinon";
@ -260,7 +259,7 @@ module("Unit | Controller | topic", function (hooks) {
}); });
test("canDeleteSelected", function (assert) { test("canDeleteSelected", function (assert) {
const currentUser = User.create({ admin: false }); const currentUser = this.store.createRecord("user", { admin: false });
const model = topicWithStream.call(this, { const model = topicWithStream.call(this, {
posts: [ posts: [
{ id: 1, can_delete: false }, { id: 1, can_delete: false },
@ -353,7 +352,7 @@ module("Unit | Controller | topic", function (hooks) {
}); });
test("canChangeOwner", function (assert) { test("canChangeOwner", function (assert) {
const currentUser = User.create({ admin: false }); const currentUser = this.store.createRecord("user", { admin: false });
const model = topicWithStream.call(this, { const model = topicWithStream.call(this, {
posts: [ posts: [
{ id: 1, username: "gary" }, { id: 1, username: "gary" },
@ -385,7 +384,7 @@ module("Unit | Controller | topic", function (hooks) {
}); });
test("modCanChangeOwner", function (assert) { test("modCanChangeOwner", function (assert) {
const currentUser = User.create({ moderator: false }); const currentUser = this.store.createRecord("user", { moderator: false });
const model = topicWithStream.call(this, { const model = topicWithStream.call(this, {
posts: [ posts: [
{ id: 1, username: "gary" }, { id: 1, username: "gary" },

View File

@ -3,7 +3,6 @@ import { setupTest } from "ember-qunit";
import sinon from "sinon"; import sinon from "sinon";
import pretender, { response } from "discourse/tests/helpers/create-pretender"; import pretender, { response } from "discourse/tests/helpers/create-pretender";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import User from "discourse/models/user";
module("Unit | Controller | user-notifications", function (hooks) { module("Unit | Controller | user-notifications", function (hooks) {
setupTest(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) { test("Marks all notifications read when no high priority notifications", function (assert) {
let markRead = false; 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"); const controller = this.owner.lookup("controller:user-notifications");
controller.setProperties({ controller.setProperties({
model: [], model: [],

View File

@ -9,7 +9,6 @@ import {
validateUploadedFiles, validateUploadedFiles,
} from "discourse/lib/uploads"; } from "discourse/lib/uploads";
import I18n from "I18n"; import I18n from "I18n";
import User from "discourse/models/user";
import sinon from "sinon"; import sinon from "sinon";
import { module, test } from "qunit"; import { module, test } from "qunit";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
@ -20,6 +19,7 @@ module("Unit | Utility | uploads", function (hooks) {
hooks.beforeEach(function () { hooks.beforeEach(function () {
this.siteSettings = getOwner(this).lookup("service:site-settings"); this.siteSettings = getOwner(this).lookup("service:site-settings");
this.store = getOwner(this).lookup("service:store");
}); });
test("validateUploadedFiles", function (assert) { test("validateUploadedFiles", function (assert) {
@ -52,7 +52,7 @@ module("Unit | Utility | uploads", function (hooks) {
assert.notOk( assert.notOk(
validateUploadedFiles([{ name: "image.png" }], { validateUploadedFiles([{ name: "image.png" }], {
user: User.create(), user: this.store.createRecord("user"),
siteSettings: this.siteSettings, siteSettings: this.siteSettings,
}), }),
"the upload is not valid" "the upload is not valid"
@ -72,7 +72,7 @@ module("Unit | Utility | uploads", function (hooks) {
assert.ok( assert.ok(
validateUploadedFiles([{ name: "image.png" }], { validateUploadedFiles([{ name: "image.png" }], {
user: User.create(), user: this.store.createRecord("user"),
siteSettings: this.siteSettings, siteSettings: this.siteSettings,
}) })
); );
@ -84,7 +84,7 @@ module("Unit | Utility | uploads", function (hooks) {
assert.ok( assert.ok(
validateUploadedFiles([{ name: "image.png" }], { validateUploadedFiles([{ name: "image.png" }], {
user: User.create({ trust_level: 1 }), user: this.store.createRecord("user", { trust_level: 1 }),
siteSettings: this.siteSettings, siteSettings: this.siteSettings,
}) })
); );
@ -96,7 +96,7 @@ module("Unit | Utility | uploads", function (hooks) {
assert.notOk( assert.notOk(
validateUploadedFiles([{ name: "roman.txt" }], { validateUploadedFiles([{ name: "roman.txt" }], {
user: User.create(), user: this.store.createRecord("user"),
siteSettings: this.siteSettings, siteSettings: this.siteSettings,
}) })
); );
@ -151,7 +151,7 @@ module("Unit | Utility | uploads", function (hooks) {
sinon.stub(dialog, "alert"); sinon.stub(dialog, "alert");
assert.notOk( assert.notOk(
validateUploadedFiles([{ name: "test.jpg" }], { validateUploadedFiles([{ name: "test.jpg" }], {
user: User.create(), user: this.store.createRecord("user"),
siteSettings: this.siteSettings, siteSettings: this.siteSettings,
}) })
); );
@ -167,7 +167,7 @@ module("Unit | Utility | uploads", function (hooks) {
sinon.stub(dialog, "alert"); sinon.stub(dialog, "alert");
assert.notOk( assert.notOk(
validateUploadedFiles([{ name: "test.jpg" }], { validateUploadedFiles([{ name: "test.jpg" }], {
user: User.create({ staff: true }), user: this.store.createRecord("user", { staff: true }),
siteSettings: this.siteSettings, siteSettings: this.siteSettings,
}) })
); );
@ -181,7 +181,7 @@ module("Unit | Utility | uploads", function (hooks) {
this.siteSettings.authorized_extensions = "jpeg"; this.siteSettings.authorized_extensions = "jpeg";
sinon.stub(dialog, "alert"); sinon.stub(dialog, "alert");
let user = User.create({ moderator: true }); let user = this.store.createRecord("user", { moderator: true });
assert.notOk( assert.notOk(
validateUploadedFiles(files, { user, siteSettings: this.siteSettings }) 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) { test("allows valid uploads to go through", function (assert) {
sinon.stub(dialog, "alert"); sinon.stub(dialog, "alert");
let user = User.create({ trust_level: 1 }); let user = this.store.createRecord("user", { trust_level: 1 });
// image // image
let image = { name: "image.png", size: imageSize }; let image = { name: "image.png", size: imageSize };

View File

@ -1,13 +1,14 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import User from "discourse/models/user";
import UserAction from "discourse/models/user-action"; import UserAction from "discourse/models/user-action";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | user-stream", function (hooks) { module("Unit | Model | user-stream", function (hooks) {
setupTest(hooks); setupTest(hooks);
test("basics", function (assert) { 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; const stream = user.stream;
assert.present(stream, "a user has a stream by default"); assert.present(stream, "a user has a stream by default");
assert.strictEqual(stream.user, user, "the stream points back to the user"); 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) { 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; const stream = user.stream;
// defaults to posts/topics // defaults to posts/topics

View File

@ -1,4 +1,3 @@
import User from "discourse/models/user";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import { module, test } from "qunit"; import { module, test } from "qunit";
@ -6,9 +5,8 @@ module("Unit | Route | review-index", function (hooks) {
setupTest(hooks); setupTest(hooks);
test("subscribes and unsubscribes /reviewable_counts(with id) when user menu enabled", function (assert) { test("subscribes and unsubscribes /reviewable_counts(with id) when user menu enabled", function (assert) {
const currentUser = User.create({ const store = this.owner.lookup("service:store");
id: "the-id", const currentUser = store.createRecord("user", { id: 654 });
});
this.owner.unregister("service:current-user"); this.owner.unregister("service:current-user");
this.owner.register("service:current-user", currentUser, { this.owner.register("service:current-user", currentUser, {
instantiate: false, instantiate: false,
@ -18,19 +16,19 @@ module("Unit | Route | review-index", function (hooks) {
const messageBus = this.owner.lookup("service:message-bus"); const messageBus = this.owner.lookup("service:message-bus");
let channels = messageBus.callbacks.map((c) => c.channel); 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")); assert.false(channels.includes("/reviewable_claimed"));
reviewIndexRoute.activate(); reviewIndexRoute.activate();
channels = messageBus.callbacks.map((c) => c.channel); 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")); assert.true(channels.includes("/reviewable_claimed"));
reviewIndexRoute.deactivate(); reviewIndexRoute.deactivate();
channels = messageBus.callbacks.map((c) => c.channel); 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")); assert.false(channels.includes("/reviewable_claimed"));
}); });
}); });

View File

@ -17,7 +17,6 @@ import { translations } from "pretty-text/emoji/data";
import { setupHashtagAutocomplete } from "discourse/lib/hashtag-autocomplete"; import { setupHashtagAutocomplete } from "discourse/lib/hashtag-autocomplete";
import { isPresent } from "@ember/utils"; import { isPresent } from "@ember/utils";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import User from "discourse/models/user";
import ChatMessageInteractor from "discourse/plugins/chat/discourse/lib/chat-message-interactor"; import ChatMessageInteractor from "discourse/plugins/chat/discourse/lib/chat-message-interactor";
import { import {
destroyUserStatuses, destroyUserStatuses,
@ -31,6 +30,7 @@ export default class ChatComposer extends Component {
@service capabilities; @service capabilities;
@service site; @service site;
@service siteSettings; @service siteSettings;
@service store;
@service chat; @service chat;
@service chatComposerPresenceManager; @service chatComposerPresenceManager;
@service chatComposerWarningsTracker; @service chatComposerWarningsTracker;
@ -397,7 +397,7 @@ export default class ChatComposer extends Component {
} }
#addMentionedUser(userData) { #addMentionedUser(userData) {
const user = User.create(userData); const user = this.store.createRecord("user", userData);
this.currentMessage.mentionedUsers.set(user.id, user); this.currentMessage.mentionedUsers.set(user.id, user);
} }