Skip to content

Commit 8adae0e

Browse files
Show current comment as a header,
After comment reloading don't return to the root, Properly handle wrapped Js exceptions, Ignore null form and header entries, Properly handle undefined values, Report if A VERY BAD thing has happened with the JS engine
1 parent 20a4921 commit 8adae0e

File tree

12 files changed

+194
-80
lines changed

12 files changed

+194
-80
lines changed

app/src/main/java/com/mrboomdev/awery/extensions/data/CatalogComment.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ public class CatalogComment {
3737
*/
3838
@Json(name = "votes")
3939
public Integer votes;
40+
public String date;
41+
42+
/**
43+
* Used only for the Frontend
44+
* Do not use in the Backend!
45+
* <p>Used to identify this comment among others</p>
46+
*/
4047
@Json(ignore = true)
4148
public long visualId;
42-
public String date;
4349
}

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsBridge.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import static com.mrboomdev.awery.app.AweryLifecycle.getAnyContext;
44

5+
import android.util.Log;
6+
57
import androidx.annotation.NonNull;
8+
import androidx.annotation.Nullable;
69

710
import com.mrboomdev.awery.app.AweryApp;
811
import com.mrboomdev.awery.data.settings.AwerySettings;
@@ -14,6 +17,7 @@
1417
import org.mozilla.javascript.NativeObject;
1518
import org.mozilla.javascript.Scriptable;
1619
import org.mozilla.javascript.ScriptableObject;
20+
import org.mozilla.javascript.Undefined;
1721

1822
import java.util.HashMap;
1923
import java.util.Locale;
@@ -25,6 +29,7 @@
2529
*/
2630
@SuppressWarnings("unused")
2731
public class JsBridge {
32+
private static final String TAG = "JsBridge";
2833
protected AwerySettings prefs;
2934
private final JsManager manager;
3035
private final JsProvider provider;
@@ -58,7 +63,13 @@ public Object fetch(@NonNull ScriptableObject options) {
5863
headers = new HashMap<>();
5964

6065
for(var entry : o.entrySet()) {
61-
headers.put(entry.getKey().toString(), entry.getValue().toString());
66+
var value = entry.getValue();
67+
68+
if(value == null) {
69+
continue;
70+
}
71+
72+
headers.put(entry.getKey().toString(), value.toString());
6273
}
6374
}
6475

@@ -81,7 +92,13 @@ public Object fetch(@NonNull ScriptableObject options) {
8192
var obj = (NativeObject) options.get("form");
8293

8394
for(var entry : obj.entrySet()) {
84-
request.addFormField(entry.getKey().toString(), entry.getValue().toString());
95+
var value = entry.getValue();
96+
97+
if(value == null) {
98+
continue;
99+
}
100+
101+
request.addFormField(entry.getKey().toString(), value.toString());
85102
}
86103
}
87104

@@ -113,6 +130,43 @@ public void onError(HttpClient.HttpException exception) {
113130
return Context.javaToJS(promise, scriptScope);
114131
}
115132

133+
public static int intFromJs(Object object) {
134+
var result = fromJs(object, Integer.class);
135+
return result == null ? 0 : result;
136+
}
137+
138+
public static boolean booleanFromJs(Object object) {
139+
var result = fromJs(object, Boolean.class);
140+
return result != null && result;
141+
}
142+
143+
public static float floatFromJs(Object object) {
144+
var result = fromJs(object, Float.class);
145+
return result == null ? 0 : result;
146+
}
147+
148+
public static long longFromJs(Object object) {
149+
var result = fromJs(object, Long.class);
150+
return result == null ? 0 : result;
151+
}
152+
153+
@Nullable
154+
public static <T> T fromJs(Object object, Class<T> clazz) {
155+
if(object == null || Undefined.isUndefined(object)) return null;
156+
157+
if(clazz.isAssignableFrom(String.class)) return clazz.cast(object.toString());
158+
if(clazz == Integer.TYPE) return clazz.cast(((Number) object).intValue());
159+
if(clazz == Boolean.TYPE) return clazz.cast(((Boolean) object));
160+
if(clazz == Float.TYPE) return clazz.cast(((Number) object).floatValue());
161+
if(clazz == Long.TYPE) return clazz.cast(((Number) object).longValue());
162+
163+
return clazz.cast(object);
164+
}
165+
166+
public void log(Object o) {
167+
Log.i(TAG, "\"" + provider.getName() + "\" logged: " + o);
168+
}
169+
116170
public Object getSaved(@NonNull Object key) {
117171
return prefs.getString(key.toString());
118172
}

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsComment.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ protected JsComment(@NonNull ScriptableObject o) {
2222
}
2323
}
2424

