summaryrefslogtreecommitdiff
path: root/app/models/voicemail_message.rb
blob: 91ba45789d4406754b7102fe636a162d30b88da1 (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
class VoicemailMessage < ActiveRecord::Base
  self.table_name = 'voicemail_msgs'
  self.primary_key = 'uuid'

#  belongs_to :sip_account, :foreign_key => 'username', :primary_key => 'auth_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

end