conversations_controller.rb 4.24 KB
Newer Older
1 2
# frozen_string_literal: true

3
class ConversationsController < ApplicationController
4
  before_action :authenticate_user!
5
  respond_to :html, :mobile, :json, :js
6 7

  def index
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    @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
24 25
    end

26 27
    gon.contacts = contacts_data

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

  def create
Augier's avatar
Augier committed
35 36 37 38 39
    # 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
40
      person_ids = current_user.contacts.mutual.where(column => params[recipients_param].split(",")).pluck(:person_id)
41 42
    end

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

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

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

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

69 70 71 72
      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)

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

80 81 82 83 84 85 86
  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
87
      head :not_found
88 89 90
    end
  end

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

97
    if session[:mobile_view] == true && request.format.html?
Augier's avatar
Augier committed
98 99 100 101 102 103 104 105
      @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

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

  private

  def contacts_data
115
    current_user.contacts.mutual.joins(person: :profile)
116 117 118 119 120
      .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
121
end