25-
authorName = o.has("authorName", o) ? o.get("authorName").toString() : null;
26-
authorAvatar = o.has("authorAvatar", o) ? o.get("authorAvatar").toString() : null;
27-
text = o.has("text", o) ? o.get("text").toString() : null;
28-
date = o.has("date", o) ? o.get("date").toString() : null;
29-
30-
likes = o.has("likes", o) ? ((Number) o.get("likes")).intValue() : CatalogComment.DISABLED;
31-
dislikes = o.has("dislikes", o) ? ((Number) o.get("dislikes")).intValue() : CatalogComment.DISABLED;
32-
comments = o.has("comments", o) ? ((Number) o.get("comments")).intValue() : CatalogComment.DISABLED;
33-
votes = o.has("votes", o) ? ((Number) o.get("votes")).intValue() : null;
34-
35-
canComment = o.has("canComment", o) && (Boolean) o.get("canComment", o);
36-
hasNextPage = o.has("hasNextPage", o) && (Boolean) o.get("hasNextPage", o);
25+
authorName = o.has("authorName", o) ? JsBridge.fromJs(o.get("authorName", o), String.class) : null;
26+
authorAvatar = o.has("authorAvatar", o) ? JsBridge.fromJs(o.get("authorAvatar", o), String.class) : null;
27+
text = o.has("text", o) ? JsBridge.fromJs(o.get("text", o), String.class) : null;
28+
date = o.has("date", o) ? JsBridge.fromJs(o.get("date", o), String.class) : null;
29+
30+
likes = o.has("likes", o) ? JsBridge.intFromJs(o.get("likes", o)) : CatalogComment.DISABLED;
31+
dislikes = o.has("dislikes", o) ? JsBridge.intFromJs(o.get("dislikes", o)) : CatalogComment.DISABLED;
32+
comments = o.has("comments", o) ? JsBridge.intFromJs(o.get("comments", o)) : CatalogComment.DISABLED;
33+
votes = o.has("votes", o) ? JsBridge.fromJs(o.get("votes", o), Integer.class) : null;
34+
35+
canComment = o.has("canComment", o) && JsBridge.booleanFromJs(o.get("canComment", o));
36+
hasNextPage = o.has("hasNextPage", o) && JsBridge.booleanFromJs(o.get("hasNextPage", o));
3737

3838
this.customData = o;
3939
}

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class JsManager extends ExtensionsManager {
4444
public JsManager() {
4545
Thread jsThread = new Thread(() -> {
4646
this.context = org.mozilla.javascript.Context.enter();
47+
this.context.setLanguageVersion(org.mozilla.javascript.Context.VERSION_ES6);
4748

4849
context.setErrorReporter(new ErrorReporter() {
4950
@Override

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsTask.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mrboomdev.awery.extensions.support.js;
22

3+
import static com.mrboomdev.awery.app.AweryApp.toast;
4+
35
import android.util.Log;
46

57
import com.mrboomdev.awery.util.Callbacks;
@@ -25,6 +27,7 @@ protected JsTask(Runnable runnable) {
2527

2628
this.callback = o -> {
2729
if(o instanceof Throwable t) {
30+
toast("Something REALLY BAD has happened");
2831
Log.e(TAG, "Returned exception, ignoring the response.", t);
2932
return;
3033
}

0 commit comments

Comments
 (0)