From 50e034769f47e0dc0c83a728978a9261d83e16a6 Mon Sep 17 00:00:00 2001 From: Frank Rousseau Date: Fri, 20 Oct 2017 01:13:27 +0200 Subject: [PATCH] Make syntax compliant with the pronto configuration --- app/controllers/api/v0/base_controller.rb | 4 --- app/controllers/api/v0/comments_controller.rb | 6 +++-- .../api/v0/conversations_controller.rb | 20 ++++++-------- app/controllers/api/v0/likes_controller.rb | 6 +++-- app/controllers/api/v0/messages_controller.rb | 18 ++++++------- app/controllers/api/v0/posts_controller.rb | 8 +++--- app/controllers/api/v0/streams_controller.rb | 4 ++- app/presenters/conversation_presenter.rb | 10 +++---- app/presenters/message_presenter.rb | 3 ++- app/presenters/person_presenter.rb | 2 +- app/services/conversation_service.rb | 20 +++++++++----- config/routes.rb | 12 ++++----- .../api/comments_controller_spec.rb | 2 ++ .../api/conversations_controller_spec.rb | 4 ++- spec/integration/api/likes_controller_spec.rb | 2 ++ .../api/messages_controller_spec.rb | 2 ++ spec/integration/api/posts_controller_spec.rb | 2 ++ .../api/streams_controller_spec.rb | 2 ++ spec/presenters/conversation_presenter.rb | 2 ++ spec/services/conversation_service.rb | 26 +++++++++---------- 20 files changed, 85 insertions(+), 70 deletions(-) diff --git a/app/controllers/api/v0/base_controller.rb b/app/controllers/api/v0/base_controller.rb index 1c20f2956..30c4eecbd 100644 --- a/app/controllers/api/v0/base_controller.rb +++ b/app/controllers/api/v0/base_controller.rb @@ -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"), diff --git a/app/controllers/api/v0/comments_controller.rb b/app/controllers/api/v0/comments_controller.rb index 5474ee532..7bb7bf6d0 100644 --- a/app/controllers/api/v0/comments_controller.rb +++ b/app/controllers/api/v0/comments_controller.rb @@ -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 diff --git a/app/controllers/api/v0/conversations_controller.rb b/app/controllers/api/v0/conversations_controller.rb index ff0addc17..cf5d6c69a 100644 --- a/app/controllers/api/v0/conversations_controller.rb +++ b/app/controllers/api/v0/conversations_controller.rb @@ -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 diff --git a/app/controllers/api/v0/likes_controller.rb b/app/controllers/api/v0/likes_controller.rb index 834bf2c80..555e84854 100644 --- a/app/controllers/api/v0/likes_controller.rb +++ b/app/controllers/api/v0/likes_controller.rb @@ -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 diff --git a/app/controllers/api/v0/messages_controller.rb b/app/controllers/api/v0/messages_controller.rb index 2a2f2f9b7..549278192 100644 --- a/app/controllers/api/v0/messages_controller.rb +++ b/app/controllers/api/v0/messages_controller.rb @@ -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 diff --git a/app/controllers/api/v0/posts_controller.rb b/app/controllers/api/v0/posts_controller.rb index 5a2b92818..6393e0e4f 100644 --- a/app/controllers/api/v0/posts_controller.rb +++ b/app/controllers/api/v0/posts_controller.rb @@ -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 diff --git a/app/controllers/api/v0/streams_controller.rb b/app/controllers/api/v0/streams_controller.rb index 5430f8376..f36c3d4ec 100644 --- a/app/controllers/api/v0/streams_controller.rb +++ b/app/controllers/api/v0/streams_controller.rb @@ -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 diff --git a/app/presenters/conversation_presenter.rb b/app/presenters/conversation_presenter.rb index 1d551d8bf..525937982 100644 --- a/app/presenters/conversation_presenter.rb +++ b/app/presenters/conversation_presenter.rb @@ -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 diff --git a/app/presenters/message_presenter.rb b/app/presenters/message_presenter.rb index 5af052b1b..868cbe9ad 100644 --- a/app/presenters/message_presenter.rb +++ b/app/presenters/message_presenter.rb @@ -1,5 +1,6 @@ -class MessagePresenter < BasePresenter +# frozen_string_literal: true +class MessagePresenter < BasePresenter def as_api_json { guid: @presentable.guid, diff --git a/app/presenters/person_presenter.rb b/app/presenters/person_presenter.rb index 1532e1791..eb7f0a785 100644 --- a/app/presenters/person_presenter.rb +++ b/app/presenters/person_presenter.rb @@ -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 diff --git a/app/services/conversation_service.rb b/app/services/conversation_service.rb index 182dd8500..354d30c39 100644 --- a/app/services/conversation_service.rb +++ b/app/services/conversation_service.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ConversationService def initialize(user=nil) @user = user @@ -5,15 +7,19 @@ 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 - end + visibility_filter = if filter[:unread] + { + person_id: @user.person_id, + unread: 0 + } + else + {person_id: @user.person_id} + end Conversation.where(conversation_filter) .joins(:conversation_visibilities) @@ -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: { diff --git a/config/routes.rb b/config/routes.rb index 957b44c69..e74e6968b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/spec/integration/api/comments_controller_spec.rb b/spec/integration/api/comments_controller_spec.rb index bf42e64bf..fb506b4d1 100644 --- a/spec/integration/api/comments_controller_spec.rb +++ b/spec/integration/api/comments_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" describe Api::V0::PostsController do diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb index 4b9b5eb98..07abd992e 100644 --- a/spec/integration/api/conversations_controller_spec.rb +++ b/spec/integration/api/conversations_controller_spec.rb @@ -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 diff --git a/spec/integration/api/likes_controller_spec.rb b/spec/integration/api/likes_controller_spec.rb index d5e8e360c..a306220d8 100644 --- a/spec/integration/api/likes_controller_spec.rb +++ b/spec/integration/api/likes_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" describe Api::V0::LikesController do diff --git a/spec/integration/api/messages_controller_spec.rb b/spec/integration/api/messages_controller_spec.rb index a08b78876..4a5a89bb7 100644 --- a/spec/integration/api/messages_controller_spec.rb +++ b/spec/integration/api/messages_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" describe Api::V0::MessagesController do diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index f4f107146..af56363fe 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" describe Api::V0::PostsController do diff --git a/spec/integration/api/streams_controller_spec.rb b/spec/integration/api/streams_controller_spec.rb index 89896ebee..8c951b3fc 100644 --- a/spec/integration/api/streams_controller_spec.rb +++ b/spec/integration/api/streams_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" describe Api::V0::PostsController do diff --git a/spec/presenters/conversation_presenter.rb b/spec/presenters/conversation_presenter.rb index f78a236d6..0cc1f14c4 100644 --- a/spec/presenters/conversation_presenter.rb +++ b/spec/presenters/conversation_presenter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe ConversationPresenter do before do @conversation = FactoryGirl.create(:conversation) diff --git a/spec/services/conversation_service.rb b/spec/services/conversation_service.rb index 4633813e4..37d26b8ed 100644 --- a/spec/services/conversation_service.rb +++ b/spec/services/conversation_service.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + describe ConversationService do opts = { subject: "conversation subject", @@ -10,41 +12,37 @@ describe ConversationService do describe "#all_for_user" do before do opts = { - subject: "conversation subject 2", - message: {text: "conversation text 2"}, - participant_ids: [bob.person.id] + subject: "conversation subject 2", + message: {text: "conversation text 2"}, + participant_ids: [bob.person.id] } @conversation = alice.build_conversation(opts) @conversation.save! sleep(1) @date = @conversation.created_at opts = { - subject: "conversation subject 3", - message: {text: "conversation text 3"}, - participant_ids: [bob.person.id] + subject: "conversation subject 3", + message: {text: "conversation text 3"}, + participant_ids: [bob.person.id] } @conversation = alice.build_conversation(opts) @conversation.save! 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