Make syntax compliant with the pronto configuration
This commit is contained in:
parent
71d324a8e4
commit
50e034769f
20 changed files with 85 additions and 70 deletions
|
|
@ -8,25 +8,21 @@ module Api
|
|||
protected
|
||||
|
||||
rescue_from Exception do |e|
|
||||
pp e
|
||||
logger.error e.message
|
||||
render json: {error: e.message}, status: 500
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
pp e
|
||||
logger.error e.message
|
||||
render json: {error: I18n.t("api.error.not_found")}, status: 404
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordInvalid do |e|
|
||||
pp e
|
||||
logger.error e.message
|
||||
render json: {error: e.to_s}, status: 400
|
||||
end
|
||||
|
||||
rescue_from ActionController::ParameterMissing do |e|
|
||||
pp e
|
||||
logger.error e.message
|
||||
render json: {
|
||||
error: I18n.t("api.error.wrong_parameters"),
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V0
|
||||
class CommentsController < Api::V0::BaseController
|
||||
before_action only: %i(create destroy) do
|
||||
require_access_token %w(read write)
|
||||
before_action only: %i[reate destroy] do
|
||||
require_access_token %w[read write]
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V0
|
||||
class ConversationsController < Api::V0::BaseController
|
||||
include ConversationsHelper
|
||||
|
||||
before_action only: %i(create index show) do
|
||||
require_access_token %w(read)
|
||||
before_action only: %i[create index show] do
|
||||
require_access_token %w[read]
|
||||
end
|
||||
|
||||
before_action only: %i(create destroy) do
|
||||
require_access_token %w(read write)
|
||||
before_action only: %i[create destroy] do
|
||||
require_access_token %w[read write]
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
|
|
@ -16,14 +18,8 @@ module Api
|
|||
end
|
||||
|
||||
def index
|
||||
filter = {}
|
||||
if params[:only_after] then
|
||||
filter["only_after"] = params[:only_after]
|
||||
end
|
||||
if params[:unread] then
|
||||
filter["unread"] = params[:unread]
|
||||
end
|
||||
conversations = conversation_service.all_for_user(filter)
|
||||
params.permit(:only_after, :unread)
|
||||
conversations = conversation_service.all_for_user(params)
|
||||
render json: conversations.map {|x| conversation_as_json(x) }
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V0
|
||||
class LikesController < Api::V0::BaseController
|
||||
before_action only: %i(create destroy) do
|
||||
require_access_token %w(read write)
|
||||
before_action only: %i[create destroy] do
|
||||
require_access_token %w[read write]
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V0
|
||||
class MessagesController < Api::V0::BaseController
|
||||
before_action only: %i(create) do
|
||||
require_access_token %w(read write)
|
||||
before_action only: %i[create] do
|
||||
require_access_token %w[read write]
|
||||
end
|
||||
|
||||
before_action only: %i(index) do
|
||||
require_access_token %w(read)
|
||||
before_action only: %i[index] do
|
||||
require_access_token %w[read]
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
|
|
@ -16,9 +18,7 @@ module Api
|
|||
def create
|
||||
conversation = conversation_service.find!(params[:conversation_id])
|
||||
opts = params.require(:body)
|
||||
message = current_user.build_message(conversation, {
|
||||
:text => opts[:body]
|
||||
})
|
||||
message = current_user.build_message(conversation, text: opts[:body])
|
||||
message.save!
|
||||
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message)
|
||||
render json: message_json(message), status: 201
|
||||
|
|
@ -27,9 +27,7 @@ module Api
|
|||
def index
|
||||
conversation = conversation_service.find!(params[:conversation_id])
|
||||
conversation.set_read(user)
|
||||
render json: conversation.messages.map {
|
||||
|x| message_json(x)
|
||||
}, status: 201
|
||||
render json: conversation.messages.map {|x| message_json(x) }, status: 201
|
||||
end
|
||||
|
||||
def conversation_service
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V0
|
||||
class PostsController < Api::V0::BaseController
|
||||
include PostsHelper
|
||||
|
||||
before_action only: :show do
|
||||
require_access_token %w(read)
|
||||
require_access_token %w[read]
|
||||
end
|
||||
|
||||
before_action only: %i(create destroy) do
|
||||
require_access_token %w(read write)
|
||||
before_action only: %i[create destroy] do
|
||||
require_access_token %w[read write]
|
||||
end
|
||||
|
||||
def show
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V0
|
||||
class StreamsController < Api::V0::BaseController
|
||||
before_action do
|
||||
require_access_token %w(read)
|
||||
require_access_token %w[read]
|
||||
end
|
||||
|
||||
def aspects
|
||||
|
|
|
|||
|
|
@ -1,17 +1,15 @@
|
|||
class ConversationPresenter < BasePresenter
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ConversationPresenter < BasePresenter
|
||||
def as_api_json
|
||||
read =
|
||||
@presentable.conversation_visibilities.length > 0 and
|
||||
read = !@presentable.conversation_visibilities.empty? &&
|
||||
@presentable.conversation_visibilities[0].unread == 0
|
||||
{
|
||||
guid: @presentable.guid,
|
||||
subject: @presentable.subject,
|
||||
created_at: @presentable.created_at,
|
||||
read: read,
|
||||
participants: @presentable.participants.map {
|
||||
|x| PersonPresenter.new(x).as_api_json
|
||||
}
|
||||
participants: @presentable.participants.map {|x| PersonPresenter.new(x).as_api_json }
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
class MessagePresenter < BasePresenter
|
||||
# frozen_string_literal: true
|
||||
|
||||
class MessagePresenter < BasePresenter
|
||||
def as_api_json
|
||||
{
|
||||
guid: @presentable.guid,
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ class PersonPresenter < BasePresenter
|
|||
guid: guid,
|
||||
diaspora_id: diaspora_handle,
|
||||
name: name,
|
||||
avatar: AvatarPresenter.new(@presentable).medium,
|
||||
avatar: AvatarPresenter.new(@presentable).medium
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ConversationService
|
||||
def initialize(user=nil)
|
||||
@user = user
|
||||
|
|
@ -5,14 +7,18 @@ class ConversationService
|
|||
|
||||
def all_for_user(filter={})
|
||||
conversation_filter = {}
|
||||
if !filter[:only_after].nil? then
|
||||
unless filter[:only_after].nil?
|
||||
conversation_filter = \
|
||||
'conversations.created_at >= ?', filter[:only_after]
|
||||
"conversations.created_at >= ?", filter[:only_after]
|
||||
end
|
||||
|
||||
visibility_filter = {person_id: @user.person_id}
|
||||
if filter[:unread] == true then
|
||||
visibility_filter["unread"] = 0
|
||||
visibility_filter = if filter[:unread]
|
||||
{
|
||||
person_id: @user.person_id,
|
||||
unread: 0
|
||||
}
|
||||
else
|
||||
{person_id: @user.person_id}
|
||||
end
|
||||
|
||||
Conversation.where(conversation_filter)
|
||||
|
|
@ -37,7 +43,7 @@ class ConversationService
|
|||
end
|
||||
|
||||
def find!(conversation_guid)
|
||||
conversation = Conversation.find_by!({guid: conversation_guid})
|
||||
conversation = Conversation.find_by!(guid: conversation_guid)
|
||||
@user.conversations
|
||||
.joins(:conversation_visibilities)
|
||||
.where(conversation_visibilities: {
|
||||
|
|
|
|||
|
|
@ -222,14 +222,14 @@ Rails.application.routes.draw do
|
|||
get "podmin", to: "home#podmin"
|
||||
|
||||
api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do
|
||||
match "user", to: "users#show", via: %i(get post)
|
||||
resources :posts, only: %i(show create destroy) do
|
||||
resources :comments, only: %i(create destroy)
|
||||
resources :likes, only: %i(create destroy)
|
||||
match "user", to: "users#show", via: %i[get post]
|
||||
resources :posts, only: %i[show create destroy] do
|
||||
resources :comments, only: %i[create destroy]
|
||||
resources :likes, only: %i[create destroy]
|
||||
end
|
||||
resources :conversations, only: %i(show index create destroy) do
|
||||
resources :conversations, only: %i[show index create destroy] do
|
||||
delete "visibility" => "conversation_visibilities#destroy"
|
||||
resources :messages, only: %i(index create)
|
||||
resources :messages, only: %i[index create]
|
||||
end
|
||||
get "activity" => "streams#activity", :as => "activity_stream"
|
||||
get "stream" => "streams#multi", :as => "stream"
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V0::PostsController do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V0::ConversationsController do
|
||||
|
|
@ -171,7 +173,7 @@ describe Api::V0::ConversationsController do
|
|||
expect(response.status).to eq 404
|
||||
|
||||
expect {
|
||||
Conversation.find({guid: @conversation_guid})
|
||||
Conversation.find(guid: @conversation_guid)
|
||||
}.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V0::LikesController do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V0::MessagesController do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V0::PostsController do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V0::PostsController do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe ConversationPresenter do
|
||||
before do
|
||||
@conversation = FactoryGirl.create(:conversation)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe ConversationService do
|
||||
opts = {
|
||||
subject: "conversation subject",
|
||||
|
|
@ -28,23 +30,19 @@ describe ConversationService do
|
|||
end
|
||||
|
||||
it "returns all conversations" do
|
||||
expect(alice_conversation_service.all_for_user().length).to eq(3)
|
||||
expect(bob_conversation_service.all_for_user().length).to eq(3)
|
||||
expect(alice_conversation_service.all_for_user.length).to eq(2)
|
||||
expect(bob_conversation_service.all_for_user.length).to eq(3)
|
||||
end
|
||||
|
||||
it "returns all unread conversations" do
|
||||
@conversation.conversation_visibilities[0].unread = true
|
||||
@conversation.conversation_visibilities[0].save!
|
||||
conversations = bob_conversation_service.all_for_user(
|
||||
filter={unread: true}
|
||||
)
|
||||
conversations = bob_conversation_service.all_for_user(unread: true)
|
||||
expect(conversations.length).to eq(2)
|
||||
end
|
||||
|
||||
it "returns conversation after a given date" do
|
||||
conversations = bob_conversation_service.all_for_user(
|
||||
filter={only_after: @date}
|
||||
)
|
||||
conversations = bob_conversation_service.all_for_user(only_after: @date)
|
||||
expect(conversations.length).to eq(2)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue