diff --git a/src/main/java/org/sorincos/mytumble/MongoConnector.java b/src/main/java/org/sorincos/mytumble/MongoConnector.java index 0218f87..71fc2ff 100644 --- a/src/main/java/org/sorincos/mytumble/MongoConnector.java +++ b/src/main/java/org/sorincos/mytumble/MongoConnector.java @@ -69,11 +69,10 @@ private void resetUsers(Message msg) { future.fail(ex.getLocalizedMessage()); } }), result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -83,9 +82,8 @@ private void saveUser(Message msg) { MongoClient client = vertx.getOrCreateContext().get("mongoclient"); JsonObject user = msg.body(); client.save("users", user, h -> { - if (h.failed()) { + if (h.failed()) logger.info("User upsert failed: " + h.cause().getLocalizedMessage()); - } }); future.complete(); } catch (Exception ex) { @@ -93,11 +91,10 @@ private void saveUser(Message msg) { future.fail(ex.getLocalizedMessage()); } }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -119,13 +116,11 @@ private void saveUsers(Message msg) { followsme++; // await, to not kill Mongo's thread pool String res = awaitResult(h -> client.save("users", (JsonObject) user, h)); - if (res != null) { // not sure what that means + if (res != null) // not sure what that means logger.error(res); - } List found = awaitResult(h -> client.find("users", (JsonObject) user, h)); - if (found.size() != 1) { + if (found.size() != 1) logger.error(found.size() + " for user " + ((JsonObject) user).getString("name")); - } } logger.info("ifollow " + ifollow); logger.info("followsme " + followsme); @@ -135,11 +130,10 @@ private void saveUsers(Message msg) { future.fail(ex.getLocalizedMessage()); } }), result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -149,19 +143,17 @@ private void getUsers(Message msg) { MongoClient client = vertx.getOrCreateContext().get("mongoclient"); FindOptions options = new FindOptions().setSort(new JsonObject().put("name", 1)); client.findWithOptions("users", query, options, res -> { - if (res.failed()) { + if (res.failed()) future.fail(res.cause().getLocalizedMessage()); - } final JsonArray users = new JsonArray(); res.result().forEach(users::add); future.complete(users); }); }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -170,19 +162,17 @@ private void getUser(Message msg) { vertx.executeBlocking(future -> { MongoClient client = vertx.getOrCreateContext().get("mongoclient"); client.find("users", query, res -> { - if (res.failed()) { + if (res.failed()) future.fail(res.cause().getLocalizedMessage()); - } final JsonArray users = new JsonArray(); res.result().forEach(users::add); // expected max one future.complete(users); }); }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } diff --git a/src/main/java/org/sorincos/mytumble/TumblrConnector.java b/src/main/java/org/sorincos/mytumble/TumblrConnector.java index 0ef462c..7a538f5 100644 --- a/src/main/java/org/sorincos/mytumble/TumblrConnector.java +++ b/src/main/java/org/sorincos/mytumble/TumblrConnector.java @@ -77,7 +77,7 @@ public void setBlogname(String blogname) { @Override public void start() throws Exception { - if (null == vertx.getOrCreateContext().get("jumblrclient")) { + if (vertx.getOrCreateContext().get("jumblrclient") == null) { JumblrClient client = new JumblrClient(key, secret); client.setToken(oauthtoken, oauthpass); vertx.getOrCreateContext().put("jumblrclient", client); @@ -87,39 +87,42 @@ public void start() throws Exception { eb.consumer("mytumble.tumblr.loadusers").handler(this::loadUsers); eb.consumer("mytumble.tumblr.loaduserdetails").handler(this::loadUserDetails); eb.consumer("mytumble.tumblr.loadposts").handler(this::loadPosts); - eb.consumer("mytumble.tumblr.likelatest").handler(this::likeLatest); + eb.consumer("mytumble.tumblr.likelatest").handler(this::likeLatest); eb.consumer("mytumble.tumblr.followblog").handler(this::followBlog); eb.consumer("mytumble.tumblr.unfollowblog").handler(this::unfollowBlog); } - private void likeLatest(Message msg) { - vertx.executeBlocking(future -> { - String toLike = msg.body(); + private void likeLatest(Message msg) { + vertx.executeBlocking(future -> { + String toLike = msg.body().getString("name"); try { JumblrClient client = vertx.getOrCreateContext().get("jumblrclient"); - if (null == client) { + if (client == null) future.fail("Error: Jumblr not initialized"); - } + Map params = new HashMap(); List posts = client.blogPosts(toLike, params); - for (Post post : posts) { - if (null == post.getSourceUrl()) { // no use to like reblogs + boolean liked = false; + for (Post post : posts) { // no use to like reblogs or what already liked + if (post.getSourceUrl() == null && !post.isLiked()) { // jumblr bug, urls are unusable client.like(post.getId(), post.getReblogKey()); - logger.info("Liked " + toLike + ":" + post.getId()); - break; // like only latest own post + // logger.info(toLike + " liked " + post.getShortUrl()); + liked = true; + break; } } - future.complete(toLike); + if (!liked) + logger.info("Reblogger or quiet: " + toLike + " had nothing to like among latest " + posts.size()); + future.complete(msg.body()); } catch (Exception ex) { ex.printStackTrace(); future.fail(ex.getLocalizedMessage()); } }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -129,9 +132,9 @@ private void followBlog(Message msg) { logger.info("Follow " + toFollow); try { JumblrClient client = vertx.getOrCreateContext().get("jumblrclient"); - if (null == client) { + if (client == null) future.fail("Error: Jumblr not initialized"); - } + client.follow(toFollow); // no return here future.complete(toFollow); } catch (Exception ex) { @@ -139,11 +142,10 @@ private void followBlog(Message msg) { future.fail(ex.getLocalizedMessage()); } }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -153,9 +155,9 @@ private void unfollowBlog(Message msg) { logger.info("Unfollow " + toUnfollow); try { JumblrClient client = vertx.getOrCreateContext().get("jumblrclient"); - if (null == client) { + if (client == null) future.fail("Error: Jumblr not initialized"); - } + client.unfollow(toUnfollow); // no return here future.complete(toUnfollow); } catch (Exception ex) { @@ -163,11 +165,10 @@ private void unfollowBlog(Message msg) { future.fail(ex.getLocalizedMessage()); } }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -177,19 +178,19 @@ private void loadPosts(Message msg) { logger.info("Posts for " + blogname); try { JumblrClient client = vertx.getOrCreateContext().get("jumblrclient"); - if (null == client) { + if (client == null) future.fail("Error: Jumblr not initialized"); - } + Blog myBlog = null; for (Blog blog : client.user().getBlogs()) { - if (0 == blog.getName().compareTo(blogname)) { + if (blog.getName().compareTo(blogname) == 0) { myBlog = blog; break; } } - if (null == myBlog) { + if (myBlog == null) future.fail("Error: Blog name not found - " + blogname); - } + int howMany = msg.body().getInteger(0); Map params = new HashMap(); params.put("limit", howMany); @@ -202,9 +203,9 @@ private void loadPosts(Message msg) { JsonObject jsonPost = new JsonObject(); jsonPost.put("timestamp", post.getTimestamp()); jsonPost.put("postid", post.getId()); - if (null != post.getRebloggedFromName()) { + if (post.getRebloggedFromName() != null) continue; // don't care about what I reblogged - } + count++; logger.info("Post " + count); JsonArray jsonNotes = new JsonArray(); @@ -225,11 +226,10 @@ private void loadPosts(Message msg) { future.fail(ex.getLocalizedMessage()); } }, result -> { - if (result.succeeded()) { + if (result.succeeded()) msg.reply(result.result()); - } else { + else msg.fail(1, result.cause().getLocalizedMessage()); - } }); } @@ -237,10 +237,12 @@ private void loadUserDetails(Message msg) { vertx.executeBlocking(future -> { try { JsonArray jsonFollowers = msg.body(); - if (jsonFollowers.isEmpty()) { + if (jsonFollowers.isEmpty()) future.fail("No details to load"); - } JumblrClient client = vertx.getOrCreateContext().get("jumblrclient"); + if (client == null) + future.fail("Error: Jumblr not initialized"); + int count = 0; loopUsers(jsonFollowers, client, count); future.complete(jsonFollowers); @@ -287,15 +289,18 @@ private void loadUsers(Message msg) { try { long now = new Date().getTime(); JumblrClient client = vertx.getOrCreateContext().get("jumblrclient"); + if (client == null) + future.fail("Error: Jumblr not initialized"); + for (Blog blog : client.user().getBlogs()) { - if (0 == blog.getName().compareTo(blogname)) { + if (blog.getName().compareTo(blogname) == 0) { myBlog = blog; break; } } - if (null == myBlog) { + if (myBlog == null) future.fail("Error: Blog name not found - " + blogname); - } + Map mapUsers = new HashMap<>(); Map options = new HashMap(); int offset = 0, fetched = 0; diff --git a/src/main/java/org/sorincos/mytumble/WebServer.java b/src/main/java/org/sorincos/mytumble/WebServer.java index dd0b399..1669d35 100644 --- a/src/main/java/org/sorincos/mytumble/WebServer.java +++ b/src/main/java/org/sorincos/mytumble/WebServer.java @@ -86,12 +86,11 @@ private void unfollowAsocials(RoutingContext ctx) { ctx.response().setStatusCode(200).end(); } catch (Exception ex) { ex.printStackTrace(); - if (ctx.response().ended()) { + if (ctx.response().ended()) vertx.eventBus().send("mytumble.web.status", "Unfollowing asocials failed: " + ex.getLocalizedMessage()); - } else { + else ctx.response().setStatusCode(500).setStatusMessage(ex.getLocalizedMessage()).end(); - } } } @@ -105,8 +104,8 @@ private void likeUsers(RoutingContext ctx) { for (Object liker : likers) { try { @SuppressWarnings("unused") - Message res = awaitResult(h -> vertx.eventBus().send("mytumble.tumblr.likelatest", - ((JsonObject) liker).getString("name"), h)); + Message res = awaitResult( + h -> vertx.eventBus().send("mytumble.tumblr.likelatest", (JsonObject) liker, h)); } catch (Exception ex) { logger.error(ex.getLocalizedMessage()); } @@ -116,11 +115,10 @@ private void likeUsers(RoutingContext ctx) { ctx.response().setStatusCode(200).end(); } catch (Exception ex) { ex.printStackTrace(); - if (ctx.response().ended()) { + if (ctx.response().ended()) vertx.eventBus().send("mytumble.web.status", "Liking failed: " + ex.getLocalizedMessage()); - } else { + else ctx.response().setStatusCode(500).setStatusMessage(ex.getLocalizedMessage()).end(); - } } } @@ -150,18 +148,17 @@ private void refreshUsers(RoutingContext ctx) { ctx.response().setStatusCode(200).end(); } catch (Exception ex) { ex.printStackTrace(); - if (ctx.response().ended()) { + if (ctx.response().ended()) vertx.eventBus().send("mytumble.web.status", "Refresh failed: " + ex.getLocalizedMessage()); - } else { + else ctx.response().setStatusCode(500).setStatusMessage(ex.getLocalizedMessage()).end(); - } } } private void updateUser(RoutingContext ctx) { String toUpdate = ctx.request().getParam("name"); JsonArray jsonUsers = new JsonArray().add(ctx.getBodyAsJson()); - if (null == toUpdate || jsonUsers.isEmpty()) { + if (toUpdate == null || jsonUsers.isEmpty()) { ctx.response().setStatusCode(400).setStatusMessage("Nothing to update.").end(); return; } diff --git a/src/main/resources/webroot/index.html b/src/main/resources/webroot/index.html index 7e06ba0..eedc86d 100644 --- a/src/main/resources/webroot/index.html +++ b/src/main/resources/webroot/index.html @@ -75,7 +75,8 @@ - + +