lib/blather/stanza/message.rb in blather-0.4.13 vs lib/blather/stanza/message.rb in blather-0.4.14
- old
+ new
@@ -157,10 +157,13 @@
#
# @handler :message
class Message < Stanza
VALID_TYPES = [:chat, :error, :groupchat, :headline, :normal].freeze
+ VALID_CHAT_STATES = [:active, :composing, :gone, :inactive, :paused].freeze
+ CHAT_STATE_NS = 'http://jabber.org/protocol/chatstates'.freeze
+
HTML_NS = 'http://jabber.org/protocol/xhtml-im'.freeze
HTML_BODY_NS = 'http://www.w3.org/1999/xhtml'.freeze
register :message
@@ -187,10 +190,11 @@
def self.new(to = nil, body = nil, type = :chat)
node = super :message
node.to = to
node.type = type
node.body = body
+ node.chat_state = :active if [:chat, :groupchat].include?(type)
node
end
# Check if the Message is of type :chat
#
@@ -260,30 +264,31 @@
self << (h = XMPPNode.new('html', self.document))
h.namespace = HTML_NS
end
unless b = h.find_first('ns:body', :ns => HTML_BODY_NS)
- h << (b = XMPPNode.new('body', self.document))
+ b = XMPPNode.new('body', self.document)
b.namespace = HTML_BODY_NS
+ h << b
end
b
end
# Get the message xhtml
#
# @return [String]
def xhtml
- self.xhtml_node.content.strip
+ self.xhtml_node.inner_html.strip
end
# Set the message xhtml
# This will use Nokogiri to ensure the xhtml is valid
#
# @param [#to_s] valid xhtml
def xhtml=(xhtml_body)
- self.xhtml_node.content = Nokogiri::XML(xhtml_body).to_xhtml
+ self.xhtml_node.inner_html = Nokogiri::XML::DocumentFragment.parse(xhtml_body)
end
# Get the message subject
#
# @return [String]
@@ -328,9 +333,35 @@
end
# Returns the message's x:data form child
def form
X.find_or_create self
+ end
+
+ # Get the message chat state
+ #
+ # @return [Symbol]
+ def chat_state
+ if (elem = find_first('ns:*', :ns => CHAT_STATE_NS)) && VALID_CHAT_STATES.include?(name = elem.name.to_sym)
+ name
+ end
+ end
+
+ # Set the message chat state
+ #
+ # @param [#to_s] chat_state the message chat state. Must be one of VALID_CHAT_STATES
+ def chat_state=(chat_state)
+ if chat_state && !VALID_CHAT_STATES.include?(chat_state.to_sym)
+ raise ArgumentError, "Invalid Chat State (#{chat_state}), use: #{VALID_CHAT_STATES*' '}"
+ end
+
+ xpath('ns:*', :ns => CHAT_STATE_NS).remove
+
+ if chat_state
+ state = XMPPNode.new(chat_state, self.document)
+ state.namespace = CHAT_STATE_NS
+ self << state
+ end
end
end
end
end