Class: TagHashtagDataSource

Inherits:
Object
  • Object
show all
Defined in:
app/services/tag_hashtag_data_source.rb

Overview

Used as a data source via HashtagAutocompleteService to provide tag results when looking up a tag slug via markdown or searching for tags via the # autocomplete character.

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/services/tag_hashtag_data_source.rb', line 7

def self.enabled?
  SiteSetting.tagging_enabled
end

.iconObject



11
12
13
# File 'app/services/tag_hashtag_data_source.rb', line 11

def self.icon
  "tag"
end

.lookup(guardian, slugs) ⇒ Object



39
40
41
42
43
# File 'app/services/tag_hashtag_data_source.rb', line 39

def self.lookup(guardian, slugs)
  DiscourseTagging
    .filter_visible(Tag.where_name(slugs), guardian)
    .map { |tag| tag_to_hashtag_item(tag, guardian) }
end

.search(guardian, term, limit, condition = HashtagAutocompleteService.search_conditions[:contains]) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/tag_hashtag_data_source.rb', line 45

def self.search(
  guardian,
  term,
  limit,
  condition = HashtagAutocompleteService.search_conditions[:contains]
)
  tags_with_counts, _ =
    DiscourseTagging.filter_allowed_tags(
      guardian,
      term: term,
      term_type:
        (
          if condition == HashtagAutocompleteService.search_conditions[:starts_with]
            DiscourseTagging.term_types[:starts_with]
          else
            DiscourseTagging.term_types[:contains]
          end
        ),
      with_context: true,
      limit: limit,
      order_search_results: true,
    )

  TagsController
    .tag_counts_json(tags_with_counts, guardian)
    .take(limit)
    .map do |tag|
      # We want the actual ID here not the `name` as tag_counts_json gives us.
      tag[:id] = tags_with_counts.find { |t| t.name == tag[:name] }.id
      tag_to_hashtag_item(tag, guardian)
    end
end

.search_sort(search_results, _) ⇒ Object



78
79
80
# File 'app/services/tag_hashtag_data_source.rb', line 78

def self.search_sort(search_results, _)
  search_results.sort_by { |item| item.text.downcase }
end

.search_without_term(guardian, limit) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/services/tag_hashtag_data_source.rb', line 82

def self.search_without_term(guardian, limit)
  tags_with_counts, _ =
    DiscourseTagging.filter_allowed_tags(
      guardian,
      with_context: true,
      limit: limit,
      order_popularity: true,
      excluded_tag_names: DiscourseTagging.muted_tags(guardian.user),
    )

  TagsController
    .tag_counts_json(tags_with_counts, guardian)
    .take(limit)
    .map { |tag| tag_to_hashtag_item(tag, guardian) }
end

.typeObject



15
16
17
# File 'app/services/tag_hashtag_data_source.rb', line 15

def self.type
  "tag"
end