2019-04-24 11:25:54 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2019-09-18 07:17:47 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2019-04-24 11:25:54 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.google.common.cache.Cache;
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
|
2019-04-25 19:10:50 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Collections;
|
2019-04-24 11:25:54 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2019-04-25 19:10:50 +00:00
|
|
|
import eu.siacs.conversations.http.HttpConnectionManager;
|
2019-04-24 11:25:54 +00:00
|
|
|
import eu.siacs.conversations.http.services.MuclumbusService;
|
2019-04-25 19:10:50 +00:00
|
|
|
import okhttp3.OkHttpClient;
|
2019-09-18 07:55:18 +00:00
|
|
|
import okhttp3.ResponseBody;
|
2019-04-24 11:25:54 +00:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
|
|
|
public class ChannelDiscoveryService {
|
|
|
|
|
|
|
|
private final XmppConnectionService service;
|
|
|
|
|
|
|
|
|
2019-04-25 19:10:50 +00:00
|
|
|
private MuclumbusService muclumbusService;
|
2019-04-24 11:25:54 +00:00
|
|
|
|
|
|
|
private final Cache<String, List<MuclumbusService.Room>> cache;
|
|
|
|
|
2019-09-18 07:17:47 +00:00
|
|
|
ChannelDiscoveryService(XmppConnectionService service) {
|
2019-04-24 11:25:54 +00:00
|
|
|
this.service = service;
|
2019-04-25 19:10:50 +00:00
|
|
|
this.cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
|
|
|
|
}
|
|
|
|
|
2019-09-18 07:17:47 +00:00
|
|
|
void initializeMuclumbusService() {
|
2019-04-25 19:10:50 +00:00
|
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
|
|
|
if (service.useTorToConnect()) {
|
|
|
|
try {
|
|
|
|
builder.proxy(HttpConnectionManager.getProxy());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Unable to use Tor proxy", e);
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 11:25:54 +00:00
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
2019-04-25 19:10:50 +00:00
|
|
|
.client(builder.build())
|
2019-04-24 11:25:54 +00:00
|
|
|
.baseUrl(Config.CHANNEL_DISCOVERY)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
|
|
.callbackExecutor(Executors.newSingleThreadExecutor())
|
|
|
|
.build();
|
|
|
|
this.muclumbusService = retrofit.create(MuclumbusService.class);
|
|
|
|
}
|
|
|
|
|
2019-09-18 07:17:47 +00:00
|
|
|
void discover(String query, OnChannelSearchResultsFound onChannelSearchResultsFound) {
|
2019-04-24 11:25:54 +00:00
|
|
|
final boolean all = query == null || query.trim().isEmpty();
|
|
|
|
Log.d(Config.LOGTAG, "discover channels. query=" + query);
|
|
|
|
List<MuclumbusService.Room> result = cache.getIfPresent(all ? "" : query);
|
|
|
|
if (result != null) {
|
|
|
|
onChannelSearchResultsFound.onChannelSearchResultsFound(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (all) {
|
|
|
|
discoverChannels(onChannelSearchResultsFound);
|
|
|
|
} else {
|
|
|
|
discoverChannels(query, onChannelSearchResultsFound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void discoverChannels(OnChannelSearchResultsFound listener) {
|
|
|
|
Call<MuclumbusService.Rooms> call = muclumbusService.getRooms(1);
|
|
|
|
try {
|
|
|
|
call.enqueue(new Callback<MuclumbusService.Rooms>() {
|
|
|
|
@Override
|
2019-09-18 07:17:47 +00:00
|
|
|
public void onResponse(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Response<MuclumbusService.Rooms> response) {
|
2019-04-24 11:25:54 +00:00
|
|
|
final MuclumbusService.Rooms body = response.body();
|
|
|
|
if (body == null) {
|
2019-09-18 07:17:47 +00:00
|
|
|
listener.onChannelSearchResultsFound(Collections.emptyList());
|
2019-09-18 07:55:18 +00:00
|
|
|
logError(response);
|
2019-04-24 11:25:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cache.put("", body.items);
|
|
|
|
listener.onChannelSearchResultsFound(body.items);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-18 07:17:47 +00:00
|
|
|
public void onFailure(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
|
2019-04-25 19:10:50 +00:00
|
|
|
listener.onChannelSearchResultsFound(Collections.emptyList());
|
2019-04-24 11:25:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void discoverChannels(final String query, OnChannelSearchResultsFound listener) {
|
|
|
|
Call<MuclumbusService.SearchResult> searchResultCall = muclumbusService.search(new MuclumbusService.SearchRequest(query));
|
|
|
|
|
|
|
|
searchResultCall.enqueue(new Callback<MuclumbusService.SearchResult>() {
|
|
|
|
@Override
|
2019-09-18 07:17:47 +00:00
|
|
|
public void onResponse(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Response<MuclumbusService.SearchResult> response) {
|
|
|
|
final MuclumbusService.SearchResult body = response.body();
|
2019-04-24 11:25:54 +00:00
|
|
|
if (body == null) {
|
2019-09-18 07:17:47 +00:00
|
|
|
listener.onChannelSearchResultsFound(Collections.emptyList());
|
2019-09-18 07:55:18 +00:00
|
|
|
logError(response);
|
2019-04-24 11:25:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cache.put(query, body.result.items);
|
|
|
|
listener.onChannelSearchResultsFound(body.result.items);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-18 07:17:47 +00:00
|
|
|
public void onFailure(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
|
2019-04-25 19:10:50 +00:00
|
|
|
listener.onChannelSearchResultsFound(Collections.emptyList());
|
2019-04-24 11:25:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-18 07:55:18 +00:00
|
|
|
private static void logError(final Response response) {
|
|
|
|
final ResponseBody errorBody = response.errorBody();
|
|
|
|
Log.d(Config.LOGTAG, "code from muclumbus=" + response.code());
|
|
|
|
if (errorBody == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Log.d(Config.LOGTAG,"error body="+errorBody.string());
|
|
|
|
} catch (IOException e) {
|
|
|
|
//ignored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 11:25:54 +00:00
|
|
|
public interface OnChannelSearchResultsFound {
|
|
|
|
void onChannelSearchResultsFound(List<MuclumbusService.Room> results);
|
|
|
|
}
|
|
|
|
}
|