Skip to content

Pusher suffix for internal devs - #8796

Merged
Beamanator merged 15 commits into
Expensify:mainfrom
kidroca:kidroca/feature/set-pusher-suffix
May 3, 2022
Merged

Beamanator merged 15 commits into
Expensify:mainfrom
kidroca:kidroca/feature/set-pusher-suffix

Conversation

@kidroca

@kidroca kidroca commented Apr 27, 2022 •

Copy link
Copy Markdown
Contributor

Details

Reads pusher suffix from local php config (in external sibling folder project)
Exposes it to .env so it's available in CONFIG.js

Part of bigger Offline First - Pusher Changes design doc.

The reason we need to start doing this is b/c in the near future Pusher will handle some API responses, and we don't want one response to take effect on multiple engineers' local environments. Hence we're appending a unique string (the pusher suffix) to the end of pusher channels to keep things from crossing over

Fixed Issues

$ https://github.com/Expensify/Expensify/issues/207393

Tests

  • Verify that no errors appear in the JS console
  • Verify the suffix is exposed to webpack (web/mobile-web/desktop)
    • you can console.log it from src/CONFIG.js
  • Verify the suffix is exposed in metro/react-native (ios/android)
  • The suffix should fallback to empty string '' for production and staging builds
  • Inspect and see Pusher unit tests passing

Sample local dev output

[web-server] 
[web-server] > new.expensify@1.1.57-3 web-server /Users/kidroca/Desktop/Freelance/Expensify.cash
[web-server] > scripts/dev.sh && webpack-dev-server --config config/webpack/webpack.dev.js
[web-server] 
[web-proxy] 
[web-proxy] > new.expensify@1.1.57-3 web-proxy /Users/kidroca/Desktop/Freelance/Expensify.cash
[web-proxy] > node web/proxy.js
[web-proxy] 
[web-server] Using PUSHER_DEV_SUFFIX from ./config/_config.local.php
[web-server] Found suffix: 83894078fa2543c7b4c775b6645b514d
[web-server] Updating .env
...

PR Review Checklist

Contributor (PR Author) Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

PR Reviewer Checklist

  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

QA Steps

Verify production or staging are unaffected.
The changes here should apply only to local development. Sending/receiving reports and "user typing" events should be be working without regressions

  • Sending/Receiving messages still works
  • Adding/Removing users to policy still works
  • Removing users from policy from User B should reflect the changes for User A
    • Not sure how this exactly works but if User A and User B are part of the same policy and User B can remove users. When User B remove users this should be reflected for User A in real time. I think if you go to Setting -> Workspace -> Manage Members you should not see the removed members

Screenshots

Web

image

Mobile Web

image

Desktop

image

iOS

image

Android

image

cc @Beamanator

@kidroca
kidroca requested review from a team and marcaaron as code owners April 27, 2022 10:44
@melvin-bot
melvin-bot Bot requested review from arosiclair and removed request for a team April 27, 2022 10:44
@kidroca

kidroca commented Apr 27, 2022

Copy link
Copy Markdown
Contributor Author

@Beamanator can you check whether this is the direction we want to go - add a value to .env so it can be reused by both metro and webpack bundlers


Some context

Reading _config.local.php and adding PUSHER_DEV_SUFFIX only in webpack does not expose the variable to react-native ios/android, because they are bundled by metro

There's no easy way to update metro config to include and set a PUSHER_DEV_SUFFIX global variable, besides writing a custom plugin

The easiest thing to do to expose PUSHER_DEV_SUFFIX to both metro and webpack seems to be to add it to .env

@kidroca kidroca left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some notes on the changes

