REFACTOR: Remove `Discourse.Session` constants

This commit is contained in:
Robin Ward 2019-11-08 13:46:50 -05:00
parent 7ac2a55588
commit 291572a97a
9 changed files with 25 additions and 16 deletions

View File

@ -13,6 +13,9 @@
//= require ./discourse/lib/utilities //= require ./discourse/lib/utilities
//= require ./discourse/lib/page-visible //= require ./discourse/lib/page-visible
//= require ./discourse/lib/logout //= require ./discourse/lib/logout
//= require ./discourse/mixins/singleton
//= require ./discourse/models/rest
//= require ./discourse/models/session
//= require ./discourse/lib/ajax //= require ./discourse/lib/ajax
//= require ./discourse/lib/text //= require ./discourse/lib/text
//= require ./discourse/lib/hash //= require ./discourse/lib/hash
@ -34,7 +37,6 @@
//= require ./discourse/mixins/scrolling //= require ./discourse/mixins/scrolling
//= require ./discourse/lib/ajax-error //= require ./discourse/lib/ajax-error
//= require ./discourse/models/model //= require ./discourse/models/model
//= require ./discourse/models/rest
//= require ./discourse/models/result-set //= require ./discourse/models/result-set
//= require ./discourse/models/store //= require ./discourse/models/store
//= require ./discourse/models/action-summary //= require ./discourse/models/action-summary

View File

