2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
select, put, call, take, takeLatest
|
|
|
|
} from 'redux-saga/effects';
|
2020-02-21 15:59:13 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
import { CREATE_CHANNEL, LOGIN } from '../actions/actionsTypes';
|
|
|
|
import { createChannelSuccess, createChannelFailure } from '../actions/createChannel';
|
2020-04-01 12:28:54 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2017-09-01 19:42:50 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2020-04-01 12:28:54 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2020-02-21 15:59:13 +00:00
|
|
|
import database from '../lib/database';
|
2020-04-01 12:28:54 +00:00
|
|
|
import I18n from '../i18n';
|
2020-07-30 13:26:17 +00:00
|
|
|
import { logEvent, events } from '../utils/log';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { goRoom } from '../utils/goRoom';
|
2017-09-01 19:42:50 +00:00
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
const createChannel = function createChannel(data) {
|
|
|
|
return RocketChat.createChannel(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const createGroupChat = function createGroupChat() {
|
|
|
|
return RocketChat.createGroupChat();
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
|
|
|
|
2021-04-23 12:28:01 +00:00
|
|
|
const createTeam = function createTeam(data) {
|
|
|
|
return RocketChat.createTeam(data);
|
|
|
|
};
|
|
|
|
|
2018-04-10 13:03:54 +00:00
|
|
|
const handleRequest = function* handleRequest({ data }) {
|
2017-09-21 17:08:00 +00:00
|
|
|
try {
|
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(LOGIN.SUCCESS);
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2020-04-01 12:28:54 +00:00
|
|
|
|
|
|
|
let sub;
|
2021-04-23 12:28:01 +00:00
|
|
|
if (data.isTeam) {
|
|
|
|
const {
|
|
|
|
type,
|
|
|
|
readOnly,
|
|
|
|
broadcast,
|
|
|
|
encrypted
|
|
|
|
} = data;
|
|
|
|
logEvent(events.CR_CREATE, {
|
|
|
|
type,
|
|
|
|
readOnly,
|
|
|
|
broadcast,
|
|
|
|
encrypted
|
|
|
|
});
|
|
|
|
sub = yield call(createTeam, data);
|
|
|
|
} else if (data.group) {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.SELECTED_USERS_CREATE_GROUP);
|
2020-04-03 18:02:10 +00:00
|
|
|
const result = yield call(createGroupChat);
|
|
|
|
if (result.success) {
|
|
|
|
({ room: sub } = result);
|
|
|
|
}
|
2020-04-01 12:28:54 +00:00
|
|
|
} else {
|
2020-09-11 14:31:38 +00:00
|
|
|
const {
|
|
|
|
type,
|
|
|
|
readOnly,
|
|
|
|
broadcast,
|
|
|
|
encrypted
|
|
|
|
} = data;
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CR_CREATE, {
|
2020-09-11 14:31:38 +00:00
|
|
|
type: type ? 'private' : 'public',
|
|
|
|
readOnly,
|
|
|
|
broadcast,
|
|
|
|
encrypted
|
|
|
|
});
|
2020-04-01 12:28:54 +00:00
|
|
|
sub = yield call(createChannel, data);
|
|
|
|
}
|
2020-02-21 15:59:13 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2020-02-21 15:59:13 +00:00
|
|
|
yield db.action(async() => {
|
|
|
|
await subCollection.create((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: sub.rid }, subCollection.schema);
|
|
|
|
Object.assign(s, sub);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
yield put(createChannelSuccess(sub));
|
2017-09-21 17:08:00 +00:00
|
|
|
} catch (err) {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events[data.group ? 'SELECTED_USERS_CREATE_GROUP_F' : 'CR_CREATE_F']);
|
2017-09-21 17:08:00 +00:00
|
|
|
yield put(createChannelFailure(err));
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
const handleSuccess = function* handleSuccess({ data }) {
|
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
}
|
2021-04-23 12:28:01 +00:00
|
|
|
if (data.team) {
|
|
|
|
goRoom({ item: data.team, isMasterDetail });
|
|
|
|
} else {
|
|
|
|
goRoom({ item: data, isMasterDetail });
|
|
|
|
}
|
2020-04-01 12:28:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleFailure = function handleFailure({ err }) {
|
|
|
|
setTimeout(() => {
|
|
|
|
const msg = err.reason || I18n.t('There_was_an_error_while_action', { action: I18n.t('creating_channel') });
|
|
|
|
showErrorAlert(msg);
|
|
|
|
}, 300);
|
|
|
|
};
|
|
|
|
|
2018-04-10 13:03:54 +00:00
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(CREATE_CHANNEL.REQUEST, handleRequest);
|
2020-04-01 12:28:54 +00:00
|
|
|
yield takeLatest(CREATE_CHANNEL.SUCCESS, handleSuccess);
|
|
|
|
yield takeLatest(CREATE_CHANNEL.FAILURE, handleFailure);
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-04-10 13:03:54 +00:00
|
|
|
export default root;
|