Comment thread package.json Resolved
*/
function subscribeToPrivateUserChannelEvent(eventName, onEvent, isChunked = false) {
const pusherChannelName = `private-user-accountID-${currentUserAccountID}`;
const pusherChannelName = `private-user-accountID-${currentUserAccountID}${CONFIG.PUSHER.SUFFIX}`;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can introduce a method Pusher.subscribeToPrivateChannel so we have a only one place where we combine ${channelName}${CONFIG.PUSHER.SUFFIX}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the near future we're going to encrypt the user channel (private-encrypted) but the report channel will remain private. I like the idea of adding ${CONFIG.PUSHER.SUFFIX} in one place instead of all these files, maybe a different function name like Pusher.prepareChannelName or something generic just about the channel name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'm just not sure where to put it, should it be part of Pusher or not
For example there's Pusher.init where we pass values from CONFIG.js so it seems importing CONFIG.js directly in Pusher was deliberately avoided

But if we are to create a method like Pusher.prepareChannelName we'll have to import CONFIG.js to pusher

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True true, looks like @tgolen made those changes, though it was a long time ago. I can't see why we wouldn't want to import Config into Pusher since we import Config into lots of other places 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is much harm in importing CONFIG.js. I think it was at one point helpful to have it with fewer dependencies because some of this code I think originally came from OldDot. I would still be in favor of keeping it low on dependencies, and then having a method for getChannelName() which can add the suffix if it needs to.

@Beamanator Beamanator Apr 28, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so it sounds like you're good to add the new method to Pusher 👍

Edit: We'll refactor in a different PR

Comment thread scripts/set-pusher-suffix.sh Resolved
Comment thread package.json Resolved
Comment thread scripts/dev.sh Resolved
Comment thread package.json Resolved
@marcaaron
marcaaron removed their request for review April 27, 2022 16:36
@arosiclair
arosiclair removed their request for review April 28, 2022 13:59
Comment thread scripts/set-pusher-suffix.sh Resolved

@kidroca kidroca left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted Pusher channel names to CONST
Added a Pusher.getChannelName method

✔️ Ready for Review

Comment thread scripts/set-pusher-suffix.sh Resolved
Comment thread src/libs/Pusher/pusher.js Outdated
Comment on lines +369 to +373
function getChannelName(...parts) {
return _.chain([...parts, CONFIG.PUSHER.SUFFIX])
.filter(Boolean)
.join('-')
.value();

@kidroca kidroca Apr 28, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be named to getPrivateChannelName because I saw an instance that does not use the Pusher suffix (for the policy editor channel)

Or maybe we could try to make the logic "smarter" and try to append suffix only if any of the parts includes the word "private"?


Regarding the dash:
Onyx collection keys include the -inside the key name, perhaps you'd like the same here, though filtering out empty keys would require more logic

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of this being getPrivateChannelName() 👍

Onyx collection keys include the -inside the key name, perhaps you'd like the same here, though filtering out empty keys would require more logic

I'm not really following this and what changes it would imply. What do the parts in the pusher channel name have to do with Onyx collection keys?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I actually don't particularly like having the dash automatically derived in this code. I think it makes it trickier to keep the channels names in PHP in sync with the channel names in JS. I would propose that this function is more like this:

function getChannelNameWithSuffix(name) {
    return `${name}-${CONFIG.PUSHER.SUFFIX}`;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really following this and what changes it would imply. What do the parts in the pusher channel name have to do with Onyx collection keys?

It's only about dashes and concatenation, do we have a preference to include the dash in the key (like in Onyx keys) or not?

Onyx keys

App/src/ONYXKEYS.js

Lines 99 to 103 in d1f3643

COLLECTION: {
REPORT: 'report_',
REPORT_ACTIONS: 'reportActions_',
REPORT_DRAFT_COMMENT: 'reportDraftComment_',
REPORT_ACTIONS_DRAFTS: 'reportActionsDrafts_',

Usage (no dash in concatenation)

Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actionsToRemove);

Following that pattern we can name Pusher channels like so:

    PUSHER_CHANNEL: {
        USER: 'private-user-accountID-',
        REPORT: 'private-report-reportID-',
        POLICY_EDITOR: 'public-policyEditor-',
    },

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I actually don't particularly like having the dash automatically derived in this code. I think it makes it trickier to keep the channels names in PHP in sync with the channel names in JS.

In that case maybe extracting names to CONST and a getName function is only getting in the way, can we stick with this?

const channelName = `private-user-accountID-${accountID}-${CONFIG.PUSHER.SUFFIX}`
Pusher.subscribe(channelName);

I'll drop these changes and perhaps we can open a separate refactoring PR if needed

@kidroca
kidroca requested review from Beamanator and tgolen April 28, 2022 19:38
Comment thread package.json Outdated
"ipad-sm": "npm run check-metro-bundler-port && react-native run-ios --simulator=\"iPad Pro (9.7-inch)\"",
"start": "react-native start",
"web": "node web/proxy.js & webpack-dev-server --open --config config/webpack/webpack.dev.js",
"web": "concurrently npm:web-proxy npm:web-server",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does dev.sh get ran in this instance?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does dev.sh get ran in this instance?

It's part of the web-server script

Comment thread scripts/dev.sh Outdated

SCRIPTS_DIR=$(dirname "${BASH_SOURCE[0]}")

bash "$SCRIPTS_DIR/set-pusher-suffix.sh"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have this split up into separate files if this is all it does? Let's just put it all in one file if we can.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea for dev.sh is to host a list of scripts we run before starting local development

If that's undesired, I'll just delete it and use set-pusher-suffix.sh directly

Comment thread src/libs/Pusher/pusher.js Outdated
*/
function getChannelName(...parts) {
return _.chain([...parts, CONFIG.PUSHER.SUFFIX])
.filter(Boolean)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this doing something different than _.compact() would? I'd prefer _.compact() if it's just to remove falsy values.

Comment thread src/libs/Pusher/pusher.js Outdated
Comment on lines +369 to +373
function getChannelName(...parts) {
return _.chain([...parts, CONFIG.PUSHER.SUFFIX])
.filter(Boolean)
.join('-')
.value();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of this being getPrivateChannelName() 👍

Onyx collection keys include the -inside the key name, perhaps you'd like the same here, though filtering out empty keys would require more logic

I'm not really following this and what changes it would imply. What do the parts in the pusher channel name have to do with Onyx collection keys?

Comment thread src/libs/Pusher/pusher.js Outdated
}

/**
* Combines channel parts and adds Pusher suffix when necessary

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Combines channel parts and adds Pusher suffix when necessary
* Combines all parts into the channel name (separated by a dash) and puts the Pusher suffix on the end

Comment thread src/libs/Pusher/pusher.js Outdated
Comment on lines +369 to +373
function getChannelName(...parts) {
return _.chain([...parts, CONFIG.PUSHER.SUFFIX])
.filter(Boolean)
.join('-')
.value();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I actually don't particularly like having the dash automatically derived in this code. I think it makes it trickier to keep the channels names in PHP in sync with the channel names in JS. I would propose that this function is more like this:

function getChannelNameWithSuffix(name) {
    return `${name}-${CONFIG.PUSHER.SUFFIX}`;
}

@kidroca
kidroca force-pushed the kidroca/feature/set-pusher-suffix branch from acc63a2 to 53f2ca0 Compare April 29, 2022 09:53
@kidroca

kidroca commented Apr 29, 2022

Copy link
Copy Markdown
Contributor Author

I've reverted the refactoring work out of this PR since it's not essential to the feature - it seems raw concatenation is preferable in order to keep php and js channel names in sync

We can open a separate PR to refactor
✔️ Ready for Review

@Beamanator

Beamanator commented Apr 29, 2022 •

Copy link
Copy Markdown
Contributor

I like the idea of doing the refactor in a separate PR to keep changes specific.

Can you also add the pusher suffix to the public-policyEditor channel name (in actions/Policy.js)? I didn't see that one since it was just added recently

@Beamanator Beamanator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add this comment & the suffix to the policyEditor channel? Then I think we're good to go

Comment thread scripts/set-pusher-suffix.sh Resolved
@kidroca

kidroca commented Apr 29, 2022

Copy link
Copy Markdown
Contributor Author

Can you also add the pusher suffix to the public-policyEditor channel name (in actions/Policy.js)? I didn't see that one since it was just added recently

Sure, I thought it was intentionally left out
I'm not familiar with what the suffix is for, but the logic to expose it in webpack and metro

Maybe in the future the suffix can be fetched as a param after login?

@kidroca

kidroca commented Apr 29, 2022

Copy link
Copy Markdown
Contributor Author

Could you please add this comment & the suffix to the policyEditor channel? Then I think we're good to go

✔️ Done & done

@kidroca
kidroca requested review from Beamanator and tgolen April 29, 2022 16:58
@kidroca
kidroca force-pushed the kidroca/feature/set-pusher-suffix branch from 7ae4149 to 1e04d06 Compare April 29, 2022 17:01

@Beamanator Beamanator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for the great work as always @kidroca 💪

I'm going to hold off merging till another Internal PR goes through (hopefully today)

@melvin-bot

melvin-bot Bot commented May 2, 2022

Copy link
Copy Markdown

npm has a package.json file and a package-lock.json file. It seems you updated one without the other, which is usually a sign of a mistake. If you are updating a package make sure that you update the version in package.json then run npm install

@Beamanator
Beamanator merged commit 4365203 into Expensify:main May 3, 2022
@OSBotify

OSBotify commented May 3, 2022

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@kidroca
kidroca deleted the kidroca/feature/set-pusher-suffix branch May 3, 2022 17:25
@kidroca

kidroca commented May 3, 2022 •

Copy link
Copy Markdown
Contributor Author

Hey guys sorry but it seems I've forgotten to update file permissions for the new script
It should be flagged as executable

image

I think I've forgotten to commit the change or something, because switching and pulling main prints the above error when I try to start local dev

@tgolen

tgolen commented May 3, 2022

Copy link
Copy Markdown
Contributor

Heh, yeah. I just ran into that. Oops!

@Beamanator

Copy link
Copy Markdown
Contributor

Thanks for fixing @kidroca ! 🙏

@OSBotify

OSBotify commented May 9, 2022

Copy link
Copy Markdown
Contributor

🚀 Cherry-picked to staging by @sketchydroide in version: 1.1.57-8 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@Expensify/applauseleads please QA this PR and check it off on the deploy checklist if it passes.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by @chiragsalian in version: 1.1.57-17 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants