Skip to content

Commit

Permalink
Small fixes to PubSub to work with OpenFire.
Browse files Browse the repository at this point in the history
  * Fixes pubsub/children/unsubscribe.rb to allow subids.
  * Fixes pubsub/helper/servicehelper.rb:
    * adds #get_subids_for - to get subids for a node.
    * fixes #unsubscribe_from and #get_items_from to reflect that.
  • Loading branch information
Pablo Lorenzoni committed Oct 29, 2009
1 parent 37466c5 commit 26d6a01
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
17 changes: 16 additions & 1 deletion lib/xmpp4r/pubsub/children/unsubscribe.rb
Expand Up @@ -10,10 +10,11 @@ module PubSub
# Unsubscribe
class Unsubscribe < XMPPElement
name_xmlns 'unsubscribe'
def initialize(myjid=nil,mynode=nil)
def initialize(myjid=nil,mynode=nil,mysubid=nil)
super()
jid = myjid
node = mynode
subid = mysubid
end

##
Expand Down Expand Up @@ -43,6 +44,20 @@ def node
def node=(mynode)
attributes['node'] = mynode
end

##
# shows the subid
# return:: [String]
def subid
attributes['subid']
end

##
# sets the subid
# =:: [String]
def subid=(mysubid)
attributes['subid'] = mysubid
end
end
end
end
60 changes: 47 additions & 13 deletions lib/xmpp4r/pubsub/helper/servicehelper.rb
Expand Up @@ -85,6 +85,20 @@ def get_subscriptions_from_all_nodes

res
end

##
# get subids for a passed node
# return:: [Array] of subids
def get_subids_for(node)
ret = []
get_subscriptions_from_all_nodes.each do |subscription|
if subscription.node == node
ret << subscription.subid
end
end
return ret
end

##
# subscribe to a node
# node:: [String]
Expand Down Expand Up @@ -113,27 +127,48 @@ def subscribe_to(node)
# subid:: [String] or nil (not supported)
# return:: true
def unsubscribe_from(node, subid=nil)
iq = basic_pubsub_query(:set)
unsub = PubSub::Unsubscribe.new
unsub.node = node
unsub.jid = @stream.jid.strip
iq.pubsub.add(unsub)
ret = false
@stream.send_with_id(iq) { |reply|
ret = reply.kind_of?(Jabber::Iq) and reply.type == :result
} # @stream.send_with_id(iq)
ret = []
if subid.nil?
subids = get_subids_for(node)
else
subids = [ subid ]
end
subids << nil if subids.empty?
subids.each do |sid|
iq = basic_pubsub_query(:set)
unsub = PubSub::Unsubscribe.new
unsub.node = node
unsub.jid = @stream.jid.strip
unsub.subid = sid
iq.pubsub.add(unsub)
res = false
@stream.send_with_id(iq) { |reply|
res = reply.kind_of?(Jabber::Iq) and reply.type == :result
} # @stream.send_with_id(iq)
ret << res
end
ret
end

##
# gets all items from a pubsub node
# node:: [String]
# count:: [Fixnum]
# subid:: [String]
# return:: [Hash] { id => [Jabber::PubSub::Item] }
def get_items_from(node, count=nil)
def get_items_from(node, count=nil, subid=nil)
if subid.nil?
# Hm... no subid passed. Let's see if we can provide one.
subids = get_subids_for(node)
if ! subids.empty?
# If more than one, sorry. We'll just respect the first.
subid = subids[0]
end
end
iq = basic_pubsub_query(:get)
items = Jabber::PubSub::Items.new
items.max_items = count
items.subid = subid unless subid.nil? # if subid is still nil, we haven't any... why bother?
items.node = node
iq.pubsub.add(items)
res = nil
Expand All @@ -157,7 +192,7 @@ def get_items_from(node, count=nil)
# return:: true
def publish_item_to(node,item)
iq = basic_pubsub_query(:set)
publish = iq.pubsub.add(REXML::Element.new('publish'))
publish = iq.pubsub.add(REXML::Element.new('publish'))
publish.attributes['node'] = node

if item.kind_of?(Jabber::PubSub::Item)
Expand Down Expand Up @@ -212,7 +247,6 @@ def delete_item_from(node, item_id)
@stream.send_with_id(iq)
end


##
# purges all items on a persistent node
# node:: [String]
Expand Down Expand Up @@ -354,7 +388,7 @@ def get_subscriptions_from(node)
res = []
if reply.pubsub.first_element('subscriptions').attributes['node'] == node
reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription|
res << PubSub::Subscription.import(subscription)
res << PubSub::Subscription.import(subscription)
}
end
end
Expand Down

0 comments on commit 26d6a01

Please sign in to comment.