Skip to content

Commit

Permalink
[FIX] Missing messages after reconnect (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello authored Dec 13, 2019
1 parent a3822d4 commit 7b18bf6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 62 deletions.
41 changes: 24 additions & 17 deletions app/lib/methods/subscriptions/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import reduxStore from '../../createStore';
import { addUserTyping, removeUserTyping, clearUserTyping } from '../../../actions/usersTyping';
import debounce from '../../../utils/debounce';

const unsubscribe = subscriptions => subscriptions.forEach(sub => sub.unsubscribe().catch(() => console.log('unsubscribeRoom')));
const unsubscribe = (subscriptions = []) => Promise.all(subscriptions.map(sub => sub.unsubscribe));
const removeListener = listener => listener.stop();

let promises;
let connectedListener;
let disconnectedListener;
let notifyRoomListener;
let messageReceivedListener;

export default function subscribeRoom({ rid }) {
console.log(`[RCRN] Subscribed to room ${ rid }`);
let promises;
let connectedListener;
let disconnectedListener;
let notifyRoomListener;
let messageReceivedListener;

const handleConnection = () => {
this.loadMissedMessages({ rid }).catch(e => console.log(e));
Expand Down Expand Up @@ -197,25 +198,35 @@ export default function subscribeRoom({ rid }) {
});
});

const stop = () => {
const stop = async() => {
let params;
if (promises) {
promises.then(unsubscribe);
try {
params = await promises;
await unsubscribe(params);
} catch (error) {
// Do nothing
}
promises = false;
}
if (connectedListener) {
connectedListener.then(removeListener);
params = await connectedListener;
removeListener(params);
connectedListener = false;
}
if (disconnectedListener) {
disconnectedListener.then(removeListener);
params = await disconnectedListener;
removeListener(params);
disconnectedListener = false;
}
if (notifyRoomListener) {
notifyRoomListener.then(removeListener);
params = await notifyRoomListener;
removeListener(params);
notifyRoomListener = false;
}
if (messageReceivedListener) {
messageReceivedListener.then(removeListener);
params = await messageReceivedListener;
removeListener(params);
messageReceivedListener = false;
}
reduxStore.dispatch(clearUserTyping());
Expand All @@ -226,11 +237,7 @@ export default function subscribeRoom({ rid }) {
notifyRoomListener = this.sdk.onStreamData('stream-notify-room', handleNotifyRoomReceived);
messageReceivedListener = this.sdk.onStreamData('stream-room-messages', handleMessageReceived);

try {
promises = this.sdk.subscribeRoom(rid);
} catch (e) {
log(e);
}
promises = this.sdk.subscribeRoom(rid);

return {
stop: () => stop()
Expand Down
82 changes: 37 additions & 45 deletions app/views/RoomView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class RoomView extends React.Component {
}
}
}
this.unsubscribe();
await this.unsubscribe();
if (this.didFocusListener && this.didFocusListener.remove) {
this.didFocusListener.remove();
}
Expand Down Expand Up @@ -324,38 +324,41 @@ class RoomView extends React.Component {
}

// eslint-disable-next-line react/sort-comp
init = () => {
init = async() => {
try {
this.setState({ loading: true });
this.initInteraction = InteractionManager.runAfterInteractions(async() => {
const { room, joined } = this.state;
if (this.tmid) {
await this.getThreadMessages();
} else {
const newLastOpen = new Date();
await this.getMessages(room);

// if room is joined
if (joined) {
if (room.alert || room.unread || room.userMentions) {
this.setLastOpen(room.ls);
} else {
this.setLastOpen(null);
}
RocketChat.readMessages(room.rid, newLastOpen).catch(e => console.log(e));
this.unsubscribe();
this.sub = await RocketChat.subscribeRoom(room);
const { room, joined } = this.state;
if (this.tmid) {
await this.getThreadMessages();
} else {
const newLastOpen = new Date();
await this.getMessages(room);

// if room is joined
if (joined) {
if (room.alert || room.unread || room.userMentions) {
this.setLastOpen(room.ls);
} else {
this.setLastOpen(null);
}
RocketChat.readMessages(room.rid, newLastOpen).catch(e => console.log(e));
await this.unsubscribe();
this.sub = RocketChat.subscribeRoom(room);
}
}

// We run `canAutoTranslate` again in order to refetch auto translate permission
// in case of a missing connection or poor connection on room open
const canAutoTranslate = await RocketChat.canAutoTranslate();
this.setState({ canAutoTranslate, loading: false });
});
// We run `canAutoTranslate` again in order to refetch auto translate permission
// in case of a missing connection or poor connection on room open
const canAutoTranslate = await RocketChat.canAutoTranslate();
this.setState({ canAutoTranslate, loading: false });
} catch (e) {
this.setState({ loading: false });
log(e);
this.retryInit = this.retryInit + 1 || 1;
if (this.retryInit <= 1) {
this.retryInitTimeout = setTimeout(() => {
this.init();
}, 300);
}
}
}

Expand Down Expand Up @@ -386,9 +389,9 @@ class RoomView extends React.Component {
}
}

unsubscribe = () => {
unsubscribe = async() => {
if (this.sub && this.sub.stop) {
this.sub.stop();
await this.sub.stop();
}
}

Expand Down Expand Up @@ -568,27 +571,16 @@ class RoomView extends React.Component {
return ((room.prid || useRealName) && room.fname) || room.name;
}

getMessages = async() => {
getMessages = () => {
const { room } = this.state;
try {
if (room.lastOpen) {
await RocketChat.loadMissedMessages(room);
} else {
await RocketChat.loadMessagesForRoom(room);
}
return Promise.resolve();
} catch (e) {
log(e);
if (room.lastOpen) {
return RocketChat.loadMissedMessages(room);
} else {
return RocketChat.loadMessagesForRoom(room);
}
}

getThreadMessages = () => {
try {
return RocketChat.loadThreadMessages({ tmid: this.tmid, rid: this.rid });
} catch (e) {
log(e);
}
}
getThreadMessages = () => RocketChat.loadThreadMessages({ tmid: this.tmid, rid: this.rid })

getCustomEmoji = (name) => {
const { customEmojis } = this.props;
Expand Down

0 comments on commit 7b18bf6

Please sign in to comment.