diff --git a/app/assets/stylesheets/api/media.scss b/app/assets/stylesheets/api/media.scss new file mode 100644 index 00000000000..e3c4fac3c72 --- /dev/null +++ b/app/assets/stylesheets/api/media.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Api::Media controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/media_controller.rb b/app/controllers/api/media_controller.rb new file mode 100644 index 00000000000..d5a0a124a57 --- /dev/null +++ b/app/controllers/api/media_controller.rb @@ -0,0 +1,8 @@ +class Api::MediaController < ApiController + before_action :doorkeeper_authorize! + respond_to :json + + def create + @media = MediaAttachment.create!(account: current_user.account, file: params[:file]) + end +end diff --git a/app/helpers/api/media_helper.rb b/app/helpers/api/media_helper.rb new file mode 100644 index 00000000000..ecaa91e7b7c --- /dev/null +++ b/app/helpers/api/media_helper.rb @@ -0,0 +1,2 @@ +module Api::MediaHelper +end diff --git a/app/models/account.rb b/app/models/account.rb index bcd6c1eba65..8e0283ca162 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -31,6 +31,8 @@ class Account < ApplicationRecord has_many :following, through: :active_relationships, source: :target_account has_many :followers, through: :passive_relationships, source: :account + has_many :media_attachments, dependent: :destroy + MENTION_RE = /(?:^|\s|\.)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i def follow!(other_account) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb new file mode 100644 index 00000000000..af1a4b9af71 --- /dev/null +++ b/app/models/media_attachment.rb @@ -0,0 +1,13 @@ +class MediaAttachment < ApplicationRecord + belongs_to :account, inverse_of: :media_attachments + belongs_to :status, inverse_of: :media_attachments + + has_attached_file :file + validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/ + + validates :account, presence: true + + def local? + self.remote_url.blank? + end +end diff --git a/app/models/status.rb b/app/models/status.rb index 14a698aae81..05ae1a06ed7 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -11,6 +11,7 @@ class Status < ApplicationRecord has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread has_many :mentions, dependent: :destroy + has_many :media_attachments, dependent: :destroy validates :account, presence: true validates :uri, uniqueness: true, unless: 'local?' diff --git a/app/views/api/media/create.rabl b/app/views/api/media/create.rabl new file mode 100644 index 00000000000..23909066d38 --- /dev/null +++ b/app/views/api/media/create.rabl @@ -0,0 +1,3 @@ +object @media +attribute :id +node(:url) { |media| full_asset_url(media.file.url) } diff --git a/config/environments/test.rb b/config/environments/test.rb index fea1c51fdff..1855aadf255 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -41,3 +41,5 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true end + +Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension" diff --git a/config/routes.rb b/config/routes.rb index 7b6b1ab3add..918eb734749 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ Rails.application.routes.draw do end resources :follows, only: [:create] + resources :media, only: [:create] resources :accounts, only: [:show] do collection do diff --git a/db/migrate/20160905150353_create_media_attachments.rb b/db/migrate/20160905150353_create_media_attachments.rb new file mode 100644 index 00000000000..32d0897d864 --- /dev/null +++ b/db/migrate/20160905150353_create_media_attachments.rb @@ -0,0 +1,14 @@ +class CreateMediaAttachments < ActiveRecord::Migration[5.0] + def change + create_table :media_attachments do |t| + t.integer :status_id, null: true, default: nil + t.attachment :file + t.string :remote_url, null: false, default: '' + t.integer :account_id + + t.timestamps + end + + add_index :media_attachments, :status_id + end +end diff --git a/db/schema.rb b/db/schema.rb index b63df0fea47..4a3078e6421 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160826155805) do +ActiveRecord::Schema.define(version: 20160905150353) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -59,6 +59,19 @@ ActiveRecord::Schema.define(version: 20160826155805) do t.index ["account_id", "target_account_id"], name: "index_follows_on_account_id_and_target_account_id", unique: true, using: :btree end + create_table "media_attachments", force: :cascade do |t| + t.integer "status_id" + t.string "file_file_name" + t.string "file_content_type" + t.integer "file_file_size" + t.datetime "file_updated_at" + t.string "remote_url", default: "", null: false + t.integer "account_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["status_id"], name: "index_media_attachments_on_status_id", using: :btree + end + create_table "mentions", force: :cascade do |t| t.integer "account_id" t.integer "status_id" diff --git a/spec/controllers/api/media_controller_spec.rb b/spec/controllers/api/media_controller_spec.rb new file mode 100644 index 00000000000..2f216c1d5a1 --- /dev/null +++ b/spec/controllers/api/media_controller_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe Api::MediaController, type: :controller do + render_views + + let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let(:token) { double acceptable?: true, resource_owner_id: user.id } + + before do + allow(controller).to receive(:doorkeeper_token) { token } + end + + describe 'POST #create' do + before do + post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') } + end + + it 'returns http success' do + expect(response).to have_http_status(:success) + end + + it 'creates a media attachment' do + expect(MediaAttachment.first).to_not be_nil + end + + it 'uploads a file' do + expect(MediaAttachment.first).to have_attached_file(:file) + end + + it 'returns media ID in JSON' do + expect(body_as_json[:id]).to eq MediaAttachment.first.id + end + end +end diff --git a/spec/controllers/api/statuses_controller_spec.rb b/spec/controllers/api/statuses_controller_spec.rb index 15fdbdaffcc..64e201f3cc7 100644 --- a/spec/controllers/api/statuses_controller_spec.rb +++ b/spec/controllers/api/statuses_controller_spec.rb @@ -64,7 +64,7 @@ RSpec.describe Api::StatusesController, type: :controller do end it 'return json with updated attributes' do - hash_body = JSON.parse(response.body).with_indifferent_access + hash_body = body_as_json expect(hash_body[:reblog][:id]).to eq status.id expect(hash_body[:reblog][:reblogs_count]).to eq 1 @@ -92,7 +92,7 @@ RSpec.describe Api::StatusesController, type: :controller do end it 'return json with updated attributes' do - hash_body = JSON.parse(response.body).with_indifferent_access + hash_body = body_as_json expect(hash_body[:id]).to eq status.id expect(hash_body[:favourites_count]).to eq 1 diff --git a/spec/fabricators/media_attachment_fabricator.rb b/spec/fabricators/media_attachment_fabricator.rb new file mode 100644 index 00000000000..42aa5ab023c --- /dev/null +++ b/spec/fabricators/media_attachment_fabricator.rb @@ -0,0 +1,6 @@ +Fabricator(:media_attachment) do + status_id 1 + file "" + remote_url "MyString" + account_id 1 +end diff --git a/spec/fixtures/files/attachment.jpg b/spec/fixtures/files/attachment.jpg new file mode 100644 index 00000000000..f1d40539ac0 Binary files /dev/null and b/spec/fixtures/files/attachment.jpg differ diff --git a/spec/helpers/api/media_helper_spec.rb b/spec/helpers/api/media_helper_spec.rb new file mode 100644 index 00000000000..5813264ad3c --- /dev/null +++ b/spec/helpers/api/media_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Api::MediaHelper. For example: +# +# describe Api::MediaHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Api::MediaHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/media_attachment_spec.rb b/spec/models/media_attachment_spec.rb new file mode 100644 index 00000000000..0a162768452 --- /dev/null +++ b/spec/models/media_attachment_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe MediaAttachment, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index e9c732f6f7f..3c810eb9ebd 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,6 +6,7 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ require 'spec_helper' require 'rspec/rails' require 'webmock/rspec' +require 'paperclip/matchers' ActiveRecord::Migration.maintain_test_schema! WebMock.disable_net_connect! @@ -19,6 +20,7 @@ RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::TestHelpers, type: :view + config.include Paperclip::Shoulda::Matchers end RSpec::Sidekiq.configure do |config| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d6c1dc95b49..8d77b39f325 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,4 +12,16 @@ RSpec.configure do |config| config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end + + config.after(:suite) do + FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"]) + end +end + +def body_as_json + json_str_to_hash(response.body) +end + +def json_str_to_hash(str) + JSON.parse(str).with_indifferent_access end