Skip to content

Commit ea994cd

Browse files
KenChoiKenChoi
authored andcommitted
add chatroom api
1 parent 06650ba commit ea994cd

File tree

7 files changed

+82
-20
lines changed

7 files changed

+82
-20
lines changed

android/src/io/jchat/android/Constant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Constant {
3131
public static final String TYPE_USER = "user";
3232
public static final String TYPE_GROUP = "group";
3333
public static final String TYPE_SINGLE = "single";
34+
public static final String TYPE_CHAT_ROOM = "chatroom";
3435
public static final String ID = "id";
3536
public static final String SERVER_ID = "serverMessageId";
3637
public static final String MESSAGE_ID = "messageId";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public void createSendMessage(ReadableMap map, Callback callback) {
341341
public void sendMessage(ReadableMap map, final Callback success, final Callback fail) {
342342
try {
343343
Conversation conversation = mJMessageUtils.getConversation(map);
344-
final Message message = conversation.getMessage(map.getInt(Constant.ID));
344+
final Message message = conversation.getMessage(Integer.parseInt(map.getString(Constant.ID)));
345345
if (map.hasKey(Constant.SENDING_OPTIONS)) {
346346
MessageSendingOptions options = new MessageSendingOptions();
347347
ReadableMap optionMap = map.getMap(Constant.SENDING_OPTIONS);

android/src/io/jchat/android/utils/ResultUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ public static WritableMap toJSObject(Conversation conversation) {
234234
} else if (conversation.getType() == ConversationType.group) {
235235
GroupInfo targetInfo = (GroupInfo) conversation.getTargetInfo();
236236
map.putMap(Constant.TARGET, toJSObject(targetInfo));
237+
} else {
238+
ChatRoomInfo chatRoomInfo = (ChatRoomInfo) conversation.getTargetInfo();
239+
map.putMap(Constant.TARGET, toJSObject(chatRoomInfo, null));
237240
}
238241

239242
} catch (Exception e) {
@@ -247,14 +250,15 @@ public static WritableMap toJSObject(ChatRoomInfo chatRoomInfo, final Callback f
247250
final WritableMap map = Arguments.createMap();
248251
try {
249252
map.putString(Constant.ROOM_ID, String.valueOf(chatRoomInfo.getRoomID()));
253+
map.putString(Constant.TYPE, Constant.TYPE_CHAT_ROOM);
250254
map.putString(Constant.ROOM_NAME, chatRoomInfo.getName());
251255
map.putString(Constant.APP_KEY, chatRoomInfo.getAppkey());
252256
chatRoomInfo.getOwnerInfo(new GetUserInfoCallback() {
253257
@Override
254258
public void gotResult(int status, String desc, UserInfo userInfo) {
255259
if (status == 0) {
256260
map.putMap(Constant.OWNER, toJSObject(userInfo));
257-
} else {
261+
} else if (fail != null) {
258262
WritableMap result = Arguments.createMap();
259263
result.putInt(Constant.CODE, status);
260264
result.putString(Constant.DESCRIPTION, desc);

document/Models.md

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

33
- [UserInfo](#userinfo)
44
- [GroupInfo](#groupinfo)
5+
- [ChatRoomInfo](#ChatRoomInfo)
56
- [Conversation](#conversation)
67
- [Message](#message)
78
- [TextMessage](#textmessage)
@@ -47,6 +48,22 @@ isNoDisturb: boolean, // 是否免打扰
4748
isBlocked: boolean // 是否屏蔽群消息
4849
```
4950

51+
## ChatRoomInfo
52+
53+
```js
54+
type: 'chatroom',
55+
roomId: string, // 聊天室 id
56+
roomName: string, // 聊天室名字
57+
appKey: string, // 聊天室所属 AppKey
58+
owner: UserInfo, // 聊天室拥有者
59+
maxMemberCount: number, // 聊天室最大成员数量
60+
description: string, // 聊天室描述
61+
totalMemberCount: number, // 聊天室当前成员数量
62+
createTime: number // 聊天室创建时间,单位 秒
63+
```
64+
65+
66+
5067
## Conversation
5168

5269
- 单聊:如果用户有昵称,`title` 为昵称,否则为 username。
@@ -56,8 +73,8 @@ isBlocked: boolean // 是否屏蔽群消息
5673
title: string, // 会话标题
5774
latestMessage: Message, // 最近的一条消息对象
5875
unreadCount: number, // 未读消息数
59-
conversationType: 'single' / 'group',
60-
target: UserInfo / GroupInfo // 聊天对象信息
76+
conversationType: 'single' / 'group', / 'chatroom'
77+
target: UserInfo / GroupInfo / ChatRoomInfo // 聊天对象信息
6178
```
6279

6380
## Message

example/app/routes/Chat/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ export default class Chat extends Component {
175175
if (this.conversation.conversationType === 'single') {
176176
parames.type = 'single'
177177
parames.username = this.conversation.key
178-
} else {
178+
} else if (this.conversation.conversationType === 'group') {
179179
parames.type = 'group'
180180
parames.groupId = this.conversation.key
181+
} else {
182+
parames.type = 'chatroom'
183+
parames.roomId = this.conversation.key
181184
}
182185
this.messageListDidLoadCallback = () => {
183186

@@ -288,6 +291,7 @@ export default class Chat extends Component {
288291

289292
onMsgClick = (message) => {
290293
console.log(message)
294+
alert(JSON.stringify(message))
291295
}
292296

293297
onStatusViewClick = (message) => {

example/app/routes/Home/ConversationList/index.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import React from 'react';
4-
import ReactNative from 'react-native';
4+
import ReactNative, { ScrollView } from 'react-native';
55
import JMessage from 'jmessage-react-plugin';
66

77
import FormButton from '../../../views/FormButton'
@@ -48,14 +48,17 @@ const styles = StyleSheet.create({
4848
backgroundColor: 'rgba(0, 0, 0, 0.5)',
4949
},
5050
modalContent: {
51-
width: 200,
52-
height: 150,
53-
justifyContent: 'center',
51+
width: 300,
52+
height: 300,
53+
justifyContent: 'space-between',
5454
alignItems: 'center',
5555
backgroundColor: '#ffffff',
5656
},
5757
modalButton: {
58-
58+
margin: 10,
59+
},
60+
inputStyle: {
61+
width: 200
5962
}
6063
});
6164

@@ -84,11 +87,11 @@ export default class ConversationList extends React.Component {
8487
tabBarIcon: ({
8588
tintColor
8689
}) => (
87-
<Image
90+
<Image
8891
source={require('../../../resource/chat-icon.png')}
8992
style={[styles.icon, { tintColor: tintColor }]}
9093
/>
91-
),
94+
),
9295
}
9396
};
9497

@@ -231,16 +234,32 @@ export default class ConversationList extends React.Component {
231234
})
232235
}
233236

237+
enterChatRoom(params) {
238+
JMessage.enterChatRoom(params.roomId, (conversation) => {
239+
var chatRoom = {
240+
conversationType: 'chatroom',
241+
key: conversation.roomId,
242+
owner: conversation.owner,
243+
totalMemberCount: conversation.totalMemberCount
244+
}
245+
this.props.navigation.navigate('Chat', {
246+
conversation: chatRoom
247+
})
248+
}, (error) => {
249+
console.alert("error, code: " + error.code + ", description: " + error.description)
250+
})
251+
}
252+
234253
render() {
235254
this.listView = <FlatList
236-
data = {
237-
this.state.data
238-
}
239-
renderItem = {
255+
data={
256+
this.state.data
257+
}
258+
renderItem={
240259
({
241260
item
242261
}) => (
243-
<View>
262+
<View>
244263
<TouchableHighlight
245264
style={[styles.conversationContent]}
246265
underlayColor='#dddddd'
@@ -259,10 +278,10 @@ export default class ConversationList extends React.Component {
259278
</View>
260279
</TouchableHighlight>
261280
</View>
262-
)
281+
)
263282
} >
264283

265-
</FlatList>
284+
</FlatList>
266285
return (
267286

268287
<View>
@@ -274,6 +293,7 @@ export default class ConversationList extends React.Component {
274293
<View
275294
style={styles.modalContent}>
276295
<TextInput
296+
style={styles.inputStyle}
277297
placeholder="用户名或群聊名称"
278298
onChangeText={(e) => { this.setState({ modalText: e }) }}>
279299
</TextInput>
@@ -303,6 +323,22 @@ export default class ConversationList extends React.Component {
303323
}}
304324
style={styles.modalButton}
305325
title='创建群聊' />
326+
<Button
327+
onPress={() => {
328+
JMessage.createChatRoomConversation("1000", (conversation) => {
329+
var params = {
330+
type: conversation.type,
331+
roomId: conversation.roomId,
332+
name: conversation.roomName,
333+
appKey: conversation.appKey,
334+
owner: conversation.owner,
335+
}
336+
this.setState({ isShowModal: false })
337+
this.enterChatRoom(params)
338+
})
339+
}}
340+
style={styles.modalButton}
341+
title='创建聊天室' />
306342

307343
<Button
308344
onPress={() => { this.setState({ isShowModal: false }) }}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ export default class JMessage {
724724
* 退出会话,和 enterConversation 方法成对使用
725725
*/
726726
static exitConversation() {
727-
if (Platform.OS === 'Android') {
727+
if (Platform.OS === 'android') {
728728
JMessageModule.exitConversation()
729729
}
730730
}

0 commit comments

Comments
 (0)