From c12dc79f23950abc434f6d1094c3030a3811e7e2 Mon Sep 17 00:00:00 2001 From: Daniel Doglas Silva Date: Thu, 4 Nov 2021 20:16:17 -0300 Subject: [PATCH 1/5] Added new treatments for chunked-reportComment and chunked-reportCommentEdit --- src/libs/Pusher/EventType.js | 2 ++ src/libs/Pusher/pusher.js | 30 +++++++++++++--------------- src/libs/actions/Report.js | 38 +++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/libs/Pusher/EventType.js b/src/libs/Pusher/EventType.js index 713f69b5c128..d872885376ba 100644 --- a/src/libs/Pusher/EventType.js +++ b/src/libs/Pusher/EventType.js @@ -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', diff --git a/src/libs/Pusher/pusher.js b/src/libs/Pusher/pusher.js index 88e3c8943400..92e284d8b774 100644 --- a/src/libs/Pusher/pusher.js +++ b/src/libs/Pusher/pusher.js @@ -113,35 +113,32 @@ 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; } - // If we are chunking the requests, we need to construct a rolling list of all packets that have come through // Pusher. If we've completed one of these full packets, we'll combine the data and act on the event that it's // 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; } @@ -158,7 +155,7 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun }); } - delete chunkedDataEvents[eventData.id]; + delete chunkedDataEvents[data.id]; } }; @@ -227,6 +224,7 @@ function subscribe( reject(status); }); } else { + debugger; bindEventToChannel(channel, eventName, eventCallback, isChunked); resolve(); } diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 378196515301..c85f379c5830 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -700,6 +700,24 @@ function subscribeToUserEvents() { {error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT}, ); }); + + // 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) => { @@ -720,7 +738,25 @@ function subscribeToUserEvents() { {error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_EDIT}, ); }); - + // 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); + }, false, + () => { + 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( From 1ded0392708a95917f7cf34c1e9f527a4183b1c9 Mon Sep 17 00:00:00 2001 From: Daniel Doglas Silva Date: Thu, 4 Nov 2021 20:16:17 -0300 Subject: [PATCH 2/5] Added new treatments for chunked-reportComment and chunked-reportCommentEdit --- src/libs/Pusher/EventType.js | 2 ++ src/libs/Pusher/pusher.js | 30 +++++++++++++--------------- src/libs/actions/Report.js | 38 +++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/libs/Pusher/EventType.js b/src/libs/Pusher/EventType.js index 76cb1b354c1d..b65ca8e7d7ac 100644 --- a/src/libs/Pusher/EventType.js +++ b/src/libs/Pusher/EventType.js @@ -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', diff --git a/src/libs/Pusher/pusher.js b/src/libs/Pusher/pusher.js index 88e3c8943400..92e284d8b774 100644 --- a/src/libs/Pusher/pusher.js +++ b/src/libs/Pusher/pusher.js @@ -113,35 +113,32 @@ 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; } - // If we are chunking the requests, we need to construct a rolling list of all packets that have come through // Pusher. If we've completed one of these full packets, we'll combine the data and act on the event that it's // 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; } @@ -158,7 +155,7 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun }); } - delete chunkedDataEvents[eventData.id]; + delete chunkedDataEvents[data.id]; } }; @@ -227,6 +224,7 @@ function subscribe( reject(status); }); } else { + debugger; bindEventToChannel(channel, eventName, eventCallback, isChunked); resolve(); } diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index f8b51506a25b..17b64f47c4e8 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -714,6 +714,24 @@ function subscribeToUserEvents() { {error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT}, ); }); + + // 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) => { @@ -734,7 +752,25 @@ function subscribeToUserEvents() { {error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_EDIT}, ); }); - + // 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); + }, false, + () => { + 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( From 08b27fa2afbf71922f164c488064be0b80e2cf5f Mon Sep 17 00:00:00 2001 From: Daniel Doglas Silva Date: Thu, 9 Dec 2021 16:00:58 -0300 Subject: [PATCH 3/5] fixing js lint issues --- src/libs/Pusher/pusher.js | 1 + src/libs/actions/Report.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libs/Pusher/pusher.js b/src/libs/Pusher/pusher.js index ecaba58537f8..45e1fd2699b4 100644 --- a/src/libs/Pusher/pusher.js +++ b/src/libs/Pusher/pusher.js @@ -124,6 +124,7 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun eventCallback(data); return; } + // If we are chunking the requests, we need to construct a rolling list of all packets that have come through // Pusher. If we've completed one of these full packets, we'll combine the data and act on the event that it's // assigned to. diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 17b64f47c4e8..80a7adc857ef 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -714,7 +714,7 @@ function subscribeToUserEvents() { {error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT}, ); }); - + // 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( @@ -752,6 +752,7 @@ function subscribeToUserEvents() { {error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_EDIT}, ); }); + // 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( @@ -771,6 +772,7 @@ function subscribeToUserEvents() { {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( From 024621fdb82aa0780ef8293a3cce6d5f63c58d1c Mon Sep 17 00:00:00 2001 From: Daniel Doglas Silva Date: Thu, 9 Dec 2021 19:34:44 -0300 Subject: [PATCH 4/5] fixing isChuncked for report edit --- src/libs/actions/Report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 80a7adc857ef..89e58928c46c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -741,7 +741,7 @@ function subscribeToUserEvents() { }, ); updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message); - }, false, + }, true, () => { NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel'); }) From 9751c5491844fa01c4395f20e6caa7f8e23c3050 Mon Sep 17 00:00:00 2001 From: Daniel Doglas Silva Date: Thu, 9 Dec 2021 19:36:20 -0300 Subject: [PATCH 5/5] fixing isChuncked for report edit --- src/libs/actions/Report.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 89e58928c46c..5d2e44d3b650 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -741,7 +741,7 @@ function subscribeToUserEvents() { }, ); updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message); - }, true, + }, false, () => { NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel'); }) @@ -761,7 +761,7 @@ function subscribeToUserEvents() { }, ); updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message); - }, false, + }, true, () => { NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel'); })