summaryrefslogtreecommitdiff
path: root/lib/tasks/send_voicemail_notifications.rake
blob: 214d832b093b22fd1ea6217b2392c495edfcf8c2 (plain)
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# encoding: UTF-8

desc "Import inbound voicemail"

task :send_voicemail_notifications => :environment do
  VoicemailMessage.where(:notification => nil).each do |message|

    message.notification = false
    message.save
    if !File.exists?( message.file_path ) 
      $stderr.puts "File \"#{message.file_path}\" does not exist"
      next
    end

    sip_account = SipAccount.where(:auth_name => message.username).first
    if ! sip_account 
      $stderr.puts "SipAccount \"#{message.username}\" does not exist"
      next
    end

    user = sip_account.sip_accountable
    if user.class != User
      next
    end

    if  user.email.blank?
      $stderr.puts "no email address"
      next
    end

    voicemail_settings = sip_account.voicemail_setting
    if !voicemail_settings
      voicemail_settings = VoicemailSetting.new(:notify => user.send_voicemail_as_email_attachment, :attachment => user.send_voicemail_as_email_attachment, :mark_read => user.send_voicemail_as_email_attachment)
    end

    message.notification = voicemail_settings.notify
    if voicemail_settings.notify
      if Notifications.new_voicemail(message, voicemail_settings.attachment).deliver
        if voicemail_settings.purge
          message.delete
          next
        end
        message.save
        if voicemail_settings.mark_read
          message.mark_read
        end
      end
    else
      message.save
    end
  end
end