Skip to content

Commit

Permalink
feat(bot-api): add ui friendly usernames for bot api responses
Browse files Browse the repository at this point in the history
  • Loading branch information
vincejv committed Dec 15, 2022
1 parent 61dfce4 commit 80c240f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
37 changes: 24 additions & 13 deletions core/src/main/java/com/abavilla/fpi/bot/processor/EvtPcsr.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,28 @@ public Uni<Void> processEvent(E evt) {
Log.info("Processing event: " + getEventIdForLogging(evt));
I evtEntity = mapToEntity(evt);
return evtLog.persist(evtEntity).chain(() -> {
Log.info("Logged to db: " + getEventIdForLogging(evt));

// verify if person is registered
var login = new WebhookLoginDto();
login.setUsername(getSenderFromEvt(evt));
login.setBotSource(getBotSource().toString());
Log.info("Authenticating user: " + login);
return loginApi.webhookAuthenticate(login)
// process load
.chain(session -> processLoadQuery(login, session, evt)
//load query exceptions
.onFailure(ApiSvcEx.class).recoverWithUni(ex ->
handleApiEx(evt, session.getResp().getUsername(), ex)))
// login failures
.onFailure(ApiSvcEx.class).recoverWithUni(ex -> handleApiEx(evt, ex));
Log.info("Logged to db: " + getEventIdForLogging(evt));
Uni<WebhookLoginDto> prepWHLogin = getFriendlyUserName(evt).map(friendlyName -> {
var webhookLogin = new WebhookLoginDto();
webhookLogin.setUsername(getSenderFromEvt(evt));
webhookLogin.setBotSource(getBotSource().toString());
webhookLogin.setFriendlyName(friendlyName);
return webhookLogin;
});

return prepWHLogin.chain(login -> {
Log.info("Authenticating user: " + login);
return loginApi.webhookAuthenticate(login)
// process load
.chain(session -> processLoadQuery(login, session, evt)
//load query exceptions
.onFailure(ApiSvcEx.class).recoverWithUni(ex ->
handleApiEx(evt, session.getResp().getUsername(), ex)))
// login failures
.onFailure(ApiSvcEx.class).recoverWithUni(ex -> handleApiEx(evt, ex));
});
})
.onFailure(ex -> ex instanceof MongoWriteException wEx &&
wEx.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY))
Expand Down Expand Up @@ -156,4 +164,7 @@ private Uni<Void> sendResponse(E evt, String fpiUser, String msg) {
protected abstract MsgrMsgReqDto createMsgReqFromEvt(E evt);

protected abstract String getSenderFromEvt(E evt);

protected abstract Uni<String> getFriendlyUserName(E evt);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.quarkus.logging.Log;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.mutiny.Uni;
import org.apache.commons.lang3.StringUtils;

@ApplicationScoped
public class MetaMsgEvtPcsr extends EvtPcsr<MetaMsgEvtDto, MsgrReqApi, MetaMsgEvtRepo, MetaMsgEvt> {
Expand Down Expand Up @@ -96,6 +97,12 @@ public String getSenderFromEvt(MetaMsgEvtDto evt) {
return evt.getSender();
}

@Override
protected Uni<String> getFriendlyUserName(MetaMsgEvtDto evt) {
return msgrApi.getUserDtls(getSenderFromEvt(evt), StringUtils.EMPTY)
.map(apiResp -> apiResp.getResp().getName());
}

@Override
protected Uni<Void> sendMsgrMsg(MsgrMsgReqDto msgReq, String fpiUser) {
return msgrApi.sendMsg(msgReq, fpiUser).replaceWithVoid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ protected MsgrMsgReqDto createMsgReqFromEvt(MOEvtDto evt) {
protected String getSenderFromEvt(MOEvtDto evt) {
return evt.getMobileNo();
}

@Override
protected Uni<String> getFriendlyUserName(MOEvtDto evt) {
return Uni.createFrom().item(getSenderFromEvt(evt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.abavilla.fpi.bot.codec.TGUpdateEvtCodec;
import com.abavilla.fpi.bot.entity.telegram.TelegramEvt;
import com.abavilla.fpi.bot.repo.TgEvtRepo;
import com.abavilla.fpi.bot.util.BotConst;
import com.abavilla.fpi.fw.util.DateUtil;
import com.abavilla.fpi.load.ext.dto.QueryDto;
import com.abavilla.fpi.msgr.ext.dto.MsgrMsgReqDto;
Expand All @@ -32,6 +33,7 @@
import io.quarkus.logging.Log;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.mutiny.Uni;
import org.apache.commons.lang3.StringUtils;

@ApplicationScoped
public class TgMsgEvtPcsr extends EvtPcsr<Update, TelegramReqApi, TgEvtRepo, TelegramEvt> {
Expand Down Expand Up @@ -76,6 +78,25 @@ public String getSenderFromEvt(Update evt) {
return String.valueOf(evt.message().from().id());
}

@Override
protected Uni<String> getFriendlyUserName(Update evt) {
return Uni.createFrom().item(() -> {
String fname = evt.message().from().firstName();
String lname = evt.message().from().lastName();
String friendlyName = StringUtils.EMPTY;
if (StringUtils.isNotBlank(fname) && !StringUtils.equals(fname, BotConst.NULL_STR)) {
friendlyName += fname;
}
if (StringUtils.isNotBlank(lname) && !StringUtils.equals(lname, BotConst.NULL_STR)) {
friendlyName += StringUtils.SPACE + lname;
}
if (StringUtils.isBlank(friendlyName)) {
friendlyName = evt.message().from().username();
}
return friendlyName;
});
}

@Override
protected Uni<Void> sendMsgrMsg(MsgrMsgReqDto msgReq, String fpiUser) {
return msgrApi.sendMsg(msgReq, fpiUser).replaceWithVoid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.quarkus.logging.Log;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.mutiny.Uni;
import org.apache.commons.lang3.StringUtils;

@ApplicationScoped
public class ViberMsgEvtPcsr extends EvtPcsr<ViberUpdate, ViberReqApi, ViberEvtRepo, ViberEvt> {
Expand Down Expand Up @@ -90,6 +91,12 @@ protected String getSenderFromEvt(ViberUpdate evt) {
return evt.getSender() == null ? evt.getUserId() : evt.getSender().getId();
}

@Override
protected Uni<String> getFriendlyUserName(ViberUpdate evt) {
return msgrApi.getUserDtls(getSenderFromEvt(evt), StringUtils.EMPTY)
.map(resp -> resp.getResp().getUser().getName());
}

@Override
public BotSource getBotSource() {
return BotSource.VIBER;
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/abavilla/fpi/bot/util/BotConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ public final class BotConst {

public static final String PH_REGION_CODE = "PH";

public static final String NULL_STR = "null";

private BotConst() {}
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<dependency>
<groupId>com.abavilla</groupId>
<artifactId>fpi-login-api</artifactId>
<version>1.5.1</version>
<version>1.6.0</version>
</dependency>

<dependency>
Expand All @@ -91,7 +91,7 @@
<dependency>
<groupId>com.abavilla</groupId>
<artifactId>fpi-msgr-api</artifactId>
<version>1.3.3</version>
<version>1.4.0</version>
</dependency>

<dependency>
Expand All @@ -109,7 +109,7 @@
<dependency>
<groupId>com.abavilla</groupId>
<artifactId>fpi-viber-plugin</artifactId>
<version>1.1.7</version>
<version>1.2.0</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 80c240f

Please sign in to comment.