about summary refs log tree commit diff
path: root/app/services/import_service.rb
blob: 248178e8cd0797c737daa06bacb4a9a4768200f5 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
127
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# frozen_string_literal: true

require 'csv'

class ImportService < BaseService
  include RoutingHelper
  include JsonLdHelper

  ROWS_PROCESSING_LIMIT = 20_000
  CONTENT_TYPES = %w(text/bbcode+markdown text/markdown text/bbcode text/html text/plain).freeze
  VISIBILITIES = [:public, :unlisted, :private, :direct, :limited].freeze
  IMPORT_STATUS_ATTRIBUTES = [
    'id',
    'content_type',
    'spoiler_text',
    'text',
    'footer',
    'in_reply_to_id',
    'reply',
    'reblog_of_id',
    'created_at',
    'conversation_id',
    'sensitive',
    'language',
    'local_only',
    'visibility',
  ].freeze

  def call(import)
    @import  = import
    @account = @import.account

    case @import.type
    when 'following'
      import_follows!
    when 'blocking'
      import_blocks!
    when 'muting'
      import_mutes!
    when 'domain_blocking'
      import_domain_blocks!
    when 'statuses'
      import_statuses!
    end
  end

  private

  def import_follows!
    parse_import_data!(['Account address'])
    import_relationships!('follow', 'unfollow', @account.following, follow_limit, reblogs: 'Show boosts')
  end

  def import_blocks!
    parse_import_data!(['Account address'])
    import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
  end

  def import_mutes!
    parse_import_data!(['Account address'])
    import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT, notifications: 'Hide notifications')
  end

  def import_domain_blocks!
    parse_import_data!(['#domain'])
    items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row['#domain'].strip }

    if @import.overwrite?
      presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }

      @account.domain_blocks.find_each do |domain_block|
        if presence_hash[domain_block.domain]
          items.delete(domain_block.domain)
        else
          @account.unblock_domain!(domain_block.domain)
        end
      end
    end

    items.each do |domain|
      @account.block_domain!(domain)
    end

    AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
      [@account.id, domain]
    end
  end

  def import_statuses!
    parse_import_data_json!
    return if @data.nil?
    if @import.overwrite?
      @account.statuses.without_reblogs.reorder(nil).find_in_batches do |statuses|
        BatchedRemoveStatusService.new.call(statuses)
      end
    end
    return import_activitypub if @data.kind_of?(Hash) && @data['orderedItems'].present?
    return unless @data.kind_of?(Array)
    import_json_statuses
  end

  def import_json_statuses
    @account.user.vars['_bangtags:disable'] = true
    @account.user.save

    @data.each do |json|
      # skip if invalid status object
      next if json.nil?
      next unless json.kind_of?(Hash)
      json.slice!(*IMPORT_STATUS_ATTRIBUTES)
      json.compact!
      next if json.blank?

      # skip if missing reblog
      unless json['reblog_of_id'].nil?
        json['reblog_of_id'] = json['reblog_of_id'].to_i
        next unless (json['reblog_of_id'] != 0 ? Status.where(id: json['reblog_of_id']).exists? : false)
      end

      # convert iso8601 strings to DateTime object
      json['created_at'] = json['created_at'].kind_of?(String) ? DateTime.iso8601(json['created_at']).utc : Time.now.utc

      if json['id'].blank?
        json['id'] = nil
      else
        # make sure id is an integer
        status_id = json['id'].to_i
        json['id'] = status_id != 0 ? status_id : nil

        # check for duplicate
        existing_status = Status.find_by_id(json['id'])
        unless existing_status.nil?
          # skip if duplicate
          next if (json['created_at'] - existing_status.created_at).abs < 1
          # else drop the conflicting id value
          json['id'] = nil
        end
      end

      # ensure correct values & value types
      json['content_type'] = 'text/plain' unless CONTENT_TYPES.include?(json['content_type'])
      json['spoiler_text'] = '' unless json['spoiler_text'].kind_of?(String)
      json['text'] = '' unless json['text'].kind_of?(String)
      json['footer'] = nil unless json['footer'].kind_of?(String)
      json['reply'] = [true, 1, "1"].include?(json['reply'])
      json['in_reply_to_id'] = json['in_reply_to_id'].to_i unless json['in_reply_to_id'].nil?
      json['conversation_id'] = json['conversation_id'].to_i unless json['conversation_id'].nil?
      json['sensitive'] = [true, 1, "1"].include?(json['sensitive'])
      json['language'] = 'en' unless json['language'].kind_of?(String) && json['language'].length > 1
      json['language'] = ISO_639.find(json['language'])&.alpha2 || @account.user_default_language&.presence || 'en'
      json['local_only'] = @account.user_always_local_only? || [true, 1, "1"].include?(json['local_only'])
      json['visibility'] = VISIBILITIES[json['visibility'].to_i] || :unlisted
      json['imported'] = true

      # drop a nonexistant conversation id
      unless (json['conversation_id'] != 0 ? Conversation.where(id: json['conversation_id']).exists? : false)
        json['conversation_id'] = nil
      end

      # nullify a missing reply
      unless (json['in_reply_to_id'] != 0 ? Status.where(id: json['in_reply_to_id']).exists? : false)
        json['in_reply_to_id'] = nil
      end

      ApplicationRecord.transaction do
        status = @account.statuses.create!(json.compact.symbolize_keys)
        process_hashtags_service.call(status)
        process_mentions_service.call(status, skip_notify: true)
      end
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound, Mastodon::ValidationError => e
      Rails.logger.error "Error importing status (JSON): #{e}"
      nil
    end

    @account.user.vars.delete('_bangtags:disable')
    @account.user.save
  end

  def import_activitypub
    account_uri = ActivityPub::TagManager.instance.uri_for(@account)
    followers_uri = account_followers_url(@account)

    @data["orderedItems"].each do |activity|
      next if activity['object'].blank?
      next unless %w(Create Announce).include?(activity['type'])

      object = activity['object']
      activity['actor'] = account_uri

      activity['to'] = if activity['to'].kind_of?(Array)
                         activity['to'].uniq.map { |to| to.end_with?('/followers') ? followers_uri : to }
                       else
                         [account_uri]
                       end

      activity['cc'] = if activity['cc'].kind_of?(Array)
                         activity['cc'].uniq.map { |cc| cc.end_with?('/followers') ? followers_uri : cc }
                       else
                         []
                       end

      case activity['type']
      when 'Announce'
        next unless object.kind_of?(String)
      when 'Note'
        next unless object.kind_of?(Hash)
        object['attributedTo'] = account_uri
        object['to'] = activity['to']
        object['cc'] = activity['cc']
        object.delete('attachment')
      end

      activity = ActivityPub::Activity.factory(activity, @account, imported: true)
      activity&.perform
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound, Mastodon::ValidationError, HTTP::ConnectionError, HTTP::TimeoutError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError => e
      Rails.logger.error "Error importing status (ActivityPub): #{e}"
      nil
    end
  end

  def import_relationships!(action, undo_action, overwrite_scope, limit, extra_fields = {})
    items = @data.take(limit).map { |row| [row['Account address']&.strip, Hash[extra_fields.map { |key, header| [key, row[header]&.strip] }]] }.reject { |(id, _)| id.blank? }

    if @import.overwrite?
      presence_hash = items.each_with_object({}) { |(id, extra), mapping| mapping[id] = [true, extra] }

      overwrite_scope.find_each do |target_account|
        if presence_hash[target_account.acct]
          items.delete(target_account.acct)
          extra = presence_hash[target_account.acct][1]
          Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, extra)
        else
          Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
        end
      end
    end

    Import::RelationshipWorker.push_bulk(items) do |acct, extra|
      [@account.id, acct, action, extra]
    end
  end

  def parse_import_data!(default_headers)
    data = CSV.parse(import_data, headers: true)
    data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
    @data = data.reject(&:blank?)
  rescue CSV::MalformedCSVError
    @data = nil
  end

  def parse_import_data_json!
    @data = Oj.load(import_data, mode: :strict)
  rescue Oj::ParseError
    @data = []
  end

  def import_data
    Paperclip.io_adapters.for(@import.data).read
  end

  def follow_limit
    FollowLimitValidator.limit_for_account(@account)
  end

  def process_mentions_service
    ProcessMentionsService.new
  end

  def process_hashtags_service
    ProcessHashtagsService.new
  end
end