Class: AgentXmpp::Contact

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

Overview


Class Method Summary collapse

Class Method Details

.contactsObject

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



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

def contacts
  @contacts ||= AgentXmpp.agent_xmpp_db[:contacts]
end

.destroy_by_jid(jid) ⇒ Object

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



67
68
69
70
71
# File 'lib/agent_xmpp/models/contact.rb', line 67

def destroy_by_jid(jid)
  contact = contacts.filter(:jid => AgentXmpp.bare_jid_to_s(jid))
  Roster.destroy_by_contact_id(contact.first[:id])
  contact.delete
end

.find_allObject

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



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

def find_all
  contacts.all
end

.find_all_by_subscription(subscription) ⇒ Object

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



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

def find_all_by_subscription(subscription)
  contacts.filter(:subscription => subscription.to_s).all
end

.find_by_jid(jid) ⇒ Object

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



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

def find_by_jid(jid)
  contacts[:jid => AgentXmpp.bare_jid_to_s(jid)]
end

.has_jid?(jid) ⇒ Boolean

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/agent_xmpp/models/contact.rb', line 54

def has_jid?(jid)
  contacts.filter(:jid => AgentXmpp.bare_jid_to_s(jid)).count > 0
end

.load_configObject

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



16
17
18
19
20
# File 'lib/agent_xmpp/models/contact.rb', line 16

def load_config
  if AgentXmpp.config['roster'].kind_of?(Array)
    AgentXmpp.config['roster'].each {|c| update(c)}
  end
end

.message_statsObject

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



59
60
61
62
63
64
# File 'lib/agent_xmpp/models/contact.rb', line 59

def message_stats
  find_all.map do |c|
    stats = AgentXmpp.agent_xmpp_db["SELECT count(id) AS count, max(created_at) AS last FROM messages WHERE from_jid LIKE '#{c[:jid]}%'"].first
    {:jid=>c[:jid], :count=>stats[:count], :last=>stats[:last].nil? ? 'Never' : Time.parse(stats[:last]).strftime("%y/%m/%d %H:%M")}
  end        
end

.method_missing(meth, *args, &blk) ⇒ Object

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



74
75
76
# File 'lib/agent_xmpp/models/contact.rb', line 74

def method_missing(meth, *args, &blk)
  contacts.send(meth, *args, &blk)
end

.update(contact) ⇒ Object

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



23
24
25
26
27
28
29
30
# File 'lib/agent_xmpp/models/contact.rb', line 23

def update(contact)
  groups = contact['groups'].kind_of?(Array) ? contact['groups'].join(",") : contact['groups']
  begin
    contacts << {:jid => contact['jid'], :groups => groups, :subscription => 'new', :ask => 'new'}
  rescue 
    contacts.filter(:jid => contact['jid']).update(:groups => groups)
  end
end

.update_with_roster_item(roster_item) ⇒ Object

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



33
34
35
36
# File 'lib/agent_xmpp/models/contact.rb', line 33

def update_with_roster_item(roster_item)
  from_jid, subscription, ask = roster_item.jid.to_s, roster_item.subscription.to_s, roster_item.ask.to_s
  contacts.filter(:jid => from_jid).update(:subscription => subscription, :ask => ask)
end