Class | Jabber::Roster::Helper::RosterItem |
In: |
lib/xmpp4r/roster/helper/roster.rb
|
Parent: | Jabber::Roster::RosterItem |
These are extensions to RosterItem to carry presence information. This information is not stored in XML!
presences | [R] | Tracked (online) presences of this RosterItem |
Initialize an empty RosterItem
# File lib/xmpp4r/roster/helper/roster.rb, line 371 371: def initialize(stream) 372: super() 373: @stream = stream 374: @presences = [] 375: @presences_lock = Mutex.new 376: end
Add presence and sort presences (unless type is :unavailable or :error)
This overwrites previous stanzas with the same destination JID to keep track of resources. Presence stanzas with type == :unavailable or type == :error will be deleted as this indicates that this resource has gone offline.
If type == :error and the presence‘s origin has no specific resource the contact is treated completely offline.
# File lib/xmpp4r/roster/helper/roster.rb, line 462 462: def add_presence(newpres) 463: @presences_lock.synchronize { 464: # Delete old presences with the same JID 465: @presences.delete_if do |pres| 466: pres.from == newpres.from or pres.from.resource.nil? 467: end 468: 469: if newpres.type == :error and newpres.from.resource.nil? 470: # Replace by single error presence 471: @presences = [newpres] 472: else 473: # Add new presence 474: @presences.push(newpres) 475: end 476: 477: @presences.sort! 478: } 479: end
Deny the contact to see your presence.
This method will not wait and returns immediately as you will need no confirmation for this action.
Though, you will get a roster update for that item, carrying either subscription=‘to’ or ‘none’.
# File lib/xmpp4r/roster/helper/roster.rb, line 521 521: def cancel_subscription 522: pres = Presence.new.set_type(:unsubscribed).set_to(jid) 523: @stream.send(pres) 524: end
Import another element, also import presences if xe is a RosterItem
return: | [RosterItem] self |
# File lib/xmpp4r/roster/helper/roster.rb, line 382 382: def import(xe) 383: super 384: if xe.kind_of?(RosterItem) 385: xe.each_presence { |pres| 386: add_presence(Presence.new.import(pres)) 387: } 388: end 389: self 390: end
Remove item
This cancels both subscription from the contact to you and from you to the contact.
The methods waits for a roster push from the server (success) or throws ErrorException upon failure.
# File lib/xmpp4r/roster/helper/roster.rb, line 409 409: def remove 410: request = Iq.new_rosterset 411: request.query.add(Jabber::Roster::RosterItem.new(jid, nil, :remove)) 412: @stream.send_with_id(request) { true } 413: # Removing from list is handled by Roster#handle_iq 414: end
Send the updated RosterItem to the server, i.e. if you modified iname, groups, …
# File lib/xmpp4r/roster/helper/roster.rb, line 395 395: def send 396: request = Iq.new_rosterset 397: request.query.add(self) 398: @stream.send(request) 399: end
Send subscription request to the user
The block given to Jabber::Roster::Roster#add_update_callback will be called, carrying the RosterItem with ask="subscribe"
This function returns immediately after sending the subscription request and will not wait of approval or declination as it may take months for the contact to decide. ;-)
# File lib/xmpp4r/roster/helper/roster.rb, line 490 490: def subscribe 491: pres = Presence.new.set_type(:subscribe).set_to(jid.strip) 492: @stream.send(pres) 493: end
Unsubscribe from a contact‘s presence
This method waits for a presence with type=‘unsubscribed’ from the contact. It may throw ErrorException upon failure.
subscription attribute of the item is from or none afterwards. As long as you don‘t remove that item and subscription=‘from’ the contact is subscribed to your presence.
# File lib/xmpp4r/roster/helper/roster.rb, line 505 505: def unsubscribe 506: pres = Presence.new.set_type(:unsubscribe).set_to(jid.strip) 507: @stream.send(pres) { |answer| 508: answer.type == :unsubscribed and 509: answer.from.strip == pres.to 510: } 511: end