forked from kamax-matrix/matrix-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixHttpRoom.java
160 lines (134 loc) · 5.25 KB
/
MatrixHttpRoom.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Maxime Dor
*
* https://max.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.client;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.kamax.matrix.MatrixID;
import io.kamax.matrix._MatrixID;
import io.kamax.matrix.hs._MatrixRoom;
import io.kamax.matrix.json.RoomMessageFormattedTextPutBody;
import io.kamax.matrix.json.RoomMessageTextPutBody;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class MatrixHttpRoom extends AMatrixHttpClient implements _MatrixRoom {
private Logger log = LoggerFactory.getLogger(MatrixHttpRoom.class);
private String roomId;
public MatrixHttpRoom(MatrixClientContext context, String roomId) {
super(context);
this.roomId = roomId;
}
@Override
protected URIBuilder getClientPathBuilder(String action) {
URIBuilder builder = super.getClientPathBuilder(action);
builder.setPath(builder.getPath().replace("{roomId}", roomId));
return builder;
}
@Override
public String getAddress() {
return roomId;
}
@Override
public Optional<String> getName() {
URI path = getClientPathWithAccessToken("/rooms/{roomId}/state/m.room.name");
MatrixHttpRequest request = new MatrixHttpRequest(new HttpGet(path));
request.addIgnoredErrorCode(404);
String body = execute(request);
return extractAsStringFromBody(body, "name");
}
@Override
public Optional<String> getTopic() {
URI path = getClientPathWithAccessToken("/rooms/{roomId}/state/m.room.topic");
MatrixHttpRequest matrixRequest = new MatrixHttpRequest(new HttpGet(path));
matrixRequest.addIgnoredErrorCode(404);
String body = execute(matrixRequest);
return extractAsStringFromBody(body, "topic");
}
@Override
public void join() {
URI path = getClientPathWithAccessToken("/rooms/{roomId}/join");
execute(new HttpPost(path));
}
@Override
public void leave() {
URI path = getClientPathWithAccessToken("/rooms/{roomId}/leave");
MatrixHttpRequest request = new MatrixHttpRequest(new HttpPost(path));
// TODO Find a better way to handle room objects for unknown rooms
// Maybe throw exception?
// TODO implement method to check room existence - isValid() ?
// if (res.getStatusLine().getStatusCode() == 404) {
// log.warn("Room {} is not joined, ignoring call", roomId);
// return;
// }
request.addIgnoredErrorCode(404);
execute(request);
}
private void sendMessage(RoomMessageTextPutBody content) {
URI path = getClientPathWithAccessToken("/rooms/{roomId}/send/m.room.message/" + System.currentTimeMillis());
HttpPut httpPut = new HttpPut(path);
httpPut.setEntity(getJsonEntity(content));
execute(httpPut);
}
@Override
public void sendText(String message) {
sendMessage(new RoomMessageTextPutBody(message));
}
@Override
public void sendFormattedText(String formatted, String rawFallback) {
// TODO sanitize input
sendMessage(new RoomMessageFormattedTextPutBody(rawFallback, formatted));
}
@Override
public void sendNotice(String message) {
sendMessage(new RoomMessageTextPutBody("m.notice", message));
}
@Override
public void sendNotice(String formatted, String plain) {
// TODO sanitize input
sendMessage(new RoomMessageFormattedTextPutBody("m.notice", plain, formatted));
}
@Override
public void invite(_MatrixID mxId) {
// TODO populate
log.error("Invite is not yet supported");
}
@Override
public List<_MatrixID> getJoinedUsers() {
URI path = getClientPathWithAccessToken("/rooms/{roomId}/joined_members");
String body = execute(new HttpGet(path));
List<_MatrixID> ids = new ArrayList<>();
if (StringUtils.isNotEmpty(body)) {
JsonObject joinedUsers = jsonParser.parse(body).getAsJsonObject().get("joined").getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : joinedUsers.entrySet()) {
ids.add(new MatrixID(entry.getKey()));
}
}
return ids;
}
}