Skip to content

Commit

Permalink
fix: Add a fallback mode to wotService.getMemberByPubkey() to able to…
Browse files Browse the repository at this point in the history
… call it BEFORE blockchain indexation finished
  • Loading branch information
blavenie committed Sep 15, 2023
1 parent c7921cf commit d95fb15
Showing 1 changed file with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.duniter.core.client.model.bma.BlockchainParameters;
import org.duniter.core.client.model.bma.WotRequirements;
import org.duniter.core.client.model.local.Identity;
import org.duniter.core.client.model.local.Member;
import org.duniter.core.client.repositories.CurrencyRepository;
import org.duniter.core.client.service.bma.WotRemoteService;
Expand Down Expand Up @@ -217,53 +219,78 @@ public void onChange(ChangeEvent change) {
}

public boolean isOrWasMember(String pubkey) {
return Beans.getStream(currencyRepository.findAllIds())
.anyMatch(currencyId -> this.isOrWasMember(currencyId, pubkey));
}


String[] currencyIds = Beans.getStream(currencyRepository.findAllIds()).toArray(String[]::new);
if (ArrayUtils.isEmpty(currencyIds)) return false;
public boolean isOrWasMember(String currency, String pubkey) {
final String currencyId = safeGetCurrency(currency);
if (StringUtils.isBlank(currencyId)) return false;

SearchResponse response = client.prepareSearch()
.setIndices(currencyIds)
.setSize(0) // only need the total
.setTypes(MemberRepository.TYPE)
.setQuery(QueryBuilders.idsQuery().ids(pubkey))
.setRequestCache(true)
.execute().actionGet();
.setIndices(currencyId)
.setSize(0) // only need the total
.setTypes(MemberRepository.TYPE)
.setQuery(QueryBuilders.idsQuery().ids(pubkey))
.setRequestCache(true)
.execute().actionGet();

return response.getHits() != null && response.getHits().getTotalHits() > 0;
}

public boolean isMember(String pubkey) {
return Beans.getStream(currencyRepository.findAllIds())
.anyMatch(currencyId -> this.isMember(currencyId, pubkey));
}

String[] currencyIds = Beans.getStream(currencyRepository.findAllIds()).toArray(String[]::new);
if (ArrayUtils.isEmpty(currencyIds)) return false;
public boolean isMember(String currency, String pubkey) {

final String currencyId = safeGetCurrency(currency);

QueryBuilder query = QueryBuilders.constantScoreQuery(QueryBuilders.boolQuery()
.filter(QueryBuilders.idsQuery().addIds(pubkey))
.filter(QueryBuilders.termQuery(Member.Fields.IS_MEMBER, true))
.filter(QueryBuilders.idsQuery().addIds(pubkey))
.filter(QueryBuilders.termQuery(Member.Fields.IS_MEMBER, true))
);

SearchResponse response = client.prepareSearch()
.setIndices(currencyIds)
.setSize(0) // only need the total
.setTypes(MemberRepository.TYPE)
.setQuery(query)
.setRequestCache(true)
.execute().actionGet();
.setIndices(currencyId)
.setSize(0) // only need the total
.setTypes(MemberRepository.TYPE)
.setQuery(query)
.setRequestCache(true)
.execute().actionGet();

return response.getHits() != null && response.getHits().getTotalHits() > 0;
}

public List<WotRequirements> getRequirements(String currency, String pubkey) {
waitReady();

final String currencyId = safeGetCurrency(currency);

return this.wotRemoteService.getRequirements(currencyId, pubkey);
}

public Optional<Member> getMemberByPubkey(String currencyId, String pubkey) {
return this.memberRepository.getMemberByPubkey(currencyId, pubkey);
public Optional<Member> getMemberByPubkey(String currency, String pubkey) {
final String currencyId = safeGetCurrency(currency);

if (isBlockchainReady(currencyId)) {
return this.memberRepository.getMemberByPubkey(currencyId, pubkey);
}
// Fallback to remote duniter node (e.g. Blockchain not yet indexed)
else {
Identity source = this.wotRemoteService.getIdentity(currencyId, pubkey);
if (source == null
|| !currencyId.equals(source.getCurrency())
|| (!source.getIsMember() && source.getWasMember())) return Optional.empty(); // Not a member

Member target = new Member();
target.setPubkey(pubkey);
target.setCurrency(currencyId);
target.setUid(source.getUid());
target.setIsMember(source.getIsMember());
target.setWasMember(source.getWasMember());
target.setTimestamp(source.getTimestamp());
return Optional.of(target);
}
}

/* -- protected methods -- */
Expand Down

0 comments on commit d95fb15

Please sign in to comment.