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

#check_offline_messages(presence) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vines/stanza/presence.rb', line 28

def check_offline_messages(presence)
  priority = presence.xpath("//priority").text.to_i rescue nil
  if priority != nil && priority >= 0
    jid = stream.user.jid.to_s
    storage.find_messages(jid).each do |id, m|
      stamp = Time.parse(m[:created_at].to_s)
      doc = Nokogiri::XML::Builder.new
      doc.message(:type => "chat", :from => m[:from], :to => m[:to]) do |msg|
        msg.send(:"body", m[:message])
        msg.send(:"delay", "Offline Storage",
                 :xmlns => NAMESPACES[:delay],
                 :from => m[:from],
                 :stamp => stamp.iso8601)
      end
      xml = doc.to_xml :save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
      stream.write(xml)
      # after delivering it we should
      # delete the message from database
      storage.destroy_message(id)
    end
  end
end

#inbound?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/vines/stanza/presence.rb', line 55

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

#inbound_broadcast_presenceObject



103
104
105
# File 'lib/vines/stanza/presence.rb', line 103

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

#outbound?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/vines/stanza/presence.rb', line 51

def outbound?
  !inbound?
end

#outbound_broadcast_presenceObject



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
93
94
95
96
97
98
99
100
101
# File 'lib/vines/stanza/presence.rb', line 60

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

  # NOTE overriding vCard information is not concurring
  # with XEP-153 due the fact that the user can only update
  # his vCard via the Diaspora environment we should act
  # the same way for the avatar update
  override_vcard_update

  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
    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
24
25
26
# 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
  if Config.instance.max_offline_msgs > 0 && !validate_to
    check_offline_messages(stream.last_broadcast_presence)
  end
  dir = outbound? ? 'outbound' : 'inbound'
  method("#{dir}_broadcast_presence").call
end