Skip to content
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

fix: ... (spread vs. rest property) variable assignment #366

Conversation

DataDrivenMD
Copy link
Contributor

@DataDrivenMD DataDrivenMD commented Mar 4, 2023

👀 Please review/merge #363 before this PR

Examples of the problem:

const id = 'ActorA'
const preferredUsername = 'jorge'
const properties = { id: 'ActorB', preferredUsername: 'sven'}

const example0 = { id, preferredUsername, properties }
console.log( JSON.stringify(example0) )
// '{"id":"ActorA","preferredUsername":"jorge","properties":{"id":"ActorB","preferredUsername":"sven"}}'

const example1 = { id, preferredUsername, ...properties }
console.log( JSON.stringify(example1) )
// '{"id":"ActorB","preferredUsername":"sven"}'

const example2 = { id, ...properties, preferredUsername }
console.log( JSON.stringify(example2) )
// '{"id":"ActorB","preferredUsername":"jorge"}'

const example3 = { ...properties, id, preferredUsername }
console.log( JSON.stringify(example3) )
// '{"id":"ActorA","preferredUsername":"jorge"}'

Fix the problem

  • Non-breaking fixes to ...spread vs. ...rest destructuring in front-end code

  • Added TODO flags to code segments that need to be re-reviewed to ensure that they are working as intended. Specifically, the flags are added to array concatenations that involve the spread operator such as allStatuses = [...allStatuses, ...newStatuses], which would yield unpredictable results when multi-dimensional arrays are involved.

  • Fixed several several dozen invocations of improper ...rest property binding that involve HTTP response headers. As previously written, the resulting headers object would omit elements that followed ...cors(), in the assignment expression.

  • Fixed additional variable assignments that use ...rest property destructuring. Note: this commit breaks existing tests, which were wrongly PASS as a result of bugs in the underlying code that were not being caught by the tests.

Identify bugs caused by the problem and Fix bugs caused by the problem

  • Fix several bugs in reblog (ActivityPub Announce handler) business logic:
    • Fix bug that allowed multiple AP Announce activities for the same remote AP Object
    • Fix bug that allowed AP Announce for APObject with a null payload
    • Fix bug that would create notification when a user re-blogs a post that they authored
    • Fix bug that would call fetch() in an attempt to cache APObject whenever a local user re-blogs a post that they authored
  • Fix bugs in reblog logic that would allow reblog + notification for remote objects that were inaccessible by the local instance (i.e. due to connectivity errors or de-federation/blocking by remote instance)
  • Fix bugs in several tests that were using the wrong ID type for assertions (e.g. checking for equality of remote APObject id instead of checking for equality of local APObject id)

TL; DR:

Tests that were previously passing but are now breaking rely on assertions that validate hard-coded defaults- these values don't change when the business logic yields null or undefined values along the way, as was happening in several areas due to variable assignment and object binding using the spread and rest operators (...). Now that the correct syntax is being used and the bugs in the original code has been addressed, the proper use of ... in variable assignment should be a point of emphasis for code reviews moving forward.

References:

https://gist.github.com/yesvods/51af798dd1e7058625f4

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#copy_an_array

cc @xtuc @dario-piotrowicz

…tials-endpoint

Fix missing apps verify credentials endpoint
[X] Use the official Mastodon default image for avatar
Change Default Images to Official Mastodon Avatar
This reverts commit 0776114, reversing
changes made to 4ef9d98.
Noted the need to double-check that the code is working as intended wherever there's an array concatenation that relies on the spread (`...`) operator. This syntax will silently fail whenever multi-dimensional arrays are involved.

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#sect1
@@ -231,9 +236,12 @@ export async function sanitizeObjectProperties(properties: unknown): Promise<APO
if (!isAPObject(properties)) {
throw new Error('Invalid object properties. Expected an object but got ' + JSON.stringify(properties))
}

// prettier-ignore
const sanitized: APObject = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: Manually setting the type to APObject here is technically invalid because not all of the objects that are passed to the method have id, type, and url properties. As none of these properties are accessed in this method, the type-checking doesn't fail within the method. This explains why, despite not having an id or type property, we throw an error during mocking when we "overwrite" these properties by calling the ...properties operation during variable assignment

@@ -129,16 +130,19 @@ export async function cacheObject(

{
const properties = JSON.parse(row.properties)

// prettier-ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's unnecessary to set const properties = JSON.parse(row.properties) because row.properties === sanitizedProperties

const object = {
published: new Date(row.cdate).toISOString(),
...properties,

type: row.type,
id: new URL(row.id),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's unnecessary to set type: row.type and id: new URL(row.id), because row.properties === sanitizedProperties. So, the expansion ...sanitizedProperties should suffice

- [X] Fix reblog business logic to align with ActivityPub spec
- [X] Fix `Activities` unit tests that were broken because they were based on incorrect business logic

This command should yield a passing test result: `yarn test backend/test/activitypub/handle.spec.ts`
- [X] Fix several reblog bugs:
  - [X] Fix bug that allowed multiple AP Announce activities for the same remote AP Object
  - [X] Fix bug that allowed AP Announce for APObject with a null payload
  - [X] Fix bug that would create notification when a user re-blogs a post that they authored
  - [X] Fix bug that would call `fetch()` in an attempt to cache APObject whenever a local user re-blogs a post that they authored
- [X] Improve business logic for reblog handling to improve performance by validating `Announce` activity request for empty objects, inaccessible source objects (i.e. connectivity errors or de-federation/blocking by remote instance)
- [X] Fix bugs in several tests that were using the wrong ID type for assertions (e.g. checking for equality of remote APObject `id` instead of checking for equality of local APObject `id`)
@DataDrivenMD DataDrivenMD marked this pull request as ready for review March 6, 2023 19:45
@DataDrivenMD DataDrivenMD mentioned this pull request Mar 7, 2023
3 tasks
@DataDrivenMD
Copy link
Contributor Author

Sorry for the spam. Trying to revert an accidental merge commit that pulled in several upstream merge commits 🤦🏼

@DataDrivenMD
Copy link
Contributor Author

Closing this PR while I sort out the mess I created for myself. Apologies again for the spam

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.

1 participant