Class: QuotedPost

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

Class Method Summary collapse

Class Method Details

.extract_from(post) ⇒ Object

NOTE we already have a path that does this for topic links,

however topic links exclude quotes and links within a topic
we are double parsing this fragment, this may be worth optimising later


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'app/models/quoted_post.rb', line 10

def self.extract_from(post)
  doc = Nokogiri::HTML5.fragment(post.cooked)

  uniq = {}

  doc
    .css("aside.quote[data-topic]")
    .each do |a|
      topic_id = a["data-topic"].to_i
      post_number = a["data-post"].to_i

      next if topic_id == 0 || post_number == 0
      next if uniq[[topic_id, post_number]]
      next if post.topic_id == topic_id && post.post_number == post_number

      uniq[[topic_id, post_number]] = true
    end

  if uniq.length == 0
    DB.exec("DELETE FROM quoted_posts WHERE post_id = :post_id", post_id: post.id)
  else
    args = {
      post_id: post.id,
      topic_ids: uniq.keys.map(&:first),
      post_numbers: uniq.keys.map(&:second),
    }

    DB.exec(<<~SQL, args)
      INSERT INTO quoted_posts (post_id, quoted_post_id, created_at, updated_at)
      SELECT :post_id, p.id, current_timestamp, current_timestamp
      FROM posts p
      JOIN (
        SELECT
          unnest(ARRAY[:topic_ids]) topic_id,
          unnest(ARRAY[:post_numbers]) post_number
      ) X ON X.topic_id = p.topic_id AND X.post_number = p.post_number
      LEFT JOIN quoted_posts q on q.post_id = :post_id AND q.quoted_post_id = p.id
      WHERE q.id IS NULL
    SQL

    DB.exec(<<~SQL, args)
      DELETE FROM quoted_posts
      WHERE post_id = :post_id
      AND id IN (
        SELECT q1.id FROM quoted_posts q1
        LEFT JOIN posts p1 ON p1.id = q1.quoted_post_id
        LEFT JOIN (
          SELECT
            unnest(ARRAY[:topic_ids]) topic_id,
            unnest(ARRAY[:post_numbers]) post_number
        ) X on X.topic_id = p1.topic_id AND X.post_number = p1.post_number
        WHERE q1.post_id = :post_id AND X.topic_id IS NULL
      )
    SQL
  end

  # simplest place to add this code
  reply_quoted = false

  if post.reply_to_post_number
    reply_post_id =
      Post.where(topic_id: post.topic_id, post_number: post.reply_to_post_number).pick(:id)
    reply_quoted =
      reply_post_id.present? &&
        QuotedPost.where(post_id: post.id, quoted_post_id: reply_post_id).count > 0
  end

  post.update_columns(reply_quoted: reply_quoted) if reply_quoted != post.reply_quoted
end