2018-05-24 20:17:45 +00:00
|
|
|
import { delay } from 'redux-saga';
|
2018-07-20 19:54:46 +00:00
|
|
|
import { takeLatest, put, call } 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 {
|
|
|
|
messagesSuccess,
|
|
|
|
messagesFailure,
|
|
|
|
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);
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const get = function* get({ room }) {
|
2017-08-17 06:28:41 +00:00
|
|
|
try {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (room.lastOpen) {
|
|
|
|
yield RocketChat.loadMissedMessages(room);
|
|
|
|
} else {
|
|
|
|
yield RocketChat.loadMessagesForRoom(room);
|
|
|
|
}
|
2017-08-17 06:28:41 +00:00
|
|
|
yield put(messagesSuccess());
|
|
|
|
} catch (err) {
|
2018-12-05 20:52:08 +00:00
|
|
|
yield put(messagesFailure(err));
|
2017-08-17 06:28:41 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-21 14:55:50 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const goRoom = function* goRoom({ rid, name }) {
|
2018-10-23 21:39:48 +00:00
|
|
|
yield Navigation.popToRoot('RoomsListView');
|
|
|
|
Navigation.push('RoomsListView', {
|
|
|
|
component: {
|
|
|
|
name: 'RoomView',
|
|
|
|
passProps: {
|
2018-12-21 10:55:35 +00:00
|
|
|
rid, name, t: 'd'
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
log('handleReplyBroadcast', e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
const root = function* root() {
|
2017-08-17 06:28:41 +00:00
|
|
|
yield takeLatest(MESSAGES.REQUEST, get);
|
2017-11-21 14:55:50 +00:00
|
|
|
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;
|