Skip to content

Commit

Permalink
extra button, less brackets,, tried jumblr workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
sorin-costea committed Oct 18, 2016
1 parent ea733ef commit 974e77e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 82 deletions.
40 changes: 15 additions & 25 deletions src/main/java/org/sorincos/mytumble/MongoConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ private void resetUsers(Message<String> msg) {
future.fail(ex.getLocalizedMessage());
}
}), result -> {
if (result.succeeded()) {
if (result.succeeded())
msg.reply(result.result());
} else {
else
msg.fail(1, result.cause().getLocalizedMessage());
}
});
}

Expand All @@ -83,21 +82,19 @@ private void saveUser(Message<JsonObject> 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) {
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());
}
});
}

Expand All @@ -119,13 +116,11 @@ private void saveUsers(Message<JsonArray> 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<JsonObject> 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);
Expand All @@ -135,11 +130,10 @@ private void saveUsers(Message<JsonArray> msg) {
future.fail(ex.getLocalizedMessage());
}
}), result -> {
if (result.succeeded()) {
if (result.succeeded())
msg.reply(result.result());
} else {
else
msg.fail(1, result.cause().getLocalizedMessage());
}
});
}

Expand All @@ -149,19 +143,17 @@ private void getUsers(Message<String> 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());
}
});
}

Expand All @@ -170,19 +162,17 @@ private void getUser(Message<JsonArray> msg) {
vertx.<JsonArray>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());
}
});
}

Expand Down
85 changes: 45 additions & 40 deletions src/main/java/org/sorincos/mytumble/TumblrConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -87,39 +87,42 @@ public void start() throws Exception {
eb.<JsonArray>consumer("mytumble.tumblr.loadusers").handler(this::loadUsers);
eb.<JsonArray>consumer("mytumble.tumblr.loaduserdetails").handler(this::loadUserDetails);
eb.<JsonArray>consumer("mytumble.tumblr.loadposts").handler(this::loadPosts);
eb.<String>consumer("mytumble.tumblr.likelatest").handler(this::likeLatest);
eb.<JsonObject>consumer("mytumble.tumblr.likelatest").handler(this::likeLatest);
eb.<String>consumer("mytumble.tumblr.followblog").handler(this::followBlog);
eb.<String>consumer("mytumble.tumblr.unfollowblog").handler(this::unfollowBlog);
}

private void likeLatest(Message<String> msg) {
vertx.<String>executeBlocking(future -> {
String toLike = msg.body();
private void likeLatest(Message<JsonObject> msg) {
vertx.<JsonObject>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<String, Object> params = new HashMap<String, Object>();
List<Post> 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());
}
});
}

Expand All @@ -129,21 +132,20 @@ private void followBlog(Message<String> 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) {
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());
}
});
}

Expand All @@ -153,21 +155,20 @@ private void unfollowBlog(Message<String> 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) {
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());
}
});
}

Expand All @@ -177,19 +178,19 @@ private void loadPosts(Message<JsonArray> 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<String, Object> params = new HashMap<String, Object>();
params.put("limit", howMany);
Expand All @@ -202,9 +203,9 @@ private void loadPosts(Message<JsonArray> 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();
Expand All @@ -225,22 +226,23 @@ private void loadPosts(Message<JsonArray> msg) {
future.fail(ex.getLocalizedMessage());
}
}, result -> {
if (result.succeeded()) {
if (result.succeeded())
msg.reply(result.result());
} else {
else
msg.fail(1, result.cause().getLocalizedMessage());
}
});
}

private void loadUserDetails(Message<JsonArray> msg) {
vertx.<JsonArray>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);
Expand Down Expand Up @@ -287,15 +289,18 @@ private void loadUsers(Message<JsonArray> 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<String, JsonObject> mapUsers = new HashMap<>();
Map<String, String> options = new HashMap<String, String>();
int offset = 0, fetched = 0;
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/org/sorincos/mytumble/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand All @@ -105,8 +104,8 @@ private void likeUsers(RoutingContext ctx) {
for (Object liker : likers) {
try {
@SuppressWarnings("unused")
Message<String> res = awaitResult(h -> vertx.eventBus().send("mytumble.tumblr.likelatest",
((JsonObject) liker).getString("name"), h));
Message<JsonObject> res = awaitResult(
h -> vertx.eventBus().send("mytumble.tumblr.likelatest", (JsonObject) liker, h));
} catch (Exception ex) {
logger.error(ex.getLocalizedMessage());
}
Expand All @@ -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();
}
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/webroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
<button ng-click="unfollowAsocials()">Unfollow asocials</button>
<button ng-click="loadUsers('likers,notweird')">Likers</button>
<button ng-click="likeUsers('likers')">Like likers</button>
<button ng-click="likeUsers('followsme,notweird')">Like followers</button>
<button ng-click="likeUsers('ifollow')">Like I follow</button>
<button ng-click="likeUsers('special')">Like specials</button>
<button ng-click="refreshUsers()">Refresh users</button>
</nav>
<aside class="HolyGrail-ads">
Expand Down
Loading

0 comments on commit 974e77e

Please sign in to comment.