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!

Methods

Attributes

presences  [R]  Tracked (online) presences of this RosterItem

Public Class methods

Initialize an empty RosterItem

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 379
379:         def initialize(stream)
380:           super()
381:           @stream = stream
382:           @presences = []
383:           @presences_lock = Mutex.new
384:         end

Public Instance methods

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.

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 456
456:         def add_presence(newpres)
457:           @presences_lock.synchronize {
458:             # Delete old presences with the same JID
459:             @presences.delete_if do |pres|
460:               pres.from == newpres.from or pres.from.resource.nil?
461:             end
462: 
463:             if newpres.type == :error and newpres.from.resource.nil?
464:               # Replace by single error presence
465:               @presences = [newpres]
466:             else
467:               # Add new presence
468:               @presences.push(newpres)
469:             end
470: 
471:             @presences.sort!
472:           }
473:         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’.

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 515
515:         def cancel_subscription
516:           pres = Presence.new.set_type(:unsubscribed).set_to(jid)
517:           @stream.send(pres)
518:         end

Iterate through all received <presence/> stanzas

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 425
425:         def each_presence(&block)
426:           # Don't lock here, we don't know what block does...
427:           @presences.each { |pres|
428:             yield(pres)
429:           }
430:         end

Is any presence of this person on-line?

(Or is there any presence? Unavailable presences are deleted.)

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 415
415:         def online?
416:           @presences_lock.synchronize {
417:             @presences.select { |pres|
418:               pres.type.nil?
419:             }.size > 0
420:           }
421:         end

Get specific presence

jid:[JID] Full JID

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 435
435:         def presence(jid)
436:           @presences_lock.synchronize {
437:             @presences.each { |pres|
438:               return(pres) if pres.from == jid
439:             }
440:           }
441:           nil
442:         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 ServerError upon failure.

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 403
403:         def remove
404:           request = Iq.new_rosterset
405:           request.query.add(Jabber::Roster::RosterItem.new(jid, nil, :remove))
406:           @stream.send_with_id(request)
407:           # Removing from list is handled by Roster#handle_iq_query_roster
408:         end

Send the updated RosterItem to the server, i.e. if you modified iname, groups, …

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 389
389:         def send
390:           request = Iq.new_rosterset
391:           request.query.add(self)
392:           @stream.send(request)
393:         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. ;-)

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 484
484:         def subscribe
485:           pres = Presence.new.set_type(:subscribe).set_to(jid.strip)
486:           @stream.send(pres)
487:         end

Unsubscribe from a contact‘s presence

This method waits for a presence with type=‘unsubscribed’ from the contact. It may throw ServerError 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.

[Source]

     # File lib/xmpp4r/roster/helper/roster.rb, line 499
499:         def unsubscribe
500:           pres = Presence.new.set_type(:unsubscribe).set_to(jid.strip)
501:           @stream.send(pres) { |answer|
502:             answer.type == :unsubscribed and
503:             answer.from.strip == pres.to
504:           }
505:         end

[Validate]