Skip to content

Commit

Permalink
Show current comment as a header,
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MrBoomDeveloper committed Apr 7, 2024
1 parent 20a4921 commit 8adae0e
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public class CatalogComment {
*/
@Json(name = "votes")
public Integer votes;
public String date;

/**
* Used only for the Frontend
* Do not use in the Backend!
* <p>Used to identify this comment among others</p>
*/
@Json(ignore = true)
public long visualId;
public String date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

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

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mrboomdev.awery.app.AweryApp;
import com.mrboomdev.awery.data.settings.AwerySettings;
Expand All @@ -14,6 +17,7 @@
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;

import java.util.HashMap;
import java.util.Locale;
Expand All @@ -25,6 +29,7 @@
*/
@SuppressWarnings("unused")
public class JsBridge {
private static final String TAG = "JsBridge";
protected AwerySettings prefs;
private final JsManager manager;
private final JsProvider provider;
Expand Down Expand Up @@ -58,7 +63,13 @@ public Object fetch(@NonNull ScriptableObject options) {
headers = new HashMap<>();

for(var entry : o.entrySet()) {
headers.put(entry.getKey().toString(), entry.getValue().toString());
var value = entry.getValue();

if(value == null) {
continue;
}

headers.put(entry.getKey().toString(), value.toString());
}
}

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

for(var entry : obj.entrySet()) {
request.addFormField(entry.getKey().toString(), entry.getValue().toString());
var value = entry.getValue();

if(value == null) {
continue;
}

request.addFormField(entry.getKey().toString(), value.toString());
}
}

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

public static int intFromJs(Object object) {
var result = fromJs(object, Integer.class);
return result == null ? 0 : result;
}

public static boolean booleanFromJs(Object object) {
var result = fromJs(object, Boolean.class);
return result != null && result;
}

public static float floatFromJs(Object object) {
var result = fromJs(object, Float.class);
return result == null ? 0 : result;
}

public static long longFromJs(Object object) {
var result = fromJs(object, Long.class);
return result == null ? 0 : result;
}

@Nullable
public static <T> T fromJs(Object object, Class<T> clazz) {
if(object == null || Undefined.isUndefined(object)) return null;

if(clazz.isAssignableFrom(String.class)) return clazz.cast(object.toString());
if(clazz == Integer.TYPE) return clazz.cast(((Number) object).intValue());
if(clazz == Boolean.TYPE) return clazz.cast(((Boolean) object));
if(clazz == Float.TYPE) return clazz.cast(((Number) object).floatValue());
if(clazz == Long.TYPE) return clazz.cast(((Number) object).longValue());

return clazz.cast(object);
}

public void log(Object o) {
Log.i(TAG, "\"" + provider.getName() + "\" logged: " + o);
}

public Object getSaved(@NonNull Object key) {
return prefs.getString(key.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ protected JsComment(@NonNull ScriptableObject o) {
}
}

authorName = o.has("authorName", o) ? o.get("authorName").toString() : null;
authorAvatar = o.has("authorAvatar", o) ? o.get("authorAvatar").toString() : null;
text = o.has("text", o) ? o.get("text").toString() : null;
date = o.has("date", o) ? o.get("date").toString() : null;

likes = o.has("likes", o) ? ((Number) o.get("likes")).intValue() : CatalogComment.DISABLED;
dislikes = o.has("dislikes", o) ? ((Number) o.get("dislikes")).intValue() : CatalogComment.DISABLED;
comments = o.has("comments", o) ? ((Number) o.get("comments")).intValue() : CatalogComment.DISABLED;
votes = o.has("votes", o) ? ((Number) o.get("votes")).intValue() : null;

canComment = o.has("canComment", o) && (Boolean) o.get("canComment", o);
hasNextPage = o.has("hasNextPage", o) && (Boolean) o.get("hasNextPage", o);
authorName = o.has("authorName", o) ? JsBridge.fromJs(o.get("authorName", o), String.class) : null;
authorAvatar = o.has("authorAvatar", o) ? JsBridge.fromJs(o.get("authorAvatar", o), String.class) : null;
text = o.has("text", o) ? JsBridge.fromJs(o.get("text", o), String.class) : null;
date = o.has("date", o) ? JsBridge.fromJs(o.get("date", o), String.class) : null;

likes = o.has("likes", o) ? JsBridge.intFromJs(o.get("likes", o)) : CatalogComment.DISABLED;
dislikes = o.has("dislikes", o) ? JsBridge.intFromJs(o.get("dislikes", o)) : CatalogComment.DISABLED;
comments = o.has("comments", o) ? JsBridge.intFromJs(o.get("comments", o)) : CatalogComment.DISABLED;
votes = o.has("votes", o) ? JsBridge.fromJs(o.get("votes", o), Integer.class) : null;

canComment = o.has("canComment", o) && JsBridge.booleanFromJs(o.get("canComment", o));
hasNextPage = o.has("hasNextPage", o) && JsBridge.booleanFromJs(o.get("hasNextPage", o));

this.customData = o;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class JsManager extends ExtensionsManager {
public JsManager() {
Thread jsThread = new Thread(() -> {
this.context = org.mozilla.javascript.Context.enter();
this.context.setLanguageVersion(org.mozilla.javascript.Context.VERSION_ES6);

context.setErrorReporter(new ErrorReporter() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mrboomdev.awery.extensions.support.js;

import static com.mrboomdev.awery.app.AweryApp.toast;

import android.util.Log;

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

this.callback = o -> {
if(o instanceof Throwable t) {
toast("Something REALLY BAD has happened");
Log.e(TAG, "Returned exception, ignoring the response.", t);
return;
}
Expand Down
Loading

0 comments on commit 8adae0e

Please sign in to comment.