Note this is a very large PR, and some of it could have been splited, but keeping it one chunk made it to merge conflicts and to revert if necessary. Actual new code logic is also not that much, as most of the changes are removing js tests, adding system specs or moving things around. To make it possible this commit is doing the following changes: - converting (and adding new) existing js acceptances tests into system tests. This change was necessary to ensure as little regressions as possible while changing paradigm - moving away from store. Using glimmer and tracked properties requires to have class objects everywhere and as a result works well with models. However store/adapters are suffering from many bugs and limitations. As a workaround the `chat-api` and `chat-channels-manager` are an answer to this problem by encapsulating backend calls and frontend storage logic; while still using js models. - dropping `appEvents` as much as possible. Using tracked properties and a better local storage of channel models, allows to be much more reactive and doesn’t require arbitrary manual updates everywhere in the app. - while working on replacing store, the existing work of a chat api (backend) has been continued to support more cases. - removing code from the `chat` service to separate concerns, `chat-subscriptions-manager` and `chat-channels-manager`, being the largest examples of where the code has been rewritten/moved. Future wok: - improve behavior when closing/deleting a channel, it's already slightly buggy on live, it's rare enough that it's not a big issue, but should be improved - improve page objects used in chat - move more endpoints to the API - finish temporarily skipped tests - extract more code from the `chat` service - use glimmer for `chat-messages` - separate concerns in `chat-live-pane` - eventually add js tests for `chat-api`, `chat-channels-manager` and `chat-subscriptions-manager`, they are indirectly heavy tested through system tests but it would be nice to at least test the public API <!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
266 lines
7.3 KiB
JavaScript
266 lines
7.3 KiB
JavaScript
import Service, { inject as service } from "@ember/service";
|
|
import { bind } from "discourse-common/utils/decorators";
|
|
import { CHANNEL_STATUSES } from "discourse/plugins/chat/discourse/models/chat-channel";
|
|
|
|
export default class ChatSubscriptionsManager extends Service {
|
|
@service store;
|
|
@service chatChannelsManager;
|
|
@service currentUser;
|
|
@service appEvents;
|
|
|
|
_channelSubscriptions = new Set();
|
|
|
|
startChannelSubscription(channel) {
|
|
if (
|
|
channel.currentUserMembership.muted ||
|
|
this._channelSubscriptions.has(channel.id)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
this._channelSubscriptions.add(channel.id);
|
|
|
|
if (!channel.isDirectMessageChannel) {
|
|
this._startChannelMentionsSubscription(channel);
|
|
}
|
|
|
|
this._startChannelNewMessagesSubscription(channel);
|
|
}
|
|
|
|
stopChannelSubscription(channel) {
|
|
this.messageBus.unsubscribe(
|
|
`/chat/${channel.id}/new-messages`,
|
|
this._onNewMessages
|
|
);
|
|
if (!channel.isDirectMessageChannel) {
|
|
this.messageBus.unsubscribe(
|
|
`/chat/${channel.id}/new-mentions`,
|
|
this._onNewMentions
|
|
);
|
|
}
|
|
|
|
this._channelSubscriptions.delete(channel.id);
|
|
}
|
|
|
|
startChannelsSubscriptions(messageBusIds) {
|
|
this._startNewChannelSubscription(messageBusIds.new_channel);
|
|
this._startUserTrackingStateSubscription(messageBusIds.user_tracking_state);
|
|
this._startChannelsEditsSubscription(messageBusIds.channel_edits);
|
|
this._startChannelsStatusChangesSubscription(messageBusIds.channel_status);
|
|
this._startChannelsMetadataChangesSubscription(
|
|
messageBusIds.channel_metadata
|
|
);
|
|
}
|
|
|
|
stopChannelsSubscriptions() {
|
|
this._stopNewChannelSubscription();
|
|
this._stopUserTrackingStateSubscription();
|
|
this._stopChannelsEditsSubscription();
|
|
this._stopChannelsStatusChangesSubscription();
|
|
this._stopChannelsMetadataChangesSubscription();
|
|
|
|
(this.chatChannelsManager.channels || []).forEach((channel) => {
|
|
this.stopChannelSubscription(channel);
|
|
});
|
|
}
|
|
|
|
_startChannelMentionsSubscription(channel) {
|
|
this.messageBus.subscribe(
|
|
`/chat/${channel.id}/new-mentions`,
|
|
this._onNewMentions,
|
|
channel.meta.message_bus_last_ids.new_mentions
|
|
);
|
|
}
|
|
|
|
@bind
|
|
_onNewMentions(busData) {
|
|
this.chatChannelsManager.find(busData.channel_id).then((channel) => {
|
|
const membership = channel.currentUserMembership;
|
|
if (membership) {
|
|
membership.unread_mentions = (membership.unread_mentions || 0) + 1;
|
|
}
|
|
});
|
|
}
|
|
|
|
_startChannelNewMessagesSubscription(channel) {
|
|
this.messageBus.subscribe(
|
|
`/chat/${channel.id}/new-messages`,
|
|
this._onNewMessages,
|
|
channel.meta.message_bus_last_ids.new_messages
|
|
);
|
|
}
|
|
|
|
@bind
|
|
_onNewMessages(busData) {
|
|
this.chatChannelsManager.find(busData.channel_id).then((channel) => {
|
|
if (busData.user_id === this.currentUser.id) {
|
|
// User sent message, update tracking state to no unread
|
|
channel.currentUserMembership.chat_message_id = busData.message_id;
|
|
} else {
|
|
// Ignored user sent message, update tracking state to no unread
|
|
if (this.currentUser.ignored_users.includes(busData.username)) {
|
|
channel.currentUserMembership.chat_message_id = busData.message_id;
|
|
} else {
|
|
// Message from other user. Increment trackings state
|
|
if (
|
|
busData.message_id >
|
|
(channel.currentUserMembership.chat_message_id || 0)
|
|
) {
|
|
channel.currentUserMembership.unread_count =
|
|
channel.currentUserMembership.unread_count + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
channel.set("last_message_sent_at", new Date());
|
|
});
|
|
}
|
|
|
|
_startUserTrackingStateSubscription(lastId) {
|
|
if (!this.currentUser) {
|
|
return;
|
|
}
|
|
|
|
this.messageBus.subscribe(
|
|
`/chat/user-tracking-state/${this.currentUser.id}`,
|
|
this._onUserTrackingStateUpdate,
|
|
lastId
|
|
);
|
|
}
|
|
|
|
_stopUserTrackingStateSubscription() {
|
|
if (!this.currentUser) {
|
|
return;
|
|
}
|
|
|
|
this.messageBus.unsubscribe(
|
|
`/chat/user-tracking-state/${this.currentUser.id}`,
|
|
this._onUserTrackingStateUpdate
|
|
);
|
|
}
|
|
|
|
@bind
|
|
_onUserTrackingStateUpdate(data) {
|
|
this.chatChannelsManager.find(data.chat_channel_id).then((channel) => {
|
|
if (
|
|
channel?.currentUserMembership?.chat_message_id < data.chat_message_id
|
|
) {
|
|
channel.currentUserMembership.chat_message_id = data.chat_message_id;
|
|
channel.currentUserMembership.unread_count = 0;
|
|
channel.currentUserMembership.unread_mentions = 0;
|
|
}
|
|
});
|
|
}
|
|
|
|
_startNewChannelSubscription(lastId) {
|
|
this.messageBus.subscribe(
|
|
"/chat/new-channel",
|
|
this._onNewChannelSubscription,
|
|
lastId
|
|
);
|
|
}
|
|
|
|
_stopNewChannelSubscription() {
|
|
this.messageBus.unsubscribe(
|
|
"/chat/new-channel",
|
|
this._onNewChannelSubscription
|
|
);
|
|
}
|
|
|
|
@bind
|
|
_onNewChannelSubscription(data) {
|
|
this.chatChannelsManager.find(data.channel.id).then((channel) => {
|
|
// we need to refrehs here to have correct last message ids
|
|
channel.meta = data.channel.meta;
|
|
|
|
if (
|
|
channel.isDirectMessageChannel &&
|
|
!channel.currentUserMembership.following
|
|
) {
|
|
channel.currentUserMembership.unread_count = 1;
|
|
}
|
|
|
|
this.chatChannelsManager.follow(channel);
|
|
});
|
|
}
|
|
|
|
_startChannelsMetadataChangesSubscription(lastId) {
|
|
this.messageBus.subscribe(
|
|
"/chat/channel-metadata",
|
|
this._onChannelMetadata,
|
|
lastId
|
|
);
|
|
}
|
|
|
|
_startChannelsEditsSubscription(lastId) {
|
|
this.messageBus.subscribe(
|
|
"/chat/channel-edits",
|
|
this._onChannelEdits,
|
|
lastId
|
|
);
|
|
}
|
|
|
|
_startChannelsStatusChangesSubscription(lastId) {
|
|
this.messageBus.subscribe(
|
|
"/chat/channel-status",
|
|
this._onChannelStatus,
|
|
lastId
|
|
);
|
|
}
|
|
|
|
_stopChannelsStatusChangesSubscription() {
|
|
this.messageBus.unsubscribe("/chat/channel-status", this._onChannelStatus);
|
|
}
|
|
|
|
_stopChannelsEditsSubscription() {
|
|
this.messageBus.unsubscribe("/chat/channel-edits", this._onChannelEdits);
|
|
}
|
|
|
|
_stopChannelsMetadataChangesSubscription() {
|
|
this.messageBus.unsubscribe(
|
|
"/chat/channel-metadata",
|
|
this._onChannelMetadata
|
|
);
|
|
}
|
|
|
|
@bind
|
|
_onChannelMetadata(busData) {
|
|
this.chatChannelsManager.find(busData.chat_channel_id).then((channel) => {
|
|
if (channel) {
|
|
channel.setProperties({
|
|
memberships_count: busData.memberships_count,
|
|
});
|
|
this.appEvents.trigger("chat:refresh-channel-members");
|
|
}
|
|
});
|
|
}
|
|
|
|
@bind
|
|
_onChannelEdits(busData) {
|
|
this.chatChannelsManager.find(busData.chat_channel_id).then((channel) => {
|
|
if (channel) {
|
|
channel.setProperties({
|
|
title: busData.name,
|
|
description: busData.description,
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
@bind
|
|
_onChannelStatus(busData) {
|
|
this.chatChannelsManager.find(busData.chat_channel_id).then((channel) => {
|
|
channel.set("status", busData.status);
|
|
|
|
// it is not possible for the user to set their last read message id
|
|
// if the channel has been archived, because all the messages have
|
|
// been deleted. we don't want them seeing the blue dot anymore so
|
|
// just completely reset the unreads
|
|
if (busData.status === CHANNEL_STATUSES.archived) {
|
|
channel.currentUserMembership.unread_count = 0;
|
|
channel.currentUserMembership.unread_mentions = 0;
|
|
}
|
|
});
|
|
}
|
|
}
|