Skip to content

Commit

Permalink
fixed message limit (RocketChat#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spiral-Memory authored Mar 23, 2024
1 parent 7cfcba9 commit 05cc948
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
21 changes: 21 additions & 0 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,27 @@ export default class EmbeddedChatApi {
console.error(err);
}
}

async getMessageLimit() {
try {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
const response = await fetch(
`${this.host}/api/v1/settings/Message_MaxAllowedSize`,
{
headers: {
"Content-Type": "application/json",
"X-Auth-Token": authToken,
"X-User-Id": userId,
},
method: "GET",
}
);
return await response.json();
} catch (err) {
console.error(err);
}
}

async triggerBlockAction({
type,
actionId,
Expand Down
11 changes: 9 additions & 2 deletions packages/react/src/components/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ActionButton } from '../ActionButton';
import { Menu } from '../Menu';
import { useToastBarDispatch } from '../../hooks/useToastBarDispatch';
import useFetchChatData from '../../hooks/useFetchChatData';
import useSettingsStore from '../../store/settingsStore';

const ChatHeader = ({
isClosable,
Expand Down Expand Up @@ -64,12 +65,10 @@ const ChatHeader = ({
const avatarUrl = useUserStore((state) => state.avatarUrl);
const headerTitle = useMessageStore((state) => state.headerTitle);
const filtered = useMessageStore((state) => state.filtered);
const setMessages = useMessageStore((state) => state.setMessages);
const setFilter = useMessageStore((state) => state.setFilter);
const isThreadOpen = useMessageStore((state) => state.isThreadOpen);
const closeThread = useMessageStore((state) => state.closeThread);
const threadTitle = useMessageStore((state) => state.threadMainMessage?.msg);
const setHeaderTitle = useMessageStore((state) => state.setHeaderTitle);
const setMembersHandler = useMemberStore((state) => state.setMembersHandler);
const toggleShowMembers = useMemberStore((state) => state.toggleShowMembers);
const showMembers = useMemberStore((state) => state.showMembers);
Expand All @@ -84,6 +83,7 @@ const ChatHeader = ({
const setShowAllFiles = useFileStore((state) => state.setShowAllFiles);
const setShowMentions = useMentionsStore((state) => state.setShowMentions);
const toastPosition = useToastStore((state) => state.position);
const setMessageLimit = useSettingsStore((state) => state.setMessageLimit);

const handleGoBack = async () => {
if (isUserAuthenticated) {
Expand Down Expand Up @@ -156,6 +156,11 @@ const ChatHeader = ({
}, [setShowMentions, setShowSearch]);

useEffect(() => {
const getMessageLimit = async () => {
const messageLimitObj = await RCInstance.getMessageLimit();
setMessageLimit(messageLimitObj?.value);
};

const setMessageAllowed = async () => {
const permissionRes = await RCInstance.permissionInfo();
const channelRolesRes = await RCInstance.getChannelRoles(
Expand Down Expand Up @@ -205,6 +210,7 @@ const ChatHeader = ({

if (isUserAuthenticated) {
getChannelInfo();
getMessageLimit();
}
}, [
isUserAuthenticated,
Expand All @@ -216,6 +222,7 @@ const ChatHeader = ({
isChannelPrivate,
setCanSendMsg,
authenticatedUserId,
setMessageLimit,
]);

const menuOptions = useMemo(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Divider } from '../Divider';
import useComponentOverrides from '../../theme/useComponentOverrides';
import { useToastBarDispatch } from '../../hooks/useToastBarDispatch';
import { Modal } from '../Modal';
import useSettingsStore from '../../store/settingsStore';

const editingMessageCss = css`
background-color: #fff8e0;
Expand All @@ -51,6 +52,7 @@ const ChatInput = ({ scrollToBottom }) => {

const members = useMemberStore((state) => state.members);
const setMembersHandler = useMemberStore((state) => state.setMembersHandler);
const msgMaxLength = useSettingsStore((state) => state.messageLimit);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
Expand Down Expand Up @@ -173,8 +175,6 @@ const ChatInput = ({ scrollToBottom }) => {
setEditMessage({});
return;
}

const msgMaxLength = 500;
if (message.length > msgMaxLength) {
openMsgLongModal();
return;
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/store/settingsStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { create } from 'zustand';

const useSettingsStore = create((set) => ({
messageLimit: 5000,
setMessageLimit: (messageLimit) => set(() => ({ messageLimit })),
}));

export default useSettingsStore;

0 comments on commit 05cc948

Please sign in to comment.