Skip to content

Commit

Permalink
Merge pull request #1144 from thomas-portkey/fix/im-user-id
Browse files Browse the repository at this point in the history
fix: 🐛 im user id
  • Loading branch information
portkey-yellow authored Oct 21, 2023
2 parents 55e51da + 3e812fb commit 3af1ed2
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 38 deletions.
34 changes: 22 additions & 12 deletions packages/hooks/hooks-ca/im/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export const useIsStranger = (relationId: string) => {
export const useSendChannelMessage = () => {
const dispatch = useAppCommonDispatch();
const { networkType } = useCurrentNetworkInfo();
const relationId = useRelationId();
const { relationId, getRelationId } = useRelationId();
const { walletName } = useWallet();

const sendMessageToPeople = useCallback(
({
async ({
toRelationId,
channelId,
type = 'TEXT',
Expand All @@ -69,19 +69,24 @@ export const useSendChannelMessage = () => {
if (!(toRelationId || channelId)) {
throw new Error('No ID');
}
if (!relationId) {
throw new Error('No user info');
let _relationId = relationId;
if (!_relationId) {
try {
_relationId = await getRelationId();
} catch (error) {
throw new Error('No user info');
}
}
const uuid = randomId();
return im.service.sendMessage({
channelUuid: channelId,
toRelationId,
type,
content,
sendUuid: `${relationId}-${toRelationId}-${Date.now()}-${uuid}`,
sendUuid: `${_relationId}-${toRelationId}-${Date.now()}-${uuid}`,
});
},
[relationId],
[getRelationId, relationId],
);

const sendMassMessage = useCallback(
Expand Down Expand Up @@ -125,20 +130,25 @@ export const useSendChannelMessage = () => {

const sendChannelMessage = useCallback(
async (channelId: string, content: string, type = 'TEXT' as MessageType) => {
if (!relationId) {
throw new Error('No user info');
let _relationId = relationId;
if (!_relationId) {
try {
_relationId = await getRelationId();
} catch (error) {
throw new Error('No user info');
}
}
const uuid = randomId();
const msgParams = {
channelUuid: channelId,
type,
content,
sendUuid: `${relationId}-${channelId}-${Date.now()}-${uuid}`,
sendUuid: `${_relationId}-${channelId}-${Date.now()}-${uuid}`,
};

const msgObj: Message = messageParser({
...msgParams,
from: relationId,
from: _relationId,
fromAvatar: '',
fromName: walletName,
createAt: `${Date.now()}`,
Expand Down Expand Up @@ -179,7 +189,7 @@ export const useSendChannelMessage = () => {
throw error;
}
},
[dispatch, networkType, relationId, walletName],
[dispatch, getRelationId, networkType, relationId, walletName],
);
const sendChannelImageByS3Result = useCallback(
async (channelId: string, s3Result: UploadFileType & ImageMessageFileType) => {
Expand Down Expand Up @@ -391,7 +401,7 @@ export const useChannel = (channelId: string) => {
const { networkType } = useCurrentNetworkInfo();
const dispatch = useAppCommonDispatch();

const relationId = useRelationId();
const { relationId } = useRelationId();

const muteChannel = useMuteChannel();
const pinChannel = usePinChannel();
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/hooks-ca/im/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const useGroupChannelInfo = (channelId: string, isInit = false) => {
[channelId, groupInfoMapNetMap, networkType],
);

const relationId = useRelationId();
const { relationId } = useRelationId();

const refresh = useCallback(async () => {
const { data: groupInfo } = await im.service.getChannelInfo({
Expand Down
50 changes: 34 additions & 16 deletions packages/hooks/hooks-ca/im/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useChannelList } from './channelList';
import { fetchContactListAsync } from '@portkey-wallet/store/store-ca/contact/actions';
import { request } from '@portkey-wallet/api/api-did';
import useLockCallback from '../../useLockCallback';
import { handleLoopFetch } from '@portkey-wallet/utils';

export const useIMState = () => useAppCASelector(state => state.im);
export const useIMHasNextNetMapState = () => useAppCASelector(state => state.im.hasNextNetMap);
Expand All @@ -43,9 +44,38 @@ export const useUnreadCount = () => {
return unreadCount;
};

export const useRelationId = () => {
const dispatch = useAppCommonDispatch();
const { networkType } = useCurrentNetworkInfo();
const relationIdNetMap = useIMRelationIdNetMapNetMapState();

const relationId = useMemo(() => relationIdNetMap?.[networkType], [networkType, relationIdNetMap]);

const getRelationId = useCallback(async () => {
const { data: userInfo } = await im.service.getUserInfo();
if (userInfo?.relationId) {
dispatch(
setRelationId({
network: networkType,
relationId: userInfo.relationId,
}),
);

return userInfo.relationId;
}
throw new Error('can not get im info');
}, [dispatch, networkType]);

return {
relationId,
getRelationId,
};
};

export const useInitIM = () => {
const { networkType } = useCurrentNetworkInfo();
const dispatch = useAppCommonDispatch();
const { getRelationId } = useRelationId();

const channelListNetMap = useIMChannelListNetMapState();
const list = useMemo(() => channelListNetMap?.[networkType]?.list || [], [channelListNetMap, networkType]);
Expand Down Expand Up @@ -185,28 +215,16 @@ export const useInitIM = () => {
filter: `caHash: ${caHash}`,
},
});
const { data: userInfo } = await im.service.getUserInfo();
if (userInfo?.relationId) {
dispatch(
setRelationId({
network: networkType,
relationId: userInfo.relationId,
}),
);
}

handleLoopFetch(getRelationId, 3).catch(error => {
console.log('initIm getRelationId error', error);
});
},
[dispatch, networkType, relationToken],
);
return initIm;
};

export const useRelationId = () => {
const { networkType } = useCurrentNetworkInfo();
const relationIdNetMap = useIMRelationIdNetMapNetMapState();

return useMemo(() => relationIdNetMap?.[networkType], [networkType, relationIdNetMap]);
};

export const useIsIMReady = () => {
return [IMStatusEnum.AUTHORIZED, IMStatusEnum.CONNECTED].includes(im.status);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ShowShareWithOverlay } from '../components/ShareWithOverlay';
import { copyText } from 'utils';

const GroupInfoPage = () => {
const myRelationId = useRelationId();
const { relationId: myRelationId } = useRelationId();

const currentChannelId = useCurrentChannelId();
const { groupInfo, isAdmin, refresh } = useGroupChannelInfo(currentChannelId || '', false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import navigationService from 'utils/navigationService';
import { strIncludes } from '@portkey-wallet/utils';

const GroupMembersPage = () => {
const myRelationId = useRelationId();
const { relationId: myRelationId } = useRelationId();
const currentChannelId = useCurrentChannelId();
const { groupInfo } = useGroupChannelInfo(currentChannelId || '', false);
const { members = [] } = groupInfo || {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function ChatsDetailContent() {
const { list, init, next, hasNext } = useChannel(currentChannelId || '');
const [initializing, setInitializing] = useState(true);
const formattedList = useMemo(() => formatMessageList(list), [list]);
const relationId = useRelationId();
const { relationId } = useRelationId();
const user = useMemo(() => ({ _id: relationId || '' }), [relationId]);

const onLoadEarlier = useLockCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const ChatsUI = () => {
};
}, [onDismiss, onLoadEarlier]);

const relationId = useRelationId();
const { relationId } = useRelationId();
const user = useMemo(() => ({ _id: relationId || '' }), [relationId]);

useEffectOnce(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function ChatsGroupDetailContent() {
const { list, init, hasNext, next } = useGroupChannel(currentChannelId || '');
const [initializing, setInitializing] = useState(true);
const formattedList = useMemo(() => formatMessageList(list), [list]);
const relationId = useRelationId();
const { relationId } = useRelationId();
const user = useMemo(() => ({ _id: relationId || '' }), [relationId]);

const onLoadEarlier = useLockCallback(async () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,21 @@ export function handlePhoneNumber(str?: string) {
}
return str || '';
}

export const handleLoopFetch = async <T>(
fetch: () => Promise<T>,
times = 0,
interval = 0,
checkIsContinue?: () => boolean,
): Promise<T> => {
try {
return await fetch();
} catch (error) {
const isContinue = checkIsContinue ? checkIsContinue() : true;
if (!isContinue) throw new Error('fetch invalid');
if (times === 1) throw error;
console.log('handleLoopFetch: error', times, error);
}
await sleep(interval);
return handleLoopFetch(fetch, times - 1, interval, checkIsContinue);
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function ChatBox() {
init();
});
const hideChannel = useHideChannel();
const relationId = useRelationId();
const { relationId } = useRelationId();
const messageList: MessageType[] = useMemo(() => formatMessageList(list, relationId!, true), [list, relationId]);
const leaveGroup = useLeaveChannel();
const { handleDeleteMsg, handlePin, handleMute } = useHandle({ info, mute, pin, deleteMessage });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function ChatBox() {
useEffectOnce(() => {
init();
});
const relationId = useRelationId();
const { relationId } = useRelationId();
const messageList: MessageType[] = useMemo(() => formatMessageList(list, relationId!), [list, relationId]);
const handleDelete = useCallback(() => {
CustomModalConfirm({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import './index.less';
const GroupInfo = () => {
const { channelUuid } = useParams();
const leaveGroup = useLeaveChannel();
const myRelationId = useRelationId();
const { relationId: myRelationId } = useRelationId();
const [shareVisible, setShareVisible] = useState(false);
const { groupInfo, isAdmin, refresh } = useGroupChannelInfo(`${channelUuid}`);
const { sendMassMessage } = useSendChannelMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import './index.less';

export default function MemberList() {
const { channelUuid } = useParams();
const myRelationId = useRelationId();
const { relationId: myRelationId } = useRelationId();
const { groupInfo, refresh } = useGroupChannelInfo(`${channelUuid}`);
const { t } = useTranslation();
const { state } = useLocation();
Expand Down

0 comments on commit 3af1ed2

Please sign in to comment.