diff --git a/app/actions/actionsTypes.ts b/app/actions/actionsTypes.ts index 1367b225..a920e27e 100644 --- a/app/actions/actionsTypes.ts +++ b/app/actions/actionsTypes.ts @@ -11,7 +11,7 @@ function createRequestTypes(base = {}, types = defaultTypes): Record): ISetUser { }; } +export function clearUser(): Action { + return { + type: types.USER.CLEAR + }; +} + export function setLoginServices(data: Record): ISetServices { return { type: types.LOGIN.SET_SERVICES, diff --git a/app/lib/methods/subscriptions/room.ts b/app/lib/methods/subscriptions/room.ts index f72137c2..ce2bf766 100644 --- a/app/lib/methods/subscriptions/room.ts +++ b/app/lib/methods/subscriptions/room.ts @@ -103,9 +103,16 @@ export default class RoomSubscription { } }; - handleConnection = () => { - reduxStore.dispatch(clearUserTyping()); - RocketChat.loadMissedMessages({ rid: this.rid }).catch(e => console.log(e)); + handleConnection = async () => { + try { + reduxStore.dispatch(clearUserTyping()); + await RocketChat.loadMissedMessages({ rid: this.rid }); + const _lastOpen = new Date(); + this.read(_lastOpen); + this.lastOpen = _lastOpen; + } catch (e) { + log(e); + } }; handleNotifyRoomReceived = protectedFunction((ddpMessage: IDDPMessage) => { diff --git a/app/lib/rocketchat/services/connect.ts b/app/lib/rocketchat/services/connect.ts index e7f24d0a..1cb34d96 100644 --- a/app/lib/rocketchat/services/connect.ts +++ b/app/lib/rocketchat/services/connect.ts @@ -18,7 +18,7 @@ import { loginRequest, setLoginServices, setUser } from '../../../actions/login' import sdk from './sdk'; import I18n from '../../../i18n'; import RocketChat, { MIN_ROCKETCHAT_VERSION, STATUSES } from '../rocketchat'; -import { ICredentials, ILoggedUser, IRocketChat, IUser } from '../../../definitions'; +import { ICredentials, ILoggedUser, IRocketChat } from '../../../definitions'; import { isIOS } from '../../../utils/deviceInfo'; import { connectRequest, connectSuccess, disconnect as disconnectAction } from '../../../actions/connect'; import { updatePermission } from '../../../actions/permissions'; @@ -38,8 +38,8 @@ interface IServices { // FIXME: Remove `this` context function connect( this: IRocketChat, - { server, user, logoutOnError = false }: { server: string; user: IUser; logoutOnError: boolean } -) { + { server, logoutOnError = false }: { server: string; logoutOnError: boolean } +): Promise { return new Promise(resolve => { if (sdk.current?.client?.host === server) { return resolve(); @@ -107,8 +107,8 @@ function connect( return; } store.dispatch(connectSuccess()); - const { server: currentServer } = store.getState().server; - if (user?.token && server === currentServer) { + const { user } = store.getState().login; + if (user?.token) { store.dispatch(loginRequest({ resume: user.token }, logoutOnError)); } }); diff --git a/app/reducers/login.test.ts b/app/reducers/login.test.ts index abab2eba..2c92b887 100644 --- a/app/reducers/login.test.ts +++ b/app/reducers/login.test.ts @@ -1,4 +1,5 @@ import { + clearUser, loginFailure, loginRequest, loginSuccess, @@ -81,6 +82,12 @@ describe('test selectedUsers reducer', () => { expect(state).toEqual(user.username); }); + it('should clear user after clearUser', () => { + mockedStore.dispatch(clearUser()); + const state = mockedStore.getState().login.user; + expect(state).toEqual({}); + }); + // TODO PREFERENCE REDUCER WITH EMPTY PREFERENCE - NON USED? // it('should return modified store after setPreference', () => { // mockedStore.dispatch(setPreference({ showAvatar: true })); diff --git a/app/reducers/login.ts b/app/reducers/login.ts index 2969d0c2..8d402611 100644 --- a/app/reducers/login.ts +++ b/app/reducers/login.ts @@ -80,6 +80,13 @@ export default function login(state = initialState, action: TActionsLogin): ILog ...action.user } }; + case types.USER.CLEAR: + return { + ...state, + user: {}, + isAuthenticated: false, + isLocalAuthenticated: false + }; case types.LOGIN.SET_SERVICES: return { ...state, diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index bc6fd21c..28cb12fc 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -9,8 +9,6 @@ import log from '../utils/log'; import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms'; import RocketChat from '../lib/rocketchat'; import buildMessage from '../lib/methods/helpers/buildMessage'; -import protectedFunction from '../lib/methods/helpers/protectedFunction'; -import UserPreferences from '../lib/userPreferences'; const updateRooms = function* updateRooms({ server, newRoomsUpdatedAt }) { const serversDB = database.servers; @@ -82,33 +80,46 @@ const handleRoomsRequest = function* handleRoomsRequest({ params }) { }) ), ...subsToUpdate.map(subscription => { - const newSub = subscriptions.find(s => s._id === subscription._id); - return subscription.prepareUpdate(() => { - if (newSub.announcement) { - if (newSub.announcement !== subscription.announcement) { - subscription.bannerClosed = false; + try { + const newSub = subscriptions.find(s => s._id === subscription._id); + return subscription.prepareUpdate(() => { + if (newSub.announcement) { + if (newSub.announcement !== subscription.announcement) { + subscription.bannerClosed = false; + } } - } - Object.assign(subscription, newSub); - }); + Object.assign(subscription, newSub); + }); + } catch (e) { + log(e); + return null; + } + }), + ...subsToDelete.map(subscription => { + try { + return subscription.prepareDestroyPermanently(); + } catch (e) { + log(e); + return null; + } }), - ...subsToDelete.map(subscription => subscription.prepareDestroyPermanently()), ...messagesToCreate.map(message => - messagesCollection.prepareCreate( - protectedFunction(m => { - m._raw = sanitizedRaw({ id: message._id }, messagesCollection.schema); - m.subscription.id = message.rid; - return Object.assign(m, message); - }) - ) + messagesCollection.prepareCreate(m => { + m._raw = sanitizedRaw({ id: message._id }, messagesCollection.schema); + m.subscription.id = message.rid; + return Object.assign(m, message); + }) ), ...messagesToUpdate.map(message => { const newMessage = lastMessages.find(m => m._id === message.id); - return message.prepareUpdate( - protectedFunction(() => { - Object.assign(message, newMessage); - }) - ); + return message.prepareUpdate(() => { + try { + return Object.assign(message, newMessage); + } catch (e) { + log(e); + return null; + } + }); }) ]; diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index 492191ee..0d0adcda 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -9,7 +9,7 @@ import Navigation from '../lib/Navigation'; import { SERVER } from '../actions/actionsTypes'; import { selectServerFailure, selectServerRequest, selectServerSuccess, serverFailure } from '../actions/server'; import { clearSettings } from '../actions/settings'; -import { setUser } from '../actions/login'; +import { clearUser, setUser } from '../actions/login'; import { clearActiveUsers } from '../actions/activeUsers'; import RocketChat from '../lib/rocketchat'; import database from '../lib/database'; @@ -111,10 +111,11 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch if (user) { yield put(clearSettings()); - yield RocketChat.connect({ server, user, logoutOnError: true }); yield put(setUser(user)); + yield RocketChat.connect({ server, logoutOnError: true }); yield put(appStart({ root: RootEnum.ROOT_INSIDE })); } else { + yield put(clearUser()); yield RocketChat.connect({ server }); yield put(appStart({ root: RootEnum.ROOT_OUTSIDE })); } diff --git a/yarn.lock b/yarn.lock index 5e789fb7..e39d74cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3759,8 +3759,8 @@ integrity sha512-NF5KlFt642ZucP/KHnYGBNYLD6O7bcrZMKfRQlH5Y3/1xpnPX1g4wuygtiV7XArMU1FopQT+qmCUPPj8IMDTcw== "@rocket.chat/sdk@RocketChat/Rocket.Chat.js.SDK#mobile": - version "1.2.0-mobile" - resolved "https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/9d18d628b87742302bfa0789d38bbd0c66118941" + version "1.3.0-mobile" + resolved "https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/454b4ba784095057b8de862eb99340311b672e15" dependencies: js-sha256 "^0.9.0" lru-cache "^4.1.1"