Skip to content

Commit aa67c6b

Browse files
committed
[new] new entity discussions added
1 parent acc927f commit aa67c6b

File tree

4 files changed

+192
-4
lines changed

4 files changed

+192
-4
lines changed

index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var connection = require('./lib/connection'),
88
sso = require('./lib/sso'),
99
analytics = require('./lib/analytics'),
1010
provider = require('./lib/provider'),
11+
discussion = require('./lib/discussion'),
1112
Api = require('./lib/core/api'),
1213
Config = require('./lib/config');
1314

@@ -25,7 +26,8 @@ function Oneall(cfg) {
2526
'user',
2627
'sso',
2728
'analytics',
28-
'provider'
29+
'provider',
30+
'discussion'
2931
];
3032

3133
if (cfg === undefined) {
@@ -64,5 +66,7 @@ Oneall.prototype.identity = identity;
6466
Oneall.prototype.sso = sso;
6567
Oneall.prototype.analytics = analytics;
6668
Oneall.prototype.provider = provider;
69+
Oneall.prototype.discussion = discussion;
70+
6771

6872
module.exports = Oneall;

lib/core/api.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ Api.prototype.retrieveData = function (error, jsonBody, tag, callback) {
156156
}
157157
try {
158158
if (jsonBody.response.result) {
159-
if (tag !== null && jsonBody.response.result.data[tag] !== undefined) {
159+
if (tag !== null && tag !== '*' && jsonBody.response.result.data[tag] !== undefined) {
160160
response.data = jsonBody.response.result.data[tag];
161+
} if (tag === '*') {
162+
response.data = jsonBody.response.result.data;
161163
}
162164
if (jsonBody.response.result.status) {
163165
response.resStatus = jsonBody.response.result.status;

lib/discussion.js

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
'use strict';
2+
3+
/**
4+
* Send a GET request to this resource to retrieve the complete list of discussions.
5+
* @param {int} page the page number
6+
* @param {Function} callback the callback function
7+
*/
8+
function getAll(page, callback) {
9+
this.api.get('discussions', null, {page: page}, function (error, result) {
10+
this.api.retrieveData(error, result, 'discussions', callback);
11+
}.bind(this));
12+
}
13+
14+
/**
15+
* Send a GET request to this resource to retrieve a discussion.
16+
* @param {string} discussionToken the discussion token that identifies the discussion
17+
* @param {string} discussionForeignId the discussion foreign id that identifies the discussion
18+
* @param {Function} callback the callback function
19+
*/
20+
function get(discussionToken, discussionForeignId, callback) {
21+
var params = null;
22+
if (typeof discussionForeignId === 'function') {
23+
callback = discussionForeignId;
24+
discussionForeignId = null;
25+
}
26+
if (discussionForeignId) {
27+
params = {
28+
foreign_reference: discussionForeignId
29+
};
30+
}
31+
this.api.get(_getDiscussionUrl(discussionToken), discussionToken, params, function (error, result) {
32+
this.api.retrieveData(error, result, 'discussion', callback);
33+
}.bind(this));
34+
}
35+
36+
/**
37+
* Send a PUT request to create a discussion
38+
* @param {[string]} title A title for the discussion
39+
* @param {[string]} url A url for the discussion
40+
* @param {[string]} uid A uniq identifier for the discussion
41+
* @param {Function} callback the callback function
42+
*/
43+
function create (title, url, discussionForeignId, callback) {
44+
var body = {
45+
discussion: {
46+
title: title,
47+
url: url,
48+
foreign_reference: discussionForeignId
49+
}
50+
};
51+
this.api.put('discussions', null, body, function (error, result) {
52+
this.api.retrieveData(error, result, 'discussion', callback);
53+
}.bind(this));
54+
}
55+
56+
/**
57+
* Send a GET request to this resource to retrieve a discussion comment list.
58+
* @param {string} discussionToken the discussion token that identifies the discussion
59+
* @param {string} discussionForeignId the discussion foreign id that identifies the discussion
60+
* @param {Function} callback the callback function
61+
*/
62+
function getComments(discussionToken, discussionForeignId, callback) {
63+
var params = null;
64+
if (typeof discussionForeignId === 'function') {
65+
callback = discussionForeignId;
66+
discussionForeignId = null;
67+
}
68+
if (discussionForeignId) {
69+
params = {
70+
foreign_reference: discussionForeignId
71+
};
72+
}
73+
this.api.get(_getDiscussionUrl(discussionToken, 'comments'), discussionToken, params, function (error, result) {
74+
this.api.retrieveData(error, result, 'comments', callback);
75+
}.bind(this));
76+
}
77+
78+
/**
79+
* Send a POST request to create a comment for a discussion
80+
* @param {string} discussionToken the discussion token that identifies the discussion
81+
* @param {Object} author the comment author data
82+
* @param {Object} author.name the author name
83+
* @param {Object} author.email the author email
84+
* @param {Object} author.author_token the author author token created on oneall
85+
* @param {Object} author.identity_token the author identity token
86+
* @param {Object} author.foreign_reference the author foreign id
87+
* @param {Object} author.description the author description
88+
* @param {Object} author.foreign_reference the author foreign id
89+
* @param {Object} author.picture_url the author picture url
90+
* @param {string} parentCommentToken the parent comment to nest the comment
91+
* @param {string} discussionForeignId An external reference to the discussion you may use it instead of discussionToken
92+
* @param {[string]} uid A uniq identifier for the discussion
93+
* @param {Function} callback The callback function
94+
*/
95+
function createComment (discussionToken, author, text, parentCommentToken, discussionForeignId, authorForeignId, callback) {
96+
var body = { comment: {}};
97+
98+
if (!author) {
99+
author = {};
100+
}
101+
102+
if (!discussionToken && !discussionForeignId) {
103+
throw new Error('You must set either a discussionToken or a discussionForeignId');
104+
}
105+
106+
if (author.foreign_reference && !authorForeignId) {
107+
authorForeignId = author.foreign_reference;
108+
}
109+
110+
if ((!author.name || !author.email) && (!author.author_token) && (!author.identity_token) && !authorForeignId) {
111+
throw new Error('You must identify the user making the comment either by (name + email / author_token / identity_token / foreign_reference | authorForeignId)');
112+
}
113+
114+
115+
if (authorForeignId) {
116+
author.foreign_reference = authorForeignId;
117+
}
118+
body.comment.text = text;
119+
120+
if (parentCommentToken) {
121+
body.comment.parent_comment_token = parentCommentToken;
122+
}
123+
body.comment.author = author;
124+
console.log(author);
125+
this.api.post(_getDiscussionUrl(discussionToken, 'comments'), discussionToken, body, function (error, result) {
126+
this.api.retrieveData(error, result, 'comment', callback);
127+
}.bind(this));
128+
}
129+
130+
131+
/**
132+
* Send a DELETE request to this resource in order to delete a discussion.
133+
* @param {string} discussionToken the discussion token that identifies the discussion
134+
* @param {string} discussionForeignId the discussion foreign id that identifies the discussion
135+
* @param {Function} callback the callback function
136+
*/
137+
function del (discussionToken, discussionForeignId, callback) {
138+
var params = {
139+
confirm_deletion: 'true'
140+
};
141+
142+
if (discussionForeignId) {
143+
params.foreign_reference = discussionForeignId;
144+
}
145+
146+
this.api.del(_getDiscussionUrl(discussionToken, 'comments'), discussionToken, params, function (error, result) {
147+
this.api.retrieveData(error, result, 'discussion', callback);
148+
}.bind(this));
149+
}
150+
151+
// --------- PRIVATE
152+
153+
/**
154+
* Based on having a discussionToken this method will return a different url
155+
* @param {string} discussionToken The token identifying the discussions
156+
* @param {[string]} subEntity An optional sub entity to be called
157+
* @return {string} The url to be called on oneall api
158+
*/
159+
function _getDiscussionUrl(discussionToken, subEntity) {
160+
var url;
161+
if (discussionToken) {
162+
url = 'discussions/@token';
163+
} else {
164+
url = 'discussions';
165+
}
166+
if (subEntity) {
167+
url += '/' + subEntity;
168+
}
169+
return url;
170+
}
171+
172+
173+
module.exports = {
174+
getAll: getAll,
175+
get: get,
176+
create: create,
177+
getComments: getComments,
178+
createComment: createComment,
179+
del: del
180+
};
181+
182+

lib/identity.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function get(identityToken, callback) {
2828
* @param {Function} callback the callback function
2929
*/
3030
function getAll(page, callback) {
31-
this.api.get('connections', null, {page: page}, function (error, result) {
32-
this.api.retrieveData(error, result, 'connections', callback);
31+
this.api.get('identities', null, {page: page}, function (error, result) {
32+
this.api.retrieveData(error, result, 'identities', callback);
3333
}.bind(this));
3434

3535
}

0 commit comments

Comments
 (0)