2020-03-30 19:50:27 +00:00
|
|
|
import {
|
|
|
|
select, put, call, take, takeLatest
|
|
|
|
} from 'redux-saga/effects';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
|
|
|
|
import { CREATE_DISCUSSION, LOGIN } from '../actions/actionsTypes';
|
|
|
|
import { createDiscussionSuccess, createDiscussionFailure } from '../actions/createDiscussion';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import database from '../lib/database';
|
2020-07-30 13:26:17 +00:00
|
|
|
import { logEvent, events } from '../utils/log';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
const create = function* create(data) {
|
|
|
|
return yield RocketChat.createDiscussion(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRequest = function* handleRequest({ data }) {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_CREATE);
|
2020-03-30 19:50:27 +00:00
|
|
|
try {
|
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(LOGIN.SUCCESS);
|
|
|
|
}
|
|
|
|
const result = yield call(create, data);
|
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
const { discussion: sub } = result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2020-03-30 19:50:27 +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(createDiscussionSuccess(sub));
|
|
|
|
} else {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_CREATE_F);
|
2020-03-30 19:50:27 +00:00
|
|
|
yield put(createDiscussionFailure(result));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_CREATE_F);
|
2020-03-30 19:50:27 +00:00
|
|
|
yield put(createDiscussionFailure(err));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(CREATE_DISCUSSION.REQUEST, handleRequest);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default root;
|