Skip to content
This repository was archived by the owner on Oct 30, 2025. It is now read-only.

Commit 2760dfa

Browse files
committed
Merge branch 'filipedelimabrito-feature/room-pinned-messages' into develop
2 parents c502862 + 31086e7 commit 2760dfa

4 files changed

Lines changed: 202 additions & 42 deletions

File tree

‎rocketchat-core/src/main/java/com/rocketchat/core/ChatRoom.java‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.rocketchat.common.data.model.BaseRoom;
44
import com.rocketchat.common.data.model.BaseUser;
5+
import com.rocketchat.common.data.model.User;
56
import com.rocketchat.common.listener.PaginatedCallback;
67
import com.rocketchat.common.listener.SimpleCallback;
78
import com.rocketchat.common.listener.SimpleListCallback;
@@ -107,10 +108,36 @@ public void getChatHistory(int limit, Date oldestMessageTimestamp, Date lasttime
107108
public void getMembers(int offset,
108109
BaseUser.SortBy sortBy,
109110
Sort sort,
110-
PaginatedCallback callback) {
111+
PaginatedCallback<User> callback) {
111112
client.getRoomMembers(room.roomId(), room.type(), offset, sortBy, sort, callback);
112113
}
113114

115+
/**
116+
* Gets the room pinned message list.
117+
*
118+
* <p>Example of expected usage:
119+
*
120+
* <blockquote><pre>
121+
* room.getPinnedMessages(0, new PaginatedCallback() {
122+
* public void onSuccess(List list, int total) {
123+
* // Handle the pinned message list and the total of pinned messages in the room (this is not the pinned message list size).
124+
* }
125+
*
126+
* public void onError(RocketChatException error) {
127+
* // Handle the error like showing a message to the user
128+
* }
129+
* });
130+
* </pre></blockquote>
131+
*
132+
* @param offset The number of items to “skip” in the query, is zero based so it starts off at 0 being the first item.
133+
* @param callback The paginated callback.
134+
* @see BaseRoom
135+
* @since 0.8.0
136+
*/
137+
public void getPinnedMessages(int offset, PaginatedCallback<Message> callback) {
138+
client.getRoomPinnedMessages(room.roomId(), room.type(), offset, callback);
139+
}
140+
114141
/**
115142
* Gets the room file list.
116143
*
@@ -140,7 +167,7 @@ public void getMembers(int offset,
140167
public void getFiles(int offset,
141168
Attachment.SortBy sortBy,
142169
Sort sort,
143-
PaginatedCallback callback) {
170+
PaginatedCallback<Attachment> callback) {
144171
client.getRoomFiles(room.roomId(), room.type(), offset, sortBy, sort, callback);
145172
}
146173

‎rocketchat-core/src/main/java/com/rocketchat/core/RestImpl.java‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
import java.lang.reflect.Type;
3535
import java.util.ArrayList;
3636
import java.util.List;
37-
import com.squareup.moshi.Types;
38-
39-
import java.io.IOException;
40-
import java.lang.reflect.Type;
4137

4238
import okhttp3.Call;
4339
import okhttp3.FormBody;
@@ -47,8 +43,6 @@
4743
import okhttp3.RequestBody;
4844
import okhttp3.Response;
4945

50-
import javax.annotation.Nonnull;
51-
5246
import static com.rocketchat.common.utils.Preconditions.checkNotNull;
5347

5448
class RestImpl {
@@ -154,7 +148,7 @@ void getRoomMembers(String roomId,
154148
int offset,
155149
BaseUser.SortBy sortBy,
156150
Sort sort,
157-
final PaginatedCallback callback) {
151+
final PaginatedCallback<User> callback) {
158152
checkNotNull(roomId, "roomId == null");
159153
checkNotNull(roomType, "roomType == null");
160154
checkNotNull(sortBy, "sortBy == null");
@@ -165,7 +159,7 @@ void getRoomMembers(String roomId,
165159
.addQueryParameter("roomId", roomId)
166160
.addQueryParameter("offset", String.valueOf(offset))
167161
// TODO add the sort on the query parameter. Track the status here:
168-
// .addQueryParameter("sort", "{\"" + sortBy.getPropertyName() + "\":" + sort.getDirection() + "}")
162+
//.addQueryParameter("sort", "{\"" + sortBy.getPropertyName() + "\":" + sort.getDirection() + "}")
169163
.build();
170164

171165
Request request = requestBuilder(httpUrl)
@@ -182,7 +176,7 @@ public void onFailure(Call call, IOException e) {
182176
public void onResponse(Call call, Response response) throws IOException {
183177
if (!response.isSuccessful()) {
184178
logger.info("Response = " + response.body().string());
185-
processCallbackError(response, callback);
179+
processCallbackError(response, ERROR_HANDLER(callback));
186180
return;
187181
}
188182
try {
@@ -206,17 +200,59 @@ void getRoomFavoriteMessages() {
206200

207201
}
208202

209-
// TODO
210-
void getRoomPinnedMessages() {
203+
void getRoomPinnedMessages(String roomId,
204+
BaseRoom.RoomType roomType,
205+
int offset,
206+
final PaginatedCallback<Message> callback) {
207+
checkNotNull(roomId,"roomId == null");
208+
checkNotNull(roomType,"roomType == null");
209+
checkNotNull(callback,"callback == null");
210+
211+
HttpUrl httpUrl = requestUrl(baseUrl, getRestApiMethodNameByRoomType(roomType, "messages"))
212+
.addQueryParameter("roomId", roomId)
213+
.addQueryParameter("offset", String.valueOf(offset))
214+
.addQueryParameter("query", "{\"pinned\":true}")
215+
.build();
216+
217+
Request request = requestBuilder(httpUrl)
218+
.get()
219+
.build();
220+
221+
client.newCall(request).enqueue(new okhttp3.Callback() {
222+
@Override
223+
public void onFailure(Call call, IOException e) {
224+
callback.onError(new RocketChatNetworkErrorException("network error", e));
225+
}
226+
227+
@Override
228+
public void onResponse(Call call, Response response) throws IOException {
229+
if (!response.isSuccessful()) {
230+
processCallbackError(response, ERROR_HANDLER(callback));
231+
return;
232+
}
211233

234+
try {
235+
JSONObject json = new JSONObject(response.body().string());
236+
logger.info("Response = " + json.toString());
237+
238+
Type type = Types.newParameterizedType(List.class, Message.class);
239+
JsonAdapter<List<Message>> adapter = moshi.adapter(type);
240+
List<Message> messageList = adapter.fromJson(json.getJSONArray("messages").toString());
241+
242+
callback.onSuccess(messageList, json.optInt("total"));
243+
} catch (JSONException e) {
244+
callback.onError(new RocketChatInvalidResponseException(e.getMessage(), e));
245+
}
246+
}
247+
});
212248
}
213249

214250
void getRoomFiles(String roomId,
215251
BaseRoom.RoomType roomType,
216252
int offset,
217253
Attachment.SortBy sortBy,
218254
Sort sort,
219-
final PaginatedCallback callback) {
255+
final PaginatedCallback<Attachment> callback) {
220256
checkNotNull(roomId, "roomId == null");
221257
checkNotNull(roomType, "roomType == null");
222258
checkNotNull(sortBy, "sortBy == null");

‎rocketchat-core/src/main/java/com/rocketchat/core/RocketChatClient.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void getRoomMembers(String roomId,
177177
int offset,
178178
BaseUser.SortBy sortBy,
179179
Sort sort,
180-
final PaginatedCallback callback) {
180+
final PaginatedCallback<User> callback) {
181181
restImpl.getRoomMembers(roomId, roomType, offset, sortBy, sort, callback);
182182
}
183183

@@ -186,17 +186,19 @@ public void getRoomFavoriteMessages() {
186186

187187
}
188188

189-
// TODO
190-
public void getRoomPinnedMessages() {
191-
189+
public void getRoomPinnedMessages(String roomId,
190+
BaseRoom.RoomType roomType,
191+
int offset,
192+
final PaginatedCallback<Message> callback) {
193+
restImpl.getRoomPinnedMessages(roomId, roomType, offset, callback);
192194
}
193195

194196
public void getRoomFiles(String roomId,
195197
BaseRoom.RoomType roomType,
196198
int offset,
197199
Attachment.SortBy sortBy,
198200
Sort sort,
199-
final PaginatedCallback callback) {
201+
final PaginatedCallback<Attachment> callback) {
200202
restImpl.getRoomFiles(roomId, roomType, offset, sortBy, sort, callback);
201203
}
202204

0 commit comments

Comments
 (0)