Merge the matrix-java-sdk due to it no longer maintained. Remove thirdparty repositories.
This commit is contained in:
193
src/main/java/io/kamax/matrix/json/GsonUtil.java
Normal file
193
src/main/java/io/kamax/matrix/json/GsonUtil.java
Normal file
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GsonUtil {
|
||||
|
||||
private static Gson instance = build();
|
||||
private static Gson instancePretty = buildPretty();
|
||||
|
||||
private static GsonBuilder buildImpl() {
|
||||
return new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
|
||||
.disableHtmlEscaping();
|
||||
}
|
||||
|
||||
public static Gson buildPretty() {
|
||||
return buildImpl().setPrettyPrinting().create();
|
||||
}
|
||||
|
||||
public static Gson build() {
|
||||
return buildImpl().create();
|
||||
}
|
||||
|
||||
public static JsonArray asArray(List<JsonElement> elements) {
|
||||
JsonArray a = new JsonArray();
|
||||
elements.forEach(a::add);
|
||||
return a;
|
||||
}
|
||||
|
||||
public static JsonArray asArrayObj(List<? extends Object> elements) {
|
||||
return asArray(elements.stream().map(e -> get().toJsonTree(e)).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static JsonArray asArray(String... elements) {
|
||||
return asArray(
|
||||
Arrays.stream(elements).map(JsonPrimitive::new).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static JsonArray asArray(Collection<String> elements) {
|
||||
JsonArray a = new JsonArray();
|
||||
elements.forEach(a::add);
|
||||
return a;
|
||||
}
|
||||
|
||||
public static <T> List<T> asList(JsonArray a, Class<T> c) {
|
||||
List<T> l = new ArrayList<>();
|
||||
for (JsonElement v : a) {
|
||||
l.add(GsonUtil.get().fromJson(v, c));
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public static <T> List<T> asList(JsonObject obj, String member, Class<T> c) {
|
||||
return asList(getArray(obj, member), c);
|
||||
}
|
||||
|
||||
public static JsonObject makeObj(Object o) {
|
||||
return instance.toJsonTree(o).getAsJsonObject();
|
||||
}
|
||||
|
||||
public static JsonObject makeObj(String key, Object value) {
|
||||
return makeObj(key, instance.toJsonTree(value));
|
||||
}
|
||||
|
||||
public static JsonObject makeObj(String key, JsonElement el) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.add(key, el);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static JsonObject makeObj(Consumer<JsonObject> consumer) {
|
||||
JsonObject obj = new JsonObject();
|
||||
consumer.accept(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static Gson get() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static Gson getPretty() {
|
||||
return instancePretty;
|
||||
}
|
||||
|
||||
public static String getPrettyForLog(Object o) {
|
||||
return System.lineSeparator() + getPretty().toJson(o);
|
||||
}
|
||||
|
||||
public static JsonElement parse(String s) {
|
||||
try {
|
||||
return new JsonParser().parse(s);
|
||||
} catch (JsonParseException e) {
|
||||
throw new InvalidJsonException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonObject parseObj(String s) {
|
||||
try {
|
||||
return parse(s).getAsJsonObject();
|
||||
} catch (IllegalStateException e) {
|
||||
throw new InvalidJsonException("Not an object");
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonArray getArray(JsonObject obj, String member) {
|
||||
return findArray(obj, member).orElseThrow(() -> new InvalidJsonException("Not an array"));
|
||||
}
|
||||
|
||||
public static JsonObject getObj(JsonObject obj, String member) {
|
||||
return findObj(obj, member).orElseThrow(() -> new InvalidJsonException("No object for member " + member));
|
||||
}
|
||||
|
||||
public static Optional<String> findString(JsonObject o, String key) {
|
||||
return findPrimitive(o, key).map(JsonPrimitive::getAsString);
|
||||
}
|
||||
|
||||
public static String getStringOrNull(JsonObject o, String key) {
|
||||
JsonElement el = o.get(key);
|
||||
if (el != null && el.isJsonPrimitive()) {
|
||||
return el.getAsString();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getStringOrThrow(JsonObject obj, String member) {
|
||||
if (!obj.has(member)) {
|
||||
throw new InvalidJsonException(member + " key is missing");
|
||||
}
|
||||
|
||||
return obj.get(member).getAsString();
|
||||
}
|
||||
|
||||
public static Optional<JsonElement> findElement(JsonObject o, String key) {
|
||||
return Optional.ofNullable(o.get(key));
|
||||
}
|
||||
|
||||
public static Optional<JsonPrimitive> findPrimitive(JsonObject o, String key) {
|
||||
return findElement(o, key).map(el -> el.isJsonPrimitive() ? el.getAsJsonPrimitive() : null);
|
||||
}
|
||||
|
||||
public static JsonPrimitive getPrimitive(JsonObject o, String key) {
|
||||
return findPrimitive(o, key).orElseThrow(() -> new InvalidJsonException("No primitive value for key " + key));
|
||||
}
|
||||
|
||||
public static Optional<Long> findLong(JsonObject o, String key) {
|
||||
return findPrimitive(o, key).map(JsonPrimitive::getAsLong);
|
||||
}
|
||||
|
||||
public static long getLong(JsonObject o, String key) {
|
||||
return findLong(o, key).orElseThrow(() -> new InvalidJsonException("No numeric value for key " + key));
|
||||
}
|
||||
|
||||
public static Optional<JsonObject> findObj(JsonObject o, String key) {
|
||||
if (!o.has(key)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(o.getAsJsonObject(key));
|
||||
}
|
||||
|
||||
public static Optional<JsonArray> findArray(JsonObject o, String key) {
|
||||
return findElement(o, key).filter(JsonElement::isJsonArray).map(JsonElement::getAsJsonArray);
|
||||
}
|
||||
|
||||
}
|
||||
35
src/main/java/io/kamax/matrix/json/InvalidJsonException.java
Normal file
35
src/main/java/io/kamax/matrix/json/InvalidJsonException.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import io.kamax.matrix.MatrixException;
|
||||
|
||||
public class InvalidJsonException extends MatrixException {
|
||||
|
||||
public InvalidJsonException(Throwable t) {
|
||||
super("M_BAD_JSON", t.getMessage(), t);
|
||||
}
|
||||
|
||||
public InvalidJsonException(String error) {
|
||||
super("M_BAD_JSON", error);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class JsonCanonicalException extends RuntimeException {
|
||||
|
||||
public JsonCanonicalException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JsonCanonicalException(Throwable t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
}
|
||||
58
src/main/java/io/kamax/matrix/json/LoginPostBody.java
Normal file
58
src/main/java/io/kamax/matrix/json/LoginPostBody.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class LoginPostBody {
|
||||
|
||||
private String type = "m.login.password";
|
||||
private String user;
|
||||
private String password;
|
||||
private String deviceId;
|
||||
private String initialDeviceDisplayName;
|
||||
|
||||
public LoginPostBody(String user, String password) {
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public LoginPostBody(String user, String password, String deviceId) {
|
||||
this(user, password);
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getDeviceId() {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceId(String deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getInitialDeviceDisplayName() {
|
||||
return initialDeviceDisplayName;
|
||||
}
|
||||
|
||||
public void setInitialDeviceDisplayName(String initialDeviceDisplayName) {
|
||||
this.initialDeviceDisplayName = initialDeviceDisplayName;
|
||||
}
|
||||
|
||||
}
|
||||
45
src/main/java/io/kamax/matrix/json/LoginResponse.java
Normal file
45
src/main/java/io/kamax/matrix/json/LoginResponse.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class LoginResponse {
|
||||
|
||||
private String access_token;
|
||||
private String home_server;
|
||||
private String user_id;
|
||||
private String device_id;
|
||||
|
||||
public String getAccessToken() {
|
||||
return access_token;
|
||||
}
|
||||
|
||||
public String getHomeServer() {
|
||||
return home_server;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return user_id;
|
||||
}
|
||||
|
||||
public String getDeviceId() {
|
||||
return device_id;
|
||||
}
|
||||
}
|
||||
115
src/main/java/io/kamax/matrix/json/MatrixJson.java
Normal file
115
src/main/java/io/kamax/matrix/json/MatrixJson.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
|
||||
public class MatrixJson {
|
||||
|
||||
// Needed to avoid silly try/catch block in lambdas
|
||||
// We only use ByteArray streams, so IOException will not happen (unless irrecoverable situation like OOM)
|
||||
private static class JsonWriterUnchecked extends JsonWriter {
|
||||
|
||||
public JsonWriterUnchecked(Writer out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonWriter name(String value) {
|
||||
try {
|
||||
return super.name(value);
|
||||
} catch (IOException e) {
|
||||
throw new JsonCanonicalException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonParser parser = new JsonParser();
|
||||
|
||||
private static void encodeCanonical(JsonObject el, JsonWriterUnchecked writer) throws IOException {
|
||||
writer.beginObject();
|
||||
el.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEachOrdered(entry -> {
|
||||
writer.name(entry.getKey());
|
||||
encodeCanonicalElement(entry.getValue(), writer);
|
||||
});
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
private static void encodeCanonicalArray(JsonArray array, JsonWriterUnchecked writer) throws IOException {
|
||||
writer.beginArray();
|
||||
array.forEach(el -> encodeCanonicalElement(el, writer));
|
||||
writer.endArray();
|
||||
}
|
||||
|
||||
private static void encodeCanonicalElement(JsonElement el, JsonWriterUnchecked writer) {
|
||||
try {
|
||||
if (el.isJsonObject()) encodeCanonical(el.getAsJsonObject(), writer);
|
||||
else if (el.isJsonPrimitive()) writer.jsonValue(el.toString());
|
||||
else if (el.isJsonArray()) encodeCanonicalArray(el.getAsJsonArray(), writer);
|
||||
else if (el.isJsonNull()) writer.nullValue();
|
||||
else throw new JsonCanonicalException("Unexpected JSON type, this is a bug, report!");
|
||||
} catch (IOException e) {
|
||||
throw new JsonCanonicalException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String encodeCanonical(JsonObject obj) {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
JsonWriterUnchecked writer = new JsonWriterUnchecked(new OutputStreamWriter(out, StandardCharsets.UTF_8));
|
||||
writer.setIndent("");
|
||||
writer.setHtmlSafe(false);
|
||||
writer.setLenient(false);
|
||||
|
||||
encodeCanonical(obj, writer);
|
||||
writer.close();
|
||||
return out.toString(StandardCharsets.UTF_8.name());
|
||||
} catch (IOException e) {
|
||||
throw new JsonCanonicalException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String encodeCanonical(String data) {
|
||||
JsonElement el = parser.parse(data);
|
||||
if (!el.isJsonObject()) {
|
||||
/*
|
||||
* TODO seems implied because of how signing/checking signatures is done and because of
|
||||
* https://matrix.to/#/!XqBunHwQIXUiqCaoxq:matrix.org/$15075894901530229RWcIi:matrix.org
|
||||
* with the "whole object".
|
||||
*/
|
||||
throw new JsonCanonicalException("Not a JSON object, cannot encode canonical");
|
||||
}
|
||||
return encodeCanonical(el.getAsJsonObject());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event.*;
|
||||
import io.kamax.matrix.json.event.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonEventFactory {
|
||||
|
||||
public static _MatrixEvent get(JsonObject obj) {
|
||||
String type = obj.get("type").getAsString();
|
||||
|
||||
if ("m.room.member".contentEquals(type)) {
|
||||
return new MatrixJsonRoomMembershipEvent(obj);
|
||||
} else if ("m.room.power_levels".contentEquals(type)) {
|
||||
return new MatrixJsonRoomPowerLevelsEvent(obj);
|
||||
} else if ("m.room.avatar".contentEquals(type)) {
|
||||
return new MatrixJsonRoomAvatarEvent(obj);
|
||||
} else if ("m.room.name".contentEquals(type)) {
|
||||
return new MatrixJsonRoomNameEvent(obj);
|
||||
} else if ("m.room.topic".contentEquals(type)) {
|
||||
return new MatrixJsonRoomTopicEvent(obj);
|
||||
} else if ("m.room.aliases".contentEquals(type)) {
|
||||
return new MatrixJsonRoomAliasesEvent(obj);
|
||||
} else if (_RoomCanonicalAliasEvent.Type.contentEquals(type)) {
|
||||
return new MatrixJsonRoomCanonicalAliasEvent(obj);
|
||||
} else if ("m.room.message".contentEquals(type)) {
|
||||
return new MatrixJsonRoomMessageEvent(obj);
|
||||
} else if ("m.receipt".contentEquals(type)) {
|
||||
return new MatrixJsonReadReceiptEvent(obj);
|
||||
} else if ("m.room.history_visibility".contentEquals(type)) {
|
||||
return new MatrixJsonRoomHistoryVisibilityEvent(obj);
|
||||
} else if (_TagsEvent.Type.contentEquals(type)) {
|
||||
return new MatrixJsonRoomTagsEvent(obj);
|
||||
} else if (_DirectEvent.Type.contentEquals(type)) {
|
||||
return new MatrixJsonDirectEvent(obj);
|
||||
} else {
|
||||
Optional<String> timestamp = EventKey.Timestamp.findString(obj);
|
||||
Optional<String> sender = EventKey.Sender.findString(obj);
|
||||
|
||||
if (!timestamp.isPresent() || !sender.isPresent()) {
|
||||
return new MatrixJsonEphemeralEvent(obj);
|
||||
} else {
|
||||
Optional<String> rId = EventKey.RoomId.findString(obj);
|
||||
if (rId.isPresent()) {
|
||||
return new MatrixJsonRoomEvent(obj);
|
||||
}
|
||||
|
||||
return new MatrixJsonPersistentEvent(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
110
src/main/java/io/kamax/matrix/json/MatrixJsonObject.java
Normal file
110
src/main/java/io/kamax/matrix/json/MatrixJsonObject.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonObject {
|
||||
|
||||
private JsonObject obj;
|
||||
|
||||
public MatrixJsonObject(JsonObject obj) {
|
||||
if (Objects.isNull(obj)) {
|
||||
throw new InvalidJsonException("JSON Object is null");
|
||||
}
|
||||
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
protected Optional<String> findString(String field) {
|
||||
return GsonUtil.findString(obj, field);
|
||||
}
|
||||
|
||||
protected String getString(String field) {
|
||||
return GsonUtil.getStringOrNull(obj, field);
|
||||
}
|
||||
|
||||
protected String getStringOrNull(JsonObject obj, String field) {
|
||||
return GsonUtil.findString(obj, field).orElse(null);
|
||||
}
|
||||
|
||||
protected String getStringOrNull(String field) {
|
||||
return getStringOrNull(obj, field);
|
||||
}
|
||||
|
||||
protected int getInt(String field) {
|
||||
return GsonUtil.getPrimitive(obj, field).getAsInt();
|
||||
}
|
||||
|
||||
protected int getInt(String field, int failover) {
|
||||
return GsonUtil.findPrimitive(obj, field).map(JsonPrimitive::getAsInt).orElse(failover);
|
||||
}
|
||||
|
||||
protected Optional<Long> findLong(String field) {
|
||||
return GsonUtil.findLong(obj, field);
|
||||
}
|
||||
|
||||
protected long getLong(String field) {
|
||||
return GsonUtil.getLong(obj, field);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the Double value, if the key is present, null else
|
||||
*/
|
||||
protected Double getDoubleIfPresent(String field) {
|
||||
if (obj.get(field) != null) {
|
||||
return GsonUtil.getPrimitive(obj, field).getAsDouble();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected JsonObject asObj(JsonElement el) {
|
||||
if (!el.isJsonObject()) {
|
||||
throw new IllegalArgumentException("Not a JSON object");
|
||||
}
|
||||
|
||||
return el.getAsJsonObject();
|
||||
}
|
||||
|
||||
protected JsonObject getObj(String field) {
|
||||
return GsonUtil.getObj(obj, field);
|
||||
}
|
||||
|
||||
protected Optional<JsonObject> findObj(String field) {
|
||||
return GsonUtil.findObj(obj, field);
|
||||
}
|
||||
|
||||
protected JsonObject computeObj(String field) {
|
||||
return findObj(field).orElseGet(JsonObject::new);
|
||||
}
|
||||
|
||||
protected Optional<JsonArray> findArray(String field) {
|
||||
return GsonUtil.findArray(obj, field);
|
||||
}
|
||||
|
||||
public JsonObject getJson() {
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
46
src/main/java/io/kamax/matrix/json/RoomAliasLookupJson.java
Normal file
46
src/main/java/io/kamax/matrix/json/RoomAliasLookupJson.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sàrl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RoomAliasLookupJson {
|
||||
|
||||
private String roomId;
|
||||
private List<String> servers;
|
||||
|
||||
public String getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public void setRoomId(String roomId) {
|
||||
this.roomId = roomId;
|
||||
}
|
||||
|
||||
public List<String> getServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
public void setServers(List<String> servers) {
|
||||
this.servers = servers;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.matrix.room._RoomCreationOptions;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RoomCreationRequestJson {
|
||||
|
||||
private String visibility;
|
||||
private String roomAliasName;
|
||||
private String name;
|
||||
private String topic;
|
||||
private Set<String> invite;
|
||||
private Map<String, JsonElement> creationContent;
|
||||
private String preset;
|
||||
private Boolean isDirect;
|
||||
private Boolean guestCanJoin;
|
||||
|
||||
public RoomCreationRequestJson(_RoomCreationOptions options) {
|
||||
this.visibility = options.getVisibility().orElse(null);
|
||||
this.roomAliasName = options.getAliasName().orElse(null);
|
||||
this.name = options.getName().orElse(null);
|
||||
this.topic = options.getTopic().orElse(null);
|
||||
this.invite = options.getInvites().filter(ids -> !ids.isEmpty())
|
||||
.map(ids -> ids.stream().map(_MatrixID::getId).collect(Collectors.toSet())).orElse(null);
|
||||
this.creationContent = options.getCreationContent().filter(c -> !c.isEmpty()).orElse(null);
|
||||
this.preset = options.getPreset().orElse(null);
|
||||
this.isDirect = options.isDirect().orElse(null);
|
||||
this.guestCanJoin = options.isGuestCanJoin().orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class RoomCreationResponseJson {
|
||||
|
||||
private String roomId;
|
||||
|
||||
public String getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RoomMessageChunkResponseJson {
|
||||
|
||||
private String start;
|
||||
private String end;
|
||||
private List<JsonObject> chunk;
|
||||
|
||||
public String getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public String getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public List<JsonObject> getChunk() {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class RoomMessageFormattedTextPutBody extends RoomMessageTextPutBody {
|
||||
|
||||
private String formatted_body;
|
||||
private String format = "org.matrix.custom.html";
|
||||
|
||||
public RoomMessageFormattedTextPutBody(String body, String formattedBody) {
|
||||
super(body);
|
||||
|
||||
this.formatted_body = formattedBody;
|
||||
}
|
||||
|
||||
public RoomMessageFormattedTextPutBody(String msgtype, String body, String formattedBody) {
|
||||
super(msgtype, body);
|
||||
|
||||
this.formatted_body = formattedBody;
|
||||
}
|
||||
|
||||
public String getFormatted_body() {
|
||||
return formatted_body;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class RoomMessageTextPutBody {
|
||||
|
||||
private String msgtype = "m.text";
|
||||
private String body;
|
||||
|
||||
public RoomMessageTextPutBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public RoomMessageTextPutBody(String msgType, String body) {
|
||||
this(body);
|
||||
this.msgtype = msgType;
|
||||
}
|
||||
|
||||
public String getMsgtype() {
|
||||
return msgtype;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
}
|
||||
31
src/main/java/io/kamax/matrix/json/RoomTagSetBody.java
Normal file
31
src/main/java/io/kamax/matrix/json/RoomTagSetBody.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Arne Augenstein
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class RoomTagSetBody {
|
||||
|
||||
private String order;
|
||||
|
||||
public RoomTagSetBody(double order) {
|
||||
this.order = String.valueOf(order);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class UserDisplaynameSetBody {
|
||||
|
||||
private String displayname;
|
||||
|
||||
public UserDisplaynameSetBody(String displayname) {
|
||||
this.displayname = displayname;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json;
|
||||
|
||||
public class VirtualUserRegistrationBody {
|
||||
|
||||
private String type = "m.login.application_server";
|
||||
private String username;
|
||||
|
||||
public VirtualUserRegistrationBody(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.MatrixID;
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.matrix.event._DirectEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.matrix.json.InvalidJsonException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MatrixJsonDirectEvent extends MatrixJsonEphemeralEvent implements _DirectEvent {
|
||||
|
||||
private Map<_MatrixID, List<String>> mappings = new HashMap<>();
|
||||
|
||||
public MatrixJsonDirectEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
if (!StringUtils.equals(Type, getType())) { // FIXME check should be done in the abstract class
|
||||
throw new InvalidJsonException("Type is not " + Type);
|
||||
}
|
||||
|
||||
getObj("content").entrySet().forEach(entry -> {
|
||||
if (!entry.getValue().isJsonArray()) {
|
||||
throw new InvalidJsonException("Content key " + entry.getKey() + " is not an array");
|
||||
}
|
||||
_MatrixID id = MatrixID.asAcceptable(entry.getKey());
|
||||
mappings.put(id, GsonUtil.asList(entry.getValue().getAsJsonArray(), String.class));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<_MatrixID, List<String>> getMappings() {
|
||||
return Collections.unmodifiableMap(mappings);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Arne Augenstein
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._MatrixEphemeralEvent;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
public class MatrixJsonEphemeralEvent extends MatrixJsonObject implements _MatrixEphemeralEvent {
|
||||
|
||||
private String type;
|
||||
|
||||
public MatrixJsonEphemeralEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
type = getString("type");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.MatrixID;
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.matrix.event._MatrixPersistentEvent;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
public class MatrixJsonPersistentEvent extends MatrixJsonObject implements _MatrixPersistentEvent {
|
||||
|
||||
private String id;
|
||||
private String type;
|
||||
private Long time;
|
||||
private int age;
|
||||
private _MatrixID sender;
|
||||
|
||||
public MatrixJsonPersistentEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
id = getString("event_id");
|
||||
type = getString("type");
|
||||
time = getLong("origin_server_ts");
|
||||
age = getInt("age", -1);
|
||||
sender = MatrixID.asAcceptable(getString("sender"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public _MatrixID getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Arne Augenstein
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.MatrixID;
|
||||
import io.kamax.matrix.event._ReadReceiptEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MatrixJsonReadReceiptEvent extends MatrixJsonEphemeralEvent implements _ReadReceiptEvent {
|
||||
|
||||
/**
|
||||
* Read receipts for a specific event.
|
||||
*/
|
||||
public static class Receipt {
|
||||
/**
|
||||
* ID of the event, the read markers point to.
|
||||
*/
|
||||
private String eventId;
|
||||
|
||||
/**
|
||||
* Every user whose read marker is set to the event specified by eventId
|
||||
* and it's point in time when this marker has been set to this event.
|
||||
*/
|
||||
private Map<MatrixID, Long> users;
|
||||
|
||||
public Receipt(String id, Map<MatrixID, Long> readMarkers) {
|
||||
this.eventId = id;
|
||||
this.users = readMarkers;
|
||||
}
|
||||
|
||||
public Map<MatrixID, Long> getUsersWithTimestamp() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public Set<MatrixID> getUsers() {
|
||||
return users.keySet();
|
||||
}
|
||||
|
||||
public String getEventId() {
|
||||
return eventId;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Receipt> receipts;
|
||||
|
||||
@Override
|
||||
public List<Receipt> getReceipts() {
|
||||
return receipts;
|
||||
}
|
||||
|
||||
public MatrixJsonReadReceiptEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
JsonObject content = getObj("content");
|
||||
List<String> eventIds = content.entrySet().stream().map(Map.Entry::getKey)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
receipts = eventIds.stream().map(id -> {
|
||||
JsonObject targetEvent = content.getAsJsonObject(id);
|
||||
JsonObject mRead = targetEvent.getAsJsonObject("m.read");
|
||||
|
||||
Map<MatrixID, Long> readMarkers = mRead.entrySet().stream()
|
||||
.collect(Collectors.toMap(it -> MatrixID.asAcceptable(it.getKey()),
|
||||
it -> GsonUtil.getLong(it.getValue().getAsJsonObject(), "ts")));
|
||||
return new Receipt(id, readMarkers);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomAliasesEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class MatrixJsonRoomAliasesEvent extends MatrixJsonRoomEvent implements _RoomAliasesEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private List<String> aliases;
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
setAliases(GsonUtil.asList(obj, "aliases", String.class));
|
||||
}
|
||||
|
||||
public List<String> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public void setAliases(List<String> aliases) {
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomAliasesEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
this.content = new Content(getObj("content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAliases() {
|
||||
return Collections.unmodifiableList(content.getAliases());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomAvatarEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
|
||||
public class MatrixJsonRoomAvatarEvent extends MatrixJsonRoomEvent implements _RoomAvatarEvent {
|
||||
|
||||
public static class Content {
|
||||
|
||||
private String url;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomAvatarEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
this.content = GsonUtil.get().fromJson(getObj("content"), Content.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return content.getUrl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomCanonicalAliasEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonRoomCanonicalAliasEvent extends MatrixJsonRoomEvent implements _RoomCanonicalAliasEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private String alias;
|
||||
|
||||
public Content(JsonObject content) {
|
||||
super(content);
|
||||
findString("alias").filter(StringUtils::isNotEmpty).ifPresent(this::setAlias);
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
public void setAlias(String alias) {
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomCanonicalAliasEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
if (!StringUtils.equals(Type, getType())) { // FIXME check should be done in the abstract class
|
||||
throw new IllegalArgumentException("Type is not " + Type);
|
||||
}
|
||||
|
||||
this.content = new Content(GsonUtil.getObj(obj, "content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAlias() {
|
||||
return StringUtils.isNotEmpty(content.getAlias());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getAlias() {
|
||||
return Optional.ofNullable(content.getAlias());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomEvent;
|
||||
|
||||
public class MatrixJsonRoomEvent extends MatrixJsonPersistentEvent implements _RoomEvent {
|
||||
|
||||
private String roomId;
|
||||
|
||||
public MatrixJsonRoomEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
roomId = getString("room_id");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Arne Augenstein
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomHistoryVisibilityEvent;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
public class MatrixJsonRoomHistoryVisibilityEvent extends MatrixJsonRoomEvent implements _RoomHistoryVisibilityEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private String historyVisibility;
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
setHistoryVisibility(getString("history_visibility"));
|
||||
}
|
||||
|
||||
public String getHistoryVisibility() {
|
||||
return historyVisibility;
|
||||
}
|
||||
|
||||
public void setHistoryVisibility(String historyVisibility) {
|
||||
this.historyVisibility = historyVisibility;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomHistoryVisibilityEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
content = new Content(getObj("content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHistoryVisibility() {
|
||||
return content.getHistoryVisibility();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.MatrixID;
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.matrix.event._RoomMembershipEvent;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonRoomMembershipEvent extends MatrixJsonRoomEvent implements _RoomMembershipEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private String membership;
|
||||
private String avatar;
|
||||
private String displayName;
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
setMembership(getString("membership"));
|
||||
setAvatar(avatar = getStringOrNull("avatar_url"));
|
||||
setDisplayName(displayName = getStringOrNull("displayname"));
|
||||
}
|
||||
|
||||
public String getMembership() {
|
||||
return membership;
|
||||
}
|
||||
|
||||
public void setMembership(String membership) {
|
||||
this.membership = membership;
|
||||
}
|
||||
|
||||
public String getAvatar() {
|
||||
return avatar;
|
||||
}
|
||||
|
||||
public void setAvatar(String avatar) {
|
||||
this.avatar = avatar;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public void setDisplayName(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
private _MatrixID invitee;
|
||||
|
||||
public MatrixJsonRoomMembershipEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
content = new Content(getObj("content"));
|
||||
invitee = new MatrixID(getString("state_key"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMembership() {
|
||||
return content.getMembership();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getAvatarUrl() {
|
||||
return Optional.ofNullable(content.getAvatar());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getDisplayName() {
|
||||
return Optional.ofNullable(content.getDisplayName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public _MatrixID getInvitee() {
|
||||
return invitee;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomMessageEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonRoomMessageEvent extends MatrixJsonRoomEvent implements _RoomMessageEvent {
|
||||
|
||||
protected JsonObject content;
|
||||
|
||||
public MatrixJsonRoomMessageEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
this.content = obj.getAsJsonObject("content");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBody() {
|
||||
return content.get("body").getAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBodyType() {
|
||||
return content.has("msgtype") ? content.get("msgtype").getAsString() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getFormat() {
|
||||
return GsonUtil.findString(content, "format");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getFormattedBody() {
|
||||
return GsonUtil.findString(content, "formatted_body");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomNameEvent;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonRoomNameEvent extends MatrixJsonRoomEvent implements _RoomNameEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private String name;
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
setName(getStringOrNull("name"));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomNameEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
this.content = new Content(getObj("content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getName() {
|
||||
return Optional.ofNullable(content.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Arne Augenstein
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomPowerLevelsEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MatrixJsonRoomPowerLevelsEvent extends MatrixJsonRoomEvent implements _RoomPowerLevelsEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private Double ban;
|
||||
private Map<String, Double> events = new HashMap<>();
|
||||
private Double eventsDefault;
|
||||
private Double invite;
|
||||
private Double kick;
|
||||
private Double redact;
|
||||
private Double stateDefault;
|
||||
private Map<String, Double> users = new HashMap<>();
|
||||
private Double usersDefault;
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
setBan(getDoubleIfPresent("ban"));
|
||||
setEventsDefault(getDoubleIfPresent("events_default"));
|
||||
setInvite(getDoubleIfPresent("invite"));
|
||||
setKick(getDoubleIfPresent("kick"));
|
||||
setRedact(getDoubleIfPresent("redact"));
|
||||
setStateDefault(getDoubleIfPresent("state_default"));
|
||||
setUsersDefault(getDoubleIfPresent("users_default"));
|
||||
|
||||
GsonUtil.findObj(obj, "events").ifPresent(eventsJson -> {
|
||||
Map<String, Double> eventsMap = eventsJson.entrySet().stream()
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, it -> it.getValue().getAsDouble()));
|
||||
setEvents(eventsMap);
|
||||
});
|
||||
|
||||
GsonUtil.findObj(obj, "users").ifPresent(usersJson -> {
|
||||
Map<String, Double> usersMap = usersJson.entrySet().stream()
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, it -> it.getValue().getAsDouble()));
|
||||
setUsers(usersMap);
|
||||
});
|
||||
}
|
||||
|
||||
public Double getBan() {
|
||||
return ban;
|
||||
}
|
||||
|
||||
public void setBan(Double ban) {
|
||||
this.ban = ban;
|
||||
}
|
||||
|
||||
public Double getEventsDefault() {
|
||||
return eventsDefault;
|
||||
}
|
||||
|
||||
public void setEventsDefault(Double eventsDefault) {
|
||||
this.eventsDefault = eventsDefault;
|
||||
}
|
||||
|
||||
public Map<String, Double> getEvents() {
|
||||
return events;
|
||||
}
|
||||
|
||||
public void setEvents(Map<String, Double> events) {
|
||||
this.events.putAll(events);
|
||||
}
|
||||
|
||||
public Double getInvite() {
|
||||
return invite;
|
||||
}
|
||||
|
||||
public void setInvite(Double invite) {
|
||||
this.invite = invite;
|
||||
}
|
||||
|
||||
public Double getKick() {
|
||||
return kick;
|
||||
}
|
||||
|
||||
public void setKick(Double kick) {
|
||||
this.kick = kick;
|
||||
}
|
||||
|
||||
public Double getRedact() {
|
||||
return redact;
|
||||
}
|
||||
|
||||
public void setRedact(Double redact) {
|
||||
this.redact = redact;
|
||||
}
|
||||
|
||||
public Double getStateDefault() {
|
||||
return stateDefault;
|
||||
}
|
||||
|
||||
public void setStateDefault(Double stateDefault) {
|
||||
this.stateDefault = stateDefault;
|
||||
}
|
||||
|
||||
public Map<String, Double> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(Map<String, Double> users) {
|
||||
this.users.putAll(users);
|
||||
}
|
||||
|
||||
public Double getUsersDefault() {
|
||||
return usersDefault;
|
||||
}
|
||||
|
||||
public void setUsersDefault(Double usersDefault) {
|
||||
this.usersDefault = usersDefault;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomPowerLevelsEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
content = new Content(getObj("content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getBan() {
|
||||
return Optional.ofNullable(content.getBan());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Double> getEvents() {
|
||||
return Collections.unmodifiableMap(content.getEvents());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getEventsDefault() {
|
||||
return Optional.ofNullable(content.getEventsDefault());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getInvite() {
|
||||
return Optional.ofNullable(content.getInvite());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getKick() {
|
||||
return Optional.ofNullable(content.getKick());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getRedact() {
|
||||
return Optional.ofNullable(content.getRedact());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getStateDefault() {
|
||||
return Optional.ofNullable(content.getStateDefault());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Double> getUsers() {
|
||||
return Collections.unmodifiableMap(content.getUsers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Double> getUsersDefault() {
|
||||
return Optional.ofNullable(content.getUsersDefault());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Arne Augenstein
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._TagsEvent;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
import io.kamax.matrix.room.Tag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MatrixJsonRoomTagsEvent extends MatrixJsonObject implements _TagsEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
GsonUtil.findObj(obj, "tags").ifPresent(tagsJson -> {
|
||||
List<Tag> tags = tagsJson.entrySet().stream().map(it -> {
|
||||
String completeName = it.getKey();
|
||||
String name = completeName;
|
||||
String namespace = "";
|
||||
|
||||
int lastDotIndex = completeName.lastIndexOf(".");
|
||||
|
||||
if (lastDotIndex >= 0 && lastDotIndex < completeName.length() - 1) {
|
||||
namespace = completeName.substring(0, lastDotIndex);
|
||||
name = completeName.substring(lastDotIndex + 1);
|
||||
}
|
||||
|
||||
JsonElement jsonOrder = it.getValue().getAsJsonObject().get("order");
|
||||
|
||||
if (jsonOrder != null) {
|
||||
return new Tag(namespace, name, jsonOrder.getAsDouble());
|
||||
}
|
||||
return new Tag(namespace, name, null);
|
||||
}).collect(Collectors.toList());
|
||||
setTags(tags);
|
||||
});
|
||||
}
|
||||
|
||||
public List<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(List<Tag> tags) {
|
||||
this.tags = new ArrayList<>(tags);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String type;
|
||||
protected Content content;
|
||||
|
||||
public MatrixJsonRoomTagsEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
type = getString("type");
|
||||
content = new Content(getObj("content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getTags() {
|
||||
return Collections.unmodifiableList(content.getTags());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* matrix-java-sdk - Matrix Client SDK for Java
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.matrix.json.event;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import io.kamax.matrix.event._RoomTopicEvent;
|
||||
import io.kamax.matrix.json.MatrixJsonObject;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatrixJsonRoomTopicEvent extends MatrixJsonRoomEvent implements _RoomTopicEvent {
|
||||
|
||||
public static class Content extends MatrixJsonObject {
|
||||
|
||||
private String topic;
|
||||
|
||||
public Content(JsonObject obj) {
|
||||
super(obj);
|
||||
|
||||
setTopic(getStringOrNull("topic"));
|
||||
}
|
||||
|
||||
public String getTopic() {
|
||||
return topic;
|
||||
}
|
||||
|
||||
public void setTopic(String topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Content content;
|
||||
|
||||
public MatrixJsonRoomTopicEvent(JsonObject obj) {
|
||||
super(obj);
|
||||
this.content = new Content(getObj("content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getTopic() {
|
||||
return Optional.ofNullable(content.getTopic());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user