Skip to content

Commit

Permalink
Merge pull request #6 from SEPIA-Framework/dev
Browse files Browse the repository at this point in the history
update to v2.0.2 with support for data-field/buttons in commands
  • Loading branch information
fquirin authored Nov 3, 2018
2 parents 55f8dda + 5522586 commit 29d7378
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.b07z.sepia.server.teach</groupId>
<artifactId>sepia-teach-API</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
<name>SEPIA Teach APIs</name>
<description>SEPIA APIs to teach the assistant and manage user and community contributions</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ public JSONArray getAllPersonalCommands(HashMap<String, Object> filters) {
long from = Converters.obj2LongOrDefault(filters.get("from"), -1l); if (from == -1) from = 0;
String userId = (String) filters.get("userId");
String language = (filters.containsKey("language"))? (String) filters.get("language") : "";
boolean with_button_only = filters.containsKey("button"); //Note: we don't actually check the value, if its there its true!

//build a nested query
String nestPath = "sentences";
Expand All @@ -399,6 +400,9 @@ public JSONArray getAllPersonalCommands(HashMap<String, Object> filters) {
if (!language.isEmpty()){
nestedMatches.add(new QueryElement(nestPath + ".language", language));
}
if (with_button_only){
nestedMatches.add(new QueryElement(nestPath + ".data.show_button", true));
}
JSONObject queryJson = EsQueryBuilder.getNestedBoolMustMatch(nestPath, nestedMatches);
JSON.put(queryJson, "from", from);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class Config {
public static final String SERVERNAME = "SEPIA-Teach-API"; //public server name
public static String localName = "sepia-teach-server"; //**user defined local server name
public static String localSecret = "123456"; //**user defined secret to validate local server
public static final String apiVersion = "v2.0.1"; //API version
public static final String apiVersion = "v2.0.2"; //API version
public static String privacyPolicyLink = ""; //Link to privacy policy

//helper for dynamic class creation
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/net/b07z/sepia/server/teach/server/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.b07z.sepia.server.core.tools.Converters;
import net.b07z.sepia.server.core.tools.DateTime;
import net.b07z.sepia.server.core.tools.Debugger;
import net.b07z.sepia.server.core.tools.Is;
import net.b07z.sepia.server.core.tools.JSON;
import net.b07z.sepia.server.core.tools.Timer;
import net.b07z.sepia.server.core.users.Account;
Expand Down Expand Up @@ -574,11 +575,19 @@ static String submitPersonalCommand(Request request, Response response) {
if ((cmdSummary == null || cmdSummary.isEmpty()) && (paramsJson != null && !paramsJson.isEmpty())){
cmdSummary = Converters.makeCommandSummary(command, paramsJson);
}
String userLocation = params.getString("user_location");
String userLocation = params.getString("user_location"); //TODO: The client should keeps this as detailed or vague as required
String[] repliesArr = params.getStringArray("reply");
List<String> replies = repliesArr == null ? new ArrayList<>() : Arrays.asList(repliesArr);
//custom button data and stuff
JSONObject dataJson;
String dataJsonString = params.getString("data");
if (Is.notNullOrEmpty(dataJsonString)){
dataJson = JSON.parseString(dataJsonString);
}else{
dataJson = new JSONObject(); //NOTE: If no data is submitted it will kill all previous data info (anyway the whole object is overwritten)
}

//build sentence
//build sentence - Note: Commands support sentence arrays but we use only one entry
List<Command.Sentence> sentenceList = new ArrayList<>();
Command.Sentence sentenceObj = new SentenceBuilder(sentence, account.getUserID(), "community") //TODO: add user role check to switch from "community" to "developer"?
.setLanguage(Language.valueOf(language.name().toUpperCase()))
Expand All @@ -590,6 +599,7 @@ static String submitPersonalCommand(Request request, Response response) {
.setExplicit(isExplicit)
.setEnvironment(environment)
.setUserLocation(userLocation)
.setData(dataJson)
//TODO: keep it or remove it? The general answers should be stored in an index called "answers"
//and the connector is the command. For chats, custom answers are inside parameter "reply". But I think its still useful here ...
.setReplies(new ArrayList<>(replies))
Expand Down Expand Up @@ -679,10 +689,14 @@ static String getAllPersonalCommands(Request request, Response response){
}
String language = getOrDefault("language", userAccount.getPreferredLanguage(), params);
String from = getOrDefault("from", "0", params);
String with_button_only = getOrDefault("button", null, params);
HashMap<String, Object> filters = new HashMap<>();
filters.put("userId", userAccount.getUserID());
filters.put("language", language);
filters.put("from", from);
if (with_button_only != null){
filters.put("button", true); //Its either true or not included
}

TeachDatabase db = getDatabase();
JSONArray output = db.getAllPersonalCommands(filters);
Expand Down

0 comments on commit 29d7378

Please sign in to comment.