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. -->
187 lines
4.5 KiB
JavaScript
187 lines
4.5 KiB
JavaScript
import RestModel from "discourse/models/rest";
|
|
import I18n from "I18n";
|
|
import User from "discourse/models/user";
|
|
import UserChatChannelMembership from "discourse/plugins/chat/discourse/models/user-chat-channel-membership";
|
|
import { ajax } from "discourse/lib/ajax";
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
export const CHATABLE_TYPES = {
|
|
directMessageChannel: "DirectMessage",
|
|
categoryChannel: "Category",
|
|
};
|
|
|
|
export const CHANNEL_STATUSES = {
|
|
open: "open",
|
|
readOnly: "read_only",
|
|
closed: "closed",
|
|
archived: "archived",
|
|
};
|
|
|
|
export function channelStatusName(channelStatus) {
|
|
switch (channelStatus) {
|
|
case CHANNEL_STATUSES.open:
|
|
return I18n.t("chat.channel_status.open");
|
|
case CHANNEL_STATUSES.readOnly:
|
|
return I18n.t("chat.channel_status.read_only");
|
|
case CHANNEL_STATUSES.closed:
|
|
return I18n.t("chat.channel_status.closed");
|
|
case CHANNEL_STATUSES.archived:
|
|
return I18n.t("chat.channel_status.archived");
|
|
}
|
|
}
|
|
|
|
export function channelStatusIcon(channelStatus) {
|
|
if (channelStatus === CHANNEL_STATUSES.open) {
|
|
return null;
|
|
}
|
|
|
|
switch (channelStatus) {
|
|
case CHANNEL_STATUSES.closed:
|
|
return "lock";
|
|
case CHANNEL_STATUSES.readOnly:
|
|
return "comment-slash";
|
|
case CHANNEL_STATUSES.archived:
|
|
return "archive";
|
|
}
|
|
}
|
|
|
|
const STAFF_READONLY_STATUSES = [
|
|
CHANNEL_STATUSES.readOnly,
|
|
CHANNEL_STATUSES.archived,
|
|
];
|
|
|
|
const READONLY_STATUSES = [
|
|
CHANNEL_STATUSES.closed,
|
|
CHANNEL_STATUSES.readOnly,
|
|
CHANNEL_STATUSES.archived,
|
|
];
|
|
|
|
export default class ChatChannel extends RestModel {
|
|
@tracked currentUserMembership = null;
|
|
@tracked isDraft = false;
|
|
@tracked title;
|
|
@tracked description;
|
|
@tracked chatableType;
|
|
@tracked status;
|
|
|
|
get escapedTitle() {
|
|
return escapeExpression(this.title);
|
|
}
|
|
|
|
get escapedDescription() {
|
|
return escapeExpression(this.description);
|
|
}
|
|
|
|
get isDirectMessageChannel() {
|
|
return this.chatable_type === CHATABLE_TYPES.directMessageChannel;
|
|
}
|
|
|
|
get isCategoryChannel() {
|
|
return this.chatable_type === CHATABLE_TYPES.categoryChannel;
|
|
}
|
|
|
|
get isOpen() {
|
|
return !this.status || this.status === CHANNEL_STATUSES.open;
|
|
}
|
|
|
|
get isReadOnly() {
|
|
return this.status === CHANNEL_STATUSES.readOnly;
|
|
}
|
|
|
|
get isClosed() {
|
|
return this.status === CHANNEL_STATUSES.closed;
|
|
}
|
|
|
|
get isArchived() {
|
|
return this.status === CHANNEL_STATUSES.archived;
|
|
}
|
|
|
|
get isJoinable() {
|
|
return this.isOpen && !this.isArchived;
|
|
}
|
|
|
|
get isFollowing() {
|
|
return this.currentUserMembership.following;
|
|
}
|
|
|
|
canModifyMessages(user) {
|
|
if (user.staff) {
|
|
return !STAFF_READONLY_STATUSES.includes(this.status);
|
|
}
|
|
|
|
return !READONLY_STATUSES.includes(this.status);
|
|
}
|
|
|
|
updateMembership(membership) {
|
|
this.currentUserMembership.following = membership.following;
|
|
this.currentUserMembership.muted = membership.muted;
|
|
this.currentUserMembership.desktop_notification_level =
|
|
membership.desktop_notification_level;
|
|
this.currentUserMembership.mobile_notification_level =
|
|
membership.mobile_notification_level;
|
|
}
|
|
|
|
updateLastReadMessage(messageId) {
|
|
if (!this.isFollowing || !messageId) {
|
|
return;
|
|
}
|
|
|
|
return ajax(`/chat/${this.id}/read/${messageId}.json`, {
|
|
method: "PUT",
|
|
}).then(() => {
|
|
this.currentUserMembership.last_read_message_id = messageId;
|
|
});
|
|
}
|
|
}
|
|
|
|
ChatChannel.reopenClass({
|
|
create(args) {
|
|
args = args || {};
|
|
|
|
this._initUserModels(args);
|
|
this._initUserMembership(args);
|
|
|
|
args.chatableType = args.chatable_type;
|
|
args.membershipsCount = args.memberships_count;
|
|
|
|
return this._super(args);
|
|
},
|
|
|
|
_initUserModels(args) {
|
|
if (args.chatable?.users?.length) {
|
|
for (let i = 0; i < args.chatable?.users?.length; i++) {
|
|
const userData = args.chatable.users[i];
|
|
args.chatable.users[i] = User.create(userData);
|
|
}
|
|
}
|
|
},
|
|
|
|
_initUserMembership(args) {
|
|
if (args.currentUserMembership instanceof UserChatChannelMembership) {
|
|
return;
|
|
}
|
|
|
|
args.currentUserMembership = UserChatChannelMembership.create(
|
|
args.current_user_membership || {
|
|
following: false,
|
|
muted: false,
|
|
unread_count: 0,
|
|
unread_mentions: 0,
|
|
}
|
|
);
|
|
|
|
delete args.current_user_membership;
|
|
},
|
|
});
|
|
|
|
export function createDirectMessageChannelDraft() {
|
|
return ChatChannel.create({
|
|
isDraft: true,
|
|
chatable_type: CHATABLE_TYPES.directMessageChannel,
|
|
chatable: {
|
|
users: [],
|
|
},
|
|
});
|
|
}
|