This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/assets/javascripts/discourse/components/chat-channel-archive-status.js
Jarek Radosz 19214aff18
DEV: Clean up all message bus subscriptions (#19268)
1. "What Goes Up Must Come Down" – if you subscribe to message bus, make sure you also unsubscribe
2. When you unsubscribe - remove only your subscription, not **all** subscriptions on given channel

Attempt #2. The first attempt tried to extend a core `@bound` method in new-user-narrative plugin which did not work. I reworked that plugin in the meantime. This new PR also cleans up message bus subscriptions in now core-merged chat plugin.
2022-12-12 16:32:25 +01:00

85 lines
2.2 KiB
JavaScript

import Component from "@ember/component";
import { htmlSafe } from "@ember/template";
import I18n from "I18n";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { ajax } from "discourse/lib/ajax";
import getURL from "discourse-common/lib/get-url";
import { action } from "@ember/object";
import discourseComputed, { bind } from "discourse-common/utils/decorators";
export default Component.extend({
channel: null,
tagName: "",
@discourseComputed(
"channel.status",
"channel.archived_messages",
"channel.total_messages",
"channel.archive_failed"
)
channelArchiveFailedMessage() {
return htmlSafe(
I18n.t("chat.channel_status.archive_failed", {
completed: this.channel.archived_messages,
total: this.channel.total_messages,
topic_url: this._getTopicUrl(),
})
);
},
@discourseComputed(
"channel.status",
"channel.archived_messages",
"channel.total_messages",
"channel.archive_completed"
)
channelArchiveCompletedMessage() {
return htmlSafe(
I18n.t("chat.channel_status.archive_completed", {
topic_url: this._getTopicUrl(),
})
);
},
@action
retryArchive() {
return ajax({
url: `/chat/chat_channels/${this.channel.id}/retry_archive.json`,
type: "PUT",
})
.then(() => {
this.channel.set("archive_failed", false);
})
.catch(popupAjaxError);
},
didInsertElement() {
this._super(...arguments);
if (this.currentUser.admin) {
this.messageBus.subscribe("/chat/channel-archive-status", this.onMessage);
}
},
willDestroyElement() {
this._super(...arguments);
this.messageBus.unsubscribe("/chat/channel-archive-status", this.onMessage);
},
_getTopicUrl() {
return getURL(`/t/-/${this.channel.archive_topic_id}`);
},
@bind
onMessage(busData) {
if (busData.chat_channel_id === this.channel.id) {
this.channel.setProperties({
archive_failed: busData.archive_failed,
archive_completed: busData.archive_completed,
archived_messages: busData.archived_messages,
archive_topic_id: busData.archive_topic_id,
total_messages: busData.total_messages,
});
}
},
});