StatusMessageController#create: respond 422 when aspect_ids are wrong

fixes #3862

closes #7742
This commit is contained in:
cmrd Senya 2018-03-16 20:34:34 +02:00 committed by Benjamin Neff
parent e0e40f73c3
commit 0080a2567f
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
4 changed files with 15 additions and 6 deletions

View file

@ -5,6 +5,7 @@
## Bug fixes ## Bug fixes
* Prefill conversation form on contacts page only with mutual contacts [#7744](https://github.com/diaspora/diaspora/pull/7744) * 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) * 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 ## Features

View file

@ -49,6 +49,8 @@ class StatusMessagesController < ApplicationController
format.mobile { redirect_to stream_path } format.mobile { redirect_to stream_path }
format.json { render json: PostPresenter.new(status_message, current_user), status: 201 } format.json { render json: PostPresenter.new(status_message, current_user), status: 201 }
end end
rescue StatusMessageCreationService::BadAspectsIDs
render status: 422, plain: I18n.t("status_messages.bad_aspects")
rescue StandardError => error rescue StandardError => error
handle_create_error(error) handle_create_error(error)
end end

View file

@ -9,15 +9,16 @@ class StatusMessageCreationService
def create(params) def create(params)
build_status_message(params).tap do |status_message| build_status_message(params).tap do |status_message|
load_aspects(params[:aspect_ids]) unless status_message.public?
add_attachments(status_message, params) add_attachments(status_message, params)
status_message.save status_message.save
process(status_message, params[:aspect_ids], params[:services]) process(status_message, params[:services])
end end
end end
private private
attr_reader :user attr_reader :user, :aspects
def build_status_message(params) def build_status_message(params)
public = params[:public] || false public = params[:public] || false
@ -54,13 +55,17 @@ class StatusMessageCreationService
end end
end end
def process(status_message, aspect_ids, services) def load_aspects(aspect_ids)
add_to_streams(status_message, aspect_ids) unless status_message.public @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) dispatch(status_message, services)
end end
def add_to_streams(status_message, aspect_ids) def add_to_streams(status_message)
aspects = user.aspects_from_ids(aspect_ids)
user.add_to_streams(status_message, aspects) user.add_to_streams(status_message, aspects)
status_message.photos.each {|photo| user.add_to_streams(photo, aspects) } status_message.photos.each {|photo| user.add_to_streams(photo, aspects) }
end end

View file

@ -1122,6 +1122,7 @@ en:
new: new:
mentioning: "Mentioning: %{person}" mentioning: "Mentioning: %{person}"
too_long: "Please make your status message fewer than %{count} characters. Right now it is %{current_length} characters" 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: stream_helper:
no_more_posts: "You have reached the end of the stream." no_more_posts: "You have reached the end of the stream."