profiles_controller.rb 2.48 KB
Newer Older
1 2
# frozen_string_literal: true

danielgrippi's avatar
danielgrippi committed
3
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
4 5 6 7
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class ProfilesController < ApplicationController
8
  before_action :authenticate_user!, :except => ['show']
Ilya Zhitomirskiy's avatar
Ilya Zhitomirskiy committed
9

10
  respond_to :html, :except => [:show]
Ilya Zhitomirskiy's avatar
Ilya Zhitomirskiy committed
11
  respond_to :js, :only => :update
12

13 14
  # this is terrible because we're actually serving up the associated person here;
  # however, this is the effect that we want for now
15
  def show
16
    @person = Person.find_by_guid!(params[:id])
17 18

    respond_to do |format|
19
      format.json { render :json => PersonPresenter.new(@person, current_user) }
20 21 22
    end
  end

23 24 25 26
  def edit
    @person = current_user.person
    @aspect  = :person_edit
    @profile = @person.profile
27

28
    gon.preloads[:tagsArray] = @profile.tags.map {|tag| {name: "##{tag.name}", value: "##{tag.name}"} }
29 30 31
  end

  def update
32
    # upload and set new profile photo
33
    @profile_attrs = profile_params
Steffen van Bergerem's avatar
Steffen van Bergerem committed
34

35 36
    munge_tag_string

Dennis Collinson's avatar
Dennis Collinson committed
37
    #checkbox tags wtf
38
    @profile_attrs[:searchable] ||= false
39
    @profile_attrs[:nsfw] ||= false
40
    @profile_attrs[:public_details] ||= false
41 42

    if params[:photo_id]
43
      @profile_attrs[:photo] = Photo.where(:author_id => current_user.person_id, :id => params[:photo_id]).first
44 45 46 47 48 49 50 51 52
    end

    if current_user.update_profile(@profile_attrs)
      flash[:notice] = I18n.t 'profiles.update.updated'
    else
      flash[:error] = I18n.t 'profiles.update.failed'
    end

    respond_to do |format|
53
      format.js { head :ok }
movilla's avatar
movilla committed
54
      format.any {
55 56 57 58 59 60 61 62 63
        if current_user.getting_started?
          redirect_to getting_started_path
        else
          redirect_to edit_profile_path
        end
      }
    end
  end

64
  private
65 66 67 68

  def munge_tag_string
    unless @profile_attrs[:tag_string].nil? || @profile_attrs[:tag_string] == I18n.t('profiles.edit.your_tags_placeholder')
      @profile_attrs[:tag_string].split( " " ).each do |extra_tag|
69 70 71
        extra_tag.strip!
        unless extra_tag == ""
          extra_tag = "##{extra_tag}" unless extra_tag.start_with?( "#" )
72
          params[:tags] += " #{extra_tag}"
73 74 75
        end
      end
    end
76
    @profile_attrs[:tag_string] = (params[:tags]) ? params[:tags].gsub(',',' ') : ""
77
  end
78 79

  def profile_params
80 81
    params.require(:profile).permit(:first_name, :last_name, :gender, :bio,
                                    :location, :searchable, :tag_string, :nsfw,
82
                                    :public_details, date: %i[year month day]).to_h || {}
83
  end
84
end