REFACTOR: Deprecate Discourse.Site and Discourse.User

This commit is contained in:
Robin Ward 2019-11-12 10:34:50 -05:00
parent 3dd2f2f701
commit 7f651b9666
4 changed files with 34 additions and 41 deletions

View File

@ -9,6 +9,7 @@ import Singleton from "discourse/mixins/singleton";
import RestModel from "discourse/models/rest"; import RestModel from "discourse/models/rest";
import TrustLevel from "discourse/models/trust-level"; import TrustLevel from "discourse/models/trust-level";
import PreloadStore from "preload-store"; import PreloadStore from "preload-store";
import deprecated from "discourse-common/lib/deprecated";
const Site = RestModel.extend({ const Site = RestModel.extend({
isReadOnly: alias("is_readonly"), isReadOnly: alias("is_readonly"),
@ -214,4 +215,18 @@ Site.reopenClass(Singleton, {
} }
}); });
let warned = false;
Object.defineProperty(Discourse, "Site", {
get() {
if (!warned) {
deprecated("Import the Site class instead of using Discourse.Site", {
since: "2.4.0",
dropFrom: "2.6.0"
});
warned = true;
}
return Site;
}
});
export default Site; export default Site;

View File

@ -25,6 +25,7 @@ import { userPath } from "discourse/lib/url";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import { getProperties } from "@ember/object"; import { getProperties } from "@ember/object";
import deprecated from "discourse-common/lib/deprecated";
export const SECOND_FACTOR_METHODS = { export const SECOND_FACTOR_METHODS = {
TOTP: 1, TOTP: 1,
@ -901,4 +902,18 @@ User.reopenClass(Singleton, {
} }
}); });
let warned = false;
Object.defineProperty(Discourse, "User", {
get() {
if (!warned) {
deprecated("Import the User class instead of using Discourse.User", {
since: "2.4.0",
dropFrom: "2.6.0"
});
warned = true;
}
return User;
}
});
export default User; export default User;

View File

@ -8,6 +8,8 @@ import {
default as TopicTrackingState default as TopicTrackingState
} from "discourse/models/topic-tracking-state"; } from "discourse/models/topic-tracking-state";
import ScreenTrack from "discourse/lib/screen-track"; import ScreenTrack from "discourse/lib/screen-track";
import Site from "discourse/models/site";
import User from "discourse/models/user";
const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"]; const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"];
@ -29,7 +31,7 @@ export default {
app.register("message-bus:main", messageBus, { instantiate: false }); app.register("message-bus:main", messageBus, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, "messageBus", "message-bus:main")); ALL_TARGETS.forEach(t => app.inject(t, "messageBus", "message-bus:main"));
const currentUser = Discourse.User.current(); const currentUser = User.current();
app.register("current-user:main", currentUser, { instantiate: false }); app.register("current-user:main", currentUser, { instantiate: false });
const topicTrackingState = TopicTrackingState.create({ const topicTrackingState = TopicTrackingState.create({
@ -49,7 +51,7 @@ export default {
app.inject(t, "siteSettings", "site-settings:main") app.inject(t, "siteSettings", "site-settings:main")
); );
const site = Discourse.Site.current(); const site = Site.current();
app.register("site:main", site, { instantiate: false }); app.register("site:main", site, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, "site", "site:main")); ALL_TARGETS.forEach(t => app.inject(t, "site", "site:main"));

View File

@ -82,18 +82,6 @@ JS
end end
end end
def whitelisted?(path)
@@whitelisted ||= Set.new(
[
"discourse/models/site",
"discourse/models/user",
]
)
@@whitelisted.include?(path)
end
def babel_transpile(source) def babel_transpile(source)
klass = self.class klass = self.class
klass.protect do klass.protect do
@ -131,33 +119,6 @@ JS
@output = klass.v8.eval(source) @output = klass.v8.eval(source)
end end
# For backwards compatibility with plugins, for now export the Global format too.
# We should eventually have an upgrade system for plugins to use ES6 or some other
# resolve based API.
if whitelisted?(scope.logical_path) &&
scope.logical_path =~ /(discourse|admin)\/(controllers|components|views|routes|mixins|models)\/(.*)/
type = Regexp.last_match[2]
file_name = Regexp.last_match[3].gsub(/[\-\/]/, '_')
class_name = file_name.classify
# Rails removes pluralization when calling classify
if file_name.end_with?('s') && (!class_name.end_with?('s'))
class_name << "s"
end
require_name = module_name(scope.root_path, scope.logical_path)
if require_name !~ /\-test$/ && require_name !~ /^discourse\/plugins\//
result = "#{class_name}#{type.classify}"
result = result.gsub(/Model$/, '')
if result != "PostMenuView"
@output << "\n\nDiscourse.#{result} = require('#{require_name}').default;\n"
end
end
end
@output @output
end end