2023-09-26 14:33:53 +00:00
|
|
|
import { CompositeNavigationProp, RouteProp, useNavigation, useRoute } from '@react-navigation/native';
|
2023-03-01 18:26:56 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2023-08-11 20:35:24 +00:00
|
|
|
import { uniq } from 'lodash';
|
2023-03-01 18:26:56 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2023-09-26 14:33:53 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
import { ScrollView, View } from 'react-native';
|
|
|
|
import { Subscription } from 'rxjs';
|
2023-03-01 18:26:56 +00:00
|
|
|
import UAParser from 'ua-parser-js';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2023-03-01 18:26:56 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-07-17 17:08:15 +00:00
|
|
|
import { LISTENER } from '../../containers/Toast';
|
2023-09-26 14:33:53 +00:00
|
|
|
import { ISubscription, IUser, SubscriptionType } from '../../definitions';
|
2023-03-01 18:26:56 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { getSubscriptionByRoomId } from '../../lib/database/services/Subscription';
|
2023-09-26 14:33:53 +00:00
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
2023-03-01 18:26:56 +00:00
|
|
|
import { getRoomTitle, getUidDirectMessage, hasPermission } from '../../lib/methods/helpers';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
|
|
|
import { goRoom } from '../../lib/methods/helpers/goRoom';
|
2023-03-01 18:26:56 +00:00
|
|
|
import { handleIgnore } from '../../lib/methods/helpers/handleIgnore';
|
|
|
|
import log, { events, logEvent } from '../../lib/methods/helpers/log';
|
|
|
|
import { Services } from '../../lib/services';
|
|
|
|
import { MasterDetailInsideStackParamList } from '../../stacks/MasterDetailStack/types';
|
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
2023-09-26 14:33:53 +00:00
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import RoomInfoButtons from './components/RoomInfoButtons';
|
|
|
|
import RoomInfoViewAvatar from './components/RoomInfoViewAvatar';
|
|
|
|
import RoomInfoViewBody from './components/RoomInfoViewBody';
|
|
|
|
import RoomInfoViewTitle from './components/RoomInfoViewTitle';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
2022-03-03 21:46:53 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
type TRoomInfoViewNavigationProp = CompositeNavigationProp<
|
2024-01-03 21:14:31 +00:00
|
|
|
StackNavigationProp<ChatsStackParamList, 'RoomInfoView'>,
|
|
|
|
StackNavigationProp<MasterDetailInsideStackParamList>
|
2023-09-26 14:33:53 +00:00
|
|
|
>;
|
|
|
|
|
|
|
|
type TRoomInfoViewRouteProp = RouteProp<ChatsStackParamList, 'RoomInfoView'>;
|
|
|
|
|
|
|
|
const RoomInfoView = (): React.ReactElement => {
|
|
|
|
const {
|
2023-11-08 18:03:53 +00:00
|
|
|
params: { rid, t, fromRid, member, room: roomParam, showCloseModal, itsMe }
|
2023-09-26 14:33:53 +00:00
|
|
|
} = useRoute<TRoomInfoViewRouteProp>();
|
|
|
|
const { addListener, setOptions, navigate, goBack } = useNavigation<TRoomInfoViewNavigationProp>();
|
|
|
|
|
2024-01-22 17:52:44 +00:00
|
|
|
const [room, setRoom] = useState(roomParam || ({ rid, t } as ISubscription));
|
2023-09-26 14:33:53 +00:00
|
|
|
const [roomFromRid, setRoomFromRid] = useState<ISubscription | undefined>();
|
|
|
|
const [roomUser, setRoomUser] = useState(member || {});
|
|
|
|
const [showEdit, setShowEdit] = useState(false);
|
|
|
|
|
|
|
|
const roomType = room?.t || t;
|
|
|
|
const isDirect = roomType === SubscriptionType.DIRECT;
|
|
|
|
const isLivechat = roomType === SubscriptionType.OMNICHANNEL;
|
|
|
|
|
|
|
|
const subscription = useRef<Subscription | undefined>(undefined);
|
|
|
|
|
|
|
|
const {
|
|
|
|
isMasterDetail,
|
|
|
|
subscribedRoom,
|
|
|
|
usersRoles,
|
|
|
|
roles,
|
2024-01-03 21:14:31 +00:00
|
|
|
serverVersion,
|
2023-09-26 14:33:53 +00:00
|
|
|
// permissions
|
|
|
|
editRoomPermission,
|
|
|
|
editOmnichannelContact,
|
|
|
|
editLivechatRoomCustomfields
|
|
|
|
} = useAppSelector(state => ({
|
|
|
|
subscribedRoom: state.room.subscribedRoom,
|
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
|
|
|
roles: state.roles,
|
|
|
|
usersRoles: state.usersRoles,
|
2024-01-03 21:14:31 +00:00
|
|
|
serverVersion: state.server.version,
|
2023-09-26 14:33:53 +00:00
|
|
|
// permissions
|
|
|
|
editRoomPermission: state.permissions['edit-room'],
|
|
|
|
editOmnichannelContact: state.permissions['edit-omnichannel-contact'],
|
|
|
|
editLivechatRoomCustomfields: state.permissions['edit-livechat-room-customfields']
|
|
|
|
}));
|
|
|
|
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const listener = addListener('focus', () => (isLivechat ? loadVisitor() : null));
|
|
|
|
return () => listener();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
subscription.current?.unsubscribe();
|
|
|
|
},
|
|
|
|
[]
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
loadRoom();
|
|
|
|
if (isDirect) loadUser();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const setHeader = (canEdit?: boolean) => {
|
|
|
|
const HeaderRight = () => (
|
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item
|
|
|
|
iconName='edit'
|
|
|
|
onPress={() => {
|
|
|
|
if (!room) return;
|
|
|
|
logEvent(events[`RI_GO_${isLivechat ? 'LIVECHAT' : 'RI'}_EDIT`]);
|
|
|
|
const navigationProps = { room, roomUser };
|
|
|
|
if (isLivechat) navigate('LivechatEditView', navigationProps);
|
|
|
|
else navigate('RoomInfoEditView', { rid, ...navigationProps });
|
|
|
|
}}
|
|
|
|
testID='room-info-view-edit-button'
|
|
|
|
/>
|
|
|
|
</HeaderButton.Container>
|
|
|
|
);
|
|
|
|
setOptions({
|
|
|
|
headerLeft: showCloseModal ? () => <HeaderButton.CloseModal /> : undefined,
|
|
|
|
title: isDirect ? I18n.t('User_Info') : I18n.t('Room_Info'),
|
|
|
|
headerRight: canEdit ? () => <HeaderRight /> : undefined
|
2020-06-15 14:00:46 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const loadVisitor = async () => {
|
2020-05-08 17:36:10 +00:00
|
|
|
try {
|
2023-09-26 14:33:53 +00:00
|
|
|
if (room?.visitor?._id) {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getVisitorInfo(room.visitor._id);
|
2022-03-03 21:46:53 +00:00
|
|
|
if (result.success) {
|
|
|
|
const { visitor } = result;
|
|
|
|
const params: { os?: string; browser?: string } = {};
|
|
|
|
if (visitor.userAgent) {
|
|
|
|
const ua = new UAParser();
|
|
|
|
ua.setUA(visitor.userAgent);
|
|
|
|
params.os = `${ua.getOS().name} ${ua.getOS().version}`;
|
|
|
|
params.browser = `${ua.getBrowser().name} ${ua.getBrowser().version}`;
|
|
|
|
}
|
2023-09-26 14:33:53 +00:00
|
|
|
setRoomUser({ ...visitor, ...params });
|
|
|
|
setHeader();
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
2020-03-30 20:19:01 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const parseRoles = (roleArray: string[]) => roleArray.map(role => roles[role]);
|
2022-01-12 14:04:15 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const handleRoles = (user: Pick<IUser, 'username' | 'roles'>) => {
|
|
|
|
const rrr = (() => {
|
2023-08-11 20:35:24 +00:00
|
|
|
const userRoles = usersRoles.find(u => u?.username === user.username);
|
|
|
|
let r: string[] = [];
|
|
|
|
if (userRoles?.roles?.length) r = userRoles.roles;
|
|
|
|
if (user.roles?.length) r = [...r, ...user.roles];
|
|
|
|
return uniq(r);
|
|
|
|
})();
|
2023-09-26 14:33:53 +00:00
|
|
|
if (rrr.length) {
|
|
|
|
const parsedRoles = parseRoles(rrr);
|
|
|
|
return parsedRoles;
|
2023-08-11 20:35:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const loadUser = async () => {
|
2021-02-01 17:18:55 +00:00
|
|
|
if (isEmpty(roomUser)) {
|
2019-04-29 16:03:52 +00:00
|
|
|
try {
|
2023-11-08 18:03:53 +00:00
|
|
|
const roomUserId = getUidDirectMessage(room || { rid, t, itsMe });
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getUserInfo(roomUserId);
|
2019-04-29 16:03:52 +00:00
|
|
|
if (result.success) {
|
2020-05-08 17:36:10 +00:00
|
|
|
const { user } = result;
|
2023-09-26 14:33:53 +00:00
|
|
|
const r = handleRoles(user);
|
|
|
|
setRoomUser({ ...user, roles: r });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2023-12-26 14:43:57 +00:00
|
|
|
} else {
|
|
|
|
const r = handleRoles(roomUser);
|
|
|
|
if (r) setRoomUser({ ...roomUser, roles: r });
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const loadRoom = async () => {
|
|
|
|
const permissionToEdit = isLivechat ? [editOmnichannelContact, editLivechatRoomCustomfields] : [editRoomPermission];
|
|
|
|
const permissions = await hasPermission(permissionToEdit, rid);
|
|
|
|
const canEdit = permissions.some(Boolean);
|
|
|
|
const subRoom = await getSubscriptionByRoomId(rid);
|
|
|
|
if (!subRoom && isDirect && fromRid) {
|
|
|
|
const roomFromRid = await getSubscriptionByRoomId(fromRid);
|
|
|
|
if (roomFromRid?.observe) {
|
|
|
|
const sub = roomFromRid.observe();
|
|
|
|
subscription.current = sub.subscribe(changes => {
|
|
|
|
setRoomFromRid(changes.asPlain());
|
2022-10-13 21:29:55 +00:00
|
|
|
});
|
|
|
|
}
|
2023-09-26 14:33:53 +00:00
|
|
|
} else if (subRoom?.observe) {
|
|
|
|
const sub = subRoom.observe();
|
|
|
|
subscription.current = sub.subscribe(changes => {
|
|
|
|
setRoom(changes.asPlain());
|
|
|
|
setHeader(canEdit);
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-08-22 18:08:07 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2023-09-26 14:33:53 +00:00
|
|
|
if (!isDirect) {
|
|
|
|
const result = await Services.getRoomInfo(rid);
|
|
|
|
if (result.success) setRoom({ ...room, ...(result.room as unknown as ISubscription) });
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
2019-08-30 12:43:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-26 14:33:53 +00:00
|
|
|
setShowEdit(canEdit);
|
|
|
|
setHeader(canEdit);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const createDirect = () =>
|
|
|
|
new Promise<void | ISubscription>(async (resolve, reject) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
// We don't need to create a direct
|
2023-09-26 14:33:53 +00:00
|
|
|
if (!isEmpty(member)) return resolve();
|
2021-09-13 20:41:05 +00:00
|
|
|
try {
|
2023-09-26 14:33:53 +00:00
|
|
|
const result = await Services.createDirectMessage(roomUser.username);
|
|
|
|
if (result.success) return resolve({ ...roomUser, rid: result.room.rid });
|
2021-09-13 20:41:05 +00:00
|
|
|
} catch {
|
2023-09-26 14:33:53 +00:00
|
|
|
reject();
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const handleGoRoom = (r?: ISubscription) => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.RI_GO_ROOM_USER);
|
2020-06-15 14:00:46 +00:00
|
|
|
const params = {
|
2023-09-26 14:33:53 +00:00
|
|
|
rid: r?.rid,
|
|
|
|
name: getRoomTitle(r),
|
2024-01-22 17:52:44 +00:00
|
|
|
t: roomType,
|
2023-09-26 14:33:53 +00:00
|
|
|
roomUserId: getUidDirectMessage(r)
|
2020-06-15 14:00:46 +00:00
|
|
|
};
|
2020-05-11 14:19:28 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
if (r?.rid) {
|
|
|
|
if (r.rid === subscribedRoom) {
|
2022-11-25 13:21:56 +00:00
|
|
|
if (isMasterDetail) {
|
2023-09-26 14:33:53 +00:00
|
|
|
return navigate('DrawerNavigator');
|
2020-06-15 14:00:46 +00:00
|
|
|
}
|
2023-09-26 14:33:53 +00:00
|
|
|
goBack();
|
|
|
|
goBack();
|
|
|
|
return;
|
2020-02-14 00:25:42 +00:00
|
|
|
}
|
2022-11-25 13:21:56 +00:00
|
|
|
// if it's on master detail layout, we close the modal and replace RoomView
|
|
|
|
goRoom({ item: params, isMasterDetail, popToRoot: true });
|
2020-02-14 00:25:42 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-14 00:25:42 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const handleCreateDirectMessage = async () => {
|
2022-10-13 21:29:55 +00:00
|
|
|
try {
|
2023-09-26 14:33:53 +00:00
|
|
|
let r = room;
|
|
|
|
if (isDirect) {
|
|
|
|
const direct = await createDirect();
|
|
|
|
if (direct) r = direct;
|
2022-10-13 21:29:55 +00:00
|
|
|
}
|
2023-09-26 14:33:53 +00:00
|
|
|
handleGoRoom(r);
|
2022-10-13 21:29:55 +00:00
|
|
|
} catch {
|
|
|
|
EventEmitter.emit(LISTENER, {
|
|
|
|
message: I18n.t('error-action-not-allowed', { action: I18n.t('Create_Direct_Messages') })
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const handleBlockUser = async () => {
|
|
|
|
const r = roomFromRid || room;
|
|
|
|
const userBlocked = roomUser._id;
|
|
|
|
const blocker = r?.blocker;
|
|
|
|
if (!r?.rid) return;
|
2022-10-13 21:29:55 +00:00
|
|
|
logEvent(events.RI_TOGGLE_BLOCK_USER);
|
|
|
|
try {
|
2023-09-26 14:33:53 +00:00
|
|
|
await Services.toggleBlockUser(r.rid, userBlocked, !blocker);
|
2022-10-13 21:29:55 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
};
|
2023-04-10 14:59:00 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
const handleIgnoreUser = () => {
|
|
|
|
const r = roomFromRid || room;
|
|
|
|
const isIgnored = r?.ignored?.includes?.(roomUser._id);
|
|
|
|
if (r?.rid) handleIgnore(roomUser._id, !isIgnored, r?.rid);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-14 00:25:42 +00:00
|
|
|
|
2024-01-03 21:14:31 +00:00
|
|
|
const handleReportUser = () => {
|
|
|
|
navigate('ReportUserView', {
|
|
|
|
name: roomUser?.name,
|
|
|
|
userId: roomUser?._id,
|
|
|
|
username: roomUser.username
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
return (
|
|
|
|
<ScrollView style={[styles.scroll, { backgroundColor: colors.backgroundColor }]}>
|
|
|
|
<StatusBar />
|
|
|
|
<SafeAreaView style={{ backgroundColor: colors.backgroundColor }} testID='room-info-view'>
|
|
|
|
<View style={[styles.avatarContainer, { backgroundColor: colors.auxiliaryBackground }]}>
|
|
|
|
<RoomInfoViewAvatar
|
|
|
|
username={room?.name || roomUser.username}
|
|
|
|
rid={room?.rid}
|
|
|
|
userId={roomUser?._id}
|
|
|
|
handleEditAvatar={() => navigate('ChangeAvatarView', { titleHeader: I18n.t('Room_Info'), room, t, context: 'room' })}
|
|
|
|
showEdit={showEdit}
|
|
|
|
type={t}
|
|
|
|
/>
|
|
|
|
<RoomInfoViewTitle
|
|
|
|
type={t}
|
|
|
|
room={room || roomUser}
|
|
|
|
name={roomUser?.name}
|
|
|
|
username={roomUser?.username}
|
|
|
|
statusText={roomUser?.statusText}
|
|
|
|
/>
|
|
|
|
<RoomInfoButtons
|
|
|
|
rid={room?.rid || rid}
|
|
|
|
fromRid={fromRid}
|
|
|
|
handleBlockUser={handleBlockUser}
|
|
|
|
handleCreateDirectMessage={handleCreateDirectMessage}
|
|
|
|
handleIgnoreUser={handleIgnoreUser}
|
2024-01-03 21:14:31 +00:00
|
|
|
handleReportUser={handleReportUser}
|
2023-09-26 14:33:53 +00:00
|
|
|
isDirect={isDirect}
|
|
|
|
room={room || roomUser}
|
|
|
|
roomUserId={roomUser?._id}
|
|
|
|
roomFromRid={roomFromRid}
|
2024-01-03 21:14:31 +00:00
|
|
|
serverVersion={serverVersion}
|
|
|
|
itsMe={itsMe}
|
2023-09-26 14:33:53 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<RoomInfoViewBody isDirect={isDirect} room={room} roomUser={roomUser} />
|
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
};
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2023-09-26 14:33:53 +00:00
|
|
|
export default RoomInfoView;
|