2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
require 'sanitize'
|
|
|
|
|
2013-05-24 02:26:51 +08:00
|
|
|
class Search
|
|
|
|
|
|
|
|
class GroupedSearchResults
|
2014-09-02 17:15:08 +08:00
|
|
|
include ActiveModel::Serialization
|
2013-05-24 02:26:51 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
|
2017-07-17 23:57:13 +08:00
|
|
|
attr_reader(
|
|
|
|
:type_filter,
|
|
|
|
:posts,
|
|
|
|
:categories,
|
|
|
|
:users,
|
2017-08-25 23:52:18 +08:00
|
|
|
:tags,
|
2019-03-04 17:30:09 +08:00
|
|
|
:groups,
|
2017-07-17 23:57:13 +08:00
|
|
|
:more_posts,
|
|
|
|
:more_categories,
|
|
|
|
:more_users,
|
|
|
|
:term,
|
|
|
|
:search_context,
|
2019-07-02 09:21:52 +08:00
|
|
|
:more_full_page_results,
|
|
|
|
:error
|
2017-07-17 23:57:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
attr_accessor :search_log_id
|
2014-09-02 17:15:08 +08:00
|
|
|
|
2020-07-14 11:05:57 +08:00
|
|
|
BLURB_LENGTH = 200
|
|
|
|
|
|
|
|
def initialize(type_filter:, term:, search_context:, blurb_length: nil, blurb_term: nil)
|
2014-09-02 17:15:08 +08:00
|
|
|
@type_filter = type_filter
|
|
|
|
@term = term
|
2020-07-14 11:05:57 +08:00
|
|
|
@blurb_term = blurb_term || term
|
2014-09-02 17:15:08 +08:00
|
|
|
@search_context = search_context
|
2020-07-14 11:05:57 +08:00
|
|
|
@blurb_length = blurb_length || BLURB_LENGTH
|
2014-09-02 17:15:08 +08:00
|
|
|
@posts = []
|
|
|
|
@categories = []
|
|
|
|
@users = []
|
2017-08-25 23:52:18 +08:00
|
|
|
@tags = []
|
2019-03-04 17:30:09 +08:00
|
|
|
@groups = []
|
2019-07-02 09:21:52 +08:00
|
|
|
@error = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def error=(error)
|
|
|
|
@error = error
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
|
2016-03-18 13:26:20 +08:00
|
|
|
def find_user_data(guardian)
|
|
|
|
if user = guardian.user
|
|
|
|
topics = @posts.map(&:topic)
|
|
|
|
topic_lookup = TopicUser.lookup_for(user, topics)
|
|
|
|
topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
def blurb(post)
|
2020-07-17 16:27:30 +08:00
|
|
|
opts = {
|
|
|
|
term: @blurb_term,
|
|
|
|
blurb_length: @blurb_length
|
|
|
|
}
|
|
|
|
|
|
|
|
if post.post_search_data.version > SearchIndexer::MIN_POST_REINDEX_VERSION
|
|
|
|
opts[:cooked] = post.post_search_data.raw_data
|
|
|
|
opts[:scrub] = false
|
|
|
|
else
|
|
|
|
opts[:cooked] = post.cooked
|
|
|
|
end
|
|
|
|
|
|
|
|
GroupedSearchResults.blurb_for(**opts)
|
2014-09-02 17:15:08 +08:00
|
|
|
end
|
2013-05-24 02:26:51 +08:00
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
def add(object)
|
|
|
|
type = object.class.to_s.downcase.pluralize
|
2013-05-24 02:26:51 +08:00
|
|
|
|
2019-05-07 10:22:37 +08:00
|
|
|
if @type_filter.present? && public_send(type).length == Search.per_filter
|
2017-07-21 00:07:13 +08:00
|
|
|
@more_full_page_results = true
|
2019-05-07 10:22:37 +08:00
|
|
|
elsif !@type_filter.present? && public_send(type).length == Search.per_facet
|
2014-09-02 17:15:08 +08:00
|
|
|
instance_variable_set("@more_#{type}".to_sym, true)
|
2013-05-24 02:26:51 +08:00
|
|
|
else
|
2019-05-07 10:05:58 +08:00
|
|
|
(self.public_send(type)) << object
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-04 14:37:32 +08:00
|
|
|
URI_REGEXP = URI.regexp(%w{http https})
|
|
|
|
|
2020-07-17 16:27:30 +08:00
|
|
|
def self.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true)
|
2015-06-25 03:08:22 +08:00
|
|
|
blurb = nil
|
2020-07-17 16:27:30 +08:00
|
|
|
cooked = SearchIndexer.scrub_html_for_search(cooked) if scrub
|
2018-09-17 16:31:15 +08:00
|
|
|
|
2019-10-31 01:07:16 +08:00
|
|
|
urls = Set.new
|
2020-08-04 14:37:32 +08:00
|
|
|
cooked.scan(URI_REGEXP) { urls << $& }
|
2019-10-31 01:07:16 +08:00
|
|
|
urls.each do |url|
|
2019-11-06 23:32:15 +08:00
|
|
|
begin
|
|
|
|
case File.extname(URI(url).path || "")
|
|
|
|
when Oneboxer::VIDEO_REGEX
|
|
|
|
cooked.gsub!(url, I18n.t("search.video"))
|
|
|
|
when Oneboxer::AUDIO_REGEX
|
|
|
|
cooked.gsub!(url, I18n.t("search.audio"))
|
|
|
|
end
|
|
|
|
rescue URI::InvalidURIError
|
2019-10-31 21:13:24 +08:00
|
|
|
end
|
2019-10-31 01:07:16 +08:00
|
|
|
end
|
|
|
|
|
2015-06-25 03:08:22 +08:00
|
|
|
if term
|
2020-07-14 11:05:57 +08:00
|
|
|
if term =~ Regexp.new(Search::PHRASE_MATCH_REGEXP_PATTERN)
|
|
|
|
term = Regexp.last_match[1]
|
2019-03-26 17:01:19 +08:00
|
|
|
end
|
|
|
|
|
2020-07-14 11:05:57 +08:00
|
|
|
blurb = TextHelper.excerpt(cooked, term,
|
2019-10-31 21:32:42 +08:00
|
|
|
radius: blurb_length / 2
|
2019-03-26 17:01:19 +08:00
|
|
|
)
|
2015-06-25 03:08:22 +08:00
|
|
|
end
|
2018-09-17 16:31:15 +08:00
|
|
|
|
2019-10-31 21:32:42 +08:00
|
|
|
blurb = TextHelper.truncate(cooked, length: blurb_length) if blurb.blank?
|
2015-06-25 03:08:22 +08:00
|
|
|
Sanitize.clean(blurb)
|
|
|
|
end
|
2013-05-24 02:26:51 +08:00
|
|
|
end
|
|
|
|
|
2014-02-17 11:34:14 +08:00
|
|
|
end
|