Skip to content

[Sources] Wordpress_com #16500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions components/wordpress_com/actions/create-post/create-post.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "wordpress_com-create-post",
name: "Create New Post",
description: "Creates a new post on a WordPress.com site. [See the documentation](https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/posts/new/)",
version: "0.0.1",
version: "0.0.9",
type: "action",
props: {
wordpress,
Expand Down Expand Up @@ -80,7 +80,9 @@ export default {
wordpress.throwCustomError("Could not create post", error, warnings);
};

$.export("$summary", `Post successfully created. ID = ${response?.ID}` + "\n- " + warnings.join("\n- "));
$.export("$summary",
`Post “${this.title}” is successfully created with ID “${response?.ID}”` +
"\n- " + warnings.join("\n- "));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the warnings stuff here. Just export the summary as plain text

Suggested change
$.export("$summary",
`Post “${this.title}” is successfully created with ID “${response?.ID}”` +
"\n- " + warnings.join("\n- "));
$.export("$summary",
`Post “${this.title}” is successfully created with ID “${response?.ID}”`);

},
};

5 changes: 3 additions & 2 deletions components/wordpress_com/actions/delete-post/delete-post.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "wordpress_com-delete-post",
name: "Delete Post",
description: "Deletes a post. [See the documentation](https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/posts/%24post_ID/delete/)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
wordpress,
Expand Down Expand Up @@ -48,7 +48,8 @@ export default {
wordpress.throwCustomError("Could not delete post", error, warnings);
};

$.export("$summary", `Post ID = ${response?.ID} successfully deleted.` + "\n- " + warnings.join("\n- "));
$.export("$summary", `Post ID “${response?.ID}” has been successfully deleted` +
"\n- " + warnings.join("\n- "));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the warnings stuff here. Just export the summary as plain text

