2019-02-07 16:13:21 +00:00
|
|
|
import { Alert } from 'react-native';
|
2020-05-08 17:36:10 +00:00
|
|
|
import prompt from 'react-native-prompt-android';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { delay, put, race, select, take, takeLatest } from 'redux-saga/effects';
|
2019-02-07 16:13:21 +00:00
|
|
|
|
2021-06-07 16:18:14 +00:00
|
|
|
import EventEmitter from '../utils/events';
|
2022-04-07 13:22:19 +00:00
|
|
|
import Navigation from '../lib/navigation/appNavigation';
|
2019-02-07 16:13:21 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2020-03-20 16:38:01 +00:00
|
|
|
import { removedRoom } from '../actions/room';
|
2019-02-07 16:13:21 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2021-09-13 20:41:05 +00:00
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2019-02-07 16:13:21 +00:00
|
|
|
import I18n from '../i18n';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2021-06-07 16:18:14 +00:00
|
|
|
import { LISTENER } from '../containers/Toast';
|
2019-02-07 16:13:21 +00:00
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
const watchUserTyping = function* watchUserTyping({ rid, status }) {
|
2019-02-07 16:13:21 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(types.LOGIN.SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-04-08 12:35:28 +00:00
|
|
|
yield RocketChat.emitTyping(rid, status);
|
2019-02-07 16:13:21 +00:00
|
|
|
|
|
|
|
if (status) {
|
2019-09-16 20:26:32 +00:00
|
|
|
yield delay(5000);
|
2019-04-08 12:35:28 +00:00
|
|
|
yield RocketChat.emitTyping(rid, false);
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-02 19:13:41 +00:00
|
|
|
const handleRemovedRoom = function* handleRemovedRoom(roomType, actionType) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
yield Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
yield Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2021-06-07 16:18:14 +00:00
|
|
|
|
2021-07-02 19:13:41 +00:00
|
|
|
if (actionType === 'leave') {
|
2021-09-13 20:41:05 +00:00
|
|
|
EventEmitter.emit(LISTENER, {
|
|
|
|
message: roomType === 'team' ? I18n.t('Left_The_Team_Successfully') : I18n.t('Left_The_Room_Successfully')
|
|
|
|
});
|
2021-06-07 16:18:14 +00:00
|
|
|
}
|
2021-07-02 19:13:41 +00:00
|
|
|
if (actionType === 'delete') {
|
2021-09-13 20:41:05 +00:00
|
|
|
EventEmitter.emit(LISTENER, {
|
|
|
|
message: roomType === 'team' ? I18n.t('Deleted_The_Team_Successfully') : I18n.t('Deleted_The_Room_Successfully')
|
|
|
|
});
|
2021-07-02 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 16:38:01 +00:00
|
|
|
// types.ROOM.REMOVE is triggered by `subscriptions-changed` with `removed` arg
|
|
|
|
const { timeout } = yield race({
|
|
|
|
deleteFinished: take(types.ROOM.REMOVED),
|
|
|
|
timeout: delay(3000)
|
|
|
|
});
|
|
|
|
if (timeout) {
|
|
|
|
put(removedRoom());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:18:14 +00:00
|
|
|
const handleLeaveRoom = function* handleLeaveRoom({ room, roomType, selected }) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.RA_LEAVE);
|
2019-02-07 16:13:21 +00:00
|
|
|
try {
|
2021-06-07 16:18:14 +00:00
|
|
|
let result = {};
|
|
|
|
|
|
|
|
if (roomType === 'channel') {
|
|
|
|
result = yield RocketChat.leaveRoom(room.rid, room.t);
|
|
|
|
} else if (roomType === 'team') {
|
2022-01-11 14:44:53 +00:00
|
|
|
result = yield RocketChat.leaveTeam({ teamId: room.teamId, ...(selected && { rooms: selected }) });
|
2021-06-07 16:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result?.success) {
|
2021-07-02 19:13:41 +00:00
|
|
|
yield handleRemovedRoom(roomType, 'leave');
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2019-02-07 16:13:21 +00:00
|
|
|
} catch (e) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.RA_LEAVE_F);
|
2019-02-07 16:13:21 +00:00
|
|
|
if (e.data && e.data.errorType === 'error-you-are-last-owner') {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t(e.data.errorType));
|
2021-06-07 20:20:07 +00:00
|
|
|
} else if (e?.data?.error === 'last-owner-can-not-be-removed') {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t(e.data.error));
|
2019-02-07 16:13:21 +00:00
|
|
|
} else {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('leaving_room') }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-02 19:13:41 +00:00
|
|
|
const handleDeleteRoom = function* handleDeleteRoom({ room, roomType, selected }) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.RI_EDIT_DELETE);
|
2019-02-07 16:13:21 +00:00
|
|
|
try {
|
2021-07-02 19:13:41 +00:00
|
|
|
let result = {};
|
|
|
|
|
|
|
|
if (roomType === 'channel') {
|
|
|
|
result = yield RocketChat.deleteRoom(room.rid, room.t);
|
|
|
|
} else if (roomType === 'team') {
|
|
|
|
result = yield RocketChat.deleteTeam({ teamId: room.teamId, ...(selected && { roomsToRemove: selected }) });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result?.success) {
|
|
|
|
yield handleRemovedRoom(roomType, 'delete');
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2019-02-07 16:13:21 +00:00
|
|
|
} catch (e) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.RI_EDIT_DELETE_F);
|
2021-09-13 20:41:05 +00:00
|
|
|
Alert.alert(
|
|
|
|
I18n.t('Oops'),
|
|
|
|
I18n.t('There_was_an_error_while_action', {
|
|
|
|
action: roomType === 'team' ? I18n.t('deleting_team') : I18n.t('deleting_room')
|
|
|
|
})
|
|
|
|
);
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
const handleCloseRoom = function* handleCloseRoom({ rid }) {
|
2020-06-26 20:45:21 +00:00
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
2020-05-08 17:36:10 +00:00
|
|
|
const requestComment = yield select(state => state.settings.Livechat_request_comment_when_closing_conversation);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const closeRoom = async (comment = '') => {
|
2020-05-08 17:36:10 +00:00
|
|
|
try {
|
|
|
|
await RocketChat.closeLivechat(rid, comment);
|
2020-06-15 14:00:46 +00:00
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!requestComment) {
|
|
|
|
const comment = I18n.t('Chat_closed_by_agent');
|
|
|
|
return closeRoom(comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt(
|
|
|
|
I18n.t('Closing_chat'),
|
|
|
|
I18n.t('Please_add_a_comment'),
|
|
|
|
[
|
2021-09-13 20:41:05 +00:00
|
|
|
{ text: I18n.t('Cancel'), onPress: () => {}, style: 'cancel' },
|
2020-05-08 17:36:10 +00:00
|
|
|
{
|
|
|
|
text: I18n.t('Submit'),
|
|
|
|
onPress: comment => closeRoom(comment)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cancelable: true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleForwardRoom = function* handleForwardRoom({ transferData }) {
|
|
|
|
try {
|
|
|
|
const result = yield RocketChat.forwardLivechat(transferData);
|
|
|
|
if (result === true) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} else {
|
|
|
|
showErrorAlert(I18n.t('No_available_agents_to_transfer'), I18n.t('Oops'));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
showErrorAlert(e.reason, I18n.t('Oops'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-07 16:13:21 +00:00
|
|
|
const root = function* root() {
|
2019-04-08 12:35:28 +00:00
|
|
|
yield takeLatest(types.ROOM.USER_TYPING, watchUserTyping);
|
2019-02-07 16:13:21 +00:00
|
|
|
yield takeLatest(types.ROOM.LEAVE, handleLeaveRoom);
|
2020-03-20 16:38:01 +00:00
|
|
|
yield takeLatest(types.ROOM.DELETE, handleDeleteRoom);
|
2020-05-08 17:36:10 +00:00
|
|
|
yield takeLatest(types.ROOM.CLOSE, handleCloseRoom);
|
|
|
|
yield takeLatest(types.ROOM.FORWARD, handleForwardRoom);
|
2019-02-07 16:13:21 +00:00
|
|
|
};
|
|
|
|
export default root;
|