aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/favorite.rb
blob: 701b8caebea6d05dd466b1ad7a7a74c75cc7a739 (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
31
class Favorite < ActiveRecord::Base
  belongs_to :tweet
  belongs_to :user

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

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

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

  # Unregisters favorite event in bulk from an array of Streaming API 'unfavorite' events.
  # This method doesn't update Tweet#reactions_count.
  #
  # @param [Array] array An array of Streaming API events.
  def self.delete_bulk_from_json(array)
    array.each do |json|
      self.delete_all(user_id: json[:source][:id], tweet_id: json[:target_object][:id])
    end
  end
end