DEV: Convert remaining core services to native class syntax (#23756)

This commit is contained in:
David Taylor
2023-10-18 12:28:56 +01:00
committed by GitHub
parent 93c67eeb4f
commit 25ddbd1aa6
4 changed files with 104 additions and 107 deletions

View File

@@ -3,29 +3,29 @@ import Service from "@ember/service";
import A11yDialog from "a11y-dialog";
import { bind } from "discourse-common/utils/decorators";
export default Service.extend({
dialogInstance: null,
message: null,
title: null,
titleElementId: null,
type: null,
export default class DialogService extends Service {
dialogInstance = null;
message = null;
title = null;
titleElementId = null;
type = null;
bodyComponent: null,
bodyComponentModel: null,
bodyComponent = null;
bodyComponentModel = null;
confirmButtonIcon: null,
confirmButtonLabel: null,
confirmButtonClass: null,
confirmButtonDisabled: false,
cancelButtonLabel: null,
cancelButtonClass: null,
shouldDisplayCancel: null,
confirmButtonIcon = null;
confirmButtonLabel = null;
confirmButtonClass = null;
confirmButtonDisabled = false;
cancelButtonLabel = null;
cancelButtonClass = null;
shouldDisplayCancel = null;
didConfirm: null,
didCancel: null,
buttons: null,
class: null,
_confirming: false,
didConfirm = null;
didCancel = null;
buttons = null;
class = null;
_confirming = false;
async dialog(params) {
const {
@@ -97,7 +97,7 @@ export default Service.extend({
this.reset();
});
},
}
alert(params) {
// support string param for easier porting of bootbox.alert
@@ -112,7 +112,7 @@ export default Service.extend({
...params,
type: "alert",
});
},
}
confirm(params) {
return this.dialog({
@@ -121,14 +121,14 @@ export default Service.extend({
buttons: null,
type: "confirm",
});
},
}
notice(message) {
return this.dialog({
message,
type: "notice",
});
},
}
yesNoConfirm(params) {
return this.confirm({
@@ -136,7 +136,7 @@ export default Service.extend({
confirmButtonLabel: "yes_value",
cancelButtonLabel: "no_value",
});
},
}
deleteConfirm(params) {
return this.confirm({
@@ -144,7 +144,7 @@ export default Service.extend({
confirmButtonClass: "btn-danger",
confirmButtonLabel: params.confirmButtonLabel || "delete",
});
},
}
reset() {
this.setProperties({
@@ -172,12 +172,12 @@ export default Service.extend({
_confirming: false,
});
},
}
willDestroy() {
this.dialogInstance?.destroy();
this.reset();
},
}
@bind
didConfirmWrapped() {
@@ -186,15 +186,15 @@ export default Service.extend({
}
this._confirming = true;
this.dialogInstance.hide();
},
}
@bind
cancel() {
this.dialogInstance.hide();
},
}
@bind
enableConfirmButton() {
this.set("confirmButtonDisabled", false);
},
});
}
}

View File

@@ -2,23 +2,21 @@ import { readOnly } from "@ember/object/computed";
import Service from "@ember/service";
import { htmlSafe } from "@ember/template";
import { isEmpty } from "@ember/utils";
import { observes } from "@ember-decorators/object";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import getURL from "discourse-common/lib/get-url";
import discourseComputed, {
bind,
observes,
} from "discourse-common/utils/decorators";
import discourseComputed, { bind } from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
const LOGS_NOTICE_KEY = "logs-notice-text";
export default Service.extend({
text: "",
export default class LogsNoticeService extends Service {
text = "";
isAdmin: readOnly("currentUser.admin"),
@readOnly("currentUser.admin") isAdmin;
init() {
this._super(...arguments);
super.init(...arguments);
if (
this.siteSettings.alert_admins_if_errors_per_hour === 0 &&
@@ -33,16 +31,16 @@ export default Service.extend({
}
this.messageBus.subscribe("/logs_error_rate_exceeded", this.onLogRateLimit);
},
}
willDestroy() {
this._super(...arguments);
super.willDestroy(...arguments);
this.messageBus.unsubscribe(
"/logs_error_rate_exceeded",
this.onLogRateLimit
);
},
}
@bind
onLogRateLimit(data) {
@@ -67,25 +65,25 @@ export default Service.extend({
url: getURL("/logs"),
})
);
},
}
@discourseComputed("text")
isEmpty(text) {
return isEmpty(text);
},
}
@discourseComputed("text")
message(text) {
return htmlSafe(text);
},
}
@discourseComputed("isEmpty", "isAdmin")
hidden(thisIsEmpty, isAdmin) {
return !isAdmin || thisIsEmpty;
},
}
@observes("text")
_updateKeyValueStore() {
this.keyValueStore.setItem(LOGS_NOTICE_KEY, this.text);
},
});
}
}

