|
| 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 | + |
0 commit comments