diff options
author | theworldbright <kent@kentshikama.com> | 2015-08-02 13:05:14 +0300 |
---|---|---|
committer | Jonne Haß <me@jhass.eu> | 2015-08-02 17:58:01 +0300 |
commit | cae5f94af091cca9bd833d455860f0377e266b08 (patch) | |
tree | 0994c417e93efc49df473fcf8766a349468672d6 /app/services | |
parent | b3242500b0b86822922b53f072633b0c8984e2c1 (diff) |
Refactor status messages controller
closes #6280
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/status_message_creation_service.rb | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/app/services/status_message_creation_service.rb b/app/services/status_message_creation_service.rb new file mode 100644 index 000000000..3f0093bca --- /dev/null +++ b/app/services/status_message_creation_service.rb @@ -0,0 +1,89 @@ +class StatusMessageCreationService + include Rails.application.routes.url_helpers + + attr_reader :status_message + + def initialize(params, user) + normalize_params(params, user) + status_message_initial = user.build_post(:status_message, params[:status_message]) + @status_message = add_attachments(params, status_message_initial) + @status_message.save + process_status_message(user) + end + + private + + attr_reader :services, :destination_aspect_ids + + def normalize_params(params, user) + normalize_aspect_ids(params) + normalize_public_flag!(params) + @services = [*params[:services]].compact + @destination_aspect_ids = destination_aspect_ids(params, user) + end + + def normalize_aspect_ids(params) + params[:status_message][:aspect_ids] = [*params[:aspect_ids]] + end + + def normalize_public_flag!(params) + sm = params[:status_message] + public_flag_string = (sm[:aspect_ids] && sm[:aspect_ids].first == "public") || sm[:public] + public_flag = public_flag_string.to_s.match(/(true)|(on)/) ? true : false + params[:status_message][:public] = public_flag + end + + def destination_aspect_ids(params, user) + if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects" + user.aspect_ids + else + params[:aspect_ids] + end + end + + def add_attachments(params, status_message_initial) + status_message_with_location = add_location(params, status_message_initial) + status_message_with_poll = add_poll(params, status_message_with_location) + add_photos(params, status_message_with_poll) + end + + def add_location(params, status_message) + address = params[:location_address] + coordinates = params[:location_coords] + status_message.build_location(address: address, coordinates: coordinates) if address.present? + status_message + end + + def add_poll(params, status_message) + if params[:poll_question].present? + status_message.build_poll(question: params[:poll_question]) + [*params[:poll_answers]].each do |poll_answer| + status_message.poll.poll_answers.build(answer: poll_answer) + end + end + status_message + end + + def add_photos(params, status_message) + status_message.attach_photos_by_ids(params[:photos]) + status_message + end + + def process_status_message(user) + add_status_message_to_streams(user) + dispatch_status_message(user) + user.participate!(@status_message) + end + + def add_status_message_to_streams(user) + aspects = user.aspects_from_ids(@destination_aspect_ids) + user.add_to_streams(@status_message, aspects) + end + + def dispatch_status_message(user) + receiving_services = Service.titles(@services) + user.dispatch_post(@status_message, + url: short_post_url(@status_message.guid, host: AppConfig.environment.url), + service_types: receiving_services) + end +end |