Class: ConversationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/conversations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/conversations_controller.rb', line 34

def create
  # 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
    person_ids = current_user.contacts.mutual.where(column => params[recipients_param].split(",")).pluck(:person_id)
  end

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

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

  if @conversation.save
    Diaspora::Federation::Dispatcher.defer_dispatch(current_user, @conversation)
    flash[:notice] = I18n.t("conversations.create.sent")
    render json: {id: @conversation.id}
  else
    render plain: I18n.t("conversations.create.fail"), status: 422
  end
end

#indexObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/conversations_controller.rb', line 7

def index
  @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
  end

  gon.contacts = contacts_data

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

#newObject


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/conversations_controller.rb', line 91

def new
  if !params[:modal] && !session[:mobile_view] && request.format.html?
    redirect_to conversations_path
    return
  end

  if session[:mobile_view] == true && request.format.html?
    @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

    render :layout => true
  else
    render :layout => false
  end
end

#rawObject


80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/conversations_controller.rb', line 80

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
    head :not_found
  end
end

#showObject


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/conversations_controller.rb', line 62

def show
  respond_to do |format|
    format.html do
      redirect_to conversations_path(conversation_id: params[:id])
      return
    end

    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)

      format.json { render :json => @conversation, :status => 200 }
    else
      redirect_to conversations_path
    end
  end
end