summaryrefslogtreecommitdiff
path: root/app/models/voicemail_message.rb
blob: 698aa505d1040be8048c06082768917f194abf4e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class VoicemailMessage < ActiveRecord::Base
  self.table_name = 'voicemail_msgs'
  self.primary_key = 'uuid'

  belongs_to :voicemail_account, :foreign_key => 'username', :primary_key => 'name', :readonly => true

  # Prevent objects from being destroyed
  def before_destroy
    raise ActiveRecord::ReadOnlyRecord
  end

  # Prevent objects from being deleted
  def self.delete_all
    raise ActiveRecord::ReadOnlyRecord
  end

  # Delete Message on FreeSWITCH over EventAPI
  def delete
    require 'freeswitch_event'
    result = FreeswitchAPI.execute('vm_delete', "#{self.username}@#{self.domain} #{self.uuid}");
  end

  # Alias for delete
  def destroy
    self.delete
  end

  # Mark Message read
  def mark_read(mark_read_or_unread = true)
    read_status = mark_read_or_unread ? 'read' : 'unread'
    require 'freeswitch_event'
    result = FreeswitchAPI.execute('vm_read', "#{self.username}@#{self.domain} #{read_status} #{self.uuid}");
  end

  def format_date(epoch, date_format = '%m/%d/%Y %H:%M', date_today_format = '%H:%M')
    if epoch && epoch > 0
      time = Time.at(epoch)
      if time.strftime('%Y%m%d') == Time.now.strftime('%Y%m%d')
        return time.in_time_zone.strftime(date_today_format)
      end
      return time.in_time_zone.strftime(date_format)
    end
  end

  def display_duration
    if self.message_len.to_i > 0
      minutes = (self.message_len / 1.minutes).to_i
      seconds = self.message_len - minutes.minutes.seconds
      return '%i:%02i' % [minutes, seconds]
    end
  end

  def phone_book_entry_by_number(number)
    begin
      voicemail_accountable = self.voicemail_account.voicemail_accountable
    rescue
      return nil
    end

    if ! voicemail_accountable
      return nil
    end

    if voicemail_accountable.class == SipAccount
      owner = voicemail_accountable.sip_accountable
    else
      owner = voicemail_accountable
    end

    if owner.class == User
      phone_books = owner.phone_books.all
      phone_books.concat(owner.current_tenant.phone_books.all)
    elsif owner.class == Tenant
      phone_books = owner.phone_books.all
    end

    if ! phone_books
      return nil
    end

    phone_books.each do |phone_book|
      phone_book_entry = phone_book.find_entry_by_number(number)
      if phone_book_entry
        return phone_book_entry
      end
    end

    return nil
      
  end

end