Make syntax compliant with the pronto configuration

This commit is contained in:
Frank Rousseau 2017-10-20 01:13:27 +02:00
parent 71d324a8e4
commit 50e034769f
20 changed files with 85 additions and 70 deletions

View file

@ -8,25 +8,21 @@ module Api
protected protected
rescue_from Exception do |e| rescue_from Exception do |e|
pp e
logger.error e.message logger.error e.message
render json: {error: e.message}, status: 500 render json: {error: e.message}, status: 500
end end
rescue_from ActiveRecord::RecordNotFound do rescue_from ActiveRecord::RecordNotFound do
pp e
logger.error e.message logger.error e.message
render json: {error: I18n.t("api.error.not_found")}, status: 404 render json: {error: I18n.t("api.error.not_found")}, status: 404
end end
rescue_from ActiveRecord::RecordInvalid do |e| rescue_from ActiveRecord::RecordInvalid do |e|
pp e
logger.error e.message logger.error e.message
render json: {error: e.to_s}, status: 400 render json: {error: e.to_s}, status: 400
end end
rescue_from ActionController::ParameterMissing do |e| rescue_from ActionController::ParameterMissing do |e|
pp e
logger.error e.message logger.error e.message
render json: { render json: {
error: I18n.t("api.error.wrong_parameters"), error: I18n.t("api.error.wrong_parameters"),

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
module Api module Api
module V0 module V0
class CommentsController < Api::V0::BaseController class CommentsController < Api::V0::BaseController
before_action only: %i(create destroy) do before_action only: %i[reate destroy] do
require_access_token %w(read write) require_access_token %w[read write]
end end
rescue_from ActiveRecord::RecordNotFound do rescue_from ActiveRecord::RecordNotFound do

View file

@ -1,14 +1,16 @@
# frozen_string_literal: true
module Api module Api
module V0 module V0
class ConversationsController < Api::V0::BaseController class ConversationsController < Api::V0::BaseController
include ConversationsHelper include ConversationsHelper
before_action only: %i(create index show) do before_action only: %i[create index show] do
require_access_token %w(read) require_access_token %w[read]
end end
before_action only: %i(create destroy) do before_action only: %i[create destroy] do
require_access_token %w(read write) require_access_token %w[read write]
end end
rescue_from ActiveRecord::RecordNotFound do rescue_from ActiveRecord::RecordNotFound do
@ -16,14 +18,8 @@ module Api
end end
def index def index
filter = {} params.permit(:only_after, :unread)
if params[:only_after] then conversations = conversation_service.all_for_user(params)
filter["only_after"] = params[:only_after]
end
if params[:unread] then
filter["unread"] = params[:unread]
end
conversations = conversation_service.all_for_user(filter)
render json: conversations.map {|x| conversation_as_json(x) } render json: conversations.map {|x| conversation_as_json(x) }
end end

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
module Api module Api
module V0 module V0
class LikesController < Api::V0::BaseController class LikesController < Api::V0::BaseController
before_action only: %i(create destroy) do before_action only: %i[create destroy] do
require_access_token %w(read write) require_access_token %w[read write]
end end
rescue_from ActiveRecord::RecordNotFound do rescue_from ActiveRecord::RecordNotFound do

View file

@ -1,12 +1,14 @@
# frozen_string_literal: true
module Api module Api
module V0 module V0
class MessagesController < Api::V0::BaseController class MessagesController < Api::V0::BaseController
before_action only: %i(create) do before_action only: %i[create] do
require_access_token %w(read write) require_access_token %w[read write]
end end
before_action only: %i(index) do before_action only: %i[index] do
require_access_token %w(read) require_access_token %w[read]
end end
rescue_from ActiveRecord::RecordNotFound do rescue_from ActiveRecord::RecordNotFound do
@ -16,9 +18,7 @@ module Api
def create def create
conversation = conversation_service.find!(params[:conversation_id]) conversation = conversation_service.find!(params[:conversation_id])
opts = params.require(:body) opts = params.require(:body)
message = current_user.build_message(conversation, { message = current_user.build_message(conversation, text: opts[:body])
:text => opts[:body]
})
message.save! message.save!
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message) Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message)
render json: message_json(message), status: 201 render json: message_json(message), status: 201
@ -27,9 +27,7 @@ module Api
def index def index
conversation = conversation_service.find!(params[:conversation_id]) conversation = conversation_service.find!(params[:conversation_id])
conversation.set_read(user) conversation.set_read(user)
render json: conversation.messages.map { render json: conversation.messages.map {|x| message_json(x) }, status: 201
|x| message_json(x)
}, status: 201
end end
def conversation_service def conversation_service

