Add post submission functionality

This commit is contained in:
Iván Ovejero 2021-01-15 21:20:37 -03:00
parent 97c04df938
commit 6506937751
2 changed files with 225 additions and 24 deletions

View file

@ -26,6 +26,7 @@ export async function redditApiRequest(
this: IHookFunctions | IExecuteFunctions,
method: string,
endpoint: string,
body?: IDataObject,
qs?: IDataObject,
): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = {
@ -38,6 +39,10 @@ export async function redditApiRequest(
json: true,
};
if (options.body === undefined) {
delete options.body;
}
if (options.qs === undefined) {
delete options.qs;
}

View file

@ -45,16 +45,20 @@ export class Reddit implements INodeType {
type: 'options',
options: [
{
name: 'Account',
value: 'account',
name: 'My Account',
value: 'myAccount',
},
{
name: 'Submission',
value: 'submission',
},
],
default: 'account',
default: 'myAccount',
description: 'Resource to consume',
},
// ----------------------------------
// Operations
// My Account operations
// ----------------------------------
{
displayName: 'Operation',
@ -63,45 +67,213 @@ export class Reddit implements INodeType {
displayOptions: {
show: {
resource: [
'account',
'myAccount',
],
},
},
options: [
{
name: 'Get myself',
value: 'getMyself',
name: 'Get identity',
value: 'getIdentity',
description: 'Return the identity of the logged-in user',
},
{
name: 'Get blocked users',
value: 'getMyBlockedUsers',
value: 'getBlockedUsers',
description: 'Return the identity of the logged-in user',
},
{
name: 'Get my friends',
value: 'getMyFriends',
name: 'Get friends',
value: 'getFriends',
description: 'Return a list of friends for the logged-in user',
},
{
name: 'Get my karma',
value: 'getMyKarma',
name: 'Get karma',
value: 'getKarma',
description: 'Return a breakdown of subreddit karma',
},
{
name: 'Get my preferences',
value: 'getMyPrefs',
name: 'Get preferences',
value: 'getPrefs',
description: 'Return the preference settings of the logged-in user',
},
{
name: 'Get my trophies',
value: 'getMyTrophies',
name: 'Get trophies',
value: 'getTrophies',
description: 'Return a list of trophies for the logged-in user',
},
],
default: 'getMyself',
default: 'getIdentity',
description: 'Operation to perform',
},
// ----------------------------------
// Submission operations
// ----------------------------------
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'submission',
],
},
},
options: [
{
name: 'Post',
value: 'post',
description: 'Post a submission to a subreddit',
},
],
default: 'post',
description: 'Operation to perform',
},
{
displayName: 'Title',
name: 'title',
type: 'string',
required: true,
default: '',
description: 'Title of the submission, up to 300 characters long',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'post',
],
},
},
},
{
displayName: 'Subreddit',
name: 'subreddit',
type: 'string',
required: true,
default: '',
description: 'Subreddit to post the submission to',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'post',
],
},
},
},
{
displayName: 'Kind',
name: 'kind',
type: 'options',
options: [
{
name: 'Text Post',
value: 'self',
},
{
name: 'Link Post',
value: 'link',
},
{
name: 'Image Post',
value: 'image',
},
{
name: 'Video Post',
value: 'video',
},
{
name: 'Video GIF Post',
value: 'videogif',
},
],
default: 'text',
description: 'The kind of the submission to be posted',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'post',
],
},
},
},
{
displayName: 'URL',
name: 'url',
type: 'string',
required: true,
default: '',
description: 'URL of the content of the submission',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'post',
],
kind: [
'link',
'image',
'video',
'videogif',
],
},
},
},
{
displayName: 'Text',
name: 'text',
type: 'string',
required: true,
default: '',
description: 'Text content of the submission (Markdown supported)',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'post',
],
kind: [
'self',
],
},
},
},
{
displayName: 'Resubmit',
name: 'resubmit',
type: 'boolean',
default: false,
description: 'If toggled on, the URL will be submitted even if<br>it was already submitted to the subreddit before.<br>Otherwise, a resubmission will trigger an error.',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'post',
],
kind: [
'link',
'image',
'video',
'videogif',
],
},
},
},
],
};
@ -116,35 +288,59 @@ export class Reddit implements INodeType {
for (let i = 0; i < items.length; i++) {
if (resource === 'account') {
if (resource === 'myAccount') {
if (operation === 'getMyself') {
if (operation === 'getIdentity') {
responseData = await redditApiRequest.call(this, 'GET', 'me');
responseData = responseData.features;
} else if (operation === 'getMyBlockedUsers') {
} else if (operation === 'getBlockedUsers') {
responseData = await redditApiRequest.call(this, 'GET', 'me/blocked');
} else if (operation === 'getMyFriends') {
} else if (operation === 'getFriends') {
responseData = await redditApiRequest.call(this, 'GET', 'me/friends');
} else if (operation === 'getMyKarma') {
} else if (operation === 'getKarma') {
responseData = await redditApiRequest.call(this, 'GET', 'me/karma');
} else if (operation === 'getMyPrefs') {
} else if (operation === 'getPrefs') {
responseData = await redditApiRequest.call(this, 'GET', 'me/prefs');
} else if (operation === 'getMyTrophies') {
} else if (operation === 'getTrophies') {
responseData = await redditApiRequest.call(this, 'GET', 'me/trophies');
}
} else if (resource === 'post') {
if (operation === 'submit') {
const body: IDataObject = {
title: this.getNodeParameter('title', i),
sr: this.getNodeParameter('subreddit', i),
kind: this.getNodeParameter('kind', i),
};
body.kind === 'self'
? body.text = this.getNodeParameter('text', i)
: body.url = this.getNodeParameter('url', i);
const resubmit = this.getNodeParameter('resubmit', i);
if (resubmit) {
body.resubmit = true;
}
responseData = await redditApiRequest.call(this, 'POST', 'submit', body);
}
}
Array.isArray(responseData)