Suggested change
$.export("$summary", `Post ID “${response?.ID}” has been successfully deleted` +
"\n- " + warnings.join("\n- "));
$.export("$summary", `Post ID “${response?.ID}” has been successfully deleted`);

},
};

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "wordpress_com-upload-media",
name: "Upload Media",
description: "Uploads a media file from a URL to the specified WordPress.com site. [See the documentation](https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/media/new/)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
wordpress,
Expand Down Expand Up @@ -74,7 +74,9 @@ export default {

const media = response.media[0];

$.export("$summary", `Media "${media.title}" uploaded successfully (ID: ${media.ID})` + "\n- " + warnings.join("\n- "));
`Media ID “${media.ID})” has been successfully uploaded`;
$.export("$summary", `Media ID “${media.ID})” has been successfully uploaded` +
"\n- " + warnings.join("\n- "));

console.log(response);
return response;
Expand Down
2 changes: 1 addition & 1 deletion components/wordpress_com/common/methods.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default {
Determines whether an error originated from your own validation code or from the API request.
Useful for debugging and crafting more helpful error messages.
=================================================================================================*/
throwCustomError(mainMessage, error, warnings) {
throwCustomError(mainMessage, error, warnings = []) {

const thrower = error?.response?.status
? "API response"
Expand Down
71 changes: 46 additions & 25 deletions components/wordpress_com/sources/new-comment/new-comment.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ export default {
key: "wordpress_com-new-comment",
name: "New Comment",
description: "Emit new event for each new comment added since the last run. If no new comments, emit nothing.",
version: "0.0.1",
version: "0.0.3",
type: "source",
dedupe: "unique",
methods: {
getWordpressComments($) {
return this.wordpress.getWordpressComments({
$,
site: this.site,
postId: this.postId,
number: this.number,
});
},
},
props: {
wordpress,
db: "$.service.db",
Expand Down Expand Up @@ -36,28 +46,54 @@ export default {
min: 1,
max: 100,
},
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new comments.",
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add default timer

Suggested change
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new comments.",
},
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new comments.",
default: {
// import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},

},
hooks: {
async activate() {

const warnings = [];

const {
wordpress,
db,
site,
} = this;

warnings.push(...wordpress.checkDomainOrId(site));

if (warnings.length > 0) {
console.log("Warnings:\n- " + warnings.join("\n- "));
}

await this.db.set("lastCommentId", null); //reset

const response = await this.getWordpressComments(this.wordpress._mock$);

const comments = response.comments || [];

await wordpress.initialize(comments, db, "lastCommentId");
},
},

async run({ $ }) {
const warnings = [];

const {
wordpress,
db,
site,
postId,
number,
} = this;

warnings.push(...wordpress.checkDomainOrId(site));

let response;
try {
response = await wordpress.getWordpressComments({
$,
site,
postId,
number,
});

response = await this.getWordpressComments($);

} catch (error) {
wordpress.throwCustomError("Failed to fetch comments from WordPress:", error, warnings);
Expand All @@ -66,22 +102,7 @@ export default {
const comments = response.comments || [];
const lastCommentId = Number(await db.get("lastCommentId"));

// First run: Initialize cursor
if (!lastCommentId) {
if (!comments.length) {
console.log("No comments found on first run. Source initialized with no cursor.");
return;
}

const newest = comments[0]?.ID;
if (!newest) {
throw new Error("Failed to initialize: The latest comment does not have a valid ID.");
}

await db.set("lastCommentId", newest);
console.log(`Initialized lastCommentId on first run with comment ID ${newest}.`);
return;
}
if (!lastCommentId) await wordpress.initialize(comments, db, "lastCommentId");

let maxCommentIdTracker = lastCommentId;
const newComments = [];
Expand Down
66 changes: 45 additions & 21 deletions components/wordpress_com/sources/new-follower/new-follower.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ export default {
key: "wordpress_com-new-follower",
name: "New Follower",
description: "Emit new event for each new follower that subscribes to the site.",
version: "0.0.1",
version: "0.0.3",
type: "source",
dedupe: "unique",
methods: {
getWordpressFollowers($) {

return this.wordpress.getWordpressFollowers({
$,
site: this.site,
});

},
},
props: {
wordpress,
db: "$.service.db",
Expand All @@ -16,7 +26,39 @@ export default {
"siteId",
],
},
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new followers.",
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add default timer

Suggested change
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new followers.",
},
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new followers.",
default: {
// import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},

},
hooks: {
async activate() {

const warnings = [];

const {
wordpress,
db,
site,
} = this;

warnings.push(...wordpress.checkDomainOrId(site));

if (warnings.length > 0) {
console.log("Warnings:\n- " + warnings.join("\n- "));
}

await this.db.set("lastFollowerId", null); //reset

const response = await this.getWordpressFollowers(this.wordpress._mock$);

const followers = response.subscribers || [];

await wordpress.initialize(followers, db, "lastFollowerId");
},
},

async run({ $ }) {
const warnings = [];

Expand All @@ -30,10 +72,7 @@ export default {

let response;
try {
response = await wordpress.getWordpressFollowers({
$,
site,
});
response = await this.getWordpressFollowers($);

} catch (error) {
wordpress.throwCustomError("Failed to fetch followers from WordPress:", error, warnings);
Expand All @@ -43,22 +82,7 @@ export default {

const lastFollowerId = Number(await db.get("lastFollowerId"));

// First run: Initialize cursor
if (!lastFollowerId) {
if (!followers.length) {
console.log("No followers found on first run. Source initialized with no cursor.");
return;
}

const newest = followers[0]?.ID;
if (!newest) {
throw new Error("Failed to initialize: The latest follower does not have a valid ID.");
}

await db.set("lastFollowerId", newest);
console.log(`Initialized lastFollowerId on first run with follower ID ${newest}.`);
return;
}
if (!lastFollowerId) await wordpress.initialize(followers, db, "lastPostId");

let maxFollowerIdTracker = lastFollowerId;
const newFollowers = [];
Expand Down
79 changes: 49 additions & 30 deletions components/wordpress_com/sources/new-post/new-post.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "wordpress_com-new-post",
name: "New Post",
description: "Emit new event for each new post published since the last run. If no new posts, emit nothing.",
version: "0.0.1",
version: "0.0.4",
type: "source",
dedupe: "unique",
props: {
Expand Down Expand Up @@ -45,7 +45,53 @@ export default {
min: 1,
max: 100,
},
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new posts.",
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add default timer

Suggested change
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new posts.",
},
timer: {
type: "$.interface.timer",
label: "Timer",
description: "How often to poll WordPress for new posts.",
default: {
// import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},

},
methods: {
getWordpressPosts($) {

return this.wordpress.getWordpressPosts({
$,
site: this.site,
type: this.type,
number: this.number,
});

},
},
hooks: {
async activate() {

const warnings = []
; const {
wordpress,
db,
type,
site,
} = this;

warnings.push(...wordpress.checkDomainOrId(site));

if (warnings.length > 0) {
console.log("Warnings:\n- " + warnings.join("\n- "));
}

await this.db.set("lastPostId", null); // reset

const response = await this.getWordpressPosts(this.wordpress._mock$);

const posts = (type === "attachment")
? (response.media || [])
: (response.posts || []);

await wordpress.initialize(posts, db, "lastPostId");
},
},

async run({ $ }) {
const warnings = [];

Expand All @@ -54,45 +100,18 @@ export default {
db,
site,
type,
number,
} = this;

warnings.push(...wordpress.checkDomainOrId(site));

let response;
try {
response = await wordpress.getWordpressPosts({
$,
site,
type,
number,
});

} catch (error) {
wordpress.throwCustomError("Failed to fetch posts from WordPress:", error, warnings);
}
const response = await this.getWordpressPosts($);

const posts = (type === "attachment")
? (response.media || [])
: (response.posts || []);
const lastPostId = Number(await db.get("lastPostId"));

// First run: Initialize cursor
if (!lastPostId) {
if (!posts.length) {
console.log("No posts found on first run. Source initialized with no cursor.");
return;
}

const newest = posts[0]?.ID;
if (!newest) {
throw new Error("Failed to initialize: The latest post does not have a valid ID.");
}

await db.set("lastPostId", newest);
console.log(`Initialized lastPostId on first run with post ID ${newest}.`);
return;
}
if (!lastPostId) await wordpress.initialize(posts, db, "lastPostId");

let maxPostIdTracker = lastPostId;

Expand Down
Loading
Loading