conversations_controller.rb 4.21 KB
Newer Older
1
class ConversationsController < ApplicationController
2
  before_action :authenticate_user!
3
  respond_to :html, :mobile, :json, :js
4 5

  def index
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    @visibilities = ConversationVisibility.includes(:conversation)
                                          .order("conversations.updated_at DESC")
                                          .where(person_id: current_user.person_id)
                                          .paginate(page: params[:page], per_page: 15)

    if params[:conversation_id]
      @conversation = Conversation.joins(:conversation_visibilities)
                                  .where(conversation_visibilities: {
                                           person_id:       current_user.person_id,
                                           conversation_id: params[:conversation_id]
                                         }).first

      if @conversation
        @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
        @conversation.set_read(current_user)
      end
22 23
    end

24 25
    gon.contacts = contacts_data

26
    respond_with do |format|
27
      format.html { render "index", locals: {no_contacts: current_user.contacts.mutual.empty?} }
28
      format.json { render json: @visibilities.map(&:conversation), status: 200 }
29
    end
30 31 32
  end

  def create
Augier's avatar
Augier committed
33 34 35 36 37
    # Contacts autocomplete does not work the same way on mobile and desktop
    # Mobile returns contact ids array while desktop returns person id
    # This will have to be removed when mobile autocomplete is ported to Typeahead
    recipients_param, column = [%i(contact_ids id), %i(person_ids person_id)].find {|param, _| params[param].present? }
    if recipients_param
38
      person_ids = current_user.contacts.mutual.where(column => params[recipients_param].split(",")).pluck(:person_id)
39 40
    end

41
    unless person_ids.present?
42
      render plain: I18n.t("javascripts.conversation.create.no_recipient"), status: 422
43 44 45
      return
    end

46 47 48 49
    opts = params.require(:conversation).permit(:subject)
    opts[:participant_ids] = person_ids
    opts[:message] = { text: params[:conversation][:text] }
    @conversation = current_user.build_conversation(opts)
50

51
    if @conversation.save
52
      Diaspora::Federation::Dispatcher.defer_dispatch(current_user, @conversation)
53 54
      flash[:notice] = I18n.t("conversations.create.sent")
      render json: {id: @conversation.id}
55
    else
56
      render plain: I18n.t("conversations.create.fail"), status: 422
57
    end
58 59 60
  end

  def show
61 62
    respond_to do |format|
      format.html do
63
        redirect_to conversations_path(conversation_id: params[:id])
64
        return
65
      end
66

67 68 69 70
      if @conversation = current_user.conversations.where(id: params[:id]).first
        @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
        @conversation.set_read(current_user)

71
        format.json { render :json => @conversation, :status => 200 }
72 73
      else
        redirect_to conversations_path
74
      end
75
    end
76 77
  end

78 79 80 81 82 83 84
  def raw
    @conversation = current_user.conversations.where(id: params[:conversation_id]).first
    if @conversation
      @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
      @conversation.set_read(current_user)
      render partial: "conversations/show", locals: {conversation: @conversation}
    else
85
      head :not_found
86 87 88
    end
  end

danielgrippi's avatar
danielgrippi committed
89
  def new
90
    if !params[:modal] && !session[:mobile_view] && request.format.html?
91 92 93
      redirect_to conversations_path
      return
    end
94

95
    if session[:mobile_view] == true && request.format.html?
Augier's avatar
Augier committed
96 97 98 99 100 101 102 103
      @contacts_json = contacts_data.to_json

      @contact_ids = if params[:contact_id]
                       current_user.contacts.find(params[:contact_id]).id
                     elsif params[:aspect_id]
                       current_user.aspects.find(params[:aspect_id]).contacts.pluck(:id).join(",")
                     end

104 105 106
      render :layout => true
    else
      render :layout => false
107
    end
danielgrippi's avatar
danielgrippi committed
108
  end
109 110 111 112

  private

  def contacts_data
113
    current_user.contacts.mutual.joins(person: :profile)
114 115 116 117 118
      .pluck(*%w(contacts.id profiles.first_name profiles.last_name people.diaspora_handle))
      .map {|contact_id, *name_attrs|
        {value: contact_id, name: ERB::Util.h(Person.name_from_attrs(*name_attrs)) }
      }
  end
119
end