2018-03-23 16:49:51 +00:00
|
|
|
import { Alert } from 'react-native';
|
2018-01-15 18:44:20 +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-04-24 19:34:03 +00:00
|
|
|
import { BACKGROUND } from 'redux-enhancer-react-native-appstate';
|
2017-08-17 02:06:22 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2018-04-24 19:34:03 +00:00
|
|
|
// import { roomsSuccess, roomsFailure } from '../actions/rooms';
|
2018-01-15 18:44:20 +00:00
|
|
|
import { addUserTyping, removeUserTyping, setLastOpen } from '../actions/room';
|
2017-11-20 22:18:00 +00:00
|
|
|
import { messagesRequest } 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-03-23 16:49:51 +00:00
|
|
|
import * as NavigationService from '../containers/routes/NavigationService';
|
|
|
|
|
|
|
|
const leaveRoom = rid => RocketChat.leaveRoom(rid);
|
2018-03-29 17:55:37 +00:00
|
|
|
const eraseRoom = rid => RocketChat.eraseRoom(rid);
|
2017-08-17 02:06:22 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
// const getRooms = function* getRooms() {
|
|
|
|
// return yield RocketChat.getRooms();
|
|
|
|
// };
|
2017-08-17 02:06:22 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
// const watchRoomsRequest = function* watchRoomsRequest() {
|
|
|
|
// try {
|
|
|
|
// yield call(getRooms);
|
|
|
|
// yield put(roomsSuccess());
|
|
|
|
// } catch (err) {
|
|
|
|
// yield put(roomsFailure(err.status));
|
|
|
|
// }
|
|
|
|
// };
|
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-04-24 19:34:03 +00:00
|
|
|
RocketChat.readMessages(room.rid);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('handleMessageReceived', e);
|
2018-01-15 18:44:20 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-20 22:18:00 +00:00
|
|
|
|
2017-11-21 16:55:32 +00:00
|
|
|
const watchRoomOpen = function* watchRoomOpen({ room }) {
|
2018-04-24 19:34:03 +00:00
|
|
|
yield put(messagesRequest({ ...room }));
|
|
|
|
// const { open } = yield race({
|
|
|
|
// messages: take(types.MESSAGES.SUCCESS),
|
|
|
|
// open: take(types.ROOM.OPEN)
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// if (open) {
|
|
|
|
// return;
|
|
|
|
// }
|
2017-11-21 16:55:32 +00:00
|
|
|
|
|
|
|
RocketChat.readMessages(room.rid);
|
2018-04-24 19:34:03 +00:00
|
|
|
const sub = yield RocketChat.subscribeRoom(room);
|
|
|
|
// const subscriptions = yield Promise.all([RocketChat.subscribe('stream-room-messages', room.rid, false), RocketChat.subscribe('stream-notify-room', `${ room.rid }/typing`, false)]);
|
2017-11-21 16:55:32 +00:00
|
|
|
const thread = yield fork(usersTyping, { rid: room.rid });
|
2018-01-15 18:44:20 +00:00
|
|
|
yield race({
|
|
|
|
open: take(types.ROOM.OPEN),
|
|
|
|
close: take(types.ROOM.CLOSE)
|
|
|
|
});
|
2017-11-20 22:18:00 +00:00
|
|
|
cancel(thread);
|
2018-04-24 19:34:03 +00:00
|
|
|
sub.stop();
|
|
|
|
|
|
|
|
// subscriptions.forEach((sub) => {
|
|
|
|
// sub.unsubscribe().catch(e => alert(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;
|
|
|
|
}
|
|
|
|
yield RocketChat.emitTyping(room.rid, status);
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
yield call(delay, 5000);
|
|
|
|
yield RocketChat.emitTyping(room.rid, false);
|
|
|
|
}
|
|
|
|
};
|
2017-11-20 22:18:00 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
// const updateRoom = function* updateRoom() {
|
|
|
|
// const room = yield select(state => state.room);
|
|
|
|
// if (!room || !room.rid) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// yield put(messagesRequest({ rid: room.rid }));
|
|
|
|
// };
|
2018-01-15 18:44:20 +00:00
|
|
|
|
|
|
|
const updateLastOpen = function* updateLastOpen() {
|
|
|
|
yield put(setLastOpen());
|
|
|
|
};
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
const goRoomsListAndDelete = function* goRoomsListAndDelete(rid) {
|
|
|
|
NavigationService.goRoomsList();
|
|
|
|
yield delay(1000);
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
const handleLeaveRoom = function* handleLeaveRoom({ rid }) {
|
|
|
|
try {
|
|
|
|
yield call(leaveRoom, rid);
|
2018-03-29 17:55:37 +00:00
|
|
|
yield goRoomsListAndDelete(rid);
|
2018-03-23 16:49:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.error === 'error-you-are-last-owner') {
|
|
|
|
Alert.alert('You are the last owner. Please set new owner before leaving the room.');
|
|
|
|
} else {
|
2018-03-29 17:55:37 +00:00
|
|
|
Alert.alert('Something happened when leaving room!');
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
const handleEraseRoom = function* handleEraseRoom({ rid }) {
|
|
|
|
try {
|
|
|
|
yield call(eraseRoom, rid);
|
|
|
|
yield goRoomsListAndDelete(rid);
|
|
|
|
} catch (e) {
|
|
|
|
Alert.alert('Something happened when erasing room!');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-04-24 19:34:03 +00:00
|
|
|
// yield takeLatest(FOREGROUND, updateRoom);
|
|
|
|
// yield takeLatest(FOREGROUND, watchRoomsRequest);
|
2018-01-15 18:44:20 +00:00
|
|
|
yield takeLatest(BACKGROUND, updateLastOpen);
|
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;
|