Skip to content

Commit

Permalink
Merge 7028112 into 2da3340
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Jan 30, 2020
2 parents 2da3340 + 7028112 commit 97f284c
Show file tree
Hide file tree
Showing 35 changed files with 237 additions and 341 deletions.
14 changes: 7 additions & 7 deletions app/controllers/api/v1/aspects_controller.rb
Expand Up @@ -23,7 +23,7 @@ def show
if aspect
render json: aspect_as_json(aspect, true)
else
render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found
render_error 404, "Aspect with provided ID could not be found"
end
end

Expand All @@ -33,32 +33,32 @@ def create
if aspect&.save
render json: aspect_as_json(aspect, true)
else
render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity
render_error 422, "Failed to create the aspect"
end
rescue ActionController::ParameterMissing
render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity
render_error 422, "Failed to create the aspect"
end

def update
aspect = current_user.aspects.where(id: params[:id]).first

if !aspect
render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :not_found
render_error 404, "Failed to update the aspect"
elsif aspect.update!(aspect_params(true))
render json: aspect_as_json(aspect, true)
else
render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity
render_error 422, "Failed to update the aspect"
end
rescue ActionController::ParameterMissing, ActiveRecord::RecordInvalid
render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity
render_error 422, "Failed to update the aspect"
end

def destroy
aspect = current_user.aspects.where(id: params[:id]).first
if aspect&.destroy
head :no_content
else
render json: I18n.t("api.endpoint_errors.aspects.cant_delete"), status: :unprocessable_entity
render_error 422, "Failed to delete the aspect"
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api/v1/base_controller.rb
Expand Up @@ -58,6 +58,10 @@ def render_paged_api_response(page)
render json: page[:data]
end

def render_error(code, message)
render json: {code: code, body: message}, status: code
end

def time_pager(query)
Api::Paging::RestPaginatorBuilder.new(query, request).time_pager(params)
end
Expand Down
14 changes: 7 additions & 7 deletions app/controllers/api/v1/comments_controller.rb
Expand Up @@ -12,18 +12,18 @@ class CommentsController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
end

rescue_from ActiveRecord::RecordInvalid do
render json: I18n.t("api.endpoint_errors.comments.not_allowed"), status: :unprocessable_entity
render_error 422, "User is not allowed to comment"
end

def create
find_post
comment = comment_service.create(params.require(:post_id), params.require(:body))
rescue ActiveRecord::RecordNotFound
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
else
render json: comment_as_json(comment), status: :created
end
Expand All @@ -45,7 +45,7 @@ def destroy
head :no_content
end
rescue ActiveRecord::RecordInvalid
render json: I18n.t("api.endpoint_errors.comments.no_delete"), status: :forbidden
render_error 403, "User not allowed to delete the comment"
end

def report
Expand All @@ -64,18 +64,18 @@ def report
if report.save
head :no_content
else
render json: I18n.t("api.endpoint_errors.comments.duplicate_report"), status: :conflict
render_error 409, "This item already has been reported by this user"
end
end

private

def comment_and_post_validate(post_guid, comment_guid)
if !comment_exists(comment_guid)
render json: I18n.t("api.endpoint_errors.comments.not_found"), status: :not_found
render_error 404, "Comment not found for the given post"
false
elsif !comment_is_for_post(post_guid, comment_guid)
render json: I18n.t("api.endpoint_errors.comments.not_found"), status: :not_found
render_error 404, "Comment not found for the given post"
false
else
true
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/api/v1/contacts_controller.rb
Expand Up @@ -14,7 +14,7 @@ class ContactsController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found
render_error 404, "Aspect with provided ID could not be found"
end

def index
Expand All @@ -34,10 +34,10 @@ def create
if aspect_membership
head :no_content
else
render json: I18n.t("api.endpoint_errors.contacts.cant_create"), status: :unprocessable_entity
render_error 422, "Failed to add user to aspect"
end
rescue ActiveRecord::RecordNotUnique
render json: I18n.t("api.endpoint_errors.contacts.cant_create"), status: :unprocessable_entity
render_error 422, "Failed to add user to aspect"
end

