Skip to content
Extraits de code Groupes Projets
status_messages_controller.rb 2,95 ko
Newer Older
danielgrippi's avatar
danielgrippi a validé
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
Raphael's avatar
Raphael a validé
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael a validé
#   the COPYRIGHT file.
maxwell's avatar
maxwell a validé
class StatusMessagesController < ApplicationController
  before_action :authenticate_user!
  before_action :remove_getting_started, only: :create
  respond_to :html, :mobile, :json
  layout "application", only: :bookmarklet
  # Called when a user clicks "Mention" on a profile page
  # @param person_id [Integer] The id of the person to be mentioned
    if params[:person_id] && fetch_person(params[:person_id])
      @aspect = :profile
      @contact = current_user.contact_for(@person)
      if @contact
        @aspects_with_person = @contact.aspects.load
        @aspect_ids = @aspects_with_person.map(&:id)
        gon.aspect_ids = @aspect_ids
        render layout: nil
      else
        @aspects_with_person = []
    elsif request.format == :mobile
      @aspects = current_user.aspects.load
      @aspect_ids = @aspects.map(&:id)
      gon.aspect_ids = @aspect_ids
    else
      redirect_to stream_path
    @aspects = current_user.aspects
    @aspect_ids = current_user.aspect_ids

    gon.preloads[:bookmarklet] = {
      content: params[:content],
      title:   params[:title],
      url:     params[:url],
      notes:   params[:notes]
maxwell's avatar
maxwell a validé
  def create
    status_message = StatusMessageCreationService.new(current_user).create(normalize_params)
    respond_to do |format|
      format.mobile { redirect_to stream_path }
      format.json { render json: PostPresenter.new(status_message, current_user), status: 201 }
Jannik Streek's avatar
Jannik Streek a validé
    end
  rescue StandardError => error
    handle_create_error(error)
  end
  def fetch_person(person_id)
    @person = Person.where(id: person_id).first
  end
  def handle_create_error(error)
    logger.debug error
    respond_to do |format|
      format.mobile { redirect_to stream_path }
      format.json { render text: error.message, status: 403 }
maxwell's avatar
maxwell a validé
  end
  def comes_from_others_profile_page?
    coming_from_profile_page? && !own_profile_page?
    request.env["HTTP_REFERER"].include?("people")
Asphyxia's avatar
Asphyxia a validé
  def own_profile_page?
    request.env["HTTP_REFERER"].include?("/people/" + current_user.guid)
Asphyxia's avatar
Asphyxia a validé
  end

  def normalize_params
    params.permit(
      :location_address,
      :location_coords,
      :poll_question,
      status_message: %i[text provider_display_name],
      poll_answers:   []
    ).to_h.merge(
      services:   [*params[:services]].compact,
      aspect_ids: normalize_aspect_ids,
      public:     [*params[:aspect_ids]].first == "public",
      photos:     [*params[:photos]].compact
    )
  end

  def normalize_aspect_ids
    aspect_ids = [*params[:aspect_ids]]
    if aspect_ids.first == "all_aspects"
      current_user.aspect_ids
    else
      aspect_ids
    end
  end

    current_user.disable_getting_started