Skip to content

Use new AuthenticatePusher NewDot Optimized API commands #9659

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 4 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,7 @@ export default {
FORMS: {
ADD_DEBIT_CARD_FORM: 'addDebitCardForm',
},

// Pusher token
PUSHER_AUTHORIZATION: 'pusherAuthorization',
};
16 changes: 15 additions & 1 deletion src/libs/PusherConnectionManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import * as Pusher from './Pusher/pusher';
import * as Session from './actions/Session';
import Log from './Log';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';

let authorizerCallback;
Onyx.connect({
key: ONYXKEYS.PUSHER_AUTHORIZATION,
callback: (authorizationData) => {
if (!authorizerCallback) {
return;
}
authorizerCallback(authorizationData.error, authorizationData.token);
},
});

function init() {
/**
Expand All @@ -13,7 +26,8 @@ function init() {
*/
Pusher.registerCustomAuthorizer(channel => ({
authorize: (socketID, callback) => {
Session.authenticatePusher(socketID, channel.name, callback);
authorizerCallback = callback;
Session.authenticatePusher(socketID, channel.name);
},
}));

Expand Down
37 changes: 3 additions & 34 deletions src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as ValidationUtils from '../../ValidationUtils';
import * as Authentication from '../../Authentication';
import * as ErrorUtils from '../../ErrorUtils';
import * as Welcome from '../Welcome';
import * as API from '../../API';

let credentials = {};
Onyx.connect({
Expand Down Expand Up @@ -444,47 +445,15 @@ const reauthenticatePusher = _.throttle(() => {
});
}, 5000, {trailing: false});

/**
* @param {String} socketID
* @param {String} channelName
* @param {Function} callback
*/
function authenticatePusher(socketID, channelName, callback) {
Log.info('[PusherAuthorizer] Attempting to authorize Pusher', false, {channelName});

DeprecatedAPI.Push_Authenticate({
API.read('AuthenticatePusher', {
socket_id: socketID,
channel_name: channelName,
shouldRetry: false,
forceNetworkRequest: true,
})
.then((response) => {
if (response.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher because authToken is expired');
callback(new Error('Pusher failed to authenticate because authToken is expired'), {auth: ''});

// Attempt to refresh the authToken then reconnect to Pusher
reauthenticatePusher();
return;
}

if (response.jsonCode !== CONST.JSON_CODE.SUCCESS) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher for reason other than expired session');
callback(new Error(`Pusher failed to authenticate because code: ${response.jsonCode} message: ${response.message}`), {auth: ''});
return;
}

Log.info(
'[PusherAuthorizer] Pusher authenticated successfully',
false,
{channelName},
);
callback(null, response);
})
.catch((error) => {
Log.hmmm('[PusherAuthorizer] Unhandled error: ', {channelName, error});
callback(new Error('Push_Authenticate request failed'), {auth: ''});
});
});
}

/**
Expand Down