Class: WcoEmail::ConversationsController

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

Instance Method Summary collapse

Instance Method Details

#addtagObject

many tags to many convs



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/wco_email/conversations_controller.rb', line 10

def addtag
  authorize! :addtag, WcoEmail::Conversation

  inbox = Wco::Tag.inbox
  outs = @convs.map do |conv|
    conv.tags.push @tags
    conv.tags.delete( inbox ) if params[:is_move]
    conv.save
  end
  flash_notice "Outcomes: #{outs}"

  render json: { status: 'ok' }
  # redirect_to request.referrer # || root_path
end

#editObject



25
26
27
28
# File 'app/controllers/wco_email/conversations_controller.rb', line 25

def edit
  @conversation = ::WcoEmail::Conversation.find( params[:id] )
  authorize! :edit, @conversation
end

#indexObject



30
31
32
33
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
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/wco_email/conversations_controller.rb', line 30

def index
  authorize! :index, WcoEmail::Conversation
  @conversations = WcoEmail::Conversation.all

  if params[:tagname]
    @tag = Wco::Tag.find_by slug: params[:tagname]
    @conversations = @conversations.where( :tag_ids.in => [ @tag.id ] )
  end
  if params[:tagname_not]
    @tag_not = Wco::Tag.find_by slug: params[:tagname_not]
    @conversations = @conversations.where( :tag_ids.nin => [ @tag_not.id ] )
  end

  if params[:subject].present?
    @conversations = @conversations.where({ subject: /.*#{params[:subject]}.*/i })
  end

  if params[:from_email].present?
    @conversations = @conversations.where({ from_emails: /.*#{params[:from_email]}.*/i })
  end

  if params[:lead_id].present?
    @conversations = @conversations.where( lead_ids: params[:lead_id] )
  end

  @conversations = @conversations.where(
  ).includes( :leads, :messages, :tags
  ).order_by( latest_at: :desc
  ).page( params[:conv_page] ).per( current_profile.per_page )

  conversation_ids = @conversations.map &:id

  @messages_hash = {}
  messages = WcoEmail::Message.where(
    :conversation_id.in => conversation_ids,
    read_at: nil,
  )
  messages.map do |msg|
    @messages_hash[msg.id.to_s] = msg
  end
end

#mergeObject

merge conv1 into conv2, and delete conv1



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/wco_email/conversations_controller.rb', line 73

def merge
  authorize! :merge, WcoEmail::Conversation
  conv1 = WcoEmail::Conversation.find params[:id1]
  conv2 = WcoEmail::Conversation.find params[:id2]
  conv1.messages.map do |msg|
    msg.update conversation: conv2
  end
  conv1.tags.map do |tag|
    conv2.tags.push tag
  end
  conv1.leads.map do |lead|
    conv2.leads.push lead
  end
  conv2.save!
  conv1.delete

  flash_notice "Probably ok"
  redirect_to action: :show, id: conv2.id
end

#rmtagObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/wco_email/conversations_controller.rb', line 93

def rmtag
  authorize! :addtag, WcoEmail::Conversation

  outs = @convs.map do |conv|
    @tags.map do |tag|
      conv.tags.delete( tag )
    end
    conv.save
  end
  flash_notice "Outcomes: #{outs}"
  render json: { status: 'ok' }
  # redirect_to request.referrer || root_path
end

#showObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/wco_email/conversations_controller.rb', line 107

def show
  @conversation = ::WcoEmail::Conversation.find( params[:id] )
  authorize! :show, @conversation
  @messages     = @conversation.messages(
    ).order_by( date: :asc
    ).page( params[:messages_page ] ).per( @current_profile.per_page
    )

  @conversation.update_attributes({ status: Conv::STATUS_READ })

  @conversation.messages.unread.update_all({ read_at: Time.now })

  # @other_convs = WcoEmail::Message.where( :message_id.in => @messages.map( &:in_reply_to_id )
  #   ).where( :conversation_id.ne => @conversation.id
  #   ).map( &:conversation_id ).uniq
  # other_convs_by_subj = WcoEmail::Conversation.where( subject: @conversation.subject
  #   ).where( :conversation_id.ne => @conversation.id
  #   ).map( &:id )
  # if @other_convs.present? || other_convs_by_subj.present?
  #   @other_convs = WcoEmail::Conversation.find( @other_convs + other_convs_by_subj )
  # end
end

#updateObject



130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/wco_email/conversations_controller.rb', line 130

def update
  @conversation = ::WcoEmail::Conversation.find( params[:id] )
  authorize! :update, @conversation
  if @conversation.update( params[:conversation].permit! )
    flash_notice 'ok'
  else
    flash_alert @conversation
  end
  redirect_to action: 'show', id: @conversation.id
end