Skip to content

Commit 33c3907

Browse files
aiskleeyeh
authored andcommitted
feat: add AV.Conversation#send function
1 parent 31cf60f commit 33c3907

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

src/conversation.js

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
'use strict';
22

3+
const request = require('./request').request;
34
const AV = require('./av');
45

5-
/**
6-
* @class
7-
*
8-
* <p>An AV.Conversation is a local representation of a LeanCloud realtime's
9-
* conversation. Tshi class is a subclass of an AV.Object, and retains the
10-
* same functionality of an AV.Object, but also extends it with various
11-
* conversation specific methods, like get members, creators of this conversation.
12-
* </p>
13-
*/
146
module.exports = AV.Object.extend('_Conversation', {
15-
constructor: function(name, isSystem, isTransient) {
7+
8+
/**
9+
* @class AV.Conversation
10+
* <p>An AV.Conversation is a local representation of a LeanCloud realtime's
11+
* conversation. This class is a subclass of AV.Object, and retains the
12+
* same functionality of an AV.Object, but also extends it with various
13+
* conversation specific methods, like get members, creators of this conversation.
14+
* </p>
15+
*
16+
* @param {String} name The name of the Role to create.
17+
* @param {Boolean} [options.isSystem] Set this conversation as system conversation.
18+
* @param {Boolean} [options.isTransient] Set this conversation as transient conversation.
19+
*/
20+
constructor: function(name, options = {}) {
1621
AV.Object.prototype.constructor.call(this, null, null);
1722
this.set('name', name);
18-
this.set('sys', isSystem ? true : false);
19-
this.set('tr', isTransient ? true : false);
23+
if (options.isSystem !== undefined) {
24+
this.set('sys', options.isSystem ? true: false);
25+
}
26+
if (options.isTransient !== undefined) {
27+
this.set('tr', options.isTransient ? true : false);
28+
}
2029
},
2130
/**
2231
* Get current conversation's creator.
@@ -39,7 +48,7 @@ module.exports = AV.Object.extend('_Conversation', {
3948
/**
4049
* Get this conversation's members
4150
*
42-
* @return {Array}
51+
* @return {String[]}
4352
*/
4453
getMembers: function() {
4554
return this.get('m');
@@ -51,13 +60,13 @@ module.exports = AV.Object.extend('_Conversation', {
5160
* @param {String} member
5261
*/
5362
addMember: function(member) {
54-
this.add('m', member);
63+
return this.add('m', member);
5564
},
5665

5766
/**
5867
* Get this conversation's members who set this conversation as muted.
5968
*
60-
* @return {Boolean}
69+
* @return {String[]}
6170
*/
6271
getMutedMembers: function() {
6372
return this.get('mu');
@@ -90,7 +99,37 @@ module.exports = AV.Object.extend('_Conversation', {
9099
return this.get('sys');
91100
},
92101

93-
send: function() {
94-
102+
/**
103+
* Send realtime message to this conversation, using HTTP request.
104+
*
105+
* @param {String} clientId Sender's client id.
106+
* @param {(String|Object)} message The message which will send to conversation.
107+
* It could be a raw string, or an object with a `toJSON` method, like a
108+
* realtime SDK's Message object. See more: {@link https://leancloud.cn/docs/realtime_guide-js.html#消息}
109+
* @param {Boolean} [options.transient] Whether send this message as transient message or not.
110+
* @param {Object} [options.pushData] Push data to this message. See more: {@link https://url.leanapp.cn/pushData 推送消息内容}
111+
* @param {AuthOptions} [authOptions]
112+
* @return {Promise}
113+
*/
114+
send: function(clientId, message, options, authOptions) {
115+
if (typeof message.toJSON === 'function') {
116+
message = message.toJSON();
117+
}
118+
if (typeof message !== 'string') {
119+
message = JSON.stringify(message);
120+
}
121+
const data = {
122+
from_peer: clientId,
123+
conv_id: this.id,
124+
transient: false,
125+
message: message,
126+
};
127+
if (options.transient !== undefined) {
128+
data.transient = options.transient ? true : false;
129+
}
130+
if (options.pushData !== undefined) {
131+
data.push_data = options.pushData;
132+
}
133+
return request('rtm', 'messages', null, 'POST', data, authOptions);
95134
},
96135
});

test/conversation.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe('Conversation', () => {
44
describe('.constructor', () => {
5-
const conv = new AV.Conversation('test', true, false);
5+
const conv = new AV.Conversation('test', { isSystem: true, isTransient: false });
66
expect(conv.isTransient()).to.be(false);
77
expect(conv.isSystem()).to.be(true);
88
expect(conv.getName()).to.be('test');
@@ -15,4 +15,14 @@ describe('Conversation', () => {
1515
return conv.save();
1616
});
1717
});
18+
describe('#send', () => {
19+
it('should send a realtime message to the conversation', () => {
20+
const conv = new AV.Conversation('test');
21+
conv.addMember('test1');
22+
conv.addMember('test2');
23+
return conv.save().then(() => {
24+
return conv.send('admin', 'test test test!', {}, { useMasterKey: true });
25+
});
26+
});
27+
})
1828
});

0 commit comments

Comments
 (0)