Skip to content

Commit

Permalink
Merge pull request #1274 from push-protocol/sdk-update-chat-info
Browse files Browse the repository at this point in the history
partial fix for chat info
  • Loading branch information
HarshRajat authored May 10, 2024
2 parents 92f84f5 + a75dba6 commit 3d9a8dc
Show file tree
Hide file tree
Showing 9 changed files with 1,759 additions and 453 deletions.
8 changes: 6 additions & 2 deletions packages/restapi/src/lib/chat/getChatInfo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { convertToValidDID, getAPIBaseUrls } from '../helpers';
import Constants, { ENV } from '../constants';
import { axiosGet } from '../utils/axiosUtil';
import { handleError } from '../errors/validationError';
import { convertToValidDID, getAPIBaseUrls } from '../helpers';
import { axiosGet } from '../utils/axiosUtil';

/**
* Represents the response type for the chat status.
*/
export interface ChatInfoResponse {
meta: {
group: boolean;
encryption: boolean;
groupInfo: {
public: boolean;
};
};
list: string;
participants: string[];
Expand Down
2,031 changes: 1,687 additions & 344 deletions packages/uiweb/dist/packages/restapi/packages/restapi/CHANGELOG.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [0.5.3](https://github.com/ethereum-push-notification-service/push-sdk/compare/socket-0.5.2...socket-0.5.3) (2023-11-15)


### Bug Fixes

* shift testnet env to sepolia ([#473](https://github.com/ethereum-push-notification-service/push-sdk/issues/473)) ([12f1392](https://github.com/ethereum-push-notification-service/push-sdk/commit/12f1392a4f4e800f720a0c2c34822bf502f15dac))



## [0.5.2](https://github.com/ethereum-push-notification-service/push-sdk/compare/socket-0.5.1...socket-0.5.2) (2023-08-03)


Expand Down
2 changes: 2 additions & 0 deletions packages/uiweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"html-react-parser": "^1.4.13",
"livekit-client": "^1.13.3",
"moment": "^2.29.4",
"protobufjs": "^7.2.6",
"raf": "^3.4.0",
"react-easy-crop": "^4.1.4",
"react-icons": "^4.10.1",
"react-image-file-resizer": "^0.4.7",
"react-player": "^2.16.0",
"react-toastify": "^9.1.3",
"react-twitter-embed": "^4.0.4",
"uuid": "^9.0.1"
Expand Down
111 changes: 39 additions & 72 deletions packages/uiweb/src/lib/components/chat/ChatViewList/ChatViewList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { appendUniqueMessages, dateToFromNowDaily, pCAIP10ToWallet, walletToPCAI
import { useChatData, usePushChatStream } from '../../../hooks';
import useFetchChat from '../../../hooks/chat/useFetchChat';
import useFetchMessageUtilities from '../../../hooks/chat/useFetchMessageUtilities';
import useGetGroupByIDnew from '../../../hooks/chat/useGetGroupByIDnew';
import useGroupMemberUtilities from '../../../hooks/chat/useGroupMemberUtilities';
import usePushUser from '../../../hooks/usePushUser';
import { Section, Span, Spinner } from '../../reusables';
import { ChatViewBubble } from '../ChatViewBubble';
import { checkIfNewRequest, transformStreamToIMessageIPFSWithCID } from '../helpers';
Expand Down Expand Up @@ -44,8 +41,6 @@ interface IThemeProps {
interface IChatViewListInitialized {
loading: boolean;
chatInfo: ChatInfoResponse | null;
groupInfo: Group | null;
isParticipant: boolean;
isHidden: boolean;
invalidChat: boolean;
}
Expand All @@ -64,24 +59,19 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
const [initialized, setInitialized] = useState<IChatViewListInitialized>({
loading: true,
chatInfo: null,
groupInfo: null,
isParticipant: false,
isHidden: false,
invalidChat: false,
});

const { chatId, limit = chatLimit, chatFilterList = [] } = options || {};
const { user, toast } = useChatData();
const [groupInfo, setGroupInfo] = useState<Group | null>(null);

// const [chatStatusText, setChatStatusText] = useState<string>('');
const [messages, setMessages] = useState<IMessageIPFSWithCID[]>([]);
const { historyMessages, historyLoading: messageLoading } = useFetchMessageUtilities();
const listInnerRef = useRef<HTMLDivElement>(null);
const [stopPagination, setStopPagination] = useState<boolean>(false);
const { fetchChat } = useFetchChat();
const { getGroupByIDnew } = useGetGroupByIDnew();
const { fetchMemberStatus } = useGroupMemberUtilities();

// for stream
const {
Expand All @@ -91,52 +81,51 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
participantJoinStream,
participantLeaveStream,
participantRemoveStream,
groupUpdateStream,
} = useChatData();

const theme = useContext(ThemeContext);
const dates = new Set();

// Primary Hook that fetches and sets ChatInfo which then fetches and sets UserInfo or GroupInfo
// Primary Hook that fetches and sets ChatInfo which then fetches and sets UserInfo
// Which then calls await getMessagesCall(); to fetch messages
useEffect(() => {
(async () => {
if (!user) return;
if (chatId) {
const info = await fetchChat({ chatId: chatId });

// get group info
let groupMeta;
if (info && info?.meta?.group) {
groupMeta = await getGroupByIDnew({ groupId: chatId });
}

// get member status
const status = await fetchMemberStatus({
chatId: chatId,
accountId: user?.account || '',
});

// also find out if chat is encrypted
// if readmode, then only public true is considered
// TODO: Hack for interface not declared properly (info?.meta as any)?.encryption
// TODO: Hack for interface not declared properly (info?.meta as any)?.groupInfo?.public
let hidden = false;
if (
user &&
!user.readmode() &&
((info?.meta?.group && status?.participant) ||
(!info?.meta?.group && (info?.list === 'CHATS' || info?.list === 'REQUESTS')) ||
(info?.meta?.group && groupMeta?.isPublic))
) {
if (user && user.readmode()) {
//check if encryption is false, only true for public groups
hidden = !(info?.meta as any)?.groupInfo?.public ?? true;
} else if (user && info?.meta) {
// Only executes when user is not readmode
// if encryption is false, return as is
// covers public group
// TODO: Hack for interface not declared properly (info?.meta as any)?.encryption
if ((info?.meta as any)?.encryption === false) {
hidden = false;
} else if (info?.meta?.group) {
// if encryption is true and group is in user list then hidden is false else true
// if group is encrypted, check if user list is CHATS, encryptioon false takes care of public groups
hidden = info.list === 'CHATS' ? false : true;
} else {
// if it's not a group, then check if list is CHATS or REQUESTS
hidden = info.list === 'CHATS' || info.list === 'REQUESTS' ? false : true;
}
hidden = false;
} else {
// for everything else, set hidden to true
hidden = true;
}

// Finally initialize the component
setInitialized({
loading: false,
chatInfo: Object.keys(info || {}).length ? (info as ChatInfoResponse) : null,
groupInfo: groupMeta ? groupMeta : null,
isParticipant: status?.participant ?? false,
isHidden: hidden,
invalidChat: info === undefined ? true : false,
});
Expand All @@ -148,8 +137,6 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
setInitialized({
loading: true,
chatInfo: null,
groupInfo: null,
isParticipant: false,
isHidden: false,
invalidChat: false,
});
Expand All @@ -170,7 +157,7 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
if (Object.keys(chatAcceptStream || {}).length > 0 && chatAcceptStream.constructor === Object) {
const updatedChatInfo = { ...(initialized.chatInfo as ChatInfoResponse) };
if (updatedChatInfo) updatedChatInfo.list = 'CHATS';
setInitialized({ ...initialized, chatInfo: updatedChatInfo });
setInitialized({ ...initialized, chatInfo: updatedChatInfo, isHidden: false });
}
}, [chatAcceptStream]);

Expand All @@ -190,11 +177,6 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
}
}, [chatRequestStream]);

useEffect(() => {
if (Object.keys(groupUpdateStream || {}).length > 0 && groupUpdateStream.constructor === Object)
transformGroupDetails(groupUpdateStream);
}, [groupUpdateStream]);

const transformSteamMessage = (item: any) => {
if (!user) {
return;
Expand All @@ -210,20 +192,6 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
}
}
};
const transformGroupDetails = (item: any): void => {
if (groupInfo?.chatId === item?.chatId) {
const updatedGroupInfo = groupInfo;
if (updatedGroupInfo) {
updatedGroupInfo.groupName = item?.meta?.name;
updatedGroupInfo.groupDescription = item?.meta?.description;
updatedGroupInfo.groupImage = item?.meta?.image;
updatedGroupInfo.groupCreator = item?.meta?.owner;
updatedGroupInfo.isPublic = !item?.meta?.private;
updatedGroupInfo.rules = item?.meta?.rules;
setGroupInfo(updatedGroupInfo);
}
}
};

useEffect(() => {
if (messages && messages?.length && messages?.length <= limit) {
Expand Down Expand Up @@ -343,13 +311,16 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
/>
)}

{/* TODO: Hack for interface not declared properly (initialized.chatInfo?.meta as any)?.encryption */}
{!initialized.loading &&
((user && user.pgpPublicKey) || (initialized.groupInfo && !initialized.groupInfo?.isPublic) ? (
((initialized.chatInfo?.meta as any)?.encryption ? (
<EncryptionMessage id={ENCRYPTION_KEYS.ENCRYPTED} />
) : user && user.readmode() ? (
<EncryptionMessage id={ENCRYPTION_KEYS.PREVIEW} />
) : (
<EncryptionMessage id={groupInfo ? ENCRYPTION_KEYS.NO_ENCRYPTED_GROUP : ENCRYPTION_KEYS.NO_ENCRYPTED} />
<EncryptionMessage
id={initialized.chatInfo?.meta?.group ? ENCRYPTION_KEYS.NO_ENCRYPTED_GROUP : ENCRYPTION_KEYS.NO_ENCRYPTED}
/>
))}
</Section>

Expand All @@ -361,19 +332,15 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
margin="10px 0 0 0"
flexDirection="column"
>
<Span
fontSize="13px"
color={theme.textColor?.encryptionMessageText}
fontWeight="400"
>
{messages &&
messages.length === 0 &&
!messageLoading &&
!groupInfo &&
!initialized.invalidChat &&
CHAT_STATUS.FIRST_CHAT}
{initialized.invalidChat && CHAT_STATUS.INVALID_CHAT}
</Span>
{initialized.invalidChat && (
<Span
fontSize="13px"
color={theme.textColor?.encryptionMessageText}
fontWeight="400"
>
{CHAT_STATUS.INVALID_CHAT}
</Span>
)}

{messageLoading ? <Spinner color={theme.spinnerColor} /> : ''}
</Section>
Expand All @@ -383,7 +350,7 @@ export const ChatViewList: React.FC<IChatViewListProps> = (options: IChatViewLis
flexDirection="column"
justifyContent="start"
width="100%"
blur={false}
blur={initialized.isHidden}
>
{messages &&
messages?.map((chat: IMessageIPFS, index: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const EncryptionMessage = ({ id, className }: { id: EncryptionKeys; class
const EncryptionMessageContent = {
ENCRYPTED: {
IconComponent: <EncryptionIcon size="15" />,
text: 'Messages are end-to-end encrypted. Only users in this chat can view or listen to them. Click to learn more.',
text: 'Messages are end-to-end encrypted. Only users in this chat can view or listen to them.',
},
NO_ENCRYPTED: {
IconComponent: <NoEncryptionIcon size="15" />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ChangeEvent, useContext, useEffect, useRef, useState } from 'react';

import EmojiPicker, { EmojiClickData } from 'emoji-picker-react';
import GifPicker from 'gif-picker-react';
import { createPortal } from 'react-dom';
import { MdCheckCircle, MdError } from 'react-icons/md';
import styled from 'styled-components';
import { createPortal } from 'react-dom';

import { deriveChatId, pCAIP10ToWallet, setAccessControl, walletToPCAIP10 } from '../../../helpers';
import { useChatData, useClickAway, useDeviceWidthCheck, usePushChatStream } from '../../../hooks';
Expand Down Expand Up @@ -236,7 +236,7 @@ export const MessageInput: React.FC<MessageInputProps> = ({
chatId: prevInfo.chatId, // Directly use the existing chatId, ensuring it's not undefined
meta: {
group: prevInfo.meta?.group ?? false, // Provide default value if undefined
encrypted: prevInfo.meta?.encrypted ?? false,
encryption: prevInfo.meta?.encryption ?? false,
},
};
});
Expand Down
5 changes: 4 additions & 1 deletion packages/uiweb/src/lib/components/chat/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export interface ChatInfoResponse {
chatId: string;
meta: {
group: boolean;
encrypted: boolean;
encryption: boolean;
groupInfo?: {
public: boolean;
};
};
participants?: Array<string>;
recipient?: string;
Expand Down
Loading

0 comments on commit 3d9a8dc

Please sign in to comment.