Skip to content

Commit 31cf60f

Browse files
aiskleeyeh
authored andcommitted
feat: add AV.Conversation class
1 parent 083cd17 commit 31cf60f

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

src/conversation.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
3+
const AV = require('./av');
4+
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+
*/
14+
module.exports = AV.Object.extend('_Conversation', {
15+
constructor: function(name, isSystem, isTransient) {
16+
AV.Object.prototype.constructor.call(this, null, null);
17+
this.set('name', name);
18+
this.set('sys', isSystem ? true : false);
19+
this.set('tr', isTransient ? true : false);
20+
},
21+
/**
22+
* Get current conversation's creator.
23+
*
24+
* @return {String}
25+
*/
26+
getCreator: function() {
27+
return this.get('c');
28+
},
29+
30+
/**
31+
* Get the last message's time.
32+
*
33+
* @return {Date}
34+
*/
35+
getLastMessageAt: function() {
36+
return this.get('lm');
37+
},
38+
39+
/**
40+
* Get this conversation's members
41+
*
42+
* @return {Array}
43+
*/
44+
getMembers: function() {
45+
return this.get('m');
46+
},
47+
48+
/**
49+
* Add a member to this conversation
50+
*
51+
* @param {String} member
52+
*/
53+
addMember: function(member) {
54+
this.add('m', member);
55+
},
56+
57+
/**
58+
* Get this conversation's members who set this conversation as muted.
59+
*
60+
* @return {Boolean}
61+
*/
62+
getMutedMembers: function() {
63+
return this.get('mu');
64+
},
65+
66+
/**
67+
* Get this conversation's name field.
68+
*
69+
* @return String
70+
*/
71+
getName: function() {
72+
return this.get('name');
73+
},
74+
75+
/**
76+
* Returns true if this conversation is transient conversation.
77+
*
78+
* @return {Boolean}
79+
*/
80+
isTransient: function() {
81+
return this.get('tr');
82+
},
83+
84+
/**
85+
* Returns true if this conversation is system conversation.
86+
*
87+
* @return {Boolean}
88+
*/
89+
isSystem: function() {
90+
return this.get('sys');
91+
},
92+
93+
send: function() {
94+
95+
},
96+
});

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ require('./status')(AV);
3232
require('./search')(AV);
3333
require('./insight')(AV);
3434

35+
AV.Conversation = require('./conversation');
36+
3537
module.exports = AV;
3638

3739
/**

test/conversation.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
describe('Conversation', () => {
4+
describe('.constructor', () => {
5+
const conv = new AV.Conversation('test', true, false);
6+
expect(conv.isTransient()).to.be(false);
7+
expect(conv.isSystem()).to.be(true);
8+
expect(conv.getName()).to.be('test');
9+
});
10+
describe('#save', () => {
11+
it('should create a realtime conversation', () => {
12+
const conv = new AV.Conversation('test');
13+
conv.addMember('test1');
14+
conv.addMember('test2');
15+
return conv.save();
16+
});
17+
});
18+
});

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ require('./status.js');
1313
require('./sms.js');
1414
require('./search.js');
1515
require('./hooks.js');
16+
require('./conversation.js');

0 commit comments

Comments
 (0)