22
33import com .hyphenate .EMConversationListener ;
44import com .hyphenate .EMMessageListener ;
5+ import com .hyphenate .EMValueCallBack ;
56import com .hyphenate .chat .EMClient ;
67import com .hyphenate .chat .*;
78import com .hyphenate .chat .EMConversation .EMSearchDirection ;
@@ -110,6 +111,12 @@ public void onMethodCall(MethodCall call, Result result) {
110111 fetchReactionDetail (param , call .method , result );
111112 } else if (EMSDKMethod .reportMessage .equals (call .method )) {
112113 reportMessage (param , call .method , result );
114+ } else if (EMSDKMethod .fetchConversationsFromServerWithPage .equals (call .method )) {
115+ getConversationsFromServerWithPage (param , call .method , result );
116+ } else if (EMSDKMethod .removeMessagesFromServerWithMsgIds .equals (call .method )) {
117+ removeMessagesFromServerWithMsgIds (param , call .method , result );
118+ } else if (EMSDKMethod .removeMessagesFromServerWithTs .equals (call .method )) {
119+ removeMessagesFromServerWithTs (param , call .method , result );
113120 }
114121 else {
115122 super .onMethodCall (call , result );
@@ -121,13 +128,14 @@ public void onMethodCall(MethodCall call, Result result) {
121128
122129 private void sendMessage (JSONObject param , String channelName , Result result ) throws JSONException {
123130 final EMMessage msg = EMMessageHelper .fromJson (param );
131+ final String localId = msg .getMsgId ();
124132 msg .setMessageStatusCallback (new EMWrapperCallBack (result , channelName , null ) {
125133 @ Override
126134 public void onSuccess () {
127135 post (() -> {
128136 Map <String , Object > map = new HashMap <>();
129137 map .put ("message" , EMMessageHelper .toJson (msg ));
130- map .put ("localTime " , msg . localTime () );
138+ map .put ("localId " , localId );
131139 messageChannel .invokeMethod (EMSDKMethod .onMessageSuccess , map );
132140 });
133141 }
@@ -137,7 +145,7 @@ public void onProgress(int progress, String status) {
137145 post (() -> {
138146 Map <String , Object > map = new HashMap <>();
139147 map .put ("progress" , progress );
140- map .put ("localTime " , msg . localTime () );
148+ map .put ("localId " , localId );
141149 messageChannel .invokeMethod (EMSDKMethod .onMessageProgressUpdate , map );
142150 });
143151 }
@@ -150,7 +158,7 @@ public void onError(int code, String desc) {
150158 post (() -> {
151159 Map <String , Object > map = new HashMap <>();
152160 map .put ("message" , EMMessageHelper .toJson (msg ));
153- map .put ("localTime " , msg . localTime () );
161+ map .put ("localId " , localId );
154162 map .put ("error" , data );
155163 messageChannel .invokeMethod (EMSDKMethod .onMessageError , map );
156164 });
@@ -170,13 +178,14 @@ private void resendMessage(JSONObject param, String channelName, Result result)
170178 }
171179 msg .setStatus (EMMessage .Status .CREATE );
172180 EMMessage finalMsg = msg ;
181+ final String localId = finalMsg .getMsgId ();
173182 finalMsg .setMessageStatusCallback (new EMWrapperCallBack (result , channelName , null ) {
174183 @ Override
175184 public void onSuccess () {
176185 post (() -> {
177186 Map <String , Object > map = new HashMap <>();
178187 map .put ("message" , EMMessageHelper .toJson (finalMsg ));
179- map .put ("localTime " , finalMsg . localTime () );
188+ map .put ("localId " , localId );
180189 messageChannel .invokeMethod (EMSDKMethod .onMessageSuccess , map );
181190 });
182191 }
@@ -186,7 +195,7 @@ public void onProgress(int progress, String status) {
186195 post (() -> {
187196 Map <String , Object > map = new HashMap <>();
188197 map .put ("progress" , progress );
189- map .put ("localTime " , finalMsg . localTime () );
198+ map .put ("localId " , localId );
190199 messageChannel .invokeMethod (EMSDKMethod .onMessageProgressUpdate , map );
191200 });
192201 }
@@ -200,7 +209,7 @@ public void onError(int code, String desc) {
200209 post (() -> {
201210 Map <String , Object > map = new HashMap <>();
202211 map .put ("message" , EMMessageHelper .toJson (finalMsg ));
203- map .put ("localTime " , finalMsg . localTime () );
212+ map .put ("localId " , localId );
204213 map .put ("error" , data );
205214 messageChannel .invokeMethod (EMSDKMethod .onMessageError , map );
206215 });
@@ -327,6 +336,82 @@ private void getUnreadMessageCount(JSONObject param, String channelName, Result
327336 });
328337 }
329338
339+ private void getConversationsFromServerWithPage (JSONObject param , String channelName , Result result ) throws JSONException {
340+ int pageNum = param .getInt ("pageNum" );
341+ int pageSize = param .getInt ("pageSize" );
342+ EMValueWrapperCallBack <Map <String , EMConversation >> callBack = new EMValueWrapperCallBack <Map <String , EMConversation >>(result ,
343+ channelName ) {
344+ @ Override
345+ public void onSuccess (Map <String , EMConversation > object ) {
346+ ArrayList <EMConversation >list = new ArrayList <>(object .values ());
347+ asyncRunnable (() -> {
348+ boolean retry = false ;
349+ List <Map > conversations = new ArrayList <>();
350+ do {
351+ try {
352+ retry = false ;
353+ Collections .sort (list , new Comparator <EMConversation >() {
354+ @ Override
355+ public int compare (EMConversation o1 , EMConversation o2 ) {
356+ if (o1 == null && o2 == null ) {
357+ return 0 ;
358+ }
359+ if (o1 .getLastMessage () == null ) {
360+ return 1 ;
361+ }
362+
363+ if (o2 .getLastMessage () == null ) {
364+ return -1 ;
365+ }
366+
367+ if (o1 .getLastMessage ().getMsgTime () == o2 .getLastMessage ().getMsgTime ()) {
368+ return 0 ;
369+ }
370+
371+ return o2 .getLastMessage ().getMsgTime () - o1 .getLastMessage ().getMsgTime () > 0 ? 1 : -1 ;
372+ }
373+ });
374+ for (EMConversation conversation : list ) {
375+ conversations .add (EMConversationHelper .toJson (conversation ));
376+ }
377+
378+ }catch (IllegalArgumentException e ) {
379+ retry = true ;
380+ }
381+ }while (retry );
382+ updateObject (conversations );
383+ });
384+ }
385+ };
386+ EMClient .getInstance ().chatManager ().asyncFetchConversationsFromServer (pageNum , pageSize , callBack );
387+ }
388+
389+ private void removeMessagesFromServerWithMsgIds (JSONObject params , String channelName , Result result ) throws JSONException {
390+ String conversationId = params .getString ("convId" );
391+ EMConversation .EMConversationType type = EMConversationHelper .typeFromInt (params .getInt ("type" ));
392+ EMConversation conversation = EMClient .getInstance ().chatManager ().getConversation (conversationId , type , true );
393+
394+ JSONArray jsonArray = params .getJSONArray ("msgIds" );
395+
396+ ArrayList <String > msgIds = new ArrayList <>();
397+ for (int i = 0 ; i < jsonArray .length (); i ++) {
398+ msgIds .add ((String ) jsonArray .get (i ));
399+ }
400+
401+ conversation .removeMessagesFromServer (msgIds , new EMWrapperCallBack (result , channelName , null ));
402+ }
403+
404+ private void removeMessagesFromServerWithTs (JSONObject params , String channelName , Result result ) throws JSONException {
405+ String conversationId = params .getString ("convId" );
406+ EMConversation .EMConversationType type = EMConversationHelper .typeFromInt (params .getInt ("type" ));
407+ EMConversation conversation = EMClient .getInstance ().chatManager ().getConversation (conversationId , type , true );
408+ long timestamp = 0 ;
409+ if (params .has ("timestamp" )) {
410+ timestamp = params .getLong ("timestamp" );
411+ }
412+ conversation .removeMessagesFromServer (timestamp , new EMWrapperCallBack (result , channelName , null ));
413+ }
414+
330415 private void updateChatMessage (JSONObject param , String channelName , Result result ) throws JSONException {
331416 EMMessage msg = EMMessageHelper .fromJson (param .getJSONObject ("message" ));
332417
0 commit comments