Class: ConversationsController

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

Instance Method Summary collapse

Instance Method Details

#createObject


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
61
62
63
# File 'app/controllers/conversations_controller.rb', line 36

def create
  # Can't split nil
  if params[:contact_ids]
    person_ids = current_user.contacts.where(id: params[:contact_ids].split(',')).pluck(:person_id)
  end

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

  @response = {}
  if person_ids.present? && @conversation.save
    Postzord::Dispatcher.build(current_user, @conversation).post
    @response[:success] = true
    @response[:message] = I18n.t('conversations.create.sent')
    @response[:conversation_id] = @conversation.id
  else
    @response[:success] = false
    @response[:message] = I18n.t('conversations.create.fail')
    if person_ids.blank?
      @response[:message] = I18n.t('conversations.create.no_contact')
    end
  end
  respond_to do |format|
    format.js
  end
end

#indexObject


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

def index
  @conversations = current_user.conversations.paginate(
    :page => params[:page], :per_page => 15)

  @visibilities = current_user.conversation_visibilities.paginate(
    :page => params[:page], :per_page => 15)

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

  @unread_counts = {}
  @visibilities.each { |v| @unread_counts[v.conversation_id] = v.unread }

  @first_unread_message_id = @conversation.try(:first_unread_message, current_user).try(:id)

  @authors = {}
  @conversations.each { |c| @authors[c.id] = c.last_author }

  @ordered_participants = {}
  @conversations.each { |c| @ordered_participants[c.id] = (c.messages.map{|m| m.author}.reverse + c.participants).uniq }

  respond_with do |format|
    format.html
    format.json { render :json => @conversations, :status => 200 }
  end
end

#newObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/conversations_controller.rb', line 84

def new
  all_contacts_and_ids = Contact.connection.select_rows(
    current_user.contacts.where(:sharing => true).joins(:person => :profile).
      select("contacts.id, profiles.first_name, profiles.last_name, people.diaspora_handle").to_sql
  ).map{|r| {:value => r[0], :name => ERB::Util.h(Person.name_from_attrs(r[1], r[2], r[3]).gsub(/(")/, "'"))} }

  @contact_ids = ""

  @contacts_json = all_contacts_and_ids.to_json
  if params[:contact_id]
    @contact_ids = current_user.contacts.find(params[:contact_id]).id
  elsif params[:aspect_id]
    @contact_ids = current_user.aspects.find(params[:aspect_id]).contacts.map{|c| c.id}.join(',')
  end
  if session[:mobile_view] == true && request.format.html?
  render :layout => true
  elsif
  render :layout => false
  end
end

#showObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/conversations_controller.rb', line 65

def show
  if @conversation = current_user.conversations.where(id: params[:id]).first

    @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
    if @visibility = ConversationVisibility.where(:conversation_id => params[:id], :person_id => current_user.person.id).first
      @visibility.unread = 0
      @visibility.save
    end

    respond_to do |format|
      format.html { redirect_to conversations_path(:conversation_id => @conversation.id) }
      format.js
      format.json { render :json => @conversation, :status => 200 }
    end
  else
    redirect_to conversations_path
  end
end