1# frozen_string_literal: true
2
3require "blather"
4
5module Snikket
6 class Launch < Blather::Stanza::Iq
7 register nil, "launch", "xmpp:snikket.org/hosting/v1"
8
9 def self.new(type=nil, to=nil, id=nil, domain: nil)
10 stanza = super(type || :set, to, id)
11 node = Nokogiri::XML::Node.new("launch", stanza.document)
12 node.default_namespace = registered_ns
13 stanza << node
14 stanza.domain = domain if domain
15 stanza
16 end
17
18 def domain=(domain)
19 query.at_xpath("./ns:domain", ns: self.class.registered_ns)&.remove
20 node = Nokogiri::XML::Node.new("domain", document)
21 node.default_namespace = self.class.registered_ns
22 node.content = domain
23 query << node
24 end
25
26 def query
27 at_xpath("./ns:launch", ns: self.class.registered_ns)
28 end
29 end
30
31 class Launched < Blather::Stanza::Iq
32 register :snikket_launched, "launched", "xmpp:snikket.org/hosting/v1"
33
34 def instance_id
35 query
36 .at_xpath("./ns:instance-id", ns: self.class.registered_ns)
37 &.content
38 end
39
40 def bootstrap_token
41 query
42 .at_xpath("./ns:bootstrap/ns:token", ns: self.class.registered_ns)
43 &.content
44 end
45
46 def bootstrap_uri(instance_domain)
47 "https://#{instance_domain}/invites_bootstrap?token=#{bootstrap_token}"
48 end
49
50 def query
51 at_xpath("./ns:launched", ns: self.class.registered_ns)
52 end
53 end
54
55 class Instance < Blather::Stanza::Iq
56 register :snikket_instance, "instance", "xmpp:snikket.org/hosting/v1"
57
58 def self.new(type=nil, to=nil, id=nil, instance_id: nil)
59 stanza = super(type || :get, to, id)
60 node = Nokogiri::XML::Node.new("instance", stanza.document)
61 node.default_namespace = registered_ns
62 stanza << node
63 stanza.instance_id = instance_id if instance_id
64 stanza
65 end
66
67 def inherit(*)
68 query.remove
69 super
70 end
71
72 def instance_id=(instance_id)
73 query.at_xpath("./ns:instance-id", ns: self.class.registered_ns)&.remove
74 node = Nokogiri::XML::Node.new("instance-id", document)
75 node.default_namespace = self.class.registered_ns
76 node.content = instance_id
77 query << node
78 end
79
80 def instance_id
81 query
82 .at_xpath("./ns:instance-id", ns: self.class.registered_ns)
83 &.content
84 end
85
86 def update_needed?
87 !!query.at_xpath("./ns:update-needed", ns: self.class.registered_ns)
88 end
89
90 def operation
91 query.at_xpath("./ns:operation", ns: self.class.registered_ns)
92 end
93
94 def status
95 query
96 .at_xpath("./ns:status", ns: self.class.registered_ns)
97 &.content&.to_sym
98 end
99
100 def query
101 at_xpath("./ns:instance", ns: self.class.registered_ns)
102 end
103 end
104end