aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/retweet.rb
blob: cefd901cabccca157bf025c83682d098d1cd4114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Retweet < ActiveRecord::Base
  belongs_to :tweet
  belongs_to :user

  # Registers retweet event in bulk from an array of Streaming API messages.
  # This doesn't update Tweet#reactions_count.
  #
  # @param [Array] array An array of Streaming API messages.
  def self.create_bulk_from_json(array)
    return if array.empty?

    objects = array.map do |json|
      {
        id: json[:id],
        user_id: json[:user][:id],
        tweet_id: json[:retweeted_status][:id]
      }
    end

    self.import(objects.first.keys, objects.map(&:values), ignore: true)
  end

  # Unregisters retweet events in bulk from array of Streaming API's delete events.
  # This doesn't update Tweet#reactions_count.
  #
  # @param [Array] array An array of Streaming API delete events.
  def self.delete_bulk_from_json(array)
    self.where(id: array.map {|json| json[:delete][:status][:id] }).delete_all
  end
end