[FIX] Issues after reconnecting (#3815)

This commit is contained in:
Diego Mello 2022-03-11 11:12:25 -03:00 committed by GitHub
parent d73da2942b
commit 65c9aec2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 36 deletions

View File

@ -11,7 +11,7 @@ function createRequestTypes(base = {}, types = defaultTypes): Record<string, str
// Login events
export const LOGIN = createRequestTypes('LOGIN', [...defaultTypes, 'SET_SERVICES', 'SET_PREFERENCE', 'SET_LOCAL_AUTHENTICATED']);
export const SHARE = createRequestTypes('SHARE', ['SELECT_SERVER', 'SET_USER', 'SET_SETTINGS', 'SET_SERVER_INFO']);
export const USER = createRequestTypes('USER', ['SET']);
export const USER = createRequestTypes('USER', ['SET', 'CLEAR']);
export const ROOMS = createRequestTypes('ROOMS', [
...defaultTypes,
'REFRESH',

View File

@ -93,6 +93,12 @@ export function setUser(user: Partial<IUser>): ISetUser {
};
}
export function clearUser(): Action {
return {
type: types.USER.CLEAR
};
}
export function setLoginServices(data: Record<string, any>): ISetServices {
return {
type: types.LOGIN.SET_SERVICES,

View File

@ -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) => {

View File

@ -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<void> {
return new Promise<void>(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));
}
});

View File

@ -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 }));

View File

@ -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,

View File

@ -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;
}
});
})
];

View File

@ -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 }));
}

View File

@ -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"