Browse Source

Added API endpoints to allow registered names to be followed/unfollowed.

These will ultimately be used by the UI to control which data the node should host.
qdn
CalDescent 3 years ago
parent
commit
90465149e6
  1. 147
      src/main/java/org/qortal/api/resource/ListsResource.java

147
src/main/java/org/qortal/api/resource/ListsResource.java

@ -198,4 +198,151 @@ public class ListsResource {
return ResourceListManager.getInstance().getJSONStringForList("blacklist", "addresses");
}
/* Followed names */
@POST
@Path("/followed/names")
@Operation(
summary = "Add one or more registered names to the local followed list",
requestBody = @RequestBody(
required = true,
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ListRequest.class
)
)
),
responses = {
@ApiResponse(
description = "Returns true if all names were processed, false if any couldn't be " +
"processed, or an exception on failure. If false or an exception is returned, " +
"the list will not be updated, and the request will need to be re-issued.",
content = @Content(mediaType = MediaType.TEXT_PLAIN, schema = @Schema(type = "boolean"))
)
}
)
@ApiErrors({ApiError.INVALID_ADDRESS, ApiError.ADDRESS_UNKNOWN, ApiError.REPOSITORY_ISSUE})
public String addFollowedNames(ListRequest listRequest) {
Security.checkApiCallAllowed(request);
if (listRequest == null || listRequest.items == null) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
}
int successCount = 0;
int errorCount = 0;
try (final Repository repository = RepositoryManager.getRepository()) {
for (String name : listRequest.items) {
// Name not registered?
if (!repository.getNameRepository().nameExists(name)) {
errorCount++;
continue;
}
// Valid name, so go ahead and add it to the list
// Don't save as we will do this at the end of the process
boolean success = ResourceListManager.getInstance().addToList("followed", "names", name, false);
if (success) {
successCount++;
}
else {
errorCount++;
}
}
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
if (successCount > 0 && errorCount == 0) {
// All were successful, so save the list
ResourceListManager.getInstance().saveList("followed", "names");
return "true";
}
else {
// Something went wrong, so revert
ResourceListManager.getInstance().revertList("followed", "names");
return "false";
}
}
@DELETE
@Path("/followed/names")
@Operation(
summary = "Remove one or more registered names from the local followed list",
requestBody = @RequestBody(
required = true,
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ListRequest.class
)
)
),
responses = {
@ApiResponse(
description = "Returns true if all names were processed, false if any couldn't be " +
"processed, or an exception on failure. If false or an exception is returned, " +
"the list will not be updated, and the request will need to be re-issued.",
content = @Content(mediaType = MediaType.TEXT_PLAIN, schema = @Schema(type = "boolean"))
)
}
)
@ApiErrors({ApiError.INVALID_ADDRESS, ApiError.ADDRESS_UNKNOWN, ApiError.REPOSITORY_ISSUE})
public String removeFollowedNames(ListRequest listRequest) {
Security.checkApiCallAllowed(request);
if (listRequest == null || listRequest.items == null) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
}
int successCount = 0;
int errorCount = 0;
for (String name : listRequest.items) {
// Remove the name from the list
// Don't save as we will do this at the end of the process
boolean success = ResourceListManager.getInstance().removeFromList("followed", "names", name, false);
if (success) {
successCount++;
}
else {
errorCount++;
}
}
if (successCount > 0 && errorCount == 0) {
// All were successful, so save the list
ResourceListManager.getInstance().saveList("followed", "names");
return "true";
}
else {
// Something went wrong, so revert
ResourceListManager.getInstance().revertList("followed", "names");
return "false";
}
}
@GET
@Path("/followed/names")
@Operation(
summary = "Fetch the list of followed names",
responses = {
@ApiResponse(
description = "A JSON array of names",
content = @Content(mediaType = MediaType.APPLICATION_JSON, array = @ArraySchema(schema = @Schema(implementation = String.class)))
)
}
)
public String getFollowedNames() {
Security.checkApiCallAllowed(request);
return ResourceListManager.getInstance().getJSONStringForList("followed", "names");
}
}

Loading…
Cancel
Save