Search in sources :

Example 1 with BulkLookupResponse

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));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BulkLookupResponse(org.matrix.androidsdk.rest.model.BulkLookupResponse) PidResponse(org.matrix.androidsdk.rest.model.pid.PidResponse) Response(retrofit.client.Response) BulkLookupParams(org.matrix.androidsdk.rest.model.BulkLookupParams) BulkLookupResponse(org.matrix.androidsdk.rest.model.BulkLookupResponse) ArrayList(java.util.ArrayList) List(java.util.List) RetrofitError(retrofit.RetrofitError)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 BulkLookupParams (org.matrix.androidsdk.rest.model.BulkLookupParams)1 BulkLookupResponse (org.matrix.androidsdk.rest.model.BulkLookupResponse)1 PidResponse (org.matrix.androidsdk.rest.model.pid.PidResponse)1 RetrofitError (retrofit.RetrofitError)1 Response (retrofit.client.Response)1