2019-08-07 13:51:34 +00:00
|
|
|
import {
|
|
|
|
takeLatest, put, call, delay
|
|
|
|
} from 'redux-saga/effects';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { MESSAGES } from '../actions/actionsTypes';
|
2017-11-21 14:55:50 +00:00
|
|
|
import {
|
|
|
|
deleteSuccess,
|
|
|
|
deleteFailure,
|
|
|
|
editSuccess,
|
|
|
|
editFailure,
|
2017-11-24 20:44:52 +00:00
|
|
|
toggleStarSuccess,
|
|
|
|
toggleStarFailure,
|
2017-11-21 14:55:50 +00:00
|
|
|
togglePinSuccess,
|
2018-05-24 20:17:45 +00:00
|
|
|
togglePinFailure,
|
2018-07-20 19:54:46 +00:00
|
|
|
replyInit
|
2017-11-21 14:55:50 +00:00
|
|
|
} from '../actions/messages';
|
2017-08-17 06:28:41 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-05-24 20:17:45 +00:00
|
|
|
import database from '../lib/realm';
|
|
|
|
import log from '../utils/log';
|
2017-08-17 06:28:41 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
const deleteMessage = message => RocketChat.deleteMessage(message);
|
|
|
|
const editMessage = message => RocketChat.editMessage(message);
|
2017-11-24 20:44:52 +00:00
|
|
|
const toggleStarMessage = message => RocketChat.toggleStarMessage(message);
|
2017-11-21 14:55:50 +00:00
|
|
|
const togglePinMessage = message => RocketChat.togglePinMessage(message);
|
|
|
|
|
|
|
|
const handleDeleteRequest = function* handleDeleteRequest({ message }) {
|
|
|
|
try {
|
|
|
|
yield call(deleteMessage, message);
|
|
|
|
yield put(deleteSuccess());
|
|
|
|
} catch (error) {
|
|
|
|
yield put(deleteFailure());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEditRequest = function* handleEditRequest({ message }) {
|
|
|
|
try {
|
|
|
|
yield call(editMessage, message);
|
|
|
|
yield put(editSuccess());
|
|
|
|
} catch (error) {
|
|
|
|
yield put(editFailure());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
const handleToggleStarRequest = function* handleToggleStarRequest({ message }) {
|
2017-11-21 14:55:50 +00:00
|
|
|
try {
|
2017-11-24 20:44:52 +00:00
|
|
|
yield call(toggleStarMessage, message);
|
|
|
|
yield put(toggleStarSuccess());
|
2017-11-21 14:55:50 +00:00
|
|
|
} catch (error) {
|
2017-11-24 20:44:52 +00:00
|
|
|
yield put(toggleStarFailure());
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleTogglePinRequest = function* handleTogglePinRequest({ message }) {
|
|
|
|
try {
|
|
|
|
yield call(togglePinMessage, message);
|
|
|
|
yield put(togglePinSuccess());
|
|
|
|
} catch (error) {
|
|
|
|
yield put(togglePinFailure(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
const goRoom = function goRoom({ rid, name }) {
|
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
Navigation.navigate('RoomView', { rid, name, t: 'd' });
|
2018-07-10 13:40:32 +00:00
|
|
|
};
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
const handleReplyBroadcast = function* handleReplyBroadcast({ message }) {
|
|
|
|
try {
|
|
|
|
const { username } = message.u;
|
|
|
|
const subscriptions = database.objects('subscriptions').filtered('name = $0', username);
|
|
|
|
if (subscriptions.length) {
|
2018-12-21 10:55:35 +00:00
|
|
|
yield goRoom({ rid: subscriptions[0].rid, name: username });
|
2018-05-24 20:17:45 +00:00
|
|
|
} else {
|
|
|
|
const room = yield RocketChat.createDirectMessage(username);
|
2018-12-21 10:55:35 +00:00
|
|
|
yield goRoom({ rid: room.rid, name: username });
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
yield delay(500);
|
2018-07-20 19:54:46 +00:00
|
|
|
yield put(replyInit(message, false));
|
2018-05-24 20:17:45 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(MESSAGES.DELETE_REQUEST, handleDeleteRequest);
|
|
|
|
yield takeLatest(MESSAGES.EDIT_REQUEST, handleEditRequest);
|
2017-11-24 20:44:52 +00:00
|
|
|
yield takeLatest(MESSAGES.TOGGLE_STAR_REQUEST, handleToggleStarRequest);
|
2017-11-21 14:55:50 +00:00
|
|
|
yield takeLatest(MESSAGES.TOGGLE_PIN_REQUEST, handleTogglePinRequest);
|
2018-05-24 20:17:45 +00:00
|
|
|
yield takeLatest(MESSAGES.REPLY_BROADCAST, handleReplyBroadcast);
|
2017-08-17 06:28:41 +00:00
|
|
|
};
|
2017-11-21 14:55:50 +00:00
|
|
|
export default root;
|