Skip to content

Commit 3687237

Browse files
committed
feat(Status): add #resetUnreadCount
1 parent da3697c commit 3687237

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/status.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ module.exports = function(AV) {
223223
/**
224224
* Count unread statuses in someone's inbox.
225225
* @since 0.3.0
226-
* @param {Object} source The status source.
227-
* @param {String} inboxType The inbox type,'default' by default.
226+
* @param {Object} owner The status owner.
227+
* @param {String} inboxType The inbox type, 'default' by default.
228228
* @param {AuthOptions} options
229229
* @return {Promise} A promise that is fulfilled when the count
230230
* completes.
@@ -234,17 +234,43 @@ module.exports = function(AV) {
234234
* console.log(response.total); //total statuses number.
235235
* });
236236
*/
237-
AV.Status.countUnreadStatuses = function(owner){
238-
var options = (!_.isString(arguments[1]) ? arguments[1] : arguments[2]) || {};
239-
var inboxType = !_.isString(arguments[1]) ? 'default' : arguments[1];
240-
if(!options.sessionToken && owner == null && !AV.User.current()){
237+
AV.Status.countUnreadStatuses = function(owner, inboxType = 'default', options = {}){
238+
if (!_.isString(inboxType)) options = inboxType;
239+
if(!options.sessionToken && owner == null && !AV.User.current()) {
240+
throw new Error('Please signin an user or pass the owner objectId.');
241+
}
242+
return getUser(options).then(owner => {
243+
var params = {};
244+
params.inboxType = AV._encode(inboxType);
245+
params.owner = AV._encode(owner);
246+
return AVRequest('subscribe/statuses/count', null, null, 'GET', params, options);
247+
});
248+
};
249+
250+
/**
251+
* reset unread statuses count in someone's inbox.
252+
* @since 2.1.0
253+
* @param {Object} owner The status owner.
254+
* @param {String} inboxType The inbox type, 'default' by default.
255+
* @param {AuthOptions} options
256+
* @return {Promise} A promise that is fulfilled when the reset
257+
* completes.
258+
* @example
259+
* AV.Status.resetUnreadCount(AV.User.current()).then(function(response){
260+
* console.log(response.unread); //unread statuses number.
261+
* console.log(response.total); //total statuses number.
262+
* });
263+
*/
264+
AV.Status.resetUnreadCount = function(owner, inboxType = 'default', options = {}){
265+
if (!_.isString(inboxType)) options = inboxType;
266+
if(!options.sessionToken && owner == null && !AV.User.current()) {
241267
throw new Error('Please signin an user or pass the owner objectId.');
242268
}
243269
return getUser(options).then(owner => {
244270
var params = {};
245271
params.inboxType = AV._encode(inboxType);
246272
params.owner = AV._encode(owner);
247-
return AVRequest('subscribe/statuses/count', null, null, 'GET', params, options.sessionToken);
273+
return AVRequest('subscribe/statuses/resetUnreadCount', null, null, 'POST', params, options);
248274
});
249275
};
250276

test/status.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe("AV.Status",function(){
3030
});
3131

3232
describe("Query statuses.", function(){
33+
const user = AV.Object.createWithoutData('_User', '5627906060b22ef9c464cc99');
3334
it("should return unread count.", function(){
3435
return AV.Status.countUnreadStatuses().then(function(response){
3536
expect(response.total).to.be.a('number');
@@ -38,19 +39,26 @@ describe("AV.Status",function(){
3839
});
3940

4041
it("should return unread count that is greater than zero.", function(){
41-
return AV.Status.countUnreadStatuses(AV.Object.createWithoutData('_User', '5627906060b22ef9c464cc99'),'private').then(function(response){
42+
return AV.Status.countUnreadStatuses(user,'private').then(function(response){
4243
expect(response.total).to.be.a('number');
4344
expect(response.unread).to.be.a('number');
4445
});
4546
});
4647
it("should return private statuses.", function(){
47-
var query = AV.Status.inboxQuery(AV.Object.createWithoutData('_User', '5627906060b22ef9c464cc99'), 'private');
48+
var query = AV.Status.inboxQuery(user);
4849
return query.find();
4950
});
5051
it("should return published statuses.", function(){
5152
var query = AV.Status.statusQuery(AV.User.current());
5253
return query.find();
5354
});
55+
it("should reset unread statuses.", function(){
56+
return AV.Status.resetUnreadCount(user)
57+
.then(() => AV.Status.countUnreadStatuses(user))
58+
.then(function(response){
59+
expect(response.unread).to.be.eql(0);
60+
});
61+
});
5462
});
5563

5664
describe("Status guide test.", function(){

0 commit comments

Comments
 (0)