discourse/plugins/chat/test/javascripts/helpers/chat-pretenders.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

166 lines
4.7 KiB
JavaScript
Raw Normal View History

import {
chatChannels,
directMessageChannels,
generateChatView,
} from "discourse/plugins/chat/chat-fixtures";
import { cloneJSON } from "discourse-common/lib/object";
import User from "discourse/models/user";
export function baseChatPretenders(server, helper) {
server.get("/chat/:chatChannelId/messages.json", () =>
helper.response(generateChatView(User.current()))
);
server.post("/chat/:chatChannelId.json", () => {
return helper.response({ success: "OK" });
});
server.get("/notifications", () => {
return helper.response({
notifications: [
{
id: 42,
user_id: 1,
notification_type: 29,
read: false,
high_priority: true,
created_at: "2021-01-01 12:00:00 UTC",
fancy_title: "First notification",
post_number: null,
topic_id: null,
slug: null,
data: {
chat_message_id: 174,
chat_channel_id: 9,
chat_channel_title: "Site",
mentioned_by_username: "hawk",
},
},
{
id: 43,
user_id: 1,
notification_type: 29,
read: false,
high_priority: true,
created_at: "2021-01-01 12:00:00 UTC",
fancy_title: "Second notification",
post_number: null,
topic_id: null,
slug: null,
data: {
identifier: "engineers",
is_group: true,
chat_message_id: 174,
chat_channel_id: 9,
chat_channel_title: "Site",
mentioned_by_username: "hawk",
},
},
{
id: 44,
user_id: 1,
notification_type: 29,
read: false,
high_priority: true,
created_at: "2021-01-01 12:00:00 UTC",
fancy_title: "Third notification",
post_number: null,
topic_id: null,
slug: null,
data: {
identifier: "all",
chat_message_id: 174,
chat_channel_id: 9,
chat_channel_title: "Site",
mentioned_by_username: "hawk",
},
},
{
id: 45,
user_id: 1,
notification_type: 31,
read: false,
high_priority: true,
created_at: "2021-01-01 12:00:00 UTC",
fancy_title: "Fourth notification",
post_number: null,
topic_id: null,
slug: null,
data: {
message: "chat.invitation_notification",
chat_message_id: 174,
chat_channel_id: 9,
chat_channel_title: "Site",
invited_by_username: "hawk",
},
},
{
id: 46,
user_id: 1,
notification_type: 29,
read: false,
high_priority: true,
created_at: "2021-01-01 12:00:00 UTC",
fancy_title: "Fifth notification",
post_number: null,
topic_id: null,
slug: null,
data: {
chat_message_id: 174,
chat_channel_id: 9,
chat_channel_title: "Site",
is_direct_message_channel: true,
mentioned_by_username: "hawk",
},
},
],
seen_notification_id: null,
});
});
server.get("/chat/lookup/:messageId.json", () =>
helper.response(generateChatView(User.current()))
);
server.post("/uploads/lookup-urls", () => {
return helper.response([]);
});
server.get("/chat/api/category-chatables/:categoryId/permissions.json", () =>
helper.response({ allowed_groups: ["@everyone"], private: false })
);
}
export function directMessageChannelPretender(
server,
helper,
opts = { unread_count: 0, muted: false }
) {
let copy = cloneJSON(directMessageChannels[0]);
DEV: start glimmer-ification and optimisations of chat plugin (#19531) 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. -->
2022-12-21 20:21:02 +08:00
copy.chat_channel.currentUserMembership.unread_count = opts.unread_count;
copy.chat_channel.currentUserMembership.muted = opts.muted;
server.get("/chat/chat_channels/75.json", () => helper.response(copy));
}
export function chatChannelPretender(server, helper, changes = []) {
// changes is [{ id: X, unread_count: Y, muted: true}]
let copy = cloneJSON(chatChannels);
changes.forEach((change) => {
let found;
found = copy.public_channels.find((c) => c.id === change.id);
if (found) {
DEV: start glimmer-ification and optimisations of chat plugin (#19531) 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. -->
2022-12-21 20:21:02 +08:00
found.currentUserMembership.unread_count = change.unread_count;
found.currentUserMembership.muted = change.muted;
}
if (!found) {
found = copy.direct_message_channels.find((c) => c.id === change.id);
if (found) {
DEV: start glimmer-ification and optimisations of chat plugin (#19531) 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. -->
2022-12-21 20:21:02 +08:00
found.currentUserMembership.unread_count = change.unread_count;
found.currentUserMembership.muted = change.muted;
}
}
});
server.get("/chat/chat_channels.json", () => helper.response(copy));
}