Class: TopicLink

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/topic_link.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.counts_for(guardian, topic, posts) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/topic_link.rb', line 71

def self.counts_for(guardian, topic, posts)
  return {} if posts.blank?

  # Sam: this is not tidy in AR and also happens to be a critical path
  # for topic view
  builder =
    DB.build(
      "SELECT
                    l.post_id,
                    l.url,
                    l.clicks,
                    COALESCE(t.title, l.title) AS title,
                    l.internal,
                    l.reflection,
                    l.domain
            FROM topic_links l
            LEFT JOIN topics t ON t.id = l.link_topic_id
            LEFT JOIN categories AS c ON c.id = t.category_id
            /*left_join*/
            /*where*/
            ORDER BY reflection ASC, clicks DESC",
    )

  builder.where("t.deleted_at IS NULL")
  builder.where(
    "COALESCE(t.archetype, 'regular') <> :archetype",
    archetype: Archetype.private_message,
  )

  if guardian.authenticated?
    builder.left_join(
      "topic_users AS tu ON (t.id = tu.topic_id AND tu.user_id = #{guardian.user.id.to_i})",
    )
    builder.where(
      "COALESCE(tu.notification_level,1) > :muted",
      muted: TopicUser.notification_levels[:muted],
    )
  end

  # not certain if pluck is right, cause it may interfere with caching
  builder.where("l.post_id in (:post_ids)", post_ids: posts.map(&:id))
  builder.secure_category(guardian.secure_category_ids)

  result = {}
  builder.query.each do |l|
    result[l.post_id] ||= []
    result[l.post_id] << {
      url: l.url,
      clicks: l.clicks,
      title: l.title,
      internal: l.internal,
      reflection: l.reflection,
    }
  end
  result
end


159
160
161
# File 'app/models/topic_link.rb', line 159

def self.crawl_link_title(topic_link_id)
  Jobs.enqueue(:crawl_topic_link, topic_link_id: topic_link_id)
end

.duplicate_lookup(topic) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/topic_link.rb', line 167

def self.duplicate_lookup(topic)
  results =
    TopicLink
      .includes(:post, :user)
      .joins(:post, :user)
      .where("posts.id IS NOT NULL AND users.id IS NOT NULL")
      .where(topic_id: topic.id, reflection: false)
      .last(200)

  lookup = {}
  results.each do |tl|
    normalized = tl.url.downcase.sub(%r{\Ahttps?://}, "").sub(%r{/\z}, "")
    lookup[normalized] = {
      domain: tl.domain,
      username: tl.user.username_lower,
      posted_at: tl.post.created_at,
      post_number: tl.post.post_number,
    }
  end

  lookup
end

.extract_from(post) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/topic_link.rb', line 128

def self.extract_from(post)
  return if post.blank? || post.whisper? || post.user_id.blank? || post.deleted_at.present?

  current_urls = []
  reflected_ids = []

  PrettyText
    .extract_links(post.cooked)
    .map do |u|
      uri = UrlHelper.relaxed_parse(u.url)
      [u, uri]
    end
    .reject { |_, p| p.nil? || "mailto" == p.scheme }
    .uniq { |_, p| p }
    .each do |link, parsed|
      TopicLink.transaction do
        begin
          url, reflected_id = self.ensure_entry_for(post, link, parsed)
          current_urls << url unless url.nil?
          reflected_ids << reflected_id unless reflected_id.nil?
        rescue URI::Error
          # if the URI is invalid, don't store it.
        rescue ActionController::RoutingError
          # If we can't find the route, no big deal
        end
      end
    end

  self.cleanup_entries(post, current_urls, reflected_ids)
end

.max_domain_lengthObject



6
7
8
# File 'app/models/topic_link.rb', line 6

def self.max_domain_length
  100
end

.max_url_lengthObject



10
11
12
# File 'app/models/topic_link.rb', line 10

def self.max_url_length
  500
end

.topic_map(guardian, topic_id) ⇒ Object



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
# File 'app/models/topic_link.rb', line 37

def self.topic_map(guardian, topic_id)
  # Sam: complicated reports are really hard in AR
  builder = DB.build(<<~SQL)
    SELECT ftl.url,
           COALESCE(ft.title, ftl.title) AS title,
           ftl.link_topic_id,
           ftl.reflection,
           ftl.internal,
           ftl.domain,
           MIN(ftl.user_id) AS user_id,
           SUM(clicks) AS clicks
    FROM topic_links AS ftl
    LEFT JOIN topics AS ft ON ftl.link_topic_id = ft.id
    LEFT JOIN categories AS c ON c.id = ft.category_id
    /*where*/
    GROUP BY ftl.url, ft.title, ftl.title, ftl.link_topic_id, ftl.reflection, ftl.internal, ftl.domain
    ORDER BY clicks DESC, count(*) DESC
    LIMIT 50
  SQL

  builder.where("ftl.topic_id = :topic_id", topic_id: topic_id)
  builder.where("ft.deleted_at IS NULL")
  builder.where("ftl.extension IS NULL OR ftl.extension NOT IN ('png','jpg','gif')")
  builder.where(
    "COALESCE(ft.archetype, 'regular') <> :archetype",
    archetype: Archetype.private_message,
  )
  builder.where("clicks > 0")

  builder.secure_category(guardian.secure_category_ids)

  builder.query
end

Instance Method Details



163
164
165
# File 'app/models/topic_link.rb', line 163

def crawl_link_title
  TopicLink.crawl_link_title(id)
end

Make sure a topic can’t link to itself



33
34
35
# File 'app/models/topic_link.rb', line 33

def link_to_self
  errors.add(:base, "can't link to the same topic") if (topic_id == link_topic_id)
end