View File

@@ -13,40 +13,39 @@ import { deepEqual, deepMerge } from "discourse-common/lib/object";
import { bind } from "discourse-common/utils/decorators";
// See private_message_topic_tracking_state.rb for documentation
const PrivateMessageTopicTrackingState = Service.extend({
CHANNEL_PREFIX: "/private-message-topic-tracking-state",
inbox: null,
filter: null,
activeGroup: null,
class PrivateMessageTopicTrackingState extends Service {
CHANNEL_PREFIX = "/private-message-topic-tracking-state";
inbox = null;
filter = null;
activeGroup = null;
init() {
this._super(...arguments);
super.init(...arguments);
this.states = new Map();
this.statesModificationCounter = 0;
this.isTracking = false;
this.newIncoming = [];
this.stateChangeCallbacks = new Map();
},
}
willDestroy() {
this._super(...arguments);
super.willDestroy(...arguments);
if (this.currentUser) {
this.messageBus.unsubscribe(this.userChannel(), this._processMessage);
}
this.messageBus.unsubscribe(this.groupChannel("*"), this._processMessage);
},
}
onStateChange(key, callback) {
this.stateChangeCallbacks.set(key, callback);
},
}
offStateChange(key) {
this.stateChangeCallbacks.delete(key);
},
}
startTracking() {
if (this.isTracking) {
@@ -65,7 +64,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
return this._loadInitialState().finally(() => {
this.set("isTracking", true);
});
},
}
lookupCount(type, opts = {}) {
const typeFilterFn = type === "new" ? this._isNew : this._isUnread;
@@ -81,7 +80,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
return Array.from(this.states.values()).filter((topic) => {
return typeFilterFn(topic) && filterFn?.(topic, opts.groupName);
}).length;
},
}
trackIncoming(inbox, filter, group) {
this.setProperties({
@@ -90,13 +89,13 @@ const PrivateMessageTopicTrackingState = Service.extend({
activeGroup: group,
isTrackingIncoming: true,
});
},
}
resetIncomingTracking() {
if (this.isTrackingIncoming) {
this.set("newIncoming", []);
}
},
}
stopIncomingTracking() {
if (this.isTrackingIncoming) {
@@ -105,7 +104,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
newIncoming: [],
});
}
},
}
removeTopics(topicIds) {
if (!this.isTracking) {
@@ -114,19 +113,19 @@ const PrivateMessageTopicTrackingState = Service.extend({
topicIds.forEach((topicId) => this.states.delete(topicId));
this._afterStateChange();
},
}
findState(topicId) {
return this.states.get(topicId);
},
}
userChannel() {
return `${this.CHANNEL_PREFIX}/user/${this.currentUser.id}`;
},
}
groupChannel(groupId) {
return `${this.CHANNEL_PREFIX}/group/${groupId}`;
},
}
_isNew(topic) {
return (
@@ -135,7 +134,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
topic.notification_level >= NotificationLevels.TRACKING) &&
!topic.is_seen
);
},
}
_isUnread(topic) {
return (
@@ -143,7 +142,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
topic.last_read_post_number < topic.highest_post_number &&
topic.notification_level >= NotificationLevels.TRACKING
);
},
}
@bind
_isPersonal(topic) {
@@ -156,7 +155,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
return !groups.some((group) => {
return topic.group_ids?.includes(group.id);
});
},
}
@bind
_isGroup(topic, activeGroupName) {
@@ -166,7 +165,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
topic.group_ids?.includes(group.id)
);
});
},
}
@bind
_processMessage(message) {
@@ -217,14 +216,14 @@ const PrivateMessageTopicTrackingState = Service.extend({
break;
}
},
}
_displayMessageForGroupInbox(message) {
return (
this.inbox === "group" &&
message.payload.group_ids.includes(this.activeGroup.id)
);
},
}
_shouldDisplayMessageForInbox(message) {
return (
@@ -235,13 +234,13 @@ const PrivateMessageTopicTrackingState = Service.extend({
return message.payload.group_ids.includes(group.id);
}).length === 0))
);
},
}
_notifyIncoming(topicId) {
if (this.isTrackingIncoming && !this.newIncoming.includes(topicId)) {
this.newIncoming.pushObject(topicId);
}
},
}
_loadInitialState() {
return ajax(
@@ -253,7 +252,7 @@ const PrivateMessageTopicTrackingState = Service.extend({
});
})
.catch(popupAjaxError);
},
}
_modifyState(topicId, data, opts = {}) {
const oldState = this.findState(topicId);
@@ -268,12 +267,12 @@ const PrivateMessageTopicTrackingState = Service.extend({
if (!opts.skipIncrement) {
this._afterStateChange();
}
},
}
_afterStateChange() {
this.incrementProperty("statesModificationCounter");
this.stateChangeCallbacks.forEach((callback) => callback());
},
});
}
}
export default PrivateMessageTopicTrackingState;

