Repository navigation
Expand file tree
/
Copy pathconversation.js
More file actions
178 lines (167 loc) · 5.39 KB
/
Copy pathconversation.js
File metadata and controls
178 lines (167 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const _ = require('underscore');
const { _request } = require('./request');
const AV = require('./av');
const serializeMessage = message => {
if (typeof message === 'string') {
return message;
}
if (typeof message.getPayload === 'function') {
return JSON.stringify(message.getPayload());
}
return JSON.stringify(message);
};
/**
* <p>An AV.Conversation is a local representation of a LeanCloud realtime's
* conversation. This class is a subclass of AV.Object, and retains the
* same functionality of an AV.Object, but also extends it with various
* conversation specific methods, like get members, creators of this conversation.
* </p>
*
* @class AV.Conversation
* @param {String} name The name of the Role to create.
* @param {Object} [options]
* @param {Boolean} [options.isSystem] Set this conversation as system conversation.
* @param {Boolean} [options.isTransient] Set this conversation as transient conversation.
*/
module.exports = AV.Object.extend(
'_Conversation',
/** @lends AV.Conversation.prototype */ {
constructor(name, options = {}) {
AV.Object.prototype.constructor.call(this, null, null);
this.set('name', name);
if (options.isSystem !== undefined) {
this.set('sys', options.isSystem ? true : false);
}
if (options.isTransient !== undefined) {
this.set('tr', options.isTransient ? true : false);
}
},
/**
* Get current conversation's creator.
*
* @return {String}
*/
getCreator() {
return this.get('c');
},
/**
* Get the last message's time.
*
* @return {Date}
*/
getLastMessageAt() {
return this.get('lm');
},
/**
* Get this conversation's members
*
* @return {String[]}
*/
getMembers() {
return this.get('m');
},
/**
* Add a member to this conversation
*
* @param {String} member
*/
addMember(member) {
return this.add('m', member);
},
/**
* Get this conversation's members who set this conversation as muted.
*
* @return {String[]}
*/
getMutedMembers() {
return this.get('mu');
},
/**
* Get this conversation's name field.
*
* @return String
*/
getName() {
return this.get('name');
},
/**
* Returns true if this conversation is transient conversation.
*
* @return {Boolean}
*/
isTransient() {
return this.get('tr');
},
/**
* Returns true if this conversation is system conversation.
*
* @return {Boolean}
*/
isSystem() {
return this.get('sys');
},
/**
* Send realtime message to this conversation, using HTTP request.
*
* @param {String} fromClient Sender's client id.
* @param {String|Object} message The message which will send to conversation.
* It could be a raw string, or an object with a `toJSON` method, like a
* realtime SDK's Message object. See more: {@link https://leancloud.cn/docs/realtime_guide-js.html#消息}
* @param {Object} [options]
* @param {Boolean} [options.transient] Whether send this message as transient message or not.
* @param {String[]} [options.toClients] Ids of clients to send to. This option can be used only in system conversation.
* @param {Object} [options.pushData] Push data to this message. See more: {@link https://url.leanapp.cn/pushData 推送消息内容}
* @param {AuthOptions} [authOptions]
* @return {Promise}
*/
send(fromClient, message, options = {}, authOptions = {}) {
const data = {
from_peer: fromClient,
conv_id: this.id,
transient: false,
message: serializeMessage(message),
};
if (options.toClients !== undefined) {
data.to_peers = options.toClients;
}
if (options.transient !== undefined) {
data.transient = options.transient ? true : false;
}
if (options.pushData !== undefined) {
data.push_data = options.pushData;
}
return _request('rtm', 'messages', null, 'POST', data, authOptions);
},
/**
* Send realtime broadcast message to all clients, via this conversation, using HTTP request.
*
* @param {String} fromClient Sender's client id.
* @param {String|Object} message The message which will send to conversation.
* It could be a raw string, or an object with a `toJSON` method, like a
* realtime SDK's Message object. See more: {@link https://leancloud.cn/docs/realtime_guide-js.html#消息}.
* @param {Object} [options]
* @param {Object} [options.pushData] Push data to this message. See more: {@link https://url.leanapp.cn/pushData 推送消息内容}.
* @param {Object} [options.validTill] The message will valid till this time.
* @param {AuthOptions} [authOptions]
* @return {Promise}
*/
broadcast(fromClient, message, options = {}, authOptions = {}) {
const data = {
from_peer: fromClient,
conv_id: this.id,
message: serializeMessage(message),
};
if (options.pushData !== undefined) {
data.push = options.pushData;
}
if (options.validTill !== undefined) {
let ts = options.validTill;
if (_.isDate(ts)) {
ts = ts.getTime();
}
options.valid_till = ts;
}
return _request('rtm', 'broadcast', null, 'POST', data, authOptions);
},
}
);