matrix-java-sdk
matrix-java-sdk copied to clipboard
Get image info from MatrixJsonRoomMessageEvent
Currently it's not possible to query the contents of an event with an image via the API. Image events have next to the body an info JSON-object which contains the necessary information like mimetype information, the mxc-URL of the image etc. It would be nice to be able to query this stuff via the API instead of having to parse the JSON-info object itself.
The msgtype is m.image in this case.
For anyone looking at this if needed here is a code snippet used in SimpleMatrix:
package blog.nordgedanken.simplematrix.data.matrix.additionaltypes.messages;
import com.google.gson.JsonObject;
import io.kamax.matrix.json.GsonUtil;
import io.kamax.matrix.json.event.MatrixJsonRoomMessageEvent;
import java8.util.Optional;
public class MatrixJsonRoomImageMessageEvent extends MatrixJsonRoomMessageEvent {
public MatrixJsonRoomImageMessageEvent(JsonObject obj) {
super(obj);
this.content = obj;
}
public Optional<String> getURL() {
return GsonUtil.findString(this.content, "url");
}
public Optional<JsonObject> getInfo() {
return GsonUtil.findObj(this.content, "info");
}
public Optional<JsonObject> getThumbnailInfo() {
Optional<JsonObject> info = this.getInfo();
if (info.isPresent()) {
return GsonUtil.findObj(info.get(), "thumbnail_info");
}
return Optional.empty();
}
public Optional<Integer> getWidth() {
Optional<JsonObject> info = this.getInfo();
if (info.isPresent()) {
Optional<JsonObject> thumbnail_info = this.getThumbnailInfo();
if (thumbnail_info.isPresent()) {
Optional<Long> width = GsonUtil.findLong(info.get(), "w");
if (width.isPresent()) {
int width_int = width.get().intValue();
return Optional.ofNullable(width_int);
}
}
}
return Optional.empty();
}
public Optional<Integer> getHeight() {
Optional<JsonObject> info = this.getInfo();
if (info.isPresent()) {
Optional<JsonObject> thumbnail_info = this.getThumbnailInfo();
if (thumbnail_info.isPresent()) {
Optional<Long> width = GsonUtil.findLong(info.get(), "h");
if (width.isPresent()) {
int width_int = width.get().intValue();
return Optional.ofNullable(width_int);
}
}
}
return Optional.empty();
}
}