View file

@ -1,14 +1,16 @@
# frozen_string_literal: true
module Api module Api
module V0 module V0
class PostsController < Api::V0::BaseController class PostsController < Api::V0::BaseController
include PostsHelper include PostsHelper
before_action only: :show do before_action only: :show do
require_access_token %w(read) require_access_token %w[read]
end end
before_action only: %i(create destroy) do before_action only: %i[create destroy] do
require_access_token %w(read write) require_access_token %w[read write]
end end
def show def show

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
module Api module Api
module V0 module V0
class StreamsController < Api::V0::BaseController class StreamsController < Api::V0::BaseController
before_action do before_action do
require_access_token %w(read) require_access_token %w[read]
end end
def aspects def aspects

View file

@ -1,17 +1,15 @@
class ConversationPresenter < BasePresenter # frozen_string_literal: true
class ConversationPresenter < BasePresenter
def as_api_json def as_api_json
read = read = !@presentable.conversation_visibilities.empty? &&
@presentable.conversation_visibilities.length > 0 and
@presentable.conversation_visibilities[0].unread == 0 @presentable.conversation_visibilities[0].unread == 0
{ {
guid: @presentable.guid, guid: @presentable.guid,
subject: @presentable.subject, subject: @presentable.subject,
created_at: @presentable.created_at, created_at: @presentable.created_at,
read: read, read: read,
participants: @presentable.participants.map { participants: @presentable.participants.map {|x| PersonPresenter.new(x).as_api_json }
|x| PersonPresenter.new(x).as_api_json
}
} }
end end
end end

View file

