2017-11-21 16:55:32 +00:00
|
|
|
import { put, call, takeLatest, take, select, race, fork, cancel } from 'redux-saga/effects';
|
|
|
|
import { delay } from 'redux-saga';
|
2017-08-17 02:06:22 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
|
|
|
import { roomsSuccess, roomsFailure } from '../actions/rooms';
|
2017-11-20 22:18:00 +00:00
|
|
|
import { addUserTyping, removeUserTyping } from '../actions/room';
|
|
|
|
import { messagesRequest } from '../actions/messages';
|
2017-08-17 02:06:22 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const getRooms = function* getRooms() {
|
|
|
|
return yield RocketChat.getRooms();
|
|
|
|
};
|
2017-08-17 02:06:22 +00:00
|
|
|
|
|
|
|
const watchRoomsRequest = function* watchRoomsRequest() {
|
2017-08-18 21:30:16 +00:00
|
|
|
try {
|
|
|
|
yield call(getRooms);
|
|
|
|
yield put(roomsSuccess());
|
|
|
|
} catch (err) {
|
|
|
|
yield put(roomsFailure(err.status));
|
2017-08-17 02:06:22 +00:00
|
|
|
}
|
|
|
|
};
|
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-21 16:55:32 +00:00
|
|
|
timeout: yield call(delay, 5000)
|
|
|
|
});
|
|
|
|
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) {
|
|
|
|
fork(cancelTyping, username);
|
|
|
|
}
|
2017-11-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-21 16:55:32 +00:00
|
|
|
const watchRoomOpen = function* watchRoomOpen({ room }) {
|
2017-11-20 22:18:00 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(types.LOGIN.SUCCESS);
|
|
|
|
}
|
2017-11-21 16:55:32 +00:00
|
|
|
|
2017-11-20 22:18:00 +00:00
|
|
|
const subscriptions = [];
|
2017-11-21 16:55:32 +00:00
|
|
|
yield put(messagesRequest({ rid: room.rid }));
|
2017-11-20 22:18:00 +00:00
|
|
|
|
|
|
|
const { open } = yield race({
|
|
|
|
messages: take(types.MESSAGES.SUCCESS),
|
2017-11-21 16:55:32 +00:00
|
|
|
open: take(types.ROOM.OPEN)
|
2017-11-20 22:18:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (open) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 16:55:32 +00:00
|
|
|
|
|
|
|
RocketChat.readMessages(room.rid);
|
|
|
|
subscriptions.push(RocketChat.subscribe('stream-room-messages', room.rid, false));
|
|
|
|
subscriptions.push(RocketChat.subscribe('stream-notify-room', `${ room.rid }/typing`, false));
|
|
|
|
const thread = yield fork(usersTyping, { rid: room.rid });
|
|
|
|
yield take(types.ROOM.OPEN);
|
2017-11-20 22:18:00 +00:00
|
|
|
cancel(thread);
|
|
|
|
subscriptions.forEach(sub => sub.stop());
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
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-20 22:18:00 +00:00
|
|
|
yield takeLatest(types.LOGIN.SUCCESS, watchRoomsRequest);
|
2017-11-21 16:55:32 +00:00
|
|
|
yield takeLatest(types.ROOM.OPEN, watchRoomOpen);
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|