Add all operations for postComment resource

This commit is contained in:
Iván Ovejero 2021-01-29 17:39:39 -03:00
parent c2f43d8122
commit dc38273fa7
3 changed files with 152 additions and 16 deletions

View file

@ -23,7 +23,7 @@ export async function redditApiRequest(
): Promise<any> { // tslint:disable-line:no-any
const resource = this.getNodeParameter('resource', 0) as string;
const authRequired = ['profile', 'post', 'comment'].includes(resource);
const authRequired = ['profile', 'post', 'postComment'].includes(resource);
const options: OptionsWithUri = {
headers: {
@ -40,6 +40,7 @@ export async function redditApiRequest(
}
try {
console.log(options);
return authRequired
? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options)
: await this.helpers.request.call(this, options);

View file

@ -13,18 +13,22 @@ export const postCommentOperations = [
{
name: 'Add',
value: 'add',
description: 'Write a top-level comment in a post.',
},
{
name: 'Get All',
value: 'getAll',
description: 'Retrieve all comments in a post.',
},
{
name: 'Remove',
value: 'remove',
description: 'Remove a comment from a post.',
},
{
name: 'Reply',
value: 'reply',
description: 'Write a reply to a comment in a post.',
},
],
displayOptions: {
@ -42,12 +46,13 @@ export const postCommentFields = [
// postComment: add
// ----------------------------------
{
displayName: 'Target ID',
name: 'targetId',
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
description: 'ID of the comment target.',
placeholder: 't3_15bfi0',
description: 'ID of the post to write the comment to. Found in the post URL:<br><code>/r/[subreddit_name]/comments/[post_id]/[post_title]</code>',
placeholder: 'l0me7x',
displayOptions: {
show: {
resource: [
@ -60,8 +65,8 @@ export const postCommentFields = [
},
},
{
displayName: 'Text',
name: 'text',
displayName: 'Comment Text',
name: 'commentText',
type: 'string',
required: true,
default: '',
@ -77,4 +82,106 @@ export const postCommentFields = [
},
},
},
// ----------------------------------
// postComment: getAll
// ----------------------------------
{
displayName: 'Subreddit',
name: 'subreddit',
type: 'string',
required: true,
default: '',
description: 'The name of subreddit where the post is.',
displayOptions: {
show: {
resource: [
'postComment',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
description: 'ID of the post to get all comments from. Found in the post URL:<br><code>/r/[subreddit_name]/comments/[post_id]/[post_title]</code>',
placeholder: 'l0me7x',
displayOptions: {
show: {
resource: [
'postComment',
],
operation: [
'getAll',
],
},
},
},
// ----------------------------------
// postComment: delete
// ----------------------------------
{
displayName: 'Comment ID',
name: 'commentId',
type: 'string',
required: true,
default: '',
description: 'ID of the comment to remove. Found in the comment URL:<br><code>/r/[subreddit_name]/comments/[post_id]/[post_title]/[comment_id]</code>',
placeholder: 'gla7fmt',
displayOptions: {
show: {
resource: [
'postComment',
],
operation: [
'remove',
],
},
},
},
// ----------------------------------
// postComment: reply
// ----------------------------------
{
displayName: 'Comment ID',
name: 'commentId',
type: 'string',
required: true,
default: '',
description: 'ID of the comment to reply to. To be found in the comment URL:<br><code>www.reddit.com/r/[subreddit_name]/comments/[post_id]/[post_title]/[comment_id]</code>',
placeholder: 'gl9iroa',
displayOptions: {
show: {
resource: [
'postComment',
],
operation: [
'reply',
],
},
},
},
{
displayName: 'Reply Text',
name: 'replyText',
type: 'string',
required: true,
default: '',
description: 'Text of the reply. Markdown supported.',
displayOptions: {
show: {
resource: [
'postComment',
],
operation: [
'reply',
],
},
},
},
] as INodeProperties[];

View file

@ -128,7 +128,7 @@ export class Reddit implements INodeType {
for (let i = 0; i < items.length; i++) {
// *********************************************************************
// post comment
// postComment
// *********************************************************************
if (resource === 'postComment') {
@ -139,24 +139,54 @@ export class Reddit implements INodeType {
if (operation === 'add') {
// https://www.reddit.com/dev/api/#POST_api_comment
const postTypePrefix = 't3_';
const qs: IDataObject = {
text: this.getNodeParameter('text', i),
thing_id: this.getNodeParameter('targetId', i),
text: this.getNodeParameter('commentText', i),
thing_id: postTypePrefix + this.getNodeParameter('postId', i),
};
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs);
delete responseData.jquery;
} else if (operation === 'getAll') {
// ...
const subreddit = this.getNodeParameter('subreddit', i);
const postId = this.getNodeParameter('postId', i) as string;
const endpoint = `r/${subreddit}/comments/${postId}.json`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
responseData = responseData[1].data.children.map((child: any) => child.data); // tslint:disable-line:no-any
} else if (operation === 'remove') {
// ...
// https://www.reddit.com/dev/api/#POST_api_del
const commentTypePrefix = 't1_';
const qs: IDataObject = {
id: commentTypePrefix + this.getNodeParameter('commentId', i),
};
await redditApiRequest.call(this, 'POST', 'api/del', qs);
responseData = { success: true };
} else if (operation === 'reply') {
// ...
// https://www.reddit.com/dev/api/#POST_api_comment
const commentTypePrefix = 't1_';
const qs: IDataObject = {
text: this.getNodeParameter('replyText', i),
thing_id: commentTypePrefix + this.getNodeParameter('commentId', i),
};
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs);
delete responseData.jquery;
}
@ -214,13 +244,11 @@ export class Reddit implements INodeType {
// https://www.reddit.com/dev/api/#GET_r_{subreddit}_about_rules
// https://www.reddit.com/dev/api/#GET_sticky
const qs: IDataObject = {};
const subreddit = this.getNodeParameter('subreddit', i);
const content = this.getNodeParameter('content', i) as string;
const endpoint = `r/${subreddit}/about/${content}.json`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs);
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
if (content === 'rules') {
responseData = responseData.rules;