Skip to content

Adding support for chunked chats #6673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libs/Pusher/EventType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
export default {
REPORT_COMMENT: 'reportComment',
REPORT_COMMENT_CHUNK: 'chunked-reportComment',
REPORT_COMMENT_EDIT_CHUNK: 'chunked-reportCommentEdit',
REPORT_COMMENT_EDIT: 'reportCommentEdit',
REPORT_TOGGLE_PINNED: 'reportTogglePinned',
PREFERRED_LOCALE: 'preferredLocale',
Expand Down
28 changes: 13 additions & 15 deletions src/libs/Pusher/pusher.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,14 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun

const chunkedDataEvents = {};
const callback = (eventData) => {
let data;
try {
data = _.isObject(eventData) ? eventData : JSON.parse(eventData);
} catch (err) {
Log.alert('[Pusher] Unable to parse JSON response from Pusher', {error: err, eventData});
return;
}
if (!isChunked) {
let data;

try {
data = _.isObject(eventData) ? eventData : JSON.parse(eventData);
} catch (err) {
Log.alert('[Pusher] Unable to parse JSON response from Pusher', {error: err, eventData});
return;
}

eventCallback(data);
return;
}
Expand All @@ -132,16 +130,16 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun
// assigned to.

// If we haven't seen this eventID yet, initialize it into our rolling list of packets.
if (!chunkedDataEvents[eventData.id]) {
chunkedDataEvents[eventData.id] = {chunks: [], receivedFinal: false};
if (!chunkedDataEvents[data.id]) {
chunkedDataEvents[data.id] = {chunks: [], receivedFinal: false};
}

// Add it to the rolling list.
const chunkedEvent = chunkedDataEvents[eventData.id];
chunkedEvent.chunks[eventData.index] = eventData.chunk;
const chunkedEvent = chunkedDataEvents[data.id];
chunkedEvent.chunks[data.index] = data.chunk;

// If this is the last packet, mark that we've hit the end.
if (eventData.final) {
if (data.final) {
chunkedEvent.receivedFinal = true;
}

Expand All @@ -158,7 +156,7 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun
});
}

delete chunkedDataEvents[eventData.id];
delete chunkedDataEvents[data.id];
}
};

Expand Down
38 changes: 38 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,24 @@ function subscribeToUserEvents() {
);
});

// Live-update a report's actions when a 'chunked report comment' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT_CHUNK, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_COMMENT_CHUNK} event sent by Pusher`, false, {reportID: pushJSON.reportID},
);
updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference);
}, true,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_CHUNK},
);
});

// Live-update a report's actions when an 'edit comment' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT_EDIT, (pushJSON) => {
Log.info(
Expand All @@ -735,6 +753,26 @@ function subscribeToUserEvents() {
);
});

// Live-update a report's actions when an 'edit comment chunk' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK} event sent by Pusher`, false, {
reportActionID: pushJSON.reportActionID,
},
);
updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message);
}, true,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK},
);
});

// Live-update a report's pinned state when a 'report toggle pinned' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_TOGGLE_PINNED, (pushJSON) => {
Log.info(
Expand Down