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

feat(collections): Show Relevant Public Collections on Entity Pages #599

Merged
merged 21 commits into from
May 3, 2021

Conversation

akashgp09
Copy link
Contributor

@akashgp09 akashgp09 commented Apr 7, 2021

Problem

This PR Fixes: BB-614

Solution

  • Loaded the collections property in the frontend newly added to the ORM
  • created a new component <EntityRelatedCollections /> which shows all the related public collections of a entity on the enitity page

Screenshot from 2021-05-01 09-38-15

Areas of Impact

Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
@coveralls
Copy link

coveralls commented Apr 7, 2021

Coverage Status

Coverage increased (+0.05%) to 60.91% when pulling 5f98958 on akashgp09:related-collections into 82e5222 on bookbrainz:master.

Copy link
Contributor

@MonkeyDo MonkeyDo left a comment

Choose a reason for hiding this comment

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

I think this mechanism would be better suited for the bookbrainz-data-js repo.

I looked into it, the syntax takes some getting used to but there is a way to fetch the collections an entity appears in using the standard BookshelfJS withRelated relations.

The idea after that is to simply add collections to the relations we load for each entity type here:
https://github.com/bookbrainz/bookbrainz-site/blob/master/src/server/helpers/middleware.ts#L146-L153

In the ORM, adding this to the Author model (it would be needed on each entity model) does the trick:

collections() {
  return this.belongsToMany('UserCollection').through('UserCollectionItem', 'bbid', 'collection_id', 'bbid');
},

With that, the entity once loaded in react will have the collections property available to pass to a component without the need for new routes.
And we can add tests for it in bookbrainz-data-js that way it's more robust.

Afterwards we can even look at requesting the collection owner to display it alongside, using collections.owner in the middleware relations.

@akashgp09
Copy link
Contributor Author

I think this mechanism would be better suited for the bookbrainz-data-js repo.

Yeah, this would really make the solution simpler.
Thanks for the review, I Will make the changes soon :)

@akashgp09 akashgp09 requested a review from MonkeyDo May 1, 2021 04:15
Copy link
Contributor

@MonkeyDo MonkeyDo left a comment

Choose a reason for hiding this comment

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

This is currently working quite nicely, but after reviewing again I think we need one more adjustment.

We should filter out the private collections that a user shouldn't have access to on the server side, before sending the props to the client side (where they could be accessed despite rendering only public collections on screen).

The method I have in mind would also allow us to show private collections from the current logged in user along with the public collections.

Filtering the collections should probably be done in makeEntityLoader, just before caching the entity in the response (res.locals.entity = entity;).

Something like this in makeEntityLoader should do it:

if (entity.collections) {
	entity.collections = entity.collections.filter(collection => collection.public === true ||
	parseInt(collection.ownerId, 10) === parseInt(req.user?.id, 10));
}

And then removing the if (collection.public) check in the EntityRelatedCollections component.

I think it would also be better to move the Related Entities section below the EntityLinks component (relationships and identifiers) which are more important information.

Sorry about not spotting all that before !

@akashgp09
Copy link
Contributor Author

We should filter out the private collections that a user shouldn't have access to on the server side, before sending the props to the client side (where they could be accessed despite rendering only public collections on screen).

Aah Yes !!! that's really important. Thanks for all the feedbacks, I will make these changes in a bit.

Copy link
Contributor

@MonkeyDo MonkeyDo left a comment

Choose a reason for hiding this comment

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

Good to go, thank you !
Great new feature :)

@MonkeyDo MonkeyDo merged commit 124ef7b into metabrainz:master May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants