JsonObject contentElement = element.getAsJsonObject();
String type = contentElement.get("type").getAsString();
String source = contentElement.get("source").getAsString();
Long id = contentElement.get("id") != null ? contentElement.get("id").getAsLong() : null;
if(type==null) {
Error error = new Error("Type field is empty.");
error.apply(request, response);
return;
}
Content.ContentType contentType = Content.ContentType.valueOf(type.toUpperCase());
if(contentType == null) {
Error error = new Error(type + " is not a correct type value.");
error.apply(request, response);
return;
}
if(source==null) {
Error error = new Error("Source field is empty");
error.apply(request, response);
return;
}
// Se comenta esto para permitir contenido externo
/*if(id==null) {
Error error = new Error("Id field is empty");
error.apply(request, response);
return;
}*/
SharedContent sharedContent = new Message().new SharedContent();
sharedContent.type = contentType;
sharedContent.id = id;
sharedContent.source = source;
sharedContents.add(sharedContent);
}
ArrayList<User> receivers = new ArrayList<User>();
for(JsonElement element : body.get("receivers").getAsJsonArray()) {
JsonObject toElement = element.getAsJsonObject();
Long userId = toElement.get("id").getAsLong();
String firstName = toElement.get("firstName").getAsString();
if(userId == null) {
Error error = new Error("The id field of " + firstName + " is empty.");
error.apply(request, response);
return;
}
User to = User.findById(userId);
if(to == null) {
Error error = new Error(firstName + " is not a registered user.");
error.apply(request, response);
return;
}
receivers.add(to);
}