2023-09-02 13:51:00 +00:00
|
|
|
extends Reference
|
|
|
|
class_name PolyServiceBuilder
|
|
|
|
|
|
|
|
## https://xmpp.org/extensions/xep-0030.html
|
|
|
|
|
|
|
|
func request(connection: Connections.Connection) -> Sums.Promise:
|
|
|
|
return connection.promise_iq(connection.domain, "get",
|
|
|
|
Stanza.disco_items_queury,
|
|
|
|
_service_discovery(connection))
|
|
|
|
|
|
|
|
func _service_discovery(connection: Connections.Connection) -> Sums.Result:
|
|
|
|
var response = yield()
|
|
|
|
if not response.is_ok:
|
|
|
|
return response
|
|
|
|
|
|
|
|
var query := Stanza.unwrap_query_result(response.value)
|
|
|
|
if not query.is_ok:
|
|
|
|
return query.value
|
|
|
|
|
|
|
|
var poly_services := Array()
|
|
|
|
for item in query.value.children:
|
2023-09-02 14:15:15 +00:00
|
|
|
# todo: Do this at the same time, in separate coroutines.
|
2023-09-02 13:51:00 +00:00
|
|
|
if not item.is_element() or item.name != "item":
|
|
|
|
continue
|
|
|
|
|
|
|
|
var poly_service = load("res://scenes/PolyService.gd").new()
|
|
|
|
poly_service.jid = item.attributes["jid"]
|
|
|
|
if "name" in item.attributes:
|
|
|
|
poly_service.name = item.attributes["name"]
|
|
|
|
|
|
|
|
poly_services.push_back(poly_service)
|
|
|
|
|
|
|
|
var feature_promise = connection.promise_iq(
|
|
|
|
item.attributes["jid"], "get",
|
|
|
|
Stanza.disco_info_queury,
|
|
|
|
Stanza.yield_as_is())
|
|
|
|
|
|
|
|
if not feature_promise.is_done:
|
|
|
|
yield(feature_promise, "done")
|
|
|
|
|
|
|
|
# todo: Propagate error if it's unrelated to service discovery itself.
|
|
|
|
if feature_promise.is_ok:
|
|
|
|
# If features arrived, - populate services.
|
|
|
|
var jid = feature_promise.value.attributes["from"]
|
|
|
|
|
|
|
|
var features := Stanza.unwrap_query_result(feature_promise.value)
|
|
|
|
if not features.is_ok:
|
|
|
|
# todo: Propagate the error.
|
|
|
|
continue
|
|
|
|
|
|
|
|
poly_service.muc = load("res://scenes/MucService.gd").new().try_init(
|
|
|
|
connection, jid, features.value)
|
|
|
|
|
|
|
|
return Sums.Result.make_value(poly_services)
|