Search in sources :

Example 11 with ThreePidMapping

use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.

the class GoogleFirebaseProvider method populate.

@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    List<ThreePidMapping> results = new ArrayList<>();
    mappings.parallelStream().forEach(o -> {
        Optional<UserRecord> urOpt = findInternal(o.getMedium(), o.getValue());
        if (urOpt.isPresent()) {
            ThreePidMapping result = new ThreePidMapping();
            result.setMedium(o.getMedium());
            result.setValue(o.getValue());
            result.setMxid(getMxid(urOpt.get()));
            results.add(result);
        }
    });
    return results;
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) UserRecord(com.google.firebase.auth.UserRecord) ArrayList(java.util.ArrayList)

Example 12 with ThreePidMapping

use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.

the class ExecIdentityStore method populate.

@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    Processor<List<ThreePidMapping>> p = new Processor<>();
    p.withConfig(cfg.getLookup().getBulk());
    p.addInput(JsonType, () -> {
        JsonArray tpids = GsonUtil.asArray(mappings.stream().map(mapping -> GsonUtil.get().toJsonTree(new ThreePid(mapping.getMedium(), mapping.getValue()))).collect(Collectors.toList()));
        return GsonUtil.get().toJson(GsonUtil.makeObj("lookup", tpids));
    });
    p.addInput(PlainType, () -> {
        StringBuilder input = new StringBuilder();
        for (ThreePidMapping mapping : mappings) {
            input.append(mapping.getMedium()).append("\t").append(mapping.getValue()).append(System.lineSeparator());
        }
        return input.toString();
    });
    p.addSuccessMapper(JsonType, output -> {
        if (StringUtils.isBlank(output)) {
            return Collections.emptyList();
        }
        LookupBulkResponseJson response = GsonUtil.get().fromJson(output, LookupBulkResponseJson.class);
        return response.getLookup().stream().map(item -> {
            ThreePidMapping mapping = new ThreePidMapping();
            mapping.setMedium(item.getMedium());
            mapping.setValue(item.getAddress());
            if (UserIdType.Localpart.is(item.getId().getType())) {
                mapping.setValue(MatrixID.asAcceptable(item.getId().getValue(), mxCfg.getDomain()).getId());
                return mapping;
            }
            if (UserIdType.MatrixID.is(item.getId().getType())) {
                mapping.setValue(MatrixID.asAcceptable(item.getId().getValue()).getId());
                return mapping;
            }
            throw new InternalServerError("Invalid user type: " + item.getId().getType());
        }).collect(Collectors.toList());
    });
    p.withFailureDefault(output -> Collections.emptyList());
    return p.execute();
}
Also used : JsonArray(com.google.gson.JsonArray) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) JsonParseException(com.google.gson.JsonParseException) GsonUtil(io.kamax.matrix.json.GsonUtil) LookupBulkResponseJson(io.kamax.mxisd.backend.rest.LookupBulkResponseJson) MatrixConfig(io.kamax.mxisd.config.MatrixConfig) LoggerFactory(org.slf4j.LoggerFactory) MatrixID(io.kamax.matrix.MatrixID) StringUtils(org.apache.commons.lang3.StringUtils) IThreePidProvider(io.kamax.mxisd.lookup.provider.IThreePidProvider) SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) UserIdType(io.kamax.mxisd.UserIdType) LookupSingleResponseJson(io.kamax.mxisd.backend.rest.LookupSingleResponseJson) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Logger(org.slf4j.Logger) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) Collectors(java.util.stream.Collectors) ThreePid(io.kamax.matrix.ThreePid) Objects(java.util.Objects) JsonArray(com.google.gson.JsonArray) InternalServerError(io.kamax.mxisd.exception.InternalServerError) List(java.util.List) ExecConfig(io.kamax.mxisd.config.ExecConfig) UserID(io.kamax.mxisd.UserID) Optional(java.util.Optional) Collections(java.util.Collections) LookupBulkResponseJson(io.kamax.mxisd.backend.rest.LookupBulkResponseJson) List(java.util.List) ThreePid(io.kamax.matrix.ThreePid) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Aggregations

ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)12 ArrayList (java.util.ArrayList)7 MatrixConfig (io.kamax.mxisd.config.MatrixConfig)4 SingleLookupRequest (io.kamax.mxisd.lookup.SingleLookupRequest)4 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)3 RestBackendConfig (io.kamax.mxisd.config.rest.RestBackendConfig)3 IOException (java.io.IOException)3 MatrixID (io.kamax.matrix.MatrixID)2 ThreePid (io.kamax.matrix.ThreePid)2 UserID (io.kamax.mxisd.UserID)2 UserIdType (io.kamax.mxisd.UserIdType)2 InternalServerError (io.kamax.mxisd.exception.InternalServerError)2 ClientBulkLookupRequest (io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest)2 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)2 IThreePidProvider (io.kamax.mxisd.lookup.provider.IThreePidProvider)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Before (org.junit.Before)2