2018-03-23 16:49:51 +00:00
|
|
|
import { Alert } from 'react-native';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
put, call, takeLatest, take, select, race, fork, cancel, takeEvery
|
|
|
|
} from 'redux-saga/effects';
|
2017-11-21 16:55:32 +00:00
|
|
|
import { delay } from 'redux-saga';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { Navigation } from 'react-native-navigation';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2017-08-17 02:06:22 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2018-12-05 20:52:08 +00:00
|
|
|
import { addUserTyping, removeUserTyping } from '../actions/room';
|
2018-09-11 16:32:52 +00:00
|
|
|
import { messagesRequest, editCancel, replyCancel } from '../actions/messages';
|
2017-08-17 02:06:22 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-01-15 18:44:20 +00:00
|
|
|
import database from '../lib/realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2018-09-19 14:18:32 +00:00
|
|
|
import I18n from '../i18n';
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
const eraseRoom = rid => RocketChat.eraseRoom(rid);
|
2017-08-17 02:06:22 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
let sub;
|
|
|
|
let thread;
|
|
|
|
|
2017-11-21 16:55:32 +00:00
|
|
|
const cancelTyping = function* cancelTyping(username) {
|
|
|
|
while (true) {
|
|
|
|
const { typing, timeout } = yield race({
|
2017-11-21 17:09:22 +00:00
|
|
|
typing: take(types.ROOM.SOMEONE_TYPING),
|
2017-11-24 17:21:21 +00:00
|
|
|
timeout: call(delay, 5000)
|
2017-11-21 16:55:32 +00:00
|
|
|
});
|
|
|
|
if (timeout || (typing.username === username && !typing.typing)) {
|
|
|
|
return yield put(removeUserTyping(username));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const usersTyping = function* usersTyping({ rid }) {
|
2017-11-20 22:18:00 +00:00
|
|
|
while (true) {
|
2017-11-21 17:09:22 +00:00
|
|
|
const { _rid, username, typing } = yield take(types.ROOM.SOMEONE_TYPING);
|
2017-11-20 22:18:00 +00:00
|
|
|
if (_rid === rid) {
|
2017-11-21 16:55:32 +00:00
|
|
|
yield (typing ? put(addUserTyping(username)) : put(removeUserTyping(username)));
|
|
|
|
if (typing) {
|
2017-11-24 17:21:21 +00:00
|
|
|
yield fork(cancelTyping, username);
|
2017-11-21 16:55:32 +00:00
|
|
|
}
|
2017-11-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-01-15 18:44:20 +00:00
|
|
|
const handleMessageReceived = function* handleMessageReceived({ message }) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
|
|
|
const room = yield select(state => state.room);
|
2018-01-15 18:44:20 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (message.rid === room.rid) {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('messages', message, true);
|
|
|
|
});
|
2018-01-15 18:44:20 +00:00
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
if (room._id) {
|
|
|
|
RocketChat.readMessages(room.rid);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('handleMessageReceived', e);
|
2018-01-15 18:44:20 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-20 22:18:00 +00:00
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
let opened = false;
|
|
|
|
|
2017-11-21 16:55:32 +00:00
|
|
|
const watchRoomOpen = function* watchRoomOpen({ room }) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-09-19 14:18:32 +00:00
|
|
|
if (opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
opened = true;
|
|
|
|
|
2018-12-07 17:47:50 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(types.LOGIN.SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
yield put(messagesRequest({ ...room }));
|
2018-09-19 14:18:32 +00:00
|
|
|
|
|
|
|
if (room._id) {
|
|
|
|
RocketChat.readMessages(room.rid);
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
sub = yield RocketChat.subscribeRoom(room);
|
2018-12-05 20:52:08 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
thread = yield fork(usersTyping, { rid: room.rid });
|
|
|
|
yield race({
|
|
|
|
open: take(types.ROOM.OPEN),
|
|
|
|
close: take(types.ROOM.CLOSE)
|
|
|
|
});
|
2018-09-19 14:18:32 +00:00
|
|
|
opened = false;
|
2018-05-18 17:55:08 +00:00
|
|
|
cancel(thread);
|
|
|
|
sub.stop();
|
2018-06-18 13:30:36 +00:00
|
|
|
yield put(editCancel());
|
2018-09-11 16:32:52 +00:00
|
|
|
yield put(replyCancel());
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('watchRoomOpen', e);
|
|
|
|
}
|
2017-11-20 22:18:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-21 17:09:22 +00:00
|
|
|
const watchuserTyping = function* watchuserTyping({ status }) {
|
2017-11-21 16:55:32 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(types.LOGIN.SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
const room = yield select(state => state.room);
|
|
|
|
|
|
|
|
if (!room) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
yield RocketChat.emitTyping(room.rid, status);
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
yield call(delay, 5000);
|
|
|
|
yield RocketChat.emitTyping(room.rid, false);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('watchuserTyping', e);
|
2017-11-21 16:55:32 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-20 22:18:00 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
const goRoomsListAndDelete = function* goRoomsListAndDelete(rid) {
|
|
|
|
yield Navigation.popToRoot('RoomsListView');
|
2018-05-23 13:39:18 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
const messages = database.objects('messages').filtered('rid = $0', rid);
|
|
|
|
database.delete(messages);
|
|
|
|
const subscription = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
database.delete(subscription);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.warn('goRoomsListAndDelete', error);
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
};
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) {
|
2018-03-23 16:49:51 +00:00
|
|
|
try {
|
2018-05-18 17:55:08 +00:00
|
|
|
sub.stop();
|
2018-12-05 20:52:08 +00:00
|
|
|
yield RocketChat.leaveRoom(rid, t);
|
|
|
|
yield goRoomsListAndDelete(rid);
|
2018-03-23 16:49:51 +00:00
|
|
|
} catch (e) {
|
2018-12-05 20:52:08 +00:00
|
|
|
if (e.data && e.data.errorType === 'error-you-are-last-owner') {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t(e.data.errorType));
|
2018-03-23 16:49:51 +00:00
|
|
|
} else {
|
2018-12-05 20:52:08 +00:00
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('leaving_room') }));
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
const handleEraseRoom = function* handleEraseRoom({ rid }) {
|
|
|
|
try {
|
2018-05-18 17:55:08 +00:00
|
|
|
sub.stop();
|
2018-12-05 20:52:08 +00:00
|
|
|
yield eraseRoom(rid);
|
2018-10-23 21:39:48 +00:00
|
|
|
yield goRoomsListAndDelete(rid, 'erase');
|
2018-03-29 17:55:37 +00:00
|
|
|
} catch (e) {
|
2018-12-05 20:52:08 +00:00
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('erasing_room') }));
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const root = function* root() {
|
2017-11-21 17:09:22 +00:00
|
|
|
yield takeLatest(types.ROOM.USER_TYPING, watchuserTyping);
|
2017-11-21 16:55:32 +00:00
|
|
|
yield takeLatest(types.ROOM.OPEN, watchRoomOpen);
|
2018-01-15 18:44:20 +00:00
|
|
|
yield takeEvery(types.ROOM.MESSAGE_RECEIVED, handleMessageReceived);
|
2018-03-23 16:49:51 +00:00
|
|
|
yield takeLatest(types.ROOM.LEAVE, handleLeaveRoom);
|
2018-03-29 17:55:37 +00:00
|
|
|
yield takeLatest(types.ROOM.ERASE, handleEraseRoom);
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|