def destroy
Expand All @@ -48,10 +48,10 @@ def destroy
if result && result[:success]
head :no_content
else
render json: I18n.t("api.endpoint_errors.contacts.cant_delete"), status: :unprocessable_entity
render_error 422, "Failed to remove user from aspect"
end
rescue ActiveRecord::RecordNotFound
render json: I18n.t("api.endpoint_errors.contacts.not_found"), status: :not_found
render_error 404, "Aspect or contact on aspect not found"
end

def aspects_membership_service
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/conversations_controller.rb
Expand Up @@ -10,7 +10,7 @@ class ConversationsController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.conversations.not_found"), status: :not_found
render_error 404, "Conversation with provided guid could not be found"
end

def index
Expand Down Expand Up @@ -40,7 +40,7 @@ def create
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, conversation)
render json: conversation_as_json(conversation), status: :created
rescue ActiveRecord::RecordInvalid, ActionController::ParameterMissing, ActiveRecord::RecordNotFound
render json: I18n.t("api.endpoint_errors.conversations.cant_process"), status: :unprocessable_entity
render_error 422, "Couldn’t accept or process the conversation"
end

def destroy
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/api/v1/likes_controller.rb
Expand Up @@ -12,11 +12,11 @@ class LikesController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
end

rescue_from ActiveRecord::RecordInvalid do
render json: I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like"), status: :unprocessable_entity
render_error 422, "User is not allowed to like"
end

def show
Expand All @@ -35,8 +35,9 @@ def create

like_service.create(params[:post_id])
rescue ActiveRecord::RecordInvalid => e
return render json: I18n.t("api.endpoint_errors.likes.like_exists"), status: :unprocessable_entity if
e.message == "Validation failed: Target has already been taken"
if e.message == "Validation failed: Target has already been taken"
return render_error 409, "Like already exists"
end

raise
else
Expand All @@ -51,7 +52,7 @@ def destroy
if success
head :no_content
else
render json: I18n.t("api.endpoint_errors.likes.no_like"), status: :not_found
render_error 410, "Like doesn’t exist"
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/messages_controller.rb
Expand Up @@ -8,7 +8,7 @@ class MessagesController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.conversations.not_found"), status: :not_found
render_error 404, "Conversation with provided guid could not be found"
end

def create
Expand All @@ -19,7 +19,7 @@ def create
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message)
render json: message_json(message), status: :created
rescue ActionController::ParameterMissing
render json: I18n.t("api.endpoint_errors.conversations.cant_process"), status: :unprocessable_entity
render_error 422, "Couldn’t accept or process the conversation"
end

def index
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/api/v1/notifications_controller.rb
Expand Up @@ -8,7 +8,7 @@ class NotificationsController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.notifications.not_found"), status: :not_found
render_error 404, "Notification with provided guid could not be found"
end

def show
Expand All @@ -17,7 +17,7 @@ def show
if notification
render json: NotificationPresenter.new(notification).as_api_json(true)
else
render json: I18n.t("api.endpoint_errors.notifications.not_found"), status: :not_found
render_error 404, "Notification with provided guid could not be found"
end
end

Expand All @@ -31,18 +31,18 @@ def index
end
render_paged_api_response notifications_page
rescue ArgumentError
render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity
render_error 422, "Couldnt process the notifications requestt process the notifications request"
end

def update
read = ActiveModel::Type::Boolean.new.cast(params.require(:read))
if service.update_status_by_guid(params[:id], read)
head :no_content
else
render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity
render_error 422, "Couldnt process the notifications requestt process the notifications request"
end
rescue ActionController::ParameterMissing
render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity
render_error 422, "Couldnt process the notifications requestt process the notifications request"
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/photos_controller.rb
Expand Up @@ -12,7 +12,7 @@ class PhotosController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.photos.not_found"), status: :not_found
render_error 404, "Photo with provided guid could not be found"
end

