Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/iNPUTmice/Conversations.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java')
-rw-r--r--src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java208
1 files changed, 125 insertions, 83 deletions
diff --git a/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java b/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java
index c46568c3e..2f9553bfc 100644
--- a/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java
+++ b/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java
@@ -4,6 +4,7 @@ import android.util.Log;
import androidx.annotation.NonNull;
+import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
@@ -39,7 +40,6 @@ public class ChannelDiscoveryService {
private final XmppConnectionService service;
-
private MuclumbusService muclumbusService;
private final Cache<String, List<Room>> cache;
@@ -50,16 +50,21 @@ public class ChannelDiscoveryService {
}
void initializeMuclumbusService() {
+ if (Strings.isNullOrEmpty(Config.CHANNEL_DISCOVERY)) {
+ this.muclumbusService = null;
+ return;
+ }
final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder();
if (service.useTorToConnect()) {
builder.proxy(HttpConnectionManager.getProxy());
}
- Retrofit retrofit = new Retrofit.Builder()
- .client(builder.build())
- .baseUrl(Config.CHANNEL_DISCOVERY)
- .addConverterFactory(GsonConverterFactory.create())
- .callbackExecutor(Executors.newSingleThreadExecutor())
- .build();
+ final Retrofit retrofit =
+ new Retrofit.Builder()
+ .client(builder.build())
+ .baseUrl(Config.CHANNEL_DISCOVERY)
+ .addConverterFactory(GsonConverterFactory.create())
+ .callbackExecutor(Executors.newSingleThreadExecutor())
+ .build();
this.muclumbusService = retrofit.create(MuclumbusService.class);
}
@@ -67,7 +72,10 @@ public class ChannelDiscoveryService {
cache.invalidateAll();
}
- void discover(@NonNull final String query, Method method, OnChannelSearchResultsFound onChannelSearchResultsFound) {
+ void discover(
+ @NonNull final String query,
+ Method method,
+ OnChannelSearchResultsFound onChannelSearchResultsFound) {
final List<Room> result = cache.getIfPresent(key(method, query));
if (result != null) {
onChannelSearchResultsFound.onChannelSearchResultsFound(result);
@@ -84,59 +92,82 @@ public class ChannelDiscoveryService {
}
}
- private void discoverChannelsJabberNetwork(OnChannelSearchResultsFound listener) {
- Call<MuclumbusService.Rooms> call = muclumbusService.getRooms(1);
- try {
- call.enqueue(new Callback<MuclumbusService.Rooms>() {
- @Override
- public void onResponse(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Response<MuclumbusService.Rooms> response) {
- final MuclumbusService.Rooms body = response.body();
- if (body == null) {
- listener.onChannelSearchResultsFound(Collections.emptyList());
- logError(response);
- return;
+ private void discoverChannelsJabberNetwork(final OnChannelSearchResultsFound listener) {
+ if (muclumbusService == null) {
+ listener.onChannelSearchResultsFound(Collections.emptyList());
+ return;
+ }
+ final Call<MuclumbusService.Rooms> call = muclumbusService.getRooms(1);
+ call.enqueue(
+ new Callback<MuclumbusService.Rooms>() {
+ @Override
+ public void onResponse(
+ @NonNull Call<MuclumbusService.Rooms> call,
+ @NonNull Response<MuclumbusService.Rooms> response) {
+ final MuclumbusService.Rooms body = response.body();
+ if (body == null) {
+ listener.onChannelSearchResultsFound(Collections.emptyList());
+ logError(response);
+ return;
+ }
+ cache.put(key(Method.JABBER_NETWORK, ""), body.items);
+ listener.onChannelSearchResultsFound(body.items);
}
- cache.put(key(Method.JABBER_NETWORK, ""), body.items);
- listener.onChannelSearchResultsFound(body.items);
- }
- @Override
- public void onFailure(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Throwable throwable) {
- Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
- listener.onChannelSearchResultsFound(Collections.emptyList());
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
+ @Override
+ public void onFailure(
+ @NonNull Call<MuclumbusService.Rooms> call,
+ @NonNull Throwable throwable) {
+ Log.d(
+ Config.LOGTAG,
+ "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY,
+ throwable);
+ listener.onChannelSearchResultsFound(Collections.emptyList());
+ }
+ });
}
- private void discoverChannelsJabberNetwork(final String query, OnChannelSearchResultsFound listener) {
- MuclumbusService.SearchRequest searchRequest = new MuclumbusService.SearchRequest(query);
- Call<MuclumbusService.SearchResult> searchResultCall = muclumbusService.search(searchRequest);
-
- searchResultCall.enqueue(new Callback<MuclumbusService.SearchResult>() {
- @Override
- public void onResponse(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Response<MuclumbusService.SearchResult> response) {
- final MuclumbusService.SearchResult body = response.body();
- if (body == null) {
- listener.onChannelSearchResultsFound(Collections.emptyList());
- logError(response);
- return;
- }
- cache.put(key(Method.JABBER_NETWORK, query), body.result.items);
- listener.onChannelSearchResultsFound(body.result.items);
- }
+ private void discoverChannelsJabberNetwork(
+ final String query, final OnChannelSearchResultsFound listener) {
+ if (muclumbusService == null) {
+ listener.onChannelSearchResultsFound(Collections.emptyList());
+ return;
+ }
+ final MuclumbusService.SearchRequest searchRequest =
+ new MuclumbusService.SearchRequest(query);
+ final Call<MuclumbusService.SearchResult> searchResultCall =
+ muclumbusService.search(searchRequest);
+ searchResultCall.enqueue(
+ new Callback<MuclumbusService.SearchResult>() {
+ @Override
+ public void onResponse(
+ @NonNull Call<MuclumbusService.SearchResult> call,
+ @NonNull Response<MuclumbusService.SearchResult> response) {
+ final MuclumbusService.SearchResult body = response.body();
+ if (body == null) {
+ listener.onChannelSearchResultsFound(Collections.emptyList());
+ logError(response);
+ return;
+ }
+ cache.put(key(Method.JABBER_NETWORK, query), body.result.items);
+ listener.onChannelSearchResultsFound(body.result.items);
+ }
- @Override
- public void onFailure(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Throwable throwable) {
- Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
- listener.onChannelSearchResultsFound(Collections.emptyList());
- }
- });
+ @Override
+ public void onFailure(
+ @NonNull Call<MuclumbusService.SearchResult> call,
+ @NonNull Throwable throwable) {
+ Log.d(
+ Config.LOGTAG,
+ "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY,
+ throwable);
+ listener.onChannelSearchResultsFound(Collections.emptyList());
+ }
+ });
}
- private void discoverChannelsLocalServers(final String query, final OnChannelSearchResultsFound listener) {
+ private void discoverChannelsLocalServers(
+ final String query, final OnChannelSearchResultsFound listener) {
final Map<Jid, Account> localMucService = getLocalMucServices();
Log.d(Config.LOGTAG, "checking with " + localMucService.size() + " muc services");
if (localMucService.size() == 0) {
@@ -156,38 +187,49 @@ public class ChannelDiscoveryService {
for (Map.Entry<Jid, Account> entry : localMucService.entrySet()) {
IqPacket itemsRequest = service.getIqGenerator().queryDiscoItems(entry.getKey());
queriesInFlight.incrementAndGet();
- service.sendIqPacket(entry.getValue(), itemsRequest, (account, itemsResponse) -> {
- if (itemsResponse.getType() == IqPacket.TYPE.RESULT) {
- final List<Jid> items = IqParser.items(itemsResponse);
- for (Jid item : items) {
- IqPacket infoRequest = service.getIqGenerator().queryDiscoInfo(item);
- queriesInFlight.incrementAndGet();
- service.sendIqPacket(account, infoRequest, new OnIqPacketReceived() {
- @Override
- public void onIqPacketReceived(Account account, IqPacket infoResponse) {
- if (infoResponse.getType() == IqPacket.TYPE.RESULT) {
- final Room room = IqParser.parseRoom(infoResponse);
- if (room != null) {
- rooms.add(room);
- }
- if (queriesInFlight.decrementAndGet() <= 0) {
- finishDiscoSearch(rooms, query, listener);
- }
- } else {
- queriesInFlight.decrementAndGet();
- }
+ service.sendIqPacket(
+ entry.getValue(),
+ itemsRequest,
+ (account, itemsResponse) -> {
+ if (itemsResponse.getType() == IqPacket.TYPE.RESULT) {
+ final List<Jid> items = IqParser.items(itemsResponse);
+ for (Jid item : items) {
+ IqPacket infoRequest =
+ service.getIqGenerator().queryDiscoInfo(item);
+ queriesInFlight.incrementAndGet();
+ service.sendIqPacket(
+ account,
+ infoRequest,
+ new OnIqPacketReceived() {
+ @Override
+ public void onIqPacketReceived(
+ Account account, IqPacket infoResponse) {
+ if (infoResponse.getType()
+ == IqPacket.TYPE.RESULT) {
+ final Room room =
+ IqParser.parseRoom(infoResponse);
+ if (room != null) {
+ rooms.add(room);
+ }
+ if (queriesInFlight.decrementAndGet() <= 0) {
+ finishDiscoSearch(rooms, query, listener);
+ }
+ } else {
+ queriesInFlight.decrementAndGet();
+ }
+ }
+ });
}
- });
- }
- }
- if (queriesInFlight.decrementAndGet() <= 0) {
- finishDiscoSearch(rooms, query, listener);
- }
- });
+ }
+ if (queriesInFlight.decrementAndGet() <= 0) {
+ finishDiscoSearch(rooms, query, listener);
+ }
+ });
}
}
- private void finishDiscoSearch(List<Room> rooms, String query, OnChannelSearchResultsFound listener) {
+ private void finishDiscoSearch(
+ List<Room> rooms, String query, OnChannelSearchResultsFound listener) {
Collections.sort(rooms);
cache.put(key(Method.LOCAL_SERVER, ""), rooms);
if (query.isEmpty()) {
@@ -241,7 +283,7 @@ public class ChannelDiscoveryService {
try {
Log.d(Config.LOGTAG, "error body=" + errorBody.string());
} catch (IOException e) {
- //ignored
+ // ignored
}
}