use of org.matrix.androidsdk.rest.model.BulkLookupResponse in project matrix-android-sdk by matrix-org.
the class ThirdPidRestClient method lookup3Pids.
/**
* Retrieve user matrix id from a 3rd party id.
* @param addresses 3rd party ids
* @param mediums the medias.
* @param callback the 3rd parties callback
*/
public void lookup3Pids(final List<String> addresses, final List<String> mediums, final ApiCallback<List<String>> callback) {
// sanity checks
if ((null == addresses) || (null == mediums) || (addresses.size() != mediums.size())) {
callback.onUnexpectedError(new Exception("invalid params"));
return;
}
// nothing to check
if (0 == mediums.size()) {
callback.onSuccess(new ArrayList<String>());
return;
}
BulkLookupParams threePidsParams = new BulkLookupParams();
ArrayList<List<String>> list = new ArrayList<>();
for (int i = 0; i < addresses.size(); i++) {
list.add(Arrays.asList(mediums.get(i), addresses.get(i)));
}
threePidsParams.threepids = list;
try {
mApi.bulkLookup(threePidsParams, new Callback<BulkLookupResponse>() {
@Override
public void success(BulkLookupResponse bulkLookupResponse, Response response) {
HashMap<String, String> mxidByAddress = new HashMap<>();
if (null != bulkLookupResponse.threepids) {
for (int i = 0; i < bulkLookupResponse.threepids.size(); i++) {
List<String> items = bulkLookupResponse.threepids.get(i);
// [0] : medium
// [1] : address
// [2] : matrix id
mxidByAddress.put(items.get(1), items.get(2));
}
}
ArrayList<String> matrixIds = new ArrayList<>();
for (String address : addresses) {
if (mxidByAddress.containsKey(address)) {
matrixIds.add(mxidByAddress.get(address));
} else {
matrixIds.add("");
}
}
callback.onSuccess(matrixIds);
}
@Override
public void failure(RetrofitError error) {
callback.onUnexpectedError(error);
}
});
} catch (Throwable t) {
callback.onUnexpectedError(new Exception(t));
}
}
Aggregations