[FIX] Cancel ongoing tasks on server change (#984)
* Fork selectServer and loginSuccess * Fork tasks on login * Refactor * Remove room sub and requests on logout * Comment room header disabled rule * Check server on rooms stream * Uncomment disabled props * Fix updating state * Comment last fetch user presence date
This commit is contained in:
parent
892cf58ffb
commit
319630949e
|
@ -58,6 +58,7 @@ export const SERVER = createRequestTypes('SERVER', [
|
||||||
...defaultTypes,
|
...defaultTypes,
|
||||||
'SELECT_SUCCESS',
|
'SELECT_SUCCESS',
|
||||||
'SELECT_REQUEST',
|
'SELECT_REQUEST',
|
||||||
|
'SELECT_FAILURE',
|
||||||
'INIT_ADD',
|
'INIT_ADD',
|
||||||
'FINISH_ADD'
|
'FINISH_ADD'
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -17,6 +17,12 @@ export function selectServerSuccess(server, version) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function selectServerFailure() {
|
||||||
|
return {
|
||||||
|
type: SERVER.SELECT_FAILURE
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function serverRequest(server) {
|
export function serverRequest(server) {
|
||||||
return {
|
return {
|
||||||
type: SERVER.REQUEST,
|
type: SERVER.REQUEST,
|
||||||
|
|
|
@ -23,57 +23,62 @@ const create = (customEmojis) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export default async function() {
|
export default function() {
|
||||||
try {
|
return new Promise(async(resolve) => {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
try {
|
||||||
const updatedSince = getUpdatedSince();
|
const serverVersion = reduxStore.getState().server.version;
|
||||||
|
const updatedSince = getUpdatedSince();
|
||||||
|
|
||||||
// if server version is lower than 0.75.0, fetches from old api
|
// if server version is lower than 0.75.0, fetches from old api
|
||||||
if (semver.lt(serverVersion, '0.75.0')) {
|
if (semver.lt(serverVersion, '0.75.0')) {
|
||||||
// RC 0.61.0
|
// RC 0.61.0
|
||||||
const result = await this.sdk.get('emoji-custom');
|
const result = await this.sdk.get('emoji-custom');
|
||||||
|
|
||||||
InteractionManager.runAfterInteractions(() => {
|
InteractionManager.runAfterInteractions(() => {
|
||||||
let { emojis } = result;
|
let { emojis } = result;
|
||||||
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
|
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
|
||||||
database.write(() => {
|
database.write(() => {
|
||||||
create(emojis);
|
create(emojis);
|
||||||
|
});
|
||||||
|
return resolve();
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
const params = {};
|
||||||
const params = {};
|
if (updatedSince) {
|
||||||
if (updatedSince) {
|
params.updatedSince = updatedSince;
|
||||||
params.updatedSince = updatedSince;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// RC 0.75.0
|
// RC 0.75.0
|
||||||
const result = await this.sdk.get('emoji-custom.list', params);
|
const result = await this.sdk.get('emoji-custom.list', params);
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return;
|
return resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
InteractionManager.runAfterInteractions(
|
InteractionManager.runAfterInteractions(
|
||||||
() => database.write(() => {
|
() => database.write(() => {
|
||||||
const { emojis } = result;
|
const { emojis } = result;
|
||||||
create(emojis.update);
|
create(emojis.update);
|
||||||
|
|
||||||
if (emojis.delete && emojis.delete.length) {
|
if (emojis.delete && emojis.delete.length) {
|
||||||
emojis.delete.forEach((emoji) => {
|
emojis.delete.forEach((emoji) => {
|
||||||
try {
|
try {
|
||||||
const emojiRecord = database.objectForPrimaryKey('customEmojis', emoji._id);
|
const emojiRecord = database.objectForPrimaryKey('customEmojis', emoji._id);
|
||||||
if (emojiRecord) {
|
if (emojiRecord) {
|
||||||
database.delete(emojiRecord);
|
database.delete(emojiRecord);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('err_get_emojis_delete', e);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
});
|
||||||
log('err_get_emojis_delete', e);
|
}
|
||||||
}
|
return resolve();
|
||||||
});
|
})
|
||||||
}
|
);
|
||||||
})
|
}
|
||||||
);
|
} catch (e) {
|
||||||
|
log('err_get_custom_emojis', e);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
});
|
||||||
log('err_get_custom_emojis', e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,55 +22,60 @@ const create = (permissions) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function() {
|
export default function() {
|
||||||
try {
|
return new Promise(async(resolve) => {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
try {
|
||||||
|
const serverVersion = reduxStore.getState().server.version;
|
||||||
|
|
||||||
// if server version is lower than 0.73.0, fetches from old api
|
// if server version is lower than 0.73.0, fetches from old api
|
||||||
if (semver.lt(serverVersion, '0.73.0')) {
|
if (semver.lt(serverVersion, '0.73.0')) {
|
||||||
// RC 0.66.0
|
// RC 0.66.0
|
||||||
const result = await this.sdk.get('permissions.list');
|
const result = await this.sdk.get('permissions.list');
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return;
|
return resolve();
|
||||||
}
|
}
|
||||||
InteractionManager.runAfterInteractions(() => {
|
InteractionManager.runAfterInteractions(() => {
|
||||||
database.write(() => {
|
database.write(() => {
|
||||||
create(result.permissions);
|
create(result.permissions);
|
||||||
|
});
|
||||||
|
return resolve();
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
const params = {};
|
||||||
const params = {};
|
const updatedSince = getUpdatedSince();
|
||||||
const updatedSince = getUpdatedSince();
|
if (updatedSince) {
|
||||||
if (updatedSince) {
|
params.updatedSince = updatedSince;
|
||||||
params.updatedSince = updatedSince;
|
}
|
||||||
}
|
// RC 0.73.0
|
||||||
// RC 0.73.0
|
const result = await this.sdk.get('permissions.listAll', params);
|
||||||
const result = await this.sdk.get('permissions.listAll', params);
|
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return;
|
return resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
InteractionManager.runAfterInteractions(
|
InteractionManager.runAfterInteractions(
|
||||||
() => database.write(() => {
|
() => database.write(() => {
|
||||||
create(result.update);
|
create(result.update);
|
||||||
|
|
||||||
if (result.delete && result.delete.length) {
|
if (result.delete && result.delete.length) {
|
||||||
result.delete.forEach((p) => {
|
result.delete.forEach((p) => {
|
||||||
try {
|
try {
|
||||||
const permission = database.objectForPrimaryKey('permissions', p._id);
|
const permission = database.objectForPrimaryKey('permissions', p._id);
|
||||||
if (permission) {
|
if (permission) {
|
||||||
database.delete(permission);
|
database.delete(permission);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('err_get_permissions_delete', e);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
});
|
||||||
log('err_get_permissions_delete', e);
|
}
|
||||||
}
|
return resolve();
|
||||||
});
|
})
|
||||||
}
|
);
|
||||||
})
|
}
|
||||||
);
|
} catch (e) {
|
||||||
|
log('err_get_permissions', e);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
});
|
||||||
log('err_get_permissions', e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,29 +3,33 @@ import { InteractionManager } from 'react-native';
|
||||||
import database from '../realm';
|
import database from '../realm';
|
||||||
import log from '../../utils/log';
|
import log from '../../utils/log';
|
||||||
|
|
||||||
export default async function() {
|
export default function() {
|
||||||
try {
|
return new Promise(async(resolve) => {
|
||||||
// RC 0.70.0
|
try {
|
||||||
const result = await this.sdk.get('roles.list');
|
// RC 0.70.0
|
||||||
|
const result = await this.sdk.get('roles.list');
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return;
|
return resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
const { roles } = result;
|
||||||
|
|
||||||
|
if (roles && roles.length) {
|
||||||
|
InteractionManager.runAfterInteractions(() => {
|
||||||
|
database.write(() => roles.forEach((role) => {
|
||||||
|
try {
|
||||||
|
database.create('roles', role, true);
|
||||||
|
} catch (e) {
|
||||||
|
log('err_get_roles_create', e);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('err_get_roles', e);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
const { roles } = result;
|
|
||||||
|
|
||||||
if (roles && roles.length) {
|
|
||||||
InteractionManager.runAfterInteractions(() => {
|
|
||||||
database.write(() => roles.forEach((role) => {
|
|
||||||
try {
|
|
||||||
database.create('roles', role, true);
|
|
||||||
} catch (e) {
|
|
||||||
log('err_get_roles_create', e);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
log('err_get_roles', e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,29 +3,34 @@ import { InteractionManager } from 'react-native';
|
||||||
import database from '../realm';
|
import database from '../realm';
|
||||||
import log from '../../utils/log';
|
import log from '../../utils/log';
|
||||||
|
|
||||||
export default async function() {
|
export default function() {
|
||||||
try {
|
return new Promise(async(resolve) => {
|
||||||
// RC 0.60.2
|
try {
|
||||||
const result = await this.sdk.get('commands.list');
|
// RC 0.60.2
|
||||||
|
const result = await this.sdk.get('commands.list');
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return log('getSlashCommand fetch', result);
|
log('getSlashCommand fetch', result);
|
||||||
|
return resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
const { commands } = result;
|
||||||
|
|
||||||
|
if (commands && commands.length) {
|
||||||
|
InteractionManager.runAfterInteractions(() => {
|
||||||
|
database.write(() => commands.forEach((command) => {
|
||||||
|
try {
|
||||||
|
database.create('slashCommand', command, true);
|
||||||
|
} catch (e) {
|
||||||
|
log('get_slash_command', e);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('err_get_slash_command', e);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
const { commands } = result;
|
|
||||||
|
|
||||||
if (commands && commands.length) {
|
|
||||||
InteractionManager.runAfterInteractions(() => {
|
|
||||||
database.write(() => commands.forEach((command) => {
|
|
||||||
try {
|
|
||||||
database.create('slashCommand', command, true);
|
|
||||||
} catch (e) {
|
|
||||||
log('get_slash_command', e);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
log('err_get_slash_command', e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ const removeListener = listener => listener.stop();
|
||||||
let connectedListener;
|
let connectedListener;
|
||||||
let disconnectedListener;
|
let disconnectedListener;
|
||||||
let streamListener;
|
let streamListener;
|
||||||
|
let subServer;
|
||||||
|
|
||||||
export default async function subscribeRooms() {
|
export default async function subscribeRooms() {
|
||||||
let timer = null;
|
let timer = null;
|
||||||
|
@ -41,6 +42,10 @@ export default async function subscribeRooms() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleStreamMessageReceived = protectedFunction((ddpMessage) => {
|
const handleStreamMessageReceived = protectedFunction((ddpMessage) => {
|
||||||
|
// check if the server from variable is the same as the js sdk client
|
||||||
|
if (this.sdk && this.sdk.client && this.sdk.client.host !== subServer) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (ddpMessage.msg === 'added') {
|
if (ddpMessage.msg === 'added') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -149,6 +154,8 @@ export default async function subscribeRooms() {
|
||||||
streamListener = this.sdk.onStreamData('stream-notify-user', handleStreamMessageReceived);
|
streamListener = this.sdk.onStreamData('stream-notify-user', handleStreamMessageReceived);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// set the server that started this task
|
||||||
|
subServer = this.sdk.client.host;
|
||||||
await this.sdk.subscribeNotifyUser();
|
await this.sdk.subscribeNotifyUser();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('err_subscribe_rooms', e);
|
log('err_subscribe_rooms', e);
|
||||||
|
|
|
@ -8,7 +8,6 @@ import messagesStatus from '../constants/messagesStatus';
|
||||||
import database from './realm';
|
import database from './realm';
|
||||||
import log from '../utils/log';
|
import log from '../utils/log';
|
||||||
import { isIOS, getBundleId } from '../utils/deviceInfo';
|
import { isIOS, getBundleId } from '../utils/deviceInfo';
|
||||||
import EventEmitter from '../utils/events';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
setUser, setLoginServices, loginRequest, loginFailure, logout
|
setUser, setLoginServices, loginRequest, loginFailure, logout
|
||||||
|
@ -24,7 +23,7 @@ import getSettings from './methods/getSettings';
|
||||||
|
|
||||||
import getRooms from './methods/getRooms';
|
import getRooms from './methods/getRooms';
|
||||||
import getPermissions from './methods/getPermissions';
|
import getPermissions from './methods/getPermissions';
|
||||||
import getCustomEmoji from './methods/getCustomEmojis';
|
import getCustomEmojis from './methods/getCustomEmojis';
|
||||||
import getSlashCommands from './methods/getSlashCommands';
|
import getSlashCommands from './methods/getSlashCommands';
|
||||||
import getRoles from './methods/getRoles';
|
import getRoles from './methods/getRoles';
|
||||||
import canOpenRoom from './methods/canOpenRoom';
|
import canOpenRoom from './methods/canOpenRoom';
|
||||||
|
@ -37,7 +36,6 @@ import sendMessage, { getMessage, sendMessageCall } from './methods/sendMessage'
|
||||||
import { sendFileMessage, cancelUpload, isUploadActive } from './methods/sendFileMessage';
|
import { sendFileMessage, cancelUpload, isUploadActive } from './methods/sendFileMessage';
|
||||||
|
|
||||||
import { getDeviceToken } from '../notifications/push';
|
import { getDeviceToken } from '../notifications/push';
|
||||||
import { roomsRequest } from '../actions/rooms';
|
|
||||||
|
|
||||||
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
||||||
const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY';
|
const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY';
|
||||||
|
@ -141,23 +139,6 @@ const RocketChat = {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async loginSuccess({ user }) {
|
|
||||||
EventEmitter.emit('connected');
|
|
||||||
reduxStore.dispatch(setUser(user));
|
|
||||||
reduxStore.dispatch(roomsRequest());
|
|
||||||
|
|
||||||
if (this.roomsSub) {
|
|
||||||
this.roomsSub.stop();
|
|
||||||
}
|
|
||||||
this.roomsSub = await this.subscribeRooms();
|
|
||||||
|
|
||||||
this.getPermissions();
|
|
||||||
this.getCustomEmoji();
|
|
||||||
this.getRoles();
|
|
||||||
this.getSlashCommands();
|
|
||||||
this.registerPushToken().catch(e => console.log(e));
|
|
||||||
this.getUserPresence();
|
|
||||||
},
|
|
||||||
connect({ server, user }) {
|
connect({ server, user }) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
database.setActiveDB(server);
|
database.setActiveDB(server);
|
||||||
|
@ -167,6 +148,10 @@ const RocketChat = {
|
||||||
clearTimeout(this.connectTimeout);
|
clearTimeout(this.connectTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.roomsSub) {
|
||||||
|
this.roomsSub.stop();
|
||||||
|
}
|
||||||
|
|
||||||
if (this.sdk) {
|
if (this.sdk) {
|
||||||
this.sdk.disconnect();
|
this.sdk.disconnect();
|
||||||
this.sdk = null;
|
this.sdk = null;
|
||||||
|
@ -195,10 +180,10 @@ const RocketChat = {
|
||||||
|
|
||||||
this.sdk.onStreamData('connected', () => {
|
this.sdk.onStreamData('connected', () => {
|
||||||
reduxStore.dispatch(connectSuccess());
|
reduxStore.dispatch(connectSuccess());
|
||||||
const { isAuthenticated } = reduxStore.getState().login;
|
// const { isAuthenticated } = reduxStore.getState().login;
|
||||||
if (isAuthenticated) {
|
// if (isAuthenticated) {
|
||||||
this.getUserPresence();
|
// this.getUserPresence();
|
||||||
}
|
// }
|
||||||
});
|
});
|
||||||
|
|
||||||
this.sdk.onStreamData('close', () => {
|
this.sdk.onStreamData('close', () => {
|
||||||
|
@ -349,7 +334,7 @@ const RocketChat = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
registerPushToken() {
|
registerPushToken() {
|
||||||
return new Promise((resolve) => {
|
return new Promise(async(resolve) => {
|
||||||
const token = getDeviceToken();
|
const token = getDeviceToken();
|
||||||
if (token) {
|
if (token) {
|
||||||
const type = isIOS ? 'apn' : 'gcm';
|
const type = isIOS ? 'apn' : 'gcm';
|
||||||
|
@ -358,8 +343,12 @@ const RocketChat = {
|
||||||
type,
|
type,
|
||||||
appName: getBundleId
|
appName: getBundleId
|
||||||
};
|
};
|
||||||
// RC 0.60.0
|
try {
|
||||||
return this.sdk.post('push.token', data);
|
// RC 0.60.0
|
||||||
|
await this.sdk.post('push.token', data);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
|
@ -468,7 +457,7 @@ const RocketChat = {
|
||||||
isUploadActive,
|
isUploadActive,
|
||||||
getSettings,
|
getSettings,
|
||||||
getPermissions,
|
getPermissions,
|
||||||
getCustomEmoji,
|
getCustomEmojis,
|
||||||
getSlashCommands,
|
getSlashCommands,
|
||||||
getRoles,
|
getRoles,
|
||||||
parseSettings: settings => settings.reduce((ret, item) => {
|
parseSettings: settings => settings.reduce((ret, item) => {
|
||||||
|
@ -824,41 +813,45 @@ const RocketChat = {
|
||||||
command, params, roomId, previewItem
|
command, params, roomId, previewItem
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
async getUserPresence() {
|
getUserPresence() {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
return new Promise(async(resolve) => {
|
||||||
|
const serverVersion = reduxStore.getState().server.version;
|
||||||
|
|
||||||
// if server is lower than 1.1.0
|
// if server is lower than 1.1.0
|
||||||
if (semver.lt(semver.coerce(serverVersion), '1.1.0')) {
|
if (semver.lt(semver.coerce(serverVersion), '1.1.0')) {
|
||||||
if (this.activeUsersSubTimeout) {
|
if (this.activeUsersSubTimeout) {
|
||||||
clearTimeout(this.activeUsersSubTimeout);
|
clearTimeout(this.activeUsersSubTimeout);
|
||||||
this.activeUsersSubTimeout = false;
|
this.activeUsersSubTimeout = false;
|
||||||
}
|
}
|
||||||
this.activeUsersSubTimeout = setTimeout(() => {
|
this.activeUsersSubTimeout = setTimeout(() => {
|
||||||
this.sdk.subscribe('activeUsers');
|
this.sdk.subscribe('activeUsers');
|
||||||
}, 5000);
|
}, 5000);
|
||||||
} else {
|
return resolve();
|
||||||
const params = {};
|
} else {
|
||||||
if (this.lastUserPresenceFetch) {
|
const params = {};
|
||||||
params.from = this.lastUserPresenceFetch.toISOString();
|
// if (this.lastUserPresenceFetch) {
|
||||||
}
|
// params.from = this.lastUserPresenceFetch.toISOString();
|
||||||
|
// }
|
||||||
|
|
||||||
// RC 1.1.0
|
// RC 1.1.0
|
||||||
const result = await this.sdk.get('users.presence', params);
|
const result = await this.sdk.get('users.presence', params);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
this.lastUserPresenceFetch = new Date();
|
// this.lastUserPresenceFetch = new Date();
|
||||||
database.memoryDatabase.write(() => {
|
database.memoryDatabase.write(() => {
|
||||||
result.users.forEach((item) => {
|
result.users.forEach((item) => {
|
||||||
try {
|
try {
|
||||||
item.id = item._id;
|
item.id = item._id;
|
||||||
database.memoryDatabase.create('activeUsers', item, true);
|
database.memoryDatabase.create('activeUsers', item, true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
this.sdk.subscribe('stream-notify-logged', 'user-status');
|
||||||
this.sdk.subscribe('stream-notify-logged', 'user-status');
|
return resolve();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
},
|
},
|
||||||
getDirectory({
|
getDirectory({
|
||||||
query, count, offset, sort
|
query, count, offset, sort
|
||||||
|
|
|
@ -44,6 +44,13 @@ export default function server(state = initialState, action) {
|
||||||
connected: true,
|
connected: true,
|
||||||
loading: false
|
loading: false
|
||||||
};
|
};
|
||||||
|
case SERVER.SELECT_FAILURE:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
connecting: false,
|
||||||
|
connected: false,
|
||||||
|
loading: false
|
||||||
|
};
|
||||||
case SERVER.INIT_ADD:
|
case SERVER.INIT_ADD:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
import { AsyncStorage } from 'react-native';
|
import { AsyncStorage } from 'react-native';
|
||||||
import {
|
import {
|
||||||
put, call, takeLatest, select
|
put, call, takeLatest, select, take, fork, cancel
|
||||||
} from 'redux-saga/effects';
|
} from 'redux-saga/effects';
|
||||||
|
|
||||||
import * as types from '../actions/actionsTypes';
|
import * as types from '../actions/actionsTypes';
|
||||||
import { appStart } from '../actions';
|
import { appStart } from '../actions';
|
||||||
import { serverFinishAdd, selectServerRequest } from '../actions/server';
|
import { serverFinishAdd, selectServerRequest } from '../actions/server';
|
||||||
import { loginFailure, loginSuccess } from '../actions/login';
|
import { loginFailure, loginSuccess, setUser } from '../actions/login';
|
||||||
|
import { roomsRequest } from '../actions/rooms';
|
||||||
import RocketChat from '../lib/rocketchat';
|
import RocketChat from '../lib/rocketchat';
|
||||||
import log from '../utils/log';
|
import log from '../utils/log';
|
||||||
import I18n from '../i18n';
|
import I18n from '../i18n';
|
||||||
import database from '../lib/realm';
|
import database from '../lib/realm';
|
||||||
|
import EventEmitter from '../utils/events';
|
||||||
|
|
||||||
const getServer = state => state.server.server;
|
const getServer = state => state.server.server;
|
||||||
const loginWithPasswordCall = args => RocketChat.loginWithPassword(args);
|
const loginWithPasswordCall = args => RocketChat.loginWithPassword(args);
|
||||||
|
@ -31,27 +33,59 @@ const handleLoginRequest = function* handleLoginRequest({ credentials }) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLoginSuccess = function* handleLoginSuccess({ user }) {
|
const fetchPermissions = function* fetchPermissions() {
|
||||||
const adding = yield select(state => state.server.adding);
|
yield RocketChat.getPermissions();
|
||||||
yield AsyncStorage.setItem(RocketChat.TOKEN_KEY, user.token);
|
};
|
||||||
|
|
||||||
const server = yield select(getServer);
|
const fetchCustomEmojis = function* fetchCustomEmojis() {
|
||||||
|
yield RocketChat.getCustomEmojis();
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchRoles = function* fetchRoles() {
|
||||||
|
yield RocketChat.getRoles();
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchSlashCommands = function* fetchSlashCommands() {
|
||||||
|
yield RocketChat.getSlashCommands();
|
||||||
|
};
|
||||||
|
|
||||||
|
const registerPushToken = function* registerPushToken() {
|
||||||
|
yield RocketChat.registerPushToken();
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchUserPresence = function* fetchUserPresence() {
|
||||||
|
yield RocketChat.getUserPresence();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLoginSuccess = function* handleLoginSuccess({ user }) {
|
||||||
try {
|
try {
|
||||||
RocketChat.loginSuccess({ user });
|
const adding = yield select(state => state.server.adding);
|
||||||
|
yield AsyncStorage.setItem(RocketChat.TOKEN_KEY, user.token);
|
||||||
|
|
||||||
|
const server = yield select(getServer);
|
||||||
|
yield put(roomsRequest());
|
||||||
|
yield fork(fetchPermissions);
|
||||||
|
yield fork(fetchCustomEmojis);
|
||||||
|
yield fork(fetchRoles);
|
||||||
|
yield fork(fetchSlashCommands);
|
||||||
|
yield fork(registerPushToken);
|
||||||
|
yield fork(fetchUserPresence);
|
||||||
|
|
||||||
I18n.locale = user.language;
|
I18n.locale = user.language;
|
||||||
yield AsyncStorage.setItem(`${ RocketChat.TOKEN_KEY }-${ server }`, JSON.stringify(user));
|
yield AsyncStorage.setItem(`${ RocketChat.TOKEN_KEY }-${ server }`, JSON.stringify(user));
|
||||||
} catch (error) {
|
yield put(setUser(user));
|
||||||
console.log('loginSuccess saga -> error', error);
|
EventEmitter.emit('connected');
|
||||||
}
|
|
||||||
|
|
||||||
if (!user.username) {
|
if (!user.username) {
|
||||||
RocketChat.loginSuccess({ user });
|
yield put(appStart('setUsername'));
|
||||||
yield put(appStart('setUsername'));
|
} else if (adding) {
|
||||||
} else if (adding) {
|
yield put(serverFinishAdd());
|
||||||
yield put(serverFinishAdd());
|
yield put(appStart('inside'));
|
||||||
yield put(appStart('inside'));
|
} else {
|
||||||
} else {
|
yield put(appStart('inside'));
|
||||||
yield put(appStart('inside'));
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('err_handle_login_success', e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -93,8 +127,14 @@ const handleSetUser = function handleSetUser({ user }) {
|
||||||
|
|
||||||
const root = function* root() {
|
const root = function* root() {
|
||||||
yield takeLatest(types.LOGIN.REQUEST, handleLoginRequest);
|
yield takeLatest(types.LOGIN.REQUEST, handleLoginRequest);
|
||||||
yield takeLatest(types.LOGIN.SUCCESS, handleLoginSuccess);
|
|
||||||
yield takeLatest(types.LOGOUT, handleLogout);
|
yield takeLatest(types.LOGOUT, handleLogout);
|
||||||
yield takeLatest(types.USER.SET, handleSetUser);
|
yield takeLatest(types.USER.SET, handleSetUser);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const params = yield take(types.LOGIN.SUCCESS);
|
||||||
|
const loginSuccessTask = yield fork(handleLoginSuccess, params);
|
||||||
|
yield take(types.SERVER.SELECT_REQUEST);
|
||||||
|
yield cancel(loginSuccessTask);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
export default root;
|
export default root;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import {
|
||||||
put, takeLatest, select
|
put, select, race, take, fork, cancel, takeLatest
|
||||||
} from 'redux-saga/effects';
|
} from 'redux-saga/effects';
|
||||||
|
import { BACKGROUND } from 'redux-enhancer-react-native-appstate';
|
||||||
|
|
||||||
import * as types from '../actions/actionsTypes';
|
import * as types from '../actions/actionsTypes';
|
||||||
import { roomsSuccess, roomsFailure } from '../actions/rooms';
|
import { roomsSuccess, roomsFailure } from '../actions/rooms';
|
||||||
|
@ -9,8 +10,18 @@ import log from '../utils/log';
|
||||||
import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms';
|
import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms';
|
||||||
import RocketChat from '../lib/rocketchat';
|
import RocketChat from '../lib/rocketchat';
|
||||||
|
|
||||||
|
let roomsSub;
|
||||||
|
|
||||||
|
const removeSub = function removeSub() {
|
||||||
|
if (roomsSub && roomsSub.stop) {
|
||||||
|
roomsSub.stop();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleRoomsRequest = function* handleRoomsRequest() {
|
const handleRoomsRequest = function* handleRoomsRequest() {
|
||||||
try {
|
try {
|
||||||
|
removeSub();
|
||||||
|
roomsSub = yield RocketChat.subscribeRooms();
|
||||||
const newRoomsUpdatedAt = new Date();
|
const newRoomsUpdatedAt = new Date();
|
||||||
const server = yield select(state => state.server.server);
|
const server = yield select(state => state.server.server);
|
||||||
const [serverRecord] = database.databases.serversDB.objects('servers').filtered('id = $0', server);
|
const [serverRecord] = database.databases.serversDB.objects('servers').filtered('id = $0', server);
|
||||||
|
@ -42,7 +53,21 @@ const handleRoomsRequest = function* handleRoomsRequest() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleLogout = function handleLogout() {
|
||||||
|
removeSub();
|
||||||
|
};
|
||||||
|
|
||||||
const root = function* root() {
|
const root = function* root() {
|
||||||
yield takeLatest(types.ROOMS.REQUEST, handleRoomsRequest);
|
yield takeLatest(types.LOGOUT, handleLogout);
|
||||||
|
while (true) {
|
||||||
|
const params = yield take(types.ROOMS.REQUEST);
|
||||||
|
const roomsRequestTask = yield fork(handleRoomsRequest, params);
|
||||||
|
yield race({
|
||||||
|
serverReq: take(types.SERVER.SELECT_REQUEST),
|
||||||
|
background: take(BACKGROUND),
|
||||||
|
logout: take(types.LOGOUT)
|
||||||
|
});
|
||||||
|
yield cancel(roomsRequestTask);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
export default root;
|
export default root;
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
import { put, takeLatest } from 'redux-saga/effects';
|
import {
|
||||||
|
put, take, takeLatest, fork, cancel, race
|
||||||
|
} from 'redux-saga/effects';
|
||||||
import { AsyncStorage, Alert } from 'react-native';
|
import { AsyncStorage, Alert } from 'react-native';
|
||||||
|
|
||||||
import Navigation from '../lib/Navigation';
|
import Navigation from '../lib/Navigation';
|
||||||
import { SERVER } from '../actions/actionsTypes';
|
import { SERVER } from '../actions/actionsTypes';
|
||||||
import * as actions from '../actions';
|
import * as actions from '../actions';
|
||||||
import { serverFailure, selectServerRequest, selectServerSuccess } from '../actions/server';
|
import {
|
||||||
|
serverFailure, selectServerRequest, selectServerSuccess, selectServerFailure
|
||||||
|
} from '../actions/server';
|
||||||
import { setUser } from '../actions/login';
|
import { setUser } from '../actions/login';
|
||||||
import RocketChat from '../lib/rocketchat';
|
import RocketChat from '../lib/rocketchat';
|
||||||
import database from '../lib/realm';
|
import database from '../lib/realm';
|
||||||
|
@ -58,6 +62,7 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
|
||||||
// Return server version even when offline
|
// Return server version even when offline
|
||||||
yield put(selectServerSuccess(server, (serverInfo && serverInfo.version) || version));
|
yield put(selectServerSuccess(server, (serverInfo && serverInfo.version) || version));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
yield put(selectServerFailure());
|
||||||
log('err_select_server', e);
|
log('err_select_server', e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -81,7 +86,17 @@ const handleServerRequest = function* handleServerRequest({ server }) {
|
||||||
};
|
};
|
||||||
|
|
||||||
const root = function* root() {
|
const root = function* root() {
|
||||||
yield takeLatest(SERVER.SELECT_REQUEST, handleSelectServer);
|
|
||||||
yield takeLatest(SERVER.REQUEST, handleServerRequest);
|
yield takeLatest(SERVER.REQUEST, handleServerRequest);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const params = yield take(SERVER.SELECT_REQUEST);
|
||||||
|
const selectServerTask = yield fork(handleSelectServer, params);
|
||||||
|
yield race({
|
||||||
|
request: take(SERVER.SELECT_REQUEST),
|
||||||
|
success: take(SERVER.SELECT_SUCCESS),
|
||||||
|
failure: take(SERVER.SELECT_FAILURE)
|
||||||
|
});
|
||||||
|
yield cancel(selectServerTask);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
export default root;
|
export default root;
|
||||||
|
|
|
@ -29,6 +29,7 @@ import RoomsListHeaderView from './Header';
|
||||||
import { DrawerButton, CustomHeaderButtons, Item } from '../../containers/HeaderButton';
|
import { DrawerButton, CustomHeaderButtons, Item } from '../../containers/HeaderButton';
|
||||||
import StatusBar from '../../containers/StatusBar';
|
import StatusBar from '../../containers/StatusBar';
|
||||||
import ListHeader from './ListHeader';
|
import ListHeader from './ListHeader';
|
||||||
|
import { selectServerRequest as selectServerRequestAction } from '../../actions/server';
|
||||||
|
|
||||||
const SCROLL_OFFSET = 56;
|
const SCROLL_OFFSET = 56;
|
||||||
|
|
||||||
|
@ -56,7 +57,8 @@ const keyExtractor = item => item.rid;
|
||||||
openSearchHeader: () => dispatch(openSearchHeaderAction()),
|
openSearchHeader: () => dispatch(openSearchHeaderAction()),
|
||||||
closeSearchHeader: () => dispatch(closeSearchHeaderAction()),
|
closeSearchHeader: () => dispatch(closeSearchHeaderAction()),
|
||||||
appStart: () => dispatch(appStartAction()),
|
appStart: () => dispatch(appStartAction()),
|
||||||
roomsRequest: () => dispatch(roomsRequestAction())
|
roomsRequest: () => dispatch(roomsRequestAction()),
|
||||||
|
selectServerRequest: server => dispatch(selectServerRequestAction(server))
|
||||||
}))
|
}))
|
||||||
export default class RoomsListView extends React.Component {
|
export default class RoomsListView extends React.Component {
|
||||||
static navigationOptions = ({ navigation }) => {
|
static navigationOptions = ({ navigation }) => {
|
||||||
|
|
Loading…
Reference in New Issue