Skip to content

Commit

Permalink
[FEATURE] Add support for invitation elements to MUCUser presence
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Feb 25, 2012
1 parent c0db418 commit d46d5d7
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
67 changes: 67 additions & 0 deletions lib/blather/stanza/presence/muc_user.rb
Expand Up @@ -60,6 +60,10 @@ def password=(var)
password_node.content = var
end

def invite?
!!invite
end

def muc_user
unless muc_user = find_first('ns:x', :ns => self.class.registered_ns)
self << (muc_user = XMPPNode.new('x', self.document))
Expand Down Expand Up @@ -94,6 +98,15 @@ def find_password_node
muc_user.find_first 'ns:password', :ns => self.class.registered_ns
end

def invite
if invite = muc_user.find_first('ns:invite', :ns => self.class.registered_ns)
Invite.new invite
else
muc_user << (invite = Invite.new nil, nil, nil, self.document)
invite
end
end

class Item < XMPPNode
def self.new(affiliation = nil, role = nil, jid = nil, document = nil)
new_node = super :item, document
Expand Down Expand Up @@ -166,6 +179,60 @@ def code=(val)
write_attr :code, val
end
end

class Invite < XMPPNode
def self.new(to = nil, from = nil, reason = nil, document = nil)
new_node = super :invite, document

case to
when self
to.document ||= document
return to
when Nokogiri::XML::Node
new_node.inherit to
when Hash
new_node.to = to[:to]
new_node.from = to[:from]
new_node.reason = to[:reason]
else
new_node.to = to
new_node.from = from
new_node.reason = reason
end
new_node
end

def to
read_attr :to
end

def to=(val)
write_attr :to, val
end

def from
read_attr :from
end

def from=(val)
write_attr :from, val
end

def reason
reason_node.content.strip
end

def reason=(val)
reason_node.content = val
end

def reason_node
unless reason = find_first('ns:reason', :ns => MUCUser.registered_ns)
self << (reason = XMPPNode.new('reason', self.document))
end
reason
end
end
end # MUC

end # Presence
Expand Down
56 changes: 56 additions & 0 deletions spec/blather/stanza/presence/muc_user_spec.rb
Expand Up @@ -17,6 +17,23 @@ def muc_user_xml
XML
end

def muc_invite_xml
<<-XML
<message
from='coven@chat.shakespeare.lit'
id='nzd143v8'
to='hecate@shakespeare.lit'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<invite to='hecate@shakespeare.lit' from='crone1@shakespeare.lit/desktop'>
<reason>
Hey Hecate, this is the place for all good witches!
</reason>
</invite>
</x>
</message>
XML
end

describe 'Blather::Stanza::Presence::MUCUser' do
it 'registers itself' do
Blather::XMPPNode.class_from_registration(:x, 'http://jabber.org/protocol/muc#user' ).must_equal Blather::Stanza::Presence::MUCUser
Expand Down Expand Up @@ -84,4 +101,43 @@ def muc_user_xml
muc_user.password = 'hello_world'
muc_user.password.must_equal 'hello_world'
end

describe "with an invite element" do
it "should be an #invite?" do
muc_user = Blather::XMPPNode.import(parse_stanza(muc_invite_xml).root)
muc_user.invite?.must_equal true
end

it "should know the invite attributes properly" do
muc_user = Blather::XMPPNode.import(parse_stanza(muc_invite_xml).root)
invite = muc_user.invite
invite.to.must_equal 'hecate@shakespeare.lit'
invite.from.must_equal 'crone1@shakespeare.lit/desktop'
invite.reason.must_equal 'Hey Hecate, this is the place for all good witches!'
end

it "must be able to set the to jid" do
muc_user = Blather::Stanza::Presence::MUCUser.new
invite = muc_user.invite
invite.to.must_equal nil
invite.to = 'foo@bar.com'
invite.to.must_equal 'foo@bar.com'
end

it "must be able to set the from jid" do
muc_user = Blather::Stanza::Presence::MUCUser.new
invite = muc_user.invite
invite.from.must_equal nil
invite.from = 'foo@bar.com'
invite.from.must_equal 'foo@bar.com'
end

it "must be able to set the reason" do
muc_user = Blather::Stanza::Presence::MUCUser.new
invite = muc_user.invite
invite.reason.must_equal ''
invite.reason = 'Please join'
invite.reason.must_equal 'Please join'
end
end
end

0 comments on commit d46d5d7

Please sign in to comment.