From 0080a2567f279ef320f0c3176b3b0797939a5ac5 Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Fri, 16 Mar 2018 20:34:34 +0200 Subject: [PATCH] StatusMessageController#create: respond 422 when aspect_ids are wrong fixes #3862 closes #7742 --- Changelog.md | 1 + app/controllers/status_messages_controller.rb | 2 ++ app/services/status_message_creation_service.rb | 17 +++++++++++------ config/locales/diaspora/en.yml | 1 + 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Changelog.md b/Changelog.md index 427af0aba..3820b4832 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ ## Bug fixes * Prefill conversation form on contacts page only with mutual contacts [#7744](https://github.com/diaspora/diaspora/pull/7744) * Fix profiles sometimes not loading properly in background tabs [#7740](https://github.com/diaspora/diaspora/pull/7740) +* Show error message when creating posts with invalid aspects [#7742](https://github.com/diaspora/diaspora/pull/7742) ## Features diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 20d4c1c97..432f7d7c7 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -49,6 +49,8 @@ class StatusMessagesController < ApplicationController format.mobile { redirect_to stream_path } format.json { render json: PostPresenter.new(status_message, current_user), status: 201 } end + rescue StatusMessageCreationService::BadAspectsIDs + render status: 422, plain: I18n.t("status_messages.bad_aspects") rescue StandardError => error handle_create_error(error) end diff --git a/app/services/status_message_creation_service.rb b/app/services/status_message_creation_service.rb index c35a7176d..5f53c440e 100644 --- a/app/services/status_message_creation_service.rb +++ b/app/services/status_message_creation_service.rb @@ -9,15 +9,16 @@ class StatusMessageCreationService def create(params) build_status_message(params).tap do |status_message| + load_aspects(params[:aspect_ids]) unless status_message.public? add_attachments(status_message, params) status_message.save - process(status_message, params[:aspect_ids], params[:services]) + process(status_message, params[:services]) end end private - attr_reader :user + attr_reader :user, :aspects def build_status_message(params) public = params[:public] || false @@ -54,13 +55,17 @@ class StatusMessageCreationService end end - def process(status_message, aspect_ids, services) - add_to_streams(status_message, aspect_ids) unless status_message.public + def load_aspects(aspect_ids) + @aspects = user.aspects_from_ids(aspect_ids) + raise BadAspectsIDs if aspects.empty? + end + + def process(status_message, services) + add_to_streams(status_message) unless status_message.public? dispatch(status_message, services) end - def add_to_streams(status_message, aspect_ids) - aspects = user.aspects_from_ids(aspect_ids) + def add_to_streams(status_message) user.add_to_streams(status_message, aspects) status_message.photos.each {|photo| user.add_to_streams(photo, aspects) } end diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 6c6c3f394..d9e887a91 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -1122,6 +1122,7 @@ en: new: mentioning: "Mentioning: %{person}" too_long: "Please make your status message fewer than %{count} characters. Right now it is %{current_length} characters" + bad_aspects: "Provided aspects IDs aren't applicable (non-existent or not owned)" stream_helper: no_more_posts: "You have reached the end of the stream."