Welcome to mirror list, hosted at ThFree Co, Russian Federation.

share_visibility_converter.rb « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7faa1ecad3491e0a9599011b667e92a2af3c61f6 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

#we dont have the environment, and it is not carring over from the migration
unless defined?(Person)
  class Person < ApplicationRecord
    belongs_to :owner, :class_name => 'User'
  end
end

unless defined?(User)
  class User < ApplicationRecord
    serialize :hidden_shareables, Hash
  end
end

unless defined?(Contact)
  class Contact < ApplicationRecord
    belongs_to :user
  end
end

unless defined?(ShareVisibility)
  class ShareVisibility < ApplicationRecord
    belongs_to :contact
  end
end

class ShareVisibilityConverter
  RECENT = 2 # number of weeks to do in the migration
  def self.copy_hidden_share_visibilities_to_users(only_recent = false)
    query = ShareVisibility.where(:hidden => true).includes(:contact => :user)
    query = query.where('share_visibilities.updated_at > ?', RECENT.weeks.ago) if only_recent
    count = query.count
    puts "Updating #{count} records in batches of 1000..."

    batch_count = 1
    query.find_in_batches do |visibilities|
      puts "Updating batch ##{batch_count} of #{(count/1000)+1}..."
      batch_count += 1
      visibilities.each do |visibility|
        begin
          type = visibility.shareable_type
          id = visibility.shareable_id.to_s
          u = visibility.contact.user
          u.hidden_shareables ||= {}
          u.hidden_shareables[type] ||= []
          u.hidden_shareables[type] << id unless u.hidden_shareables[type].include?(id)
          u.save!(:validate => false)
        rescue => e
          puts "ERROR: #{e.message} skipping pv with id: #{visibility.id}"
        end
      end
    end
  end
end