Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class Settings::ImportsController < ApplicationController | ||
layout 'admin' | ||
|
||
before_action :authenticate_user! | ||
before_action :set_account | ||
|
||
def show | ||
@import = Import.new | ||
end | ||
|
||
def create | ||
@import = Import.new(import_params) | ||
@import.account = @account | ||
|
||
if @import.save | ||
ImportWorker.perform_async(@import.id) | ||
redirect_to settings_import_path, notice: I18n.t('imports.success') | ||
else | ||
render action: :show | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_account | ||
@account = current_user.account | ||
end | ||
|
||
def import_params | ||
params.require(:import).permit(:data, :type) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class Import < ApplicationRecord | ||
self.inheritance_column = false | ||
|
||
enum type: [:following, :blocking] | ||
|
||
belongs_to :account | ||
|
||
FILE_TYPES = ['text/plain', 'text/csv'].freeze | ||
|
||
has_attached_file :data, url: '/system/:hash.:extension', hash_secret: ENV.fetch('PAPERCLIP_SECRET') | ||
validates_attachment_content_type :data, content_type: FILE_TYPES | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
- content_for :page_title do | ||
= t('settings.import') | ||
|
||
%p.hint= t('imports.preface') | ||
|
||
= simple_form_for @import, url: settings_import_path do |f| | ||
= f.input :type, collection: Import.types.keys, wrapper: :with_label, include_blank: false, label_method: lambda { |type| I18n.t("imports.types.#{type}") } | ||
= f.input :data, wrapper: :with_label, hint: t('simple_form.hints.imports.data') | ||
|
||
.actions | ||
= f.button :button, t('imports.upload'), type: :submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'csv' | ||
|
||
class ImportWorker | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options retry: false | ||
|
||
def perform(import_id) | ||
import = Import.find(import_id) | ||
|
||
case import.type | ||
when 'blocking' | ||
process_blocks(import) | ||
when 'following' | ||
process_follows(import) | ||
end | ||
|
||
import.destroy | ||
end | ||
|
||
private | ||
|
||
def process_blocks(import) | ||
from_account = import.account | ||
|
||
CSV.foreach(import.data.path) do |row| | ||
next if row.size != 1 | ||
|
||
begin | ||
target_account = FollowRemoteAccountService.new.call(row[0]) | ||
next if target_account.nil? | ||
BlockService.new.call(from_account, target_account) | ||
rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError | ||
next | ||
end | ||
end | ||
end | ||
|
||
def process_follows(import) | ||
from_account = import.account | ||
|
||
CSV.foreach(import.data.path) do |row| | ||
next if row.size != 1 | ||
|
||
begin | ||
FollowService.new.call(from_account, row[0]) | ||
rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError | ||
next | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateImports < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :imports do |t| | ||
t.integer :account_id, null: false | ||
t.integer :type, null: false | ||
t.boolean :approved | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20170330164118_add_attachment_data_to_imports.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AddAttachmentDataToImports < ActiveRecord::Migration | ||
def self.up | ||
change_table :imports do |t| | ||
t.attachment :data | ||
end | ||
end | ||
|
||
def self.down | ||
remove_attachment :imports, :data | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fabricator(:import) do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Import, type: :model do | ||
|
||
end |