docbrown/app/serializers/search/reading_list_article_serializer.rb
rhymes 83852038e4
[Search 2.0] Reading list (#13052)
* Step one in populating the reading list with PG

This first attempt tries to recycle the `Search::ArticleSerializer` which is only
used in input in ES, but we're using it in output in PG.
For this reason it's currently 15.55x times slower

* Serialize only what is requested by the frontend

`Search::ArticleSerializer` which is only used in ES in the indexing step aims
to add as much info as possible for broader purposes, in this case
(with serialization in output) we should aim to save only what's requested from
the frontend.

* Optimize selection of articles columns

* Select only needed columns for users

* Compute total of reading list items

* Attach the basic filtering based on PG on the search controller

* Restructure in methods

* Add tags support

* Use LIKE on articles.cached_tag_list

* Fix tags as nil

* Fix default pagination

* Add optional FTS for reading list

* Reworded the tags comment explaining why

* Add index to reactions.status

* Fix total counter in Preact readingList component

* Fix total count in reading list backend search

* Add GIN index to articles.cached_tag_list

* Add service tests

* Add search request specs

* Added missing early return

* Update spec/requests/search_spec.rb

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Extract MAX_PER_PAGE constant and add comments

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
2021-03-24 15:40:00 +01:00

36 lines
1.5 KiB
Ruby

module Search
class ReadingListArticleSerializer < ApplicationSerializer
attribute :id, &:reaction_id
attribute :user_id, &:reaction_user_id
# NOTE: the list of attributes for `reactable` comes from two places:
# => the `<ItemListItem>` Preact component: https://github.com/forem/forem/blob/33d0e03dbd94fc6797693b84fcafb5040ea399d0/app/javascript/readingList/components/ItemListItem.jsx#L72-L85
# => the `performInitialSearch` function: https://github.com/forem/forem/blob/33d0e03dbd94fc6797693b84fcafb5040ea399d0/app/javascript/searchableItemList/searchableItemList.js#L78
attribute :reactable do |article, params|
user = params[:users][article.user_id]
tags = article.cached_tag_list.to_s.split(", ")
{
path: article.path,
readable_publish_date_string: article.readable_publish_date,
reading_time: article.reading_time,
# TODO: once we switch to PG we should revisit how we handle tags in the
# frontend, we're sending back tags twice in slightly different formats
tag_list: tags,
tags: tags.map { |tag| { name: tag } },
title: article.title,
# NOTE: not using the `NestedUserSerializer` because we don't need the
# the `pro` flag on the frontend, and we also avoid hitting Redis to
# fetch the cached value
user: {
name: user.name,
profile_image_90: user.profile_image_90,
username: user.username
}
}
end
end
end