3434import java .lang .reflect .Type ;
3535import java .util .ArrayList ;
3636import java .util .List ;
37- import com .squareup .moshi .Types ;
38-
39- import java .io .IOException ;
40- import java .lang .reflect .Type ;
4137
4238import okhttp3 .Call ;
4339import okhttp3 .FormBody ;
4743import okhttp3 .RequestBody ;
4844import okhttp3 .Response ;
4945
50- import javax .annotation .Nonnull ;
51-
5246import static com .rocketchat .common .utils .Preconditions .checkNotNull ;
5347
5448class 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" );
0 commit comments