Skip to content

Commit

Permalink
fix: Addressed issues related to fetching different informations in p…
Browse files Browse the repository at this point in the history
…rivate channels. (#431)
  • Loading branch information
Spiral-Memory authored Feb 6, 2024
1 parent bff3ed7 commit 3fef94b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
21 changes: 12 additions & 9 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export default class EmbeddedChatApi {
try {
const { userId, authToken } = await this.auth.getCurrentUser() || {};
const response = await fetch(
`${this.host}/api/v1/channels.info?roomId=${this.rid}`,
`${this.host}/api/v1/rooms.info?roomId=${this.rid}`,
{
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -445,7 +445,8 @@ export default class EmbeddedChatApi {
} = {
query: undefined,
field: undefined
}) {
}, isChannelPrivate = false) {
const roomType = isChannelPrivate ? 'groups' : 'channels' ;
const endp = anonymousMode ? 'anonymousread' : 'messages';
const query = options?.query
? `&query=${JSON.stringify(options.query)}`
Expand All @@ -456,7 +457,7 @@ export default class EmbeddedChatApi {
try {
const { userId, authToken } = await this.auth.getCurrentUser() || {};
const messages = await fetch(
`${this.host}/api/v1/channels.${endp}?roomId=${this.rid}${query}${field}`,
`${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}`,
{
headers: {
'Content-Type': 'application/json',
Expand All @@ -472,19 +473,20 @@ export default class EmbeddedChatApi {
}
}

async getThreadMessages(tmid: string) {
async getThreadMessages(tmid: string, isChannelPrivate = false) {
return this.getMessages(false, {
query: {
tmid,
},
});
}, isChannelPrivate);
}

async getChannelRoles() {
async getChannelRoles(isChannelPrivate = false) {
const roomType = isChannelPrivate ? 'groups' : 'channels';
try {
const { userId, authToken } = await this.auth.getCurrentUser() || {};
const roles = await fetch(
`${this.host}/api/v1/channels.roles?roomId=${this.rid}`,
`${this.host}/api/v1/${roomType}.roles?roomId=${this.rid}`,
{
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -800,11 +802,12 @@ export default class EmbeddedChatApi {
}
}

async getChannelMembers() {
async getChannelMembers(isChannelPrivate = false) {
const roomType = isChannelPrivate ? 'groups' : 'channels';
try {
const { userId, authToken } = await this.auth.getCurrentUser() || {};
const response = await fetch(
`${this.host}/api/v1/channels.members?roomId=${this.rid}`,
`${this.host}/api/v1/${roomType}.members?roomId=${this.rid}`,
{
headers: {
'Content-Type': 'application/json',
Expand Down
19 changes: 11 additions & 8 deletions packages/react/src/components/ChatBody/ChatBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useCallback, useContext, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/react';
import RCContext from '../../context/RCInstance';
import { useMessageStore, useUserStore } from '../../store';
import { useMessageStore, useUserStore, useChannelStore } from '../../store';
import MessageList from '../MessageList';
import TotpModal from '../TotpModal/TwoFactorTotpModal';
import { Box } from '../Box';
Expand Down Expand Up @@ -52,6 +52,7 @@ const ChatBody = ({ height, anonymousMode, showRoles, scrollToBottom, messageLis
const removeMessage = useMessageStore((state) => state.removeMessage);
const setFilter = useMessageStore((state) => state.setFilter);
const setRoles = useUserStore((state) => state.setRoles);
const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);

const [isThreadOpen, threadMainMessage] = useMessageStore((state) => [
state.isThreadOpen,
Expand Down Expand Up @@ -84,7 +85,7 @@ const ChatBody = ({ height, anonymousMode, showRoles, scrollToBottom, messageLis
},
},
}
: undefined
: undefined, anonymousMode ? false : isChannelPrivate
);
if (messages) {
setMessages(messages.filter((message) => message._hidden !== true));
Expand All @@ -94,12 +95,11 @@ const ChatBody = ({ height, anonymousMode, showRoles, scrollToBottom, messageLis
return;
}
if (showRoles) {
const { roles } = await RCInstance.getChannelRoles();
const { roles } = await RCInstance.getChannelRoles(isChannelPrivate);
// convert roles array from api into object for better search
const rolesObj = roles.reduce(
(obj, item) => Object.assign(obj, { [item.u.username]: item }),
{}
);
const rolesObj = roles?.length > 0
? roles.reduce((obj, item) => ({ ...obj, [item.u.username]: item }), {})
: {};
setRoles(rolesObj);
}
} catch (e) {
Expand All @@ -113,6 +113,7 @@ const ChatBody = ({ height, anonymousMode, showRoles, scrollToBottom, messageLis
showRoles,
setMessages,
setRoles,
isChannelPrivate
]
);

Expand All @@ -132,7 +133,8 @@ const ChatBody = ({ height, anonymousMode, showRoles, scrollToBottom, messageLis
return;
}
const { messages } = await RCInstance.getThreadMessages(
threadMainMessage._id
threadMainMessage._id,
isChannelPrivate
);
setThreadMessages(messages);
} catch (e) {
Expand All @@ -145,6 +147,7 @@ const ChatBody = ({ height, anonymousMode, showRoles, scrollToBottom, messageLis
RCInstance,
threadMainMessage?._id,
setThreadMessages,
isChannelPrivate
]);

useEffect(() => {
Expand Down
11 changes: 7 additions & 4 deletions packages/react/src/components/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const ChatHeader = ({
const setShowChannelinfo = useChannelStore(
(state) => state.setShowChannelinfo
);
const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);
const setIsChannelPrivate = useChannelStore((state) => state.setIsChannelPrivate);

const { RCInstance } = useRCContext();

Expand Down Expand Up @@ -91,11 +93,11 @@ const ChatHeader = ({
}, [RCInstance, setMessages, setFilter]);

const showChannelMembers = useCallback(async () => {
const { members = [] } = await RCInstance.getChannelMembers();
const { members = [] } = await RCInstance.getChannelMembers(isChannelPrivate);
setMembersHandler(members);
toggleShowMembers();
setShowSearch(false);
}, [RCInstance, setMembersHandler, toggleShowMembers, setShowSearch]);
}, [RCInstance, setMembersHandler, toggleShowMembers, setShowSearch, isChannelPrivate]);

const showSearchMessage = useCallback(() => {
setShowSearch(true);
Expand All @@ -117,7 +119,8 @@ const ChatHeader = ({
const getChannelInfo = async () => {
const res = await RCInstance.channelInfo();
if (res.success) {
setChannelInfo(res.channel);
setChannelInfo(res.room);
if (res.room.t === 'p') setIsChannelPrivate(true);
} else {
if ('errorType' in res && res.errorType === 'error-room-not-found') {
dispatchToastMessage({
Expand All @@ -133,7 +136,7 @@ const ChatHeader = ({
if (isUserAuthenticated) {
getChannelInfo();
}
}, [isUserAuthenticated, RCInstance, setChannelInfo]);
}, [isUserAuthenticated, RCInstance, setChannelInfo, setIsChannelPrivate]);

const menuOptions = useMemo(() => {
const options = [];
Expand Down
7 changes: 5 additions & 2 deletions packages/react/src/components/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useUserStore,
useMessageStore,
loginModalStore,
useChannelStore
} from '../../store';
import ChatInputFormattingToolbar from './ChatInputFormattingToolbar';
import useAttachmentWindowStore from '../../store/attachmentwindow';
Expand Down Expand Up @@ -84,6 +85,8 @@ const ChatInput = ({ scrollToBottom }) => {
const setIsLoginModalOpen = loginModalStore(
(state) => state.setIsLoginModalOpen
);
const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);
const setIsChannelPrivate = useChannelStore((state) => state.setIsChannelPrivate);

const {
editMessage,
Expand Down Expand Up @@ -213,12 +216,12 @@ const ChatInput = ({ scrollToBottom }) => {
};
const getAllChannelMembers = useCallback(async () => {
try {
const channelMembers = await RCInstance.getChannelMembers();
const channelMembers = await RCInstance.getChannelMembers(isChannelPrivate);
setRoomMembers(channelMembers.members);
} catch (e) {
console.error(e);
}
}, [RCInstance, setRoomMembers]);
}, [RCInstance, setRoomMembers, isChannelPrivate]);

useEffect(() => {
if (editMessage.msg) {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/store/channelStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { create } from 'zustand';

const useChannelStore = create((set) => ({
showChannelinfo: false,
isChannelPrivate: false,
setShowChannelinfo: (showChannelinfo) => set(() => ({ showChannelinfo })),
channelInfo: {},
setChannelInfo: (channelInfo) => set(() => ({ channelInfo })),
setIsChannelPrivate: (isChannelPrivate) => set(() => ({ isChannelPrivate }))
}));

export default useChannelStore;

0 comments on commit 3fef94b

Please sign in to comment.