57 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			GDScript
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			GDScript
		
	
	
	
	
	
| 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 feature_promises := Array()
 | |
|     var poly_services := Dictionary() # of Jid to PolyService
 | |
|     for item in query.value.children:
 | |
|         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[item.attributes["jid"]] = poly_service
 | |
| 
 | |
|         feature_promises.push_back(connection.promise_iq(
 | |
|             item.attributes["jid"], "get", 
 | |
|             Stanza.disco_info_queury,
 | |
|             Stanza.yield_as_is()))
 | |
| 
 | |
|     while not Sums.are_promises_done(feature_promises):
 | |
|         yield()
 | |
| 
 | |
|     # todo: Allow partial success.
 | |
|     if not Sums.are_promises_ok(feature_promises):
 | |
|         return Sums.Result.make_error(Sums.collect_promise_errors(feature_promises))
 | |
| 
 | |
|     for feature_response in Sums.collect_promise_values(feature_promises):
 | |
|         var jid = feature_response.attributes["from"]
 | |
| 
 | |
|         var features := Stanza.unwrap_query_result(feature_response)
 | |
|         if not features.is_ok:
 | |
|             # todo: Signal the error.
 | |
|             continue
 | |
| 
 | |
|         var poly_service = poly_services[jid]
 | |
|         poly_service.muc = load("res://scenes/MucService.gd").new().try_init(
 | |
|             connection, jid, features.value)
 | |
| 
 | |
|     return Sums.Result.make_value(poly_services.values())
 |