@ -447,9 +447,7 @@ export default Controller.extend(bufferedProperty("model"), {
: "/"; : "/";
ajax("/t/" + topic.get("id") + "/timings.json?last=1", { type: "DELETE" }) ajax("/t/" + topic.get("id") + "/timings.json?last=1", { type: "DELETE" })
.then(() => { .then(() => {
const highestSeenByTopic = Discourse.Session.currentProp( const highestSeenByTopic = this.session.get("highestSeenByTopic");
"highestSeenByTopic"
);
highestSeenByTopic[topic.get("id")] = null; highestSeenByTopic[topic.get("id")] = null;
DiscourseURL.routeTo(goToPath); DiscourseURL.routeTo(goToPath);
}) })

View File

@ -1,6 +1,7 @@
import { run } from "@ember/runloop"; import { run } from "@ember/runloop";
import pageVisible from "discourse/lib/page-visible"; import pageVisible from "discourse/lib/page-visible";
import logout from "discourse/lib/logout"; import logout from "discourse/lib/logout";
import Session from "discourse/models/session";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
let _trackView = false; let _trackView = false;
@ -44,7 +45,7 @@ function handleRedirect(data) {
export function updateCsrfToken() { export function updateCsrfToken() {
return ajax("/session/csrf").then(result => { return ajax("/session/csrf").then(result => {
Discourse.Session.currentProp("csrfToken", result.csrf); Session.currentProp("csrfToken", result.csrf);
}); });
} }
@ -120,7 +121,7 @@ export function ajax() {
// note: for bad CSRF we don't loop an extra request right away. // note: for bad CSRF we don't loop an extra request right away.
// this allows us to eliminate the possibility of having a loop. // this allows us to eliminate the possibility of having a loop.
if (xhr.status === 403 && xhr.responseText === '["BAD CSRF"]') { if (xhr.status === 403 && xhr.responseText === '["BAD CSRF"]') {
Discourse.Session.current().set("csrfToken", null); Session.current().set("csrfToken", null);
} }
// If it's a parsererror, don't reject // If it's a parsererror, don't reject
@ -162,7 +163,7 @@ export function ajax() {
args.type && args.type &&
args.type.toUpperCase() !== "GET" && args.type.toUpperCase() !== "GET" &&
url !== Discourse.getURL("/clicks/track") && url !== Discourse.getURL("/clicks/track") &&
!Discourse.Session.currentProp("csrfToken") !Session.currentProp("csrfToken")
) { ) {
promise = new Promise((resolve, reject) => { promise = new Promise((resolve, reject) => {
ajaxObj = updateCsrfToken().then(() => { ajaxObj = updateCsrfToken().then(() => {

View File

@ -2,6 +2,7 @@ import discourseComputed from "discourse-common/utils/decorators";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import { updateCsrfToken } from "discourse/lib/ajax"; import { updateCsrfToken } from "discourse/lib/ajax";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import Session from "discourse/models/session";
const LoginMethod = EmberObject.extend({ const LoginMethod = EmberObject.extend({
@discourseComputed @discourseComputed
@ -52,7 +53,7 @@ LoginMethod.reopenClass({
const input = document.createElement("input"); const input = document.createElement("input");
input.setAttribute("name", "authenticity_token"); input.setAttribute("name", "authenticity_token");
input.setAttribute("value", Discourse.Session.currentProp("csrfToken")); input.setAttribute("value", Session.currentProp("csrfToken"));
form.appendChild(input); form.appendChild(input);
document.body.appendChild(form); document.body.appendChild(form);

View File

@ -6,6 +6,7 @@ import Model from "discourse/models/model";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import Session from "discourse/models/session";
// Whether to show the category badge in topic lists // Whether to show the category badge in topic lists
function displayCategoryInList(site, category) { function displayCategoryInList(site, category) {
@ -93,7 +94,7 @@ const TopicList = RestModel.extend({
more_topics_url: result.topic_list.more_topics_url more_topics_url: result.topic_list.more_topics_url
}); });
Discourse.Session.currentProp("topicList", this); Session.currentProp("topicList", this);
return this.more_topics_url; return this.more_topics_url;
} }
}); });
@ -123,7 +124,7 @@ const TopicList = RestModel.extend({
i++; i++;
}); });
if (storeInSession) Discourse.Session.currentProp("topicList", this); if (storeInSession) Session.currentProp("topicList", this);
}); });
} }
}); });

View File

@ -19,6 +19,7 @@ import {
on on
} from "discourse-common/utils/decorators"; } from "discourse-common/utils/decorators";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import Session from "discourse/models/session";
export function loadTopicView(topic, args) { export function loadTopicView(topic, args) {
const data = _.merge({}, args); const data = _.merge({}, args);
@ -306,7 +307,7 @@ const Topic = RestModel.extend({
// So take what the browser has seen into consideration. // So take what the browser has seen into consideration.
@discourseComputed("new_posts", "id") @discourseComputed("new_posts", "id")
displayNewPosts(newPosts, id) { displayNewPosts(newPosts, id) {
const highestSeen = Discourse.Session.currentProp("highestSeenByTopic")[id]; const highestSeen = Session.currentProp("highestSeenByTopic")[id];
if (highestSeen) { if (highestSeen) {
const delta = highestSeen - this.last_read_post_number; const delta = highestSeen - this.last_read_post_number;
if (delta > 0) { if (delta > 0) {

View File

@ -1,6 +1,7 @@
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import { queryParams } from "discourse/controllers/discovery-sortable"; import { queryParams } from "discourse/controllers/discovery-sortable";
import { defaultHomepage } from "discourse/lib/utilities"; import { defaultHomepage } from "discourse/lib/utilities";
import Session from "discourse/models/session";
// A helper to build a topic route for a filter // A helper to build a topic route for a filter
function filterQueryParams(params, defaultParams) { function filterQueryParams(params, defaultParams) {
@ -19,7 +20,7 @@ function filterQueryParams(params, defaultParams) {
function findTopicList(store, tracking, filter, filterParams, extras) { function findTopicList(store, tracking, filter, filterParams, extras) {
extras = extras || {}; extras = extras || {};
return new Promise(function(resolve) { return new Promise(function(resolve) {
const session = Discourse.Session.current(); const session = Session.current();
if (extras.cached) { if (extras.cached) {
const cachedList = session.get("topicList"); const cachedList = session.get("topicList");
@ -62,7 +63,7 @@ function findTopicList(store, tracking, filter, filterParams, extras) {
tracking.sync(list, list.filter); tracking.sync(list, list.filter);
tracking.trackIncoming(list.filter); tracking.trackIncoming(list.filter);
} }
Discourse.Session.currentProp("topicList", list); Session.currentProp("topicList", list);
if (list.topic_list && list.topic_list.top_tags) { if (list.topic_list && list.topic_list.top_tags) {
Discourse.Site.currentProp("top_tags", list.topic_list.top_tags); Discourse.Site.currentProp("top_tags", list.topic_list.top_tags);
} }

View File

@ -27,13 +27,15 @@
I18n.defaultLocale = setupData.defaultLocale; I18n.defaultLocale = setupData.defaultLocale;
Discourse.start(); Discourse.start();
Discourse.set("assetVersion", setupData.assetVersion); Discourse.set("assetVersion", setupData.assetVersion);
Discourse.Session.currentProp(
let Session = require("discourse/models/session").default;
Session.currentProp(
"disableCustomCSS", "disableCustomCSS",
setupData.disableCustomCss === "true" setupData.disableCustomCss === "true"
); );
if (setupData.safeMode) { if (setupData.safeMode) {
Discourse.Session.currentProp("safe_mode", setupData.safeMode); Session.currentProp("safe_mode", setupData.safeMode);
} }
Discourse.HighlightJSPath = setupData.highlightJsPath; Discourse.HighlightJSPath = setupData.highlightJsPath;

View File

@ -117,7 +117,9 @@ QUnit.testStart(function(ctx) {
Discourse.SiteSettings = dup(Discourse.SiteSettingsOriginal); Discourse.SiteSettings = dup(Discourse.SiteSettingsOriginal);
Discourse.BaseUri = ""; Discourse.BaseUri = "";
Discourse.BaseUrl = "http://localhost:3000"; Discourse.BaseUrl = "http://localhost:3000";
Discourse.Session.resetCurrent();
let Session = require("discourse/models/session").default;
Session.resetCurrent();
Discourse.User.resetCurrent(); Discourse.User.resetCurrent();
resetSite(Discourse.SiteSettings); resetSite(Discourse.SiteSettings);