Class: C3s::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/pubsub/publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, service_name) ⇒ Publisher

Initializes the publisher

client
Jabber::Client

the client publishing

service
String

pubsub service name (eg: pubsub.jabber)



11
12
13
14
15
# File 'lib/pubsub/publisher.rb', line 11

def initialize(client, service_name)
  @client = client
  @service = service_name
  @pubsub = Jabber::PubSub::ServiceHelper.new(@client, @service)
end

Instance Method Details

#create_collection_node(name, config) ⇒ Object

Creates the collection node if needed

name
String

collection node name

config
PubSub::NodeConfig

node configuration



68
69
70
71
72
73
74
# File 'lib/pubsub/publisher.rb', line 68

def create_collection_node(name, config)
  $LOG.debug("Creating collection  node #{name}...")
  $LOG.debug("Node config: #{config.options.inspect}")
  return if node_exists?(name)
  
  node = @pubsub.create_collection_node(name, config)
end

#create_leaf_node(name, config) ⇒ Object

Creates a leaf node to publish if needed This node must be associated with the collection node

name
String

the name of the leaf node

config
PubSub::NodeConfig

node configuration



81
82
83
84
85
86
87
# File 'lib/pubsub/publisher.rb', line 81

def create_leaf_node(name, config)
  $LOG.debug("Creating leaf node #{name}")
  $LOG.debug("Node config: #{config.options.inspect}") if config
  return if node_exists?(name)
  
  node = @pubsub.create_node(name, config)
end

#create_node(nodename, options = {}) ⇒ Object

Creates a pubsub node

nodename
String

the node name

options
Hash

hash with options. These can be:

  • :collection => true or false

  • :father => the father node or nil if its root



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pubsub/publisher.rb', line 37

def create_node(nodename, options={})
  return if node_exists?(nodename)
  config = Jabber::PubSub::NodeConfig.new
  config = nil if !options || options.empty? # avoid creation of <configure/>
  
  if options[:father]
    config.options = config.options.merge({'pubsub#collection' => options[:father]})
  end
  
  if options[:collection]
    node = create_collection_node(nodename, config)
  else
    node = create_leaf_node(nodename, config)
  end
  
  # grant owner to admin user (eg: [email protected])
  grant_owner_to_admin(node)
end

#create_tree(c3snode) ⇒ Object

Creates the collection node(s) and leaf node if needed

c3snode
C3s::Node

c3s pubsub node (eg: location:[email protected]/resource)



20
21
22
23
24
25
26
27
28
29
# File 'lib/pubsub/publisher.rb', line 20

def create_tree(c3snode)
  provider = c3snode.provider # provider (eg: location)
  collection = "#{provider}:#{c3snode.jid.bare.to_s}" # collection (eg: location:[email protected])
  leaf = c3snode.to_s # leaf (eg: location:[email protected]/default)
  
  # create nodes
  create_node(provider, {:father => "", :collection => true})
  create_node(collection, {:father => provider, :collection => true})
  create_node(leaf, {:father => collection, :collection => false})
end

#delete_node(nodename) ⇒ Object

Deletes a pubsub node

nodename
String

the node name

TODO: i should delete all leaf nodes inside a collection node!



60
61
62
# File 'lib/pubsub/publisher.rb', line 60

def delete_node(nodename)
  @pubsub.delete_node(nodename)
end

#grant_owner_to_admin(node) ⇒ Object

Grants node permissions to user admin@domain so that we can have an admin that can edit/delete every node.

node
Pubsub::Node

the node to grant permissions to



93
94
95
96
# File 'lib/pubsub/publisher.rb', line 93

def grant_owner_to_admin(node)
  return unless node
  @pubsub.set_affiliations(node, "admin@#{@client.config['url']}", 'owner')
end

#node_exists?(name) ⇒ Boolean

Checks for existance of a node with a given name. This node can be a leaf or a collection

name
String

the node name

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
# File 'lib/pubsub/publisher.rb', line 115

def node_exists?(name)
  return true if NodeTracker.include?(name)
  nodebrowser = Jabber::PubSub::NodeBrowser.new(@client)
  nodebrowser.get_info(@service, name)
  NodeTracker.add_node(name)
  true
rescue Jabber::ServerError => e
  return false if e.to_s.include?("item-not-found")
end

#publish(c3snode, content) ⇒ Object

Publishes content to the leaf node

node
String

the node to plublish to

content
REXML::Element

content xml to publish into node



102
103
104
105
106
107
108
109
# File 'lib/pubsub/publisher.rb', line 102

def publish(c3snode, content)
  add_publish_date(content) if !has_publish_date(content)
  item = Jabber::PubSub::Item.new
  item.add(content)
  @pubsub.publish_item_to(c3snode.to_s, item)
rescue Exception => e
  $LOG.error("#{e.inspect} #{e.backtrace.join("\n")}")
end