Class: Vines::Stanza::Presence

Inherits:
Vines::Stanza show all
Defined in:
lib/vines/stanza/presence.rb,
lib/vines/stanza/presence/error.rb,
lib/vines/stanza/presence/probe.rb,
lib/vines/stanza/presence/subscribe.rb,
lib/vines/stanza/presence/subscribed.rb,
lib/vines/stanza/presence/unavailable.rb,
lib/vines/stanza/presence/unsubscribe.rb,
lib/vines/stanza/presence/unsubscribed.rb

Defined Under Namespace

Classes: Error, Probe, Subscribe, Subscribed, Unavailable, Unsubscribe, Unsubscribed

Constant Summary collapse

VALID_TYPES =
%w[subscribe subscribed unsubscribe unsubscribed unavailable probe error].freeze

Constants inherited from Vines::Stanza

EMPTY, ROUTABLE_STANZAS

Instance Attribute Summary

Attributes inherited from Vines::Stanza

#stream

Instance Method Summary collapse

Methods inherited from Vines::Stanza

#broadcast, from_node, #initialize, #local?, #local_jid?, #method_missing, register, #route, #router, #send_unavailable, #storage, #to_pubsub_domain?, #unavailable, #validate_from, #validate_to

Constructor Details

This class inherits a constructor from Vines::Stanza

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Vines::Stanza

Instance Method Details

#inbound?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/vines/stanza/presence.rb', line 29

def inbound?
  stream.class == Vines::Stream::Server ||
    stream.class == Vines::Stream::Component
end

#inbound_broadcast_presenceObject



86
87
88
# File 'lib/vines/stanza/presence.rb', line 86

def inbound_broadcast_presence
  broadcast(stream.available_resources(validate_to))
end

#outbound?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/vines/stanza/presence.rb', line 25

def outbound?
  !inbound?
end

#outbound_broadcast_presenceObject



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
# File 'lib/vines/stanza/presence.rb', line 34

def outbound_broadcast_presence
  self['from'] = stream.user.jid.to_s
  to = validate_to
  type = (self['type'] || '').strip
  initial = to.nil? && type.empty? && !stream.available?

  recipients = if to.nil?
    stream.available_subscribers
  else
    stream.user.subscribed_from?(to) ? stream.available_resources(to) : []
  end

  broadcast(recipients)
  broadcast(stream.available_resources(stream.user.jid))

  if initial
    stream.available_subscribed_to_resources.each do |recipient|
      if recipient.last_broadcast_presence
        el = recipient.last_broadcast_presence.clone
        el['to'] = stream.user.jid.to_s
        el['from'] = recipient.user.jid.to_s
        stream.write(el)
      end
    end
    stream.remote_subscribed_to_contacts.each do |contact|
      send_probe(contact.jid.bare)
    end
    #offline messages
    storage.fetch_offline_messages(stream.user.jid).each do |offline_msg|
      doc = Document.new
      node = doc.create_element('message') do |el|
        el['from'] = offline_msg[:from]              
        el['to'] = stream.user.jid.to_s
        el['type'] = "chat"
        el << doc.create_element("body") do |b|
          b << offline_msg[:body]
        end
      end            
      stream.write(node)
    end
    storage.delete_offline_messages(stream.user.jid)          
    stream.available!

  end

  stream.remote_subscribers(to).each do |contact|
    node = @node.clone
    node['to'] = contact.jid.bare.to_s
    router.route(node) rescue nil # ignore RemoteServerNotFound
  end
end

#processObject



16
17
18
19
20
21
22
23
# File 'lib/vines/stanza/presence.rb', line 16

def process
  stream.last_broadcast_presence = @node.clone unless validate_to
  unless self['type'].nil?
    raise StanzaErrors::BadRequest.new(self, 'modify')
  end
  dir = outbound? ? 'outbound' : 'inbound'
  method("#{dir}_broadcast_presence").call
end