Add a POST /user/:id/leave, this will be used to track whether a player is currently on the network or not.

This commit is contained in:
Francisco Saldanha 2016-05-09 16:43:27 -03:00
parent 7a10f1f70d
commit 42ab0cab63
3 changed files with 45 additions and 0 deletions

View File

@ -219,6 +219,7 @@ public final class APIv3 {
post("/user/:id/grant", new POSTUserGrant(), gson::toJson);
post("/user/:id/punish", new POSTUserPunish(), gson::toJson);
post("/user/:id/login", new POSTUserLogin(), gson::toJson);
post("/user/:id/leave", new POSTUserLeave(), gson::toJson);
post("/user/:id/notify", new POSTUserNotify(), gson::toJson);
post("/user/:id/register", new POSTUserRegister(), gson::toJson);
post("/user/:id/setupTOTP", new POSTUserSetupTOTP(), gson::toJson);

View File

@ -31,6 +31,7 @@ public final class User {
@Getter private String lastSeenOn;
@Getter private Date lastSeenAt;
@Getter private Date firstSeenAt;
@Getter private boolean online;
public static User byId(String id) {
try {
@ -140,9 +141,19 @@ public final class User {
public void seenOnServer(Server server) {
this.lastSeenOn = server.getId();
if (!online) {
this.lastSeenAt = new Date();
}
this.online = true;
}
public void leftServer() {
this.lastSeenAt = new Date();
this.online = false;
}
public void updateUsername(String username) {
if (!username.equals(lastUsername)) {
this.lastUsername = username;

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.routes.users;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.User;
import net.frozenorb.apiv3.utils.ErrorUtils;
import spark.Request;
import spark.Response;
import spark.Route;
public class POSTUserLeave implements Route {
@Override
public Object handle(Request req, Response res) throws Exception {
User user = User.byId(req.params("id"));
Actor actor = req.attribute("actor");
if (actor.getType() != ActorType.SERVER) {
return ErrorUtils.serverOnly();
}
if (user == null) {
return ErrorUtils.notFound("User", req.params("id"));
}
user.leftServer();
APIv3.getDatastore().save(user);
return user;
}
}