@ -1,5 +1,6 @@
class MessagePresenter < BasePresenter # frozen_string_literal: true
class MessagePresenter < BasePresenter
def as_api_json def as_api_json
{ {
guid: @presentable.guid, guid: @presentable.guid,

View file

@ -15,7 +15,7 @@ class PersonPresenter < BasePresenter
guid: guid, guid: guid,
diaspora_id: diaspora_handle, diaspora_id: diaspora_handle,
name: name, name: name,
avatar: AvatarPresenter.new(@presentable).medium, avatar: AvatarPresenter.new(@presentable).medium
} }
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ConversationService class ConversationService
def initialize(user=nil) def initialize(user=nil)
@user = user @user = user
@ -5,15 +7,19 @@ class ConversationService
def all_for_user(filter={}) def all_for_user(filter={})
conversation_filter = {} conversation_filter = {}
if !filter[:only_after].nil? then unless filter[:only_after].nil?
conversation_filter = \ conversation_filter = \
'conversations.created_at >= ?', filter[:only_after] "conversations.created_at >= ?", filter[:only_after]
end end
visibility_filter = {person_id: @user.person_id} visibility_filter = if filter[:unread]
if filter[:unread] == true then {
visibility_filter["unread"] = 0 person_id: @user.person_id,
end unread: 0
}
else
{person_id: @user.person_id}
end
Conversation.where(conversation_filter) Conversation.where(conversation_filter)
.joins(:conversation_visibilities) .joins(:conversation_visibilities)
@ -37,7 +43,7 @@ class ConversationService
end end
def find!(conversation_guid) def find!(conversation_guid)
conversation = Conversation.find_by!({guid: conversation_guid}) conversation = Conversation.find_by!(guid: conversation_guid)
@user.conversations @user.conversations
.joins(:conversation_visibilities) .joins(:conversation_visibilities)
.where(conversation_visibilities: { .where(conversation_visibilities: {

View file

@ -222,14 +222,14 @@ Rails.application.routes.draw do
get "podmin", to: "home#podmin" get "podmin", to: "home#podmin"
api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do
match "user", to: "users#show", via: %i(get post) match "user", to: "users#show", via: %i[get post]
resources :posts, only: %i(show create destroy) do resources :posts, only: %i[show create destroy] do
resources :comments, only: %i(create destroy) resources :comments, only: %i[create destroy]
resources :likes, only: %i(create destroy) resources :likes, only: %i[create destroy]
end end
resources :conversations, only: %i(show index create destroy) do resources :conversations, only: %i[show index create destroy] do
delete "visibility" => "conversation_visibilities#destroy" delete "visibility" => "conversation_visibilities#destroy"
resources :messages, only: %i(index create) resources :messages, only: %i[index create]
end end
get "activity" => "streams#activity", :as => "activity_stream" get "activity" => "streams#activity", :as => "activity_stream"
get "stream" => "streams#multi", :as => "stream" get "stream" => "streams#multi", :as => "stream"

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe Api::V0::PostsController do describe Api::V0::PostsController do

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe Api::V0::ConversationsController do describe Api::V0::ConversationsController do
@ -171,7 +173,7 @@ describe Api::V0::ConversationsController do
expect(response.status).to eq 404 expect(response.status).to eq 404
expect { expect {
Conversation.find({guid: @conversation_guid}) Conversation.find(guid: @conversation_guid)
}.to raise_error(ActiveRecord::RecordNotFound) }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe Api::V0::LikesController do describe Api::V0::LikesController do

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe Api::V0::MessagesController do describe Api::V0::MessagesController do

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe Api::V0::PostsController do describe Api::V0::PostsController do

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe Api::V0::PostsController do describe Api::V0::PostsController do

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
describe ConversationPresenter do describe ConversationPresenter do
before do before do
@conversation = FactoryGirl.create(:conversation) @conversation = FactoryGirl.create(:conversation)

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
describe ConversationService do describe ConversationService do
opts = { opts = {
subject: "conversation subject", subject: "conversation subject",
@ -10,41 +12,37 @@ describe ConversationService do
describe "#all_for_user" do describe "#all_for_user" do
before do before do
opts = { opts = {
subject: "conversation subject 2", subject: "conversation subject 2",
message: {text: "conversation text 2"}, message: {text: "conversation text 2"},
participant_ids: [bob.person.id] participant_ids: [bob.person.id]
} }
@conversation = alice.build_conversation(opts) @conversation = alice.build_conversation(opts)
@conversation.save! @conversation.save!
sleep(1) sleep(1)
@date = @conversation.created_at @date = @conversation.created_at
opts = { opts = {
subject: "conversation subject 3", subject: "conversation subject 3",
message: {text: "conversation text 3"}, message: {text: "conversation text 3"},
participant_ids: [bob.person.id] participant_ids: [bob.person.id]
} }
@conversation = alice.build_conversation(opts) @conversation = alice.build_conversation(opts)
@conversation.save! @conversation.save!
end end
it "returns all conversations" do it "returns all conversations" do
expect(alice_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) expect(bob_conversation_service.all_for_user.length).to eq(3)
end end
it "returns all unread conversations" do it "returns all unread conversations" do
@conversation.conversation_visibilities[0].unread = true @conversation.conversation_visibilities[0].unread = true
@conversation.conversation_visibilities[0].save! @conversation.conversation_visibilities[0].save!
conversations = bob_conversation_service.all_for_user( conversations = bob_conversation_service.all_for_user(unread: true)
filter={unread: true}
)
expect(conversations.length).to eq(2) expect(conversations.length).to eq(2)
end end
it "returns conversation after a given date" do it "returns conversation after a given date" do
conversations = bob_conversation_service.all_for_user( conversations = bob_conversation_service.all_for_user(only_after: @date)
filter={only_after: @date}
)
expect(conversations.length).to eq(2) expect(conversations.length).to eq(2)
end end
end end