--- /dev/null
+// 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/
--- /dev/null
+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
--- /dev/null
+module Api::MediaHelper
+end
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)
--- /dev/null
+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
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?'
--- /dev/null
+object @media
+attribute :id
+node(:url) { |media| full_asset_url(media.file.url) }
# 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"
end
resources :follows, only: [:create]
+ resources :media, only: [:create]
resources :accounts, only: [:show] do
collection do
--- /dev/null
+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
#
# 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"
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"
--- /dev/null
+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
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
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
--- /dev/null
+Fabricator(:media_attachment) do
+ status_id 1
+ file ""
+ remote_url "MyString"
+ account_id 1
+end
--- /dev/null
+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
--- /dev/null
+require 'rails_helper'
+
+RSpec.describe MediaAttachment, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
require 'spec_helper'
require 'rspec/rails'
require 'webmock/rspec'
+require 'paperclip/matchers'
ActiveRecord::Migration.maintain_test_schema!
WebMock.disable_net_connect!
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::TestHelpers, type: :view
+ config.include Paperclip::Shoulda::Matchers
end
RSpec::Sidekiq.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