def index
Expand Down Expand Up @@ -46,7 +46,7 @@ def create

render json: photo_json(photo)
rescue CarrierWave::IntegrityError, ActionController::ParameterMissing, RuntimeError
render json: I18n.t("api.endpoint_errors.photos.failed_create"), status: :unprocessable_entity
render_error 422, "Failed to create the photo"
end

def destroy
Expand All @@ -58,7 +58,7 @@ def destroy
if current_user.retract(photo)
head :no_content
else
render json: I18n.t("api.endpoint_errors.photos.failed_delete"), status: :unprocessable_entity
render_error 422, "Not allowed to delete the photo"
end
end

Expand Down
22 changes: 11 additions & 11 deletions app/controllers/api/v1/post_interactions_controller.rb
Expand Up @@ -10,15 +10,15 @@ class PostInteractionsController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
end

def subscribe
post = find_post
current_user.participate!(post)
head :no_content
rescue ActiveRecord::RecordInvalid
render json: I18n.t("api.endpoint_errors.interactions.cant_subscribe"), status: :unprocessable_entity
render_error 422, "Cant subscribe to this postt subscribe to this post"
end

def hide
Expand All @@ -45,27 +45,27 @@ def report
if report.save
head :no_content
else
render json: I18n.t("api.endpoint_errors.posts.cant_report"), status: :conflict
render_error 409, "Failed to create report on this post"
end
rescue ActionController::ParameterMissing
render json: I18n.t("api.endpoint_errors.posts.cant_report"), status: :unprocessable_entity
render_error 422, "Failed to create report on this post"
end

def vote
post = find_post
begin
post = find_post
poll_vote = poll_service.vote(post.id, params[:poll_answer_id])
rescue ActiveRecord::RecordNotFound
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
return
# This, but not the find_post above should return a 422,
# we just keep poll_vote nil so it goes into the else below
end
poll_vote = poll_service.vote(post.id, params[:poll_answer_id])
if poll_vote
head :no_content
else
render json: I18n.t("api.endpoint_errors.interactions.cant_vote"), status: :unprocessable_entity
render_error 422, "Cant vote on this post"
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound
render json: I18n.t("api.endpoint_errors.interactions.cant_vote"), status: :unprocessable_entity
rescue ActiveRecord::RecordInvalid
render_error 422, "Cant vote on this post"
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/posts_controller.rb
Expand Up @@ -14,7 +14,7 @@ class PostsController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
end

def show
Expand All @@ -31,14 +31,14 @@ def create
@status_message = creation_service.create(creation_params)
render json: PostPresenter.new(@status_message, current_user).as_api_response
rescue StandardError
render json: I18n.t("api.endpoint_errors.posts.failed_create"), status: :unprocessable_entity
render_error 422, "Failed to create the post"
end

def destroy
post_service.destroy(params[:id], private_modify?)
head :no_content
rescue Diaspora::NotMine, Diaspora::NonPublic
render json: I18n.t("api.endpoint_errors.posts.failed_delete"), status: :forbidden
render_error 403, "Not allowed to delete the post"
end

private
Expand Down
10 changes: 6 additions & 4 deletions app/controllers/api/v1/reshares_controller.rb
Expand Up @@ -12,11 +12,11 @@ class ResharesController < Api::V1::BaseController
end

rescue_from ActiveRecord::RecordNotFound do
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
end

rescue_from Diaspora::NonPublic do
render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
render_error 404, "Post with provided guid could not be found"
end

def show
Expand All @@ -34,8 +34,10 @@ def show

def create
reshare = reshare_service.create(params.require(:post_id))
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid, RuntimeError
render plain: I18n.t("reshares.create.error"), status: :unprocessable_entity
rescue ActiveRecord::RecordInvalid
render_error 409, "Reshare already exists"
rescue ActiveRecord::RecordNotFound, RuntimeError
render_error 422, "Failed to reshare"
else
render json: PostPresenter.new(reshare, current_user).as_api_response
end
Expand Down

0 comments on commit 97f284c

Please sign in to comment.