Compare commits
No commits in common. "feature/uploads-api" and "develop" have entirely different histories.
feature/up
...
develop
20 changed files with 4 additions and 288 deletions
|
@ -1,11 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Api
|
|
||||||
module V2
|
|
||||||
class AttachmentsController < RestfulController
|
|
||||||
def searchable_columns
|
|
||||||
[:file]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,37 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
class AttachmentsController < ApplicationController
|
|
||||||
before_action :set_attachment, only: [:destroy]
|
|
||||||
after_action :verify_authorized
|
|
||||||
|
|
||||||
def create
|
|
||||||
@attachment = Attachment.new(attachment_params)
|
|
||||||
authorize @attachment
|
|
||||||
|
|
||||||
respond_to do |format|
|
|
||||||
if @attachment.save
|
|
||||||
format.json { render json: @attachment, status: :created }
|
|
||||||
else
|
|
||||||
format.json { render json: @attachment.errors, status: :unprocessable_entity }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def destroy
|
|
||||||
@attachment.destroy
|
|
||||||
respond_to do |format|
|
|
||||||
format.json { head :no_content }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def set_attachment
|
|
||||||
@attachment = Attachment.find(params[:id])
|
|
||||||
authorize @attachment
|
|
||||||
end
|
|
||||||
|
|
||||||
def attachment_params
|
|
||||||
params.require(:attachment).permit(:id, :file, :attachable_id, :attachable_type)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -15,9 +15,7 @@ class Attachment < ApplicationRecord
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
validates :attachable, presence: true
|
|
||||||
validates_attachment_content_type :file, content_type: Attachable.allowed_types
|
validates_attachment_content_type :file, content_type: Attachable.allowed_types
|
||||||
validates_attachment_size :file, in: 0.megabytes..5.megabytes
|
|
||||||
|
|
||||||
def image?
|
def image?
|
||||||
Attachable.image_types.include?(file.instance.file_content_type)
|
Attachable.image_types.include?(file.instance.file_content_type)
|
||||||
|
|
|
@ -33,8 +33,7 @@ module Attachable
|
||||||
end
|
end
|
||||||
|
|
||||||
def audio_types
|
def audio_types
|
||||||
# .ogg files might be labelled as video
|
['audio/ogg', 'audio/mp3']
|
||||||
['audio/ogg', 'video/ogg', 'audio/mpeg', 'audio/wav', 'video/webm']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def text_types
|
def text_types
|
||||||
|
|
|
@ -69,23 +69,10 @@ class Topic < ApplicationRecord
|
||||||
Pundit.policy_scope(user, maps).map(&:id)
|
Pundit.policy_scope(user, maps).map(&:id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments_json
|
|
||||||
attachments.map do |a|
|
|
||||||
{
|
|
||||||
id: a.id,
|
|
||||||
file_name: a.file_file_name,
|
|
||||||
content_type: a.file_content_type,
|
|
||||||
file_size: a.file_file_size,
|
|
||||||
url: a.file.url
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def as_json(options = {})
|
def as_json(options = {})
|
||||||
super(methods: %i[user_name user_image collaborator_ids])
|
super(methods: %i[user_name user_image collaborator_ids])
|
||||||
.merge(inmaps: inmaps(options[:user]), inmapsLinks: inmaps_links(options[:user]),
|
.merge(inmaps: inmaps(options[:user]), inmapsLinks: inmaps_links(options[:user]),
|
||||||
map_count: map_count(options[:user]), synapse_count: synapse_count(options[:user]),
|
map_count: map_count(options[:user]), synapse_count: synapse_count(options[:user]))
|
||||||
attachments: attachments_json)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_rdf
|
def as_rdf
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
class AttachmentPolicy < ApplicationPolicy
|
|
||||||
class Scope < Scope
|
|
||||||
def resolve
|
|
||||||
scope.where(attachable: TopicPolicy::Scope.new(user, Topic).resolve)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def index?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def create?
|
|
||||||
Pundit.policy(user, record.attachable).update?
|
|
||||||
end
|
|
||||||
|
|
||||||
def destroy?
|
|
||||||
Pundit.policy(user, record.attachable).update?
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,14 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Api
|
|
||||||
module V2
|
|
||||||
class AttachmentSerializer < ApplicationSerializer
|
|
||||||
attributes :id,
|
|
||||||
:file,
|
|
||||||
:attachable_type,
|
|
||||||
:attachable_id,
|
|
||||||
:created_at,
|
|
||||||
:updated_at
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -14,8 +14,7 @@ module Api
|
||||||
def self.embeddable
|
def self.embeddable
|
||||||
{
|
{
|
||||||
user: {},
|
user: {},
|
||||||
metacode: {},
|
metacode: {}
|
||||||
attachments: {}
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@ Metamaps::Application.routes.draw do
|
||||||
root to: 'main#home', via: :get
|
root to: 'main#home', via: :get
|
||||||
get 'request', to: 'main#requestinvite', as: :request
|
get 'request', to: 'main#requestinvite', as: :request
|
||||||
|
|
||||||
resources :attachments, only: %i[create destroy], shallow: true
|
|
||||||
|
|
||||||
namespace :explore do
|
namespace :explore do
|
||||||
get 'active'
|
get 'active'
|
||||||
get 'featured'
|
get 'featured'
|
||||||
|
@ -123,7 +121,6 @@ Metamaps::Application.routes.draw do
|
||||||
|
|
||||||
namespace :api, path: '/api', default: { format: :json } do
|
namespace :api, path: '/api', default: { format: :json } do
|
||||||
namespace :v2, path: '/v2' do
|
namespace :v2, path: '/v2' do
|
||||||
resources :attachments, only: %i[index show]
|
|
||||||
resources :metacodes, only: %i[index show]
|
resources :metacodes, only: %i[index show]
|
||||||
resources :mappings, only: %i[index create show update destroy]
|
resources :mappings, only: %i[index create show update destroy]
|
||||||
resources :maps, only: %i[index create show update destroy] do
|
resources :maps, only: %i[index create show update destroy] do
|
||||||
|
|
|
@ -22,7 +22,6 @@ traits:
|
||||||
searchable: !include traits/searchable.raml
|
searchable: !include traits/searchable.raml
|
||||||
|
|
||||||
schemas:
|
schemas:
|
||||||
attachment: !include schemas/_attachment.json
|
|
||||||
map: !include schemas/_map.json
|
map: !include schemas/_map.json
|
||||||
mapping: !include schemas/_mapping.json
|
mapping: !include schemas/_mapping.json
|
||||||
metacode: !include schemas/_metacode.json
|
metacode: !include schemas/_metacode.json
|
||||||
|
@ -36,7 +35,6 @@ schemas:
|
||||||
# item: !include resourceTypes/item.raml
|
# item: !include resourceTypes/item.raml
|
||||||
# collection: !include resourceTypes/collection.raml
|
# collection: !include resourceTypes/collection.raml
|
||||||
|
|
||||||
/attachments: !include apis/attachments.raml
|
|
||||||
/maps: !include apis/maps.raml
|
/maps: !include apis/maps.raml
|
||||||
/mappings: !include apis/mappings.raml
|
/mappings: !include apis/mappings.raml
|
||||||
/metacodes: !include apis/metacodes.raml
|
/metacodes: !include apis/metacodes.raml
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
get:
|
|
||||||
is: [ searchable: { searchFields: "file" }, orderable, pageable ]
|
|
||||||
securedBy: [ null, token, oauth_2_0 ]
|
|
||||||
responses:
|
|
||||||
200:
|
|
||||||
body:
|
|
||||||
application/json:
|
|
||||||
example: !include ../examples/attachments.json
|
|
||||||
/{id}:
|
|
||||||
get:
|
|
||||||
securedBy: [ null, token, oauth_2_0 ]
|
|
||||||
responses:
|
|
||||||
200:
|
|
||||||
body:
|
|
||||||
application/json:
|
|
||||||
example: !include ../examples/attachment.json
|
|
|
@ -1,10 +0,0 @@
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"id": 1,
|
|
||||||
"file": "https://example.org/file.png",
|
|
||||||
"attachable_type": "Topic",
|
|
||||||
"attachable_id": 187,
|
|
||||||
"created_at": "2017-03-01T05:48:09.533Z",
|
|
||||||
"updated_at": "2017-03-01T05:48:09.533Z"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
{
|
|
||||||
"data": [
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"file": "https://example.org/file.png",
|
|
||||||
"attachable_type": "Topic",
|
|
||||||
"attachable_id": 187,
|
|
||||||
"created_at": "2017-03-01T05:48:09.533Z",
|
|
||||||
"updated_at": "2017-03-01T05:48:09.533Z"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2,
|
|
||||||
"file": "https://example.org/file.docx",
|
|
||||||
"attachable_type": "Message",
|
|
||||||
"attachable_id": 1043,
|
|
||||||
"created_at": "2017-03-01T05:50:19.533Z",
|
|
||||||
"updated_at": "2017-03-01T05:50:19.533Z"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"page": {
|
|
||||||
"current_page": 1,
|
|
||||||
"next_page": 2,
|
|
||||||
"prev_page": 0,
|
|
||||||
"total_pages": 156,
|
|
||||||
"total_count": 312,
|
|
||||||
"per": 2
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
{
|
|
||||||
"name": "Attachment",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"id": {
|
|
||||||
"$ref": "_id.json"
|
|
||||||
},
|
|
||||||
"file": {
|
|
||||||
"format": "uri",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"attachable_type": {
|
|
||||||
"pattern": "(Topic|Message)",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"attachable_id": {
|
|
||||||
"$ref": "_id.json"
|
|
||||||
},
|
|
||||||
"created_at": {
|
|
||||||
"$ref": "_datetimestamp.json"
|
|
||||||
},
|
|
||||||
"updated_at": {
|
|
||||||
"$ref": "_datetimestamp.json"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"id",
|
|
||||||
"file",
|
|
||||||
"attachable_type",
|
|
||||||
"attachable_id",
|
|
||||||
"created_at",
|
|
||||||
"updated_at"
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
{
|
|
||||||
"name": "Attachment Envelope",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"data": {
|
|
||||||
"$ref": "_attachment.json"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"data"
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
{
|
|
||||||
"name": "Attachments",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"data": {
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"$ref": "_attachment.json"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"page": {
|
|
||||||
"$ref": "_page.json"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"data",
|
|
||||||
"page"
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
RSpec.describe 'topics API', type: :request do
|
|
||||||
let(:user) { create(:user, admin: true) }
|
|
||||||
let(:token) { create(:token, user: user).token }
|
|
||||||
let(:attachment) { create(:attachment) }
|
|
||||||
|
|
||||||
it 'GET /api/v2/attachments' do
|
|
||||||
create_list(:attachment, 5)
|
|
||||||
get '/api/v2/attachments', params: { access_token: token }
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
expect(response).to match_json_schema(:attachments)
|
|
||||||
expect(JSON.parse(response.body)['data'].count).to eq 5
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'GET /api/v2/attachments/:id' do
|
|
||||||
get "/api/v2/attachments/#{attachment.id}"
|
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
|
||||||
expect(response).to match_json_schema(:attachment)
|
|
||||||
expect(JSON.parse(response.body)['data']['id']).to eq attachment.id
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'RAML example' do
|
|
||||||
let(:resource) { get_json_example(:attachment) }
|
|
||||||
let(:collection) { get_json_example(:attachments) }
|
|
||||||
|
|
||||||
it 'resource matches schema' do
|
|
||||||
expect(resource).to match_json_schema(:attachment)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'collection matches schema' do
|
|
||||||
expect(collection).to match_json_schema(:attachments)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,6 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
FactoryGirl.define do
|
|
||||||
factory :attachment do
|
|
||||||
association :attachable, factory: :topic
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -9,20 +9,4 @@ RSpec.describe Topic, type: :model do
|
||||||
it { is_expected.to have_many :mappings }
|
it { is_expected.to have_many :mappings }
|
||||||
it { is_expected.to validate_presence_of :permission }
|
it { is_expected.to validate_presence_of :permission }
|
||||||
it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) }
|
it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) }
|
||||||
|
|
||||||
describe 'attachments_json' do
|
|
||||||
let (:attachments) do
|
|
||||||
create_list(:attachment, 1,
|
|
||||||
file_file_name: 'file_name',
|
|
||||||
file_content_type: 'text/plain',
|
|
||||||
file_file_size: '100')
|
|
||||||
end
|
|
||||||
let (:topic) { create(:topic, attachments: attachments) }
|
|
||||||
let(:json) { topic.attachments_json }
|
|
||||||
|
|
||||||
it 'returns correct json' do
|
|
||||||
expect(json.first[:id]).to equal(attachments.first.id)
|
|
||||||
binding.pry
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ RSpec.describe MapPolicy, type: :policy do
|
||||||
context 'private' do
|
context 'private' do
|
||||||
let(:map) { create(:map, permission: :private) }
|
let(:map) { create(:map, permission: :private) }
|
||||||
permissions :show?, :create?, :update?, :destroy? do
|
permissions :show?, :create?, :update?, :destroy? do
|
||||||
it 'denies access' do
|
it 'permits access' do
|
||||||
expect(subject).to_not permit(nil, map)
|
expect(subject).to_not permit(nil, map)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue