Class: InlineOneboxer

Inherits:
Object
  • Object
show all
Defined in:
lib/inline_oneboxer.rb

Constant Summary collapse

MIN_TITLE_LENGTH =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls, opts = nil) ⇒ InlineOneboxer

Returns a new instance of InlineOneboxer.



6
7
8
9
# File 'lib/inline_oneboxer.rb', line 6

def initialize(urls, opts = nil)
  @urls = urls
  @opts = opts || {}
end

Class Method Details

.cache_lookup(url) ⇒ Object



19
20
21
# File 'lib/inline_oneboxer.rb', line 19

def self.cache_lookup(url)
  Discourse.cache.read(cache_key(url))
end

.invalidate(url) ⇒ Object



15
16
17
# File 'lib/inline_oneboxer.rb', line 15

def self.invalidate(url)
  Discourse.cache.delete(cache_key(url))
end

.local_handlersObject



23
24
25
# File 'lib/inline_oneboxer.rb', line 23

def self.local_handlers
  @local_handlers ||= {}
end

.lookup(url, opts = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/inline_oneboxer.rb', line 31

def self.lookup(url, opts = nil)
  opts ||= {}
  opts = opts.with_indifferent_access

  unless opts[:skip_cache] || opts[:invalidate]
    cached = cache_lookup(url)
    return cached if cached.present?
  end

  return unless url

  if route = Discourse.route_for(url)
    if route[:controller] == "topics"
      if topic = Oneboxer.local_topic(url, route, opts)
        opts[:skip_cache] = true
        post_number = [route[:post_number].to_i, topic.highest_post_number].min
        if post_number > 1
          opts[:post_number] = post_number
          opts[:post_author] = (topic, post_number)
        end
        return onebox_for(url, topic.title, opts)
      else
        # not permitted to see topic
        return nil
      end
    elsif handler = local_handlers[route[:controller]]
      return handler.call(url, route)
    end
  end

  always_allow = SiteSetting.enable_inline_onebox_on_all_domains
  allowed_domains = SiteSetting.allowed_inline_onebox_domains&.split("|") unless always_allow

  if always_allow || allowed_domains
    uri =
      begin
        URI(url)
      rescue URI::Error
      end

    if uri.present? && uri.hostname.present? &&
         (always_allow || allowed_domains.include?(uri.hostname)) &&
         !Onebox::DomainChecker.is_blocked?(uri.hostname)
      max_redirects = 0 if SiteSetting.block_onebox_on_redirect
      title =
        RetrieveTitle.crawl(
          url,
          max_redirects: max_redirects,
          initial_https_redirect_ignore_limit: SiteSetting.block_onebox_on_redirect,
        )
      title = nil if title && title.length < MIN_TITLE_LENGTH
      return onebox_for(url, title, opts)
    end
  end

  nil
end

.register_local_handler(controller, &handler) ⇒ Object



27
28
29
# File 'lib/inline_oneboxer.rb', line 27

def self.register_local_handler(controller, &handler)
  local_handlers[controller] = handler
end

Instance Method Details

#processObject



11
12
13
# File 'lib/inline_oneboxer.rb', line 11

def process
  @urls.map { |url| InlineOneboxer.lookup(url, @opts) }.compact
end