Skip to content

Commit 24e9634

Browse files
author
zhanq
committed
完善设置已读消息回执
1 parent 478a25e commit 24e9634

File tree

1 file changed

+58
-39
lines changed

1 file changed

+58
-39
lines changed

android/src/io/jchat/android/JMessageModule.java

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public class JMessageModule extends ReactContextBaseJavaModule {
100100
private static final int ERR_CODE_CONVERSATION = 2;
101101
private static final int ERR_CODE_MESSAGE = 3;
102102
private static final int ERR_CODE_FILE = 4;
103+
private static final int ERR_CODE_EXCEPTION = -1;
103104

104105
private static final String ERR_MSG_PARAMETER = "Parameters error";
105106
private static final String ERR_MSG_CONVERSATION = "Can't get the conversation";
@@ -2085,49 +2086,67 @@ public void gotResult(int status, String desc) {
20852086
}
20862087

20872088
@ReactMethod
2088-
public void setMsgHaveRead(ReadableMap map, final Callback callback) {
2089-
String userName = map.getString(Constant.USERNAME);
2090-
String appKey = map.getString(Constant.APP_KEY);
2091-
String msgId = map.getString(Constant.ID);
2092-
String serverMsgId = map.getString(Constant.SERVER_ID);
2093-
WritableMap callbackMap = Arguments.createMap();
2094-
if(TextUtils.isEmpty(userName)||
2095-
TextUtils.isEmpty(appKey)||
2096-
TextUtils.isEmpty(msgId)||
2097-
TextUtils.isEmpty(serverMsgId)){
2098-
callbackMap.putInt(Constant.CODE, ERR_CODE_CONVERSATION);
2099-
callbackMap.putString(Constant.DESCRIPTION, ERR_MSG_CONVERSATION);
2100-
callback.invoke(callbackMap);
2101-
return;
2102-
}
2103-
Conversation conversation = JMessageClient.getSingleConversation(userName, appKey);
2104-
if(conversation==null){
2105-
callbackMap.putInt(Constant.CODE, ERR_CODE_CONVERSATION);
2106-
callbackMap.putString(Constant.DESCRIPTION, ERR_MSG_CONVERSATION);
2107-
callback.invoke(callbackMap);
2108-
return;
2109-
}
2110-
//优先使用msgId获取conversation,获取不到再使用serverMsgId获取
2111-
Message message = conversation.getMessage(Integer.parseInt(msgId));
2112-
if(message==null){
2113-
message = conversation.getMessage(Long.parseLong(serverMsgId));
2114-
}
2115-
if (message == null){
2116-
callbackMap.putInt(Constant.CODE, ERR_CODE_MESSAGE);
2117-
callbackMap.putString(Constant.DESCRIPTION, ERR_MSG_MESSAGE);
2118-
callback.invoke(callbackMap);
2119-
return;
2120-
}
2121-
if (!message.haveRead()) {
2089+
public void setMsgHaveRead(ReadableMap map, final Callback successCallback, final Callback failCallback) {
2090+
try {
2091+
String type = map.getString(Constant.TYPE);
2092+
if (TextUtils.isEmpty(type)) {
2093+
mJMessageUtils.handleError(failCallback, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
2094+
}
2095+
Conversation conversation;
2096+
switch (type) {
2097+
case Constant.TYPE_SINGLE:
2098+
String userName = map.getString(Constant.USERNAME);
2099+
String appKey = map.getString(Constant.APP_KEY);
2100+
//如果appKey为空则默认取本应用appKey下对应userName用户的会话。
2101+
if (TextUtils.isEmpty(userName)) {
2102+
mJMessageUtils.handleError(failCallback, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
2103+
}
2104+
conversation = JMessageClient.getSingleConversation(userName, appKey);
2105+
break;
2106+
case Constant.TYPE_GROUP:
2107+
String groupId = map.getString(Constant.GROUP_ID);
2108+
if (TextUtils.isEmpty(groupId)) {
2109+
mJMessageUtils.handleError(failCallback, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
2110+
}
2111+
conversation = JMessageClient.getGroupConversation(Long.parseLong(groupId));
2112+
break;
2113+
case Constant.TYPE_CHAT_ROOM:
2114+
String roomId = map.getString(Constant.ROOM_ID);
2115+
if (TextUtils.isEmpty(roomId)) {
2116+
mJMessageUtils.handleError(failCallback, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
2117+
}
2118+
conversation = JMessageClient.getChatRoomConversation(Long.parseLong(roomId));
2119+
break;
2120+
default:
2121+
conversation = null;
2122+
mJMessageUtils.handleError(failCallback, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
2123+
break;
2124+
}
2125+
if (conversation == null) {
2126+
mJMessageUtils.handleError(failCallback, ERR_CODE_CONVERSATION, ERR_MSG_CONVERSATION);
2127+
return;
2128+
}
2129+
String messageId = map.getString(Constant.ID);
2130+
Message message = conversation.getMessage(Integer.parseInt(messageId));
2131+
if (message == null) {
2132+
mJMessageUtils.handleError(failCallback, ERR_CODE_MESSAGE, ERR_MSG_MESSAGE);
2133+
return;
2134+
}
2135+
if (message.haveRead()) {
2136+
return;
2137+
}
21222138
message.setHaveRead(new BasicCallback() {
21232139
@Override
2124-
public void gotResult(int i, String s) {
2125-
WritableMap map = Arguments.createMap();
2126-
map.putInt(Constant.CODE, i);
2127-
map.putString(Constant.DESCRIPTION, s);
2128-
callback.invoke(map);
2140+
public void gotResult(int code, String message) {
2141+
if (code == 0) {
2142+
mJMessageUtils.handleCallback(code, message, successCallback, failCallback);
2143+
} else {
2144+
mJMessageUtils.handleError(failCallback, code, message);
2145+
}
21292146
}
21302147
});
2148+
}catch (Throwable throwable){
2149+
mJMessageUtils.handleError(failCallback, ERR_CODE_EXCEPTION, throwable.getMessage());
21312150
}
21322151
}
21332152

0 commit comments

Comments
 (0)