vn-verdnaturachat/app/sagas/rooms.js

169 lines
4.3 KiB
JavaScript
Raw Normal View History

import { Alert } from 'react-native';
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';
import { Navigation } from 'react-native-navigation';
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';
import { messagesRequest, editCancel, replyCancel } from '../actions/messages';
2017-08-17 02:06:22 +00:00
import RocketChat from '../lib/rocketchat';
import database from '../lib/realm';
import log from '../utils/log';
import I18n from '../i18n';
const eraseRoom = rid => RocketChat.eraseRoom(rid);
2017-08-17 02:06:22 +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),
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) {
yield fork(cancelTyping, username);
2017-11-21 16:55:32 +00:00
}
2017-11-20 22:18:00 +00:00
}
}
};
const handleMessageReceived = function* handleMessageReceived({ message }) {
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
try {
const room = yield select(state => state.room);
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
if (message.rid === room.rid) {
database.write(() => {
database.create('messages', message, true);
});
if (room._id) {
RocketChat.readMessages(room.rid);
}
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
}
} catch (e) {
console.warn('handleMessageReceived', e);
}
};
2017-11-20 22:18:00 +00:00
let opened = false;
2017-11-21 16:55:32 +00:00
const watchRoomOpen = function* watchRoomOpen({ room }) {
try {
if (opened) {
return;
}
opened = true;
const auth = yield select(state => state.login.isAuthenticated);
if (!auth) {
yield take(types.LOGIN.SUCCESS);
}
yield put(messagesRequest({ ...room }));
if (room._id) {
RocketChat.readMessages(room.rid);
}
2018-12-05 20:52:08 +00:00
sub = yield RocketChat.subscribeRoom(room);
2018-12-05 20:52:08 +00:00
thread = yield fork(usersTyping, { rid: room.rid });
yield race({
open: take(types.ROOM.OPEN),
close: take(types.ROOM.CLOSE)
});
opened = false;
cancel(thread);
sub.stop();
yield put(editCancel());
yield put(replyCancel());
} 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;
}
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-12-05 20:52:08 +00:00
const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) {
try {
sub.stop();
2018-12-05 20:52:08 +00:00
yield RocketChat.leaveRoom(rid, t);
yield goRoomsListAndDelete(rid);
} 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));
} 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') }));
}
}
};
const handleEraseRoom = function* handleEraseRoom({ rid }) {
try {
sub.stop();
2018-12-05 20:52:08 +00:00
yield eraseRoom(rid);
yield goRoomsListAndDelete(rid, 'erase');
} 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') }));
}
};
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);
yield takeEvery(types.ROOM.MESSAGE_RECEIVED, handleMessageReceived);
yield takeLatest(types.ROOM.LEAVE, handleLeaveRoom);
yield takeLatest(types.ROOM.ERASE, handleEraseRoom);
};
export default root;