DEV: Minor code cleanup (#18225)

Various small changes made while debugging MessageBus-related tests.
This commit is contained in:
Jarek Radosz 2022-09-12 14:05:21 +02:00 committed by GitHub
parent ec32b61319
commit fa58eea64e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 82 additions and 86 deletions

View File

@ -50,11 +50,7 @@ export default Component.extend({
`.indicator-topic-${data.topic_id}`
).classList;
if (data.show_indicator) {
nodeClassList.remove("read");
} else {
nodeClassList.add("read");
}
nodeClassList.toggle("read", !data.show_indicator);
});
}
});

View File

@ -82,11 +82,7 @@ export default Component.extend({
`.indicator-topic-${data.topic_id}`
).classList;
if (data.show_indicator) {
nodeClassList.remove("read");
} else {
nodeClassList.add("read");
}
nodeClassList.toggle("read", !data.show_indicator);
});
}
@ -95,8 +91,7 @@ export default Component.extend({
const rawTopicLink = this.element.querySelector(".raw-topic-link");
rawTopicLink &&
topicTitleDecorators &&
topicTitleDecorators.forEach((cb) =>
topicTitleDecorators?.forEach((cb) =>
cb(this.topic, rawTopicLink, "topic-list-item-title")
);
}

View File

@ -6,14 +6,14 @@ export default {
after: "message-bus",
initialize(container) {
const banner = EmberObject.create(PreloadStore.get("banner") || {}),
site = container.lookup("service:site");
const site = container.lookup("service:site");
const banner = EmberObject.create(PreloadStore.get("banner") || {});
const messageBus = container.lookup("service:message-bus");
site.set("banner", banner);
const messageBus = container.lookup("service:message-bus");
messageBus.subscribe("/site/banner", function (ban) {
site.set("banner", EmberObject.create(ban || {}));
messageBus.subscribe("/site/banner", (data) => {
site.set("banner", EmberObject.create(data || {}));
});
},
};

View File

@ -11,22 +11,24 @@ export default {
initialize(container) {
const messageBus = container.lookup("service:message-bus");
messageBus.subscribe("/logout", function () {
if (!_showingLogout) {
_showingLogout = true;
bootbox.dialog(
I18n.t("logout"),
{
label: I18n.t("home"),
callback: logout,
},
{
onEscape: logout,
backdrop: "static",
}
);
messageBus.subscribe("/logout", () => {
if (_showingLogout) {
return;
}
_showingLogout = true;
bootbox.dialog(
I18n.t("logout"),
{
label: I18n.t("home"),
callback: logout,
},
{
onEscape: logout,
backdrop: "static",
}
);
});
},
};

View File

@ -6,7 +6,7 @@ export default {
initialize(container) {
const messageBus = container.lookup("service:message-bus");
const site = container.lookup("service:site");
messageBus.subscribe("/site/read-only", function (enabled) {
messageBus.subscribe("/site/read-only", (enabled) => {
site.set("isReadOnly", enabled);
});
},

View File

@ -38,7 +38,7 @@ export default {
});
bus.subscribe(
`/notification/${user.get("id")}`,
`/notification/${user.id}`,
(data) => {
const store = container.lookup("service:store");
const oldUnread = user.unread_notifications;
@ -76,10 +76,9 @@ export default {
{},
{ cacheKey: "recent-notifications" }
);
const lastNotification =
data.last_notification && data.last_notification.notification;
const lastNotification = data.last_notification?.notification;
if (stale && stale.hasResults && lastNotification) {
if (stale?.hasResults && lastNotification) {
const oldNotifications = stale.results.get("content");
const staleIndex = oldNotifications.findIndex(
(n) => n.id === lastNotification.id
@ -115,6 +114,7 @@ export default {
}
})
.filter(Boolean);
stale.results.set("content", newNotifications);
}
},
@ -153,6 +153,7 @@ export default {
}
return site.updateCategory(c);
});
(data.deleted_categories || []).forEach((id) =>
site.removeCategory(id)
);
@ -166,6 +167,7 @@ export default {
bus.subscribe(alertChannel(user), (data) =>
onNotification(data, siteSettings, user)
);
initDesktopNotifications(bus, appEvents);
if (isPushNotificationsEnabled(user)) {

View File

@ -7,7 +7,7 @@ export default {
if (site.show_welcome_topic_banner) {
const messageBus = container.lookup("service:message-bus");
messageBus.subscribe("/site/welcome-topic-banner", function (disabled) {
messageBus.subscribe("/site/welcome-topic-banner", (disabled) => {
site.set("show_welcome_topic_banner", disabled);
});
}

View File

@ -144,7 +144,7 @@ function isIdle() {
}
// Call-in point from message bus
function onNotification(data, siteSettings, user) {
async function onNotification(data, siteSettings, user) {
if (!liveEnabled) {
return;
}
@ -177,22 +177,22 @@ function onNotification(data, siteSettings, user) {
const notificationTag =
"discourse-notification-" + siteSettings.title + "-" + data.topic_id;
requestPermission().then(function () {
// This shows the notification!
const notification = new Notification(notificationTitle, {
body: notificationBody,
icon: notificationIcon,
tag: notificationTag,
});
notification.onclick = () => {
DiscourseURL.routeTo(data.post_url);
notification.close();
};
await requestPermission();
desktopNotificationHandlers.forEach((handler) =>
handler(data, siteSettings, user)
);
// This shows the notification!
const notification = new Notification(notificationTitle, {
body: notificationBody,
icon: notificationIcon,
tag: notificationTag,
});
notification.onclick = () => {
DiscourseURL.routeTo(data.post_url);
notification.close();
};
desktopNotificationHandlers.forEach((handler) =>
handler(data, siteSettings, user)
);
}
// Utility function

View File

@ -581,25 +581,27 @@ const User = RestModel.extend({
});
},
loadUserAction(id) {
const stream = this.stream;
return ajax(`/user_actions/${id}.json`).then((result) => {
if (result && result.user_action) {
const ua = result.user_action;
async loadUserAction(id) {
const result = await ajax(`/user_actions/${id}.json`);
if ((this.get("stream.filter") || ua.action_type) !== ua.action_type) {
return;
}
if (!this.get("stream.filter") && !this.inAllStream(ua)) {
return;
}
if (!result?.user_action) {
return;
}
ua.title = emojiUnescape(escapeExpression(ua.title));
const action = UserAction.collapseStream([UserAction.create(ua)]);
stream.set("itemsLoaded", stream.get("itemsLoaded") + 1);
stream.get("content").insertAt(0, action[0]);
}
});
const ua = result.user_action;
if ((this.get("stream.filter") || ua.action_type) !== ua.action_type) {
return;
}
if (!this.get("stream.filter") && !this.inAllStream(ua)) {
return;
}
ua.title = emojiUnescape(escapeExpression(ua.title));
const action = UserAction.collapseStream([UserAction.create(ua)]);
this.stream.set("itemsLoaded", this.stream.get("itemsLoaded") + 1);
this.stream.get("content").insertAt(0, action[0]);
},
inAllStream(ua) {

View File

@ -51,15 +51,12 @@ const PrivateMessageTopicTrackingState = Service.extend({
},
_establishChannels() {
this.messageBus.subscribe(
this.userChannel(),
this._processMessage.bind(this)
);
this.messageBus.subscribe(this.userChannel(), this._processMessage);
this.currentUser.groupsWithMessages?.forEach((group) => {
this.messageBus.subscribe(
this.groupChannel(group.id),
this._processMessage.bind(this)
this._processMessage
);
});
},

View File

@ -213,10 +213,10 @@ class PresenceChannelState extends EmberObject.extend(Evented) {
await this._resubscribe();
return;
} else {
this.lastSeenId = message_id;
}
this.lastSeenId = message_id;
if (this.countOnly && data.count_delta !== undefined) {
this.set("count", this.count + data.count_delta);
this.trigger("change");
@ -228,11 +228,13 @@ class PresenceChannelState extends EmberObject.extend(Evented) {
const users = data.entering_users.map((u) => User.create(u));
this.users.addObjects(users);
}
if (data.leaving_user_ids) {
const leavingIds = new Set(data.leaving_user_ids);
const toRemove = this.users.filter((u) => leavingIds.has(u.id));
this.users.removeObjects(toRemove);
}
this.set("count", this.users.length);
this.trigger("change");
} else {

View File

@ -46,21 +46,21 @@ function initialize(api) {
subscribe() {
this._super(...arguments);
this.messageBus.subscribe(`/topic/${this.get("model.id")}`, (data) => {
this.messageBus.subscribe(`/topic/${this.model.id}`, (data) => {
const topic = this.model;
// scroll only for discobot (-2 is discobot id)
if (
topic.get("isPrivateMessage") &&
topic.isPrivateMessage &&
this.currentUser &&
this.currentUser.get("id") !== data.user_id &&
this.currentUser.id !== data.user_id &&
data.user_id === -2 &&
data.type === "created"
) {
const postNumber = data.post_number;
const notInPostStream =
topic.get("highest_post_number") <= postNumber;
const postNumberDifference = postNumber - topic.get("currentPost");
const postNumberDifference = postNumber - topic.currentPost;
if (
notInPostStream &&
@ -116,7 +116,7 @@ function initialize(api) {
}
export default {
name: "new-user-narratve",
name: "new-user-narrative",
initialize(container) {
const siteSettings = container.lookup("service:site-settings");

View File

@ -31,15 +31,15 @@ function initializePolls(api) {
api.modifyClass("controller:topic", {
pluginId: PLUGIN_ID,
subscribe() {
this._super(...arguments);
this.messageBus.subscribe("/polls/" + this.get("model.id"), (msg) => {
this.messageBus.subscribe(`/polls/${this.model.id}`, (msg) => {
const post = this.get("model.postStream").findLoadedPost(msg.post_id);
if (post) {
post.set("polls", msg.polls);
}
post?.set("polls", msg.polls);
});
},
unsubscribe() {
this.messageBus.unsubscribe("/polls/*");
this._super(...arguments);