Class: AgentXmpp::Message

Inherits:
Object show all
Defined in:
lib/agent_xmpp/models/message.rb

Overview


Class Method Summary collapse

Class Method Details

.count_by_content_type(content_type) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



53
54
55
# File 'lib/agent_xmpp/models/message.rb', line 53

def count_by_content_type(content_type)
  messages.filter(:content_type => content_type).count
end

.count_by_from_jid(from_jid) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



49
50
# File 'lib/agent_xmpp/models/message.rb', line 49

def count_by_from_jid(from_jid)
end

.find_by_item_id(item_id) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



44
45
46
# File 'lib/agent_xmpp/models/message.rb', line 44

def find_by_item_id(item_id)
  item_id.nil? ? nil : messages[:item_id => item_id]        
end

.messages(renew = false) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



11
12
13
# File 'lib/agent_xmpp/models/message.rb', line 11

def messages(renew=false)
  @messages ||= AgentXmpp.agent_xmpp_db[:messages]
end

.stats_by_command_nodeObject

.….….….….….….….….….….….….….….….….….….….….….….….….….….



71
72
73
# File 'lib/agent_xmpp/models/message.rb', line 71

def stats_by_command_node
  BaseController.command_nodes.map{|n| stats_by_node(n)}        
end

.stats_by_message_typeObject

.….….….….….….….….….….….….….….….….….….….….….….….….….….



58
59
60
61
62
# File 'lib/agent_xmpp/models/message.rb', line 58

def stats_by_message_type
  AgentXmpp.agent_xmpp_db['SELECT message_type AS type, count(id) AS count, max(created_at) AS last FROM messages GROUP BY message_type'].all.map do |s|
    {:type=>s[:type], :count=>s[:count], :last=>Time.parse(s[:last]).strftime("%y/%m/%d %H:%M")}
  end        
end

.stats_by_node(node) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



65
66
67
68
# File 'lib/agent_xmpp/models/message.rb', line 65

def stats_by_node(node)
  stats = AgentXmpp.agent_xmpp_db["SELECT node, count(id) AS count, max(created_at) AS last FROM messages WHERE node = ?", node].first
  {:node=>node, :count=>stats[:count], :last=>stats[:last].nil? ? 'Never' : Time.parse(stats[:last]).strftime("%y/%m/%d %H:%M")}
end

.update(stanza) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/agent_xmpp/models/message.rb', line 16

def update(stanza)
  case stanza
    when AgentXmpp::Xmpp::Message 
      if stanza.type.eql?(:chat)
        update_with_chat_message(stanza)
      end
    when AgentXmpp::Xmpp::Iq
      if cmd = stanza.command
        if data = cmd.x
          update_with_command_data(stanza, data, cmd.node)
        else
          update_with_command_request(stanza, cmd.node)
        end
      elsif (pubsub = stanza.pubsub).kind_of?(AgentXmpp::Xmpp::IqPubSub) and pub = pubsub.publish and item = pub.item 
        if data = item.x
          update_with_published_data(stanza, data, pub.node)
        end
      end
  end                 
end

.update_received_event_item(item, from, node) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



38
39
40
41
# File 'lib/agent_xmpp/models/message.rb', line 38

def update_received_event_item(item, from, node)
  (event_item =  item.entry || item.x) if item.respond_to?(:entry) and item.respond_to?(:x)
  event_item.nil? ? false : update_with_event_item(event_item, from, node, item.id) 
end

.update_with_chat_message(stanza) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….…. private .….….….….….….….….….….….….….….….….….….….….….….….….….….



78
79
80
81
82
83
84
85
86
87
# File 'lib/agent_xmpp/models/message.rb', line 78

def update_with_chat_message(stanza)
  from_jid = stanza.from || AgentXmpp.jid 
  messages << {
    :message_text => stanza.body,
    :content_type => 'text',
    :message_type => 'chat',
    :to_jid       => stanza.to.to_s,
    :from_jid     => from_jid.to_s,
    :created_at   => Time.now}
end

.update_with_command_data(stanza, data, node) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/agent_xmpp/models/message.rb', line 102

def update_with_command_data(stanza, data, node)
  from_jid = stanza.from || AgentXmpp.jid 
  messages << {
    :message_text  => data.to_s,
    :content_type  => 'x',
    :message_type  => 'command',
    :to_jid        => stanza.to.to_s,
    :from_jid      => from_jid.to_s,
    :node          => node,
    :created_at    => Time.now}
end

.update_with_command_request(stanza, node) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



90
91
92
93
94
95
96
97
98
99
# File 'lib/agent_xmpp/models/message.rb', line 90

def update_with_command_request(stanza, node)
  from_jid = stanza.from || AgentXmpp.jid 
  messages << {
    :content_type  => 'command_request',
    :message_type   => 'command',
    :to_jid        => stanza.to.to_s,
    :from_jid      => from_jid.to_s,
    :node          => node,
    :created_at    => Time.now}
end

.update_with_event_item(event_item, from, node, item_id) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/agent_xmpp/models/message.rb', line 128

def update_with_event_item(event_item, from, node, item_id)
  unless find_by_item_id(item_id)
    messages << {
      :message_text  => event_item.to_s,
      :content_type  => event_item.name,
      :message_type  => 'event',
      :to_jid        => AgentXmpp.jid.to_s,
      :from_jid      => from.to_s,
      :node          => node,
      :item_id       => item_id,
      :created_at    => Time.now}; true
  else; false; end    
end

.update_with_published_data(stanza, data, node) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/agent_xmpp/models/message.rb', line 115

def update_with_published_data(stanza, data, node)
  from_jid = stanza.from || AgentXmpp.jid 
  messages << {
    :message_text  => data.to_s,
    :content_type  => 'x',
    :message_type  => 'event',
    :to_jid        => stanza.to.to_s,
    :from_jid      => from_jid.to_s,
    :node          => node,
    :created_at    => Time.now}
end