Skip to content

Commit f28ef5a

Browse files
aiskleeyeh
authored andcommitted
feat: add AV.Conversation#broadcast function
1 parent 148f4bb commit f28ef5a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/conversation.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

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

@@ -135,4 +136,41 @@ module.exports = AV.Object.extend('_Conversation', /** @lends AV.Conversation.pr
135136
}
136137
return request('rtm', 'messages', null, 'POST', data, authOptions);
137138
},
139+
140+
/**
141+
* Send realtime broadcast message to all clients, with this conversation, using HTTP request.
142+
*
143+
* @param {String} fromClient Sender's client id.
144+
* @param {(String|Object)} message The message which will send to conversation.
145+
* It could be a raw string, or an object with a `toJSON` method, like a
146+
* realtime SDK's Message object. See more: {@link https://leancloud.cn/docs/realtime_guide-js.html#消息}.
147+
* @param {Object} [options.pushData] Push data to this message. See more: {@link https://url.leanapp.cn/pushData 推送消息内容}.
148+
* @param {Object} [options.validTill] The message will valid till this time.
149+
* @param {AuthOptions} [authOptions]
150+
* @return {Promise}
151+
*/
152+
broadcast: function(fromClient, message, options={}, authOptions={}) {
153+
if (typeof message.toJSON === 'function') {
154+
message = message.toJSON();
155+
}
156+
if (typeof message !== 'string') {
157+
message = JSON.stringify(message);
158+
}
159+
const data = {
160+
from_peer: fromClient,
161+
conv_id: this.id,
162+
message: message,
163+
};
164+
if (options.pushData !== undefined) {
165+
data.push = options.pushData;
166+
}
167+
if (options.validTill !== undefined) {
168+
let ts = options.validTill;
169+
if (_.isDate(ts)) {
170+
ts = ts.getTime();
171+
}
172+
options.valid_till = ts;
173+
}
174+
return request('rtm', 'broadcast', null, 'POST', data, authOptions);
175+
}
138176
});

storage.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ declare namespace AV {
599599
isTransient(): boolean;
600600
isSystem(): boolean;
601601
send(fromClient: string, message: string|object, options?: { transient?: boolean, pushData?: object, toClients?: string[] }, authOptions?: AuthOptions): Promise<void>;
602+
broadcast(fromClient: string, message: string|object, options?: { pushData?: object, validTill?: number|Date }, authOptions?: AuthOptions): Promise<void>;
602603
}
603604

604605
export class Error {

test/conversation.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,19 @@ describe('Conversation', () => {
3535
});
3636
});
3737
});
38-
})
38+
});
39+
describe('#broadcast', () => {
40+
it('should broadcast a message to all clients with current conversation', () => {
41+
const conv = new AV.Conversation('test', { isSystem: true });
42+
return conv.save().then(() => {
43+
const options = {
44+
validTill: new Date().getTime() / 1000 + 1000,
45+
};
46+
const authOptions = {
47+
useMasterKey: true,
48+
};
49+
return conv.broadcast('admin', 'test broadcast!', options, authOptions);
50+
});
51+
});
52+
});
3953
});

0 commit comments

Comments
 (0)