-
Notifications
You must be signed in to change notification settings - Fork 395
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
fix: ...
(spread vs. rest property) variable assignment
#366
Conversation
…tials-endpoint Fix missing apps verify credentials endpoint
…oint Fix incompatible instance endpoint
[X] Use the official Mastodon default image for avatar
Change Default Images to Official Mastodon Avatar
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
**NOTE: This commit breaks existing tests*** This happens because the erroneous `...rest` destructuring was introducing errors that were not previously being detected by unit tests. ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#rest_property
@@ -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 = { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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), | ||
|
There was a problem hiding this comment.
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`)
Sorry for the spam. Trying to revert an accidental merge commit that pulled in several upstream merge commits 🤦🏼 |
Closing this PR while I sort out the mess I created for myself. Apologies again for the spam |
👀 Please review/merge #363 before this PR
Examples of the problem:
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 asallStatuses = [...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 wronglyPASS
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
Announce
handler) business logic:fetch()
in an attempt to cache APObject whenever a local user re-blogs a post that they authoredid
instead of checking for equality of local APObjectid
)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
orundefined
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