View File

@@ -49,26 +49,26 @@ function findAndRemoveMap(type, id) {
flushMap();
export default Service.extend({
_plurals: {
export default class StoreService extends Service {
_plurals = {
category: "categories",
"post-reply": "post-replies",
"post-reply-history": "post_reply_histories",
reviewable_history: "reviewable_histories",
},
};
init() {
this._super(...arguments);
super.init(...arguments);
this.register = this.register || getRegister(this);
},
}
pluralize(thing) {
return this._plurals[thing] || thing + "s";
},
}
addPluralization(thing, plural) {
this._plurals[thing] = plural;
},
}
findAll(type, findArgs) {
const adapter = this.adapterFor(type);
@@ -85,14 +85,14 @@ export default Service.extend({
}
return results;
});
},
}
// Mostly for legacy, things like TopicList without ResultSets
findFiltered(type, findArgs) {
return this.adapterFor(type)
.find(this, type, findArgs)
.then((result) => this._build(type, result));
},
}
_hydrateFindResults(result, type, findArgs) {
if (typeof findArgs === "object") {
@@ -101,7 +101,7 @@ export default Service.extend({
const apiName = this.adapterFor(type).apiNameFor(type);
return this._hydrate(type, result[underscore(apiName)], result);
}
},
}
// See if the store can find stale data. We sometimes prefer to show stale data and
// refresh it in the background.
@@ -112,7 +112,7 @@ export default Service.extend({
results: stale,
refresh: () => this.find(type, findArgs, opts),
};
},
}
find(type, findArgs, opts) {
let adapter = this.adapterFor(type);
@@ -130,7 +130,7 @@ export default Service.extend({
}
return hydrated;
});
},
}
_updateStale(stale, hydrated, primaryKey) {
if (!stale) {
@@ -156,7 +156,7 @@ export default Service.extend({
})
);
return hydrated;
},
}
refreshResults(resultSet, type, url) {
const adapter = this.adapterFor(type);
@@ -167,7 +167,7 @@ export default Service.extend({
);
resultSet.set("content", content);
});
},
}
appendResults(resultSet, type, url) {
const adapter = this.adapterFor(type);
@@ -190,7 +190,7 @@ export default Service.extend({
resultSet.set("loadMoreUrl", null);
}
});
},
}
update(type, id, attrs) {
const adapter = this.adapterFor(type);
@@ -201,7 +201,7 @@ export default Service.extend({
}
return result;
});
},
}
createRecord(type, attrs) {
attrs = attrs || {};
@@ -209,7 +209,7 @@ export default Service.extend({
return !!attrs[adapter.primaryKey]
? this._hydrate(type, attrs)
: this._build(type, attrs);
},
}
destroyRecord(type, record) {
const adapter = this.adapterFor(type);
@@ -224,7 +224,7 @@ export default Service.extend({
removeMap(type, record.get(adapter.primaryKey));
return result;
});
},
}
_resultSet(type, result, findArgs) {
const adapter = this.adapterFor(type);
@@ -258,7 +258,7 @@ export default Service.extend({
}
return ResultSet.create(createArgs);
},
}
_build(type, obj) {
const adapter = this.adapterFor(type);
@@ -277,14 +277,14 @@ export default Service.extend({
storeMap(type, obj[adapter.primaryKey], model);
return model;
},
}
adapterFor(type) {
return (
this.register.lookup("adapter:" + type) ||
this.register.lookup("adapter:rest")
);
},
}
_lookupSubType(subType, type, id, root) {
if (root.meta && root.meta.types) {
@@ -312,7 +312,7 @@ export default Service.extend({
return hydrated;
}
}
},
}
_hydrateEmbedded(type, obj, root) {
const adapter = this.adapterFor(type);
@@ -351,7 +351,7 @@ export default Service.extend({
}
}
});
},
}
_hydrate(type, obj, root) {
if (!obj) {
@@ -396,7 +396,7 @@ export default Service.extend({
}
return this._build(type, obj);
},
});
}
}
export { flushMap };