matrix-java-sdk icon indicating copy to clipboard operation
matrix-java-sdk copied to clipboard

Get image info from MatrixJsonRoomMessageEvent

Open MrCustomizer opened this issue 7 years ago • 2 comments

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.

MrCustomizer avatar May 11 '18 17:05 MrCustomizer

The msgtype is m.image in this case.

MrCustomizer avatar May 11 '18 17:05 MrCustomizer

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();
    }
}

MTRNord avatar Feb 06 '19 20:02 MTRNord