Skip to content

Commit

Permalink
Merge pull request #27 from Automattic/add/api-methods
Browse files Browse the repository at this point in the history
Add/api methods
  • Loading branch information
TooTallNate authored and jsnajdr committed Jan 27, 2020
1 parent c0ae343 commit 1828605
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/wpcom.js/docs/post.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ Delete a Post. Note: If the post object is of type post or page and the trash
is enabled, this request will send the post to the trash. A second request will
permanently delete the post.

### Post#likes(fn)
### Post#likesList(fn)
6 changes: 3 additions & 3 deletions packages/wpcom.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ WPCOM.prototype.site = function(id){
/**
* List Freshly Pressed Posts
*
* @param {Object} params (optional)
* @param {Object} [query]
* @param {Function} fn callback function
* @api public
*/

WPCOM.prototype.freshlyPressed = function(params, fn){
this.sendRequest('/freshly-pressed', null, params, fn);
WPCOM.prototype.freshlyPressed = function(query, fn){
this.sendRequest('/freshly-pressed', query, null, fn);
};

/**
Expand Down
76 changes: 76 additions & 0 deletions packages/wpcom.js/lib/like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

/**
* Module dependencies.
*/

var debug = require('debug')('wpcom:like');

/**
* Like methods
*
* @param {String} pid post id
* @param {String} sid site id
* @param {WPCOM} wpcom
* @api public
*/

function Like(pid, sid, wpcom){
if (!sid) {
throw new Error('`side id` is not correctly defined');
}

if (!pid) {
throw new Error('`post id` is not correctly defined');
}

if (!(this instanceof Like)) return new Like(pid, sid, wpcom);

this.wpcom = wpcom;
this._pid = pid;
this._sid = sid;
}

/**
* Get your Like status for a Post
*
* @param {Object} [query]
* @param {Function} fn
* @api public
*/

Like.prototype.mine = function(query, fn){
var path = '/sites/' + this._sid + '/posts/' + this._pid + '/likes/mine';
this.wpcom.sendRequest(path, query, null, fn);
};

/**
* Like a post
*
* @param {Object} [query]
* @param {Function} fn
* @api public
*/

Like.prototype.add = function(query, fn){
var path = '/sites/' + this._sid + '/posts/' + this._pid + '/likes/new';
this.wpcom.sendRequest({ path: path, method: 'post' }, query, null, fn);
};

/**
* Remove your Like from a Post
*
* @param {Function} fn
* @api public
*/

Like.prototype['delete'] =
Like.prototype.del = function(fn){
var path = '/sites/' + this._sid + '/posts/' + this._pid + '/likes/mine/delete';
this.wpcom.sendRequest({ path: path, method: 'post' }, null, null, fn);
};

/**
* Expose `Like` module
*/

module.exports = Like;
16 changes: 14 additions & 2 deletions packages/wpcom.js/lib/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

/**
* Module dependencies.
*/

var Like = require('./like');
var debug = require('debug')('wpcom:post');

/**
Expand Down Expand Up @@ -120,18 +122,28 @@ Post.prototype.del = function(fn){
};

/**
* Get post likes
* Get post likes list
*
* @param {Object} [query]
* @param {Function} fn
* @api public
*/

Post.prototype.likes = function(query, fn){
Post.prototype.likesList = function(query, fn){
var path = '/sites/' + this._sid + '/posts/' + this._id + '/likes';
this.wpcom.sendRequest(path, query, null, fn);
};

/**
* Create a `Like` instance
*
* @api public
*/

Post.prototype.like = function(){
return Like( this._id, this._sid, this.wpcom);
};

/**
* Expose `Post` module
*/
Expand Down
85 changes: 73 additions & 12 deletions packages/wpcom.js/test/test.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,39 +160,100 @@ describe('WPCOM#Site#Post', function(){

});

describe('delete()', function(){
describe('likesList()', function(){

it('should delete the new added post', function(done){
it('should get post likes list', function(done){
var site = util.private_site();
var post = site.post(new_post.ID);

post.delete(function(err, data){
post.likesList(function(err, data){
if (err) throw err;

assert.ok(data);
assert.equal(new_post.ID, data.ID);

assert.equal('number', typeof data.found);
assert.equal('boolean', typeof data.i_like);
assert.equal('object', typeof data.likes);
assert.ok(data.likes instanceof Array);

done();
});

});

});

describe('likes()', function(){
describe('like.add()', function(){

it('should get post likes', function(done){
it('should add a post like', function(done){
var site = util.private_site();
var post = site.post(new_post.ID);
var like = site.post(new_post.ID).like();

post.likes(function(err, data){
like.add(function(err, data){
if (err) throw err;

assert.ok(data);
assert.ok(data.success);
assert.ok(data.i_like);
assert.equal(1, data.like_count);

assert.equal('number', typeof data.found);
assert.equal('boolean', typeof data.i_like);
assert.equal('object', typeof data.likes);
assert.ok(data.likes instanceof Array);
done();
});

});

});

describe('like.mine()', function(){

it('should get the post like status of mine', function(done){
util.private_site()
.post(new_post.ID)
.like()
.mine(function(err, data){
if (err) throw err;

assert.ok(data);
assert.equal(1, data.like_count);
assert.ok(data.i_like);

done();
});
});

});

describe('like.delete()', function(){

it('should remove your like from the post', function(done){
util.private_site()
.post(new_post.ID)
.like()
.del(function(err, data){
if (err) throw err;

assert.ok(data);
assert.ok(data.success);
assert.equal(0, data.like_count);
assert.ok(!(data.i_like));

done();
});
});

});

describe('delete()', function(){

it('should delete the new added post', function(done){
var site = util.private_site();
var post = site.post(new_post.ID);

post.delete(function(err, data){
if (err) throw err;

assert.ok(data);
assert.equal(new_post.ID, data.ID);

done();
});
Expand Down

0 comments on commit 1828605

Please sign in to comment.