Skip to content

Commit 1b6788e

Browse files
committed
Fix sources
1 parent bbe6eac commit 1b6788e

File tree

7 files changed

+71
-47
lines changed

7 files changed

+71
-47
lines changed

components/wordpress_com/actions/delete-post/delete-post.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "wordpress_com-delete-post",
55
name: "Delete Post",
66
description: "Deletes a post. [See the documentation](https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/posts/%24post_ID/delete/)",
7-
version: "0.0.2",
7+
version: "0.0.1",
88
type: "action",
99
props: {
1010
wordpress,
@@ -47,7 +47,7 @@ export default {
4747
} catch (error) {
4848
wordpress.throwCustomError("Could not delete post", error, warnings);
4949
};
50-
50+
5151
$.export("$summary", `Post ID “${response?.ID}” has been successfully deleted` +
5252
"\n- " + warnings.join("\n- "));
5353
},

components/wordpress_com/common/methods.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default {
201201
Determines whether an error originated from your own validation code or from the API request.
202202
Useful for debugging and crafting more helpful error messages.
203203
=================================================================================================*/
204-
throwCustomError(mainMessage, error, warnings) {
204+
throwCustomError(mainMessage, error, warnings = []) {
205205

206206
const thrower = error?.response?.status
207207
? "API response"

components/wordpress_com/sources/new-comment/new-comment.mjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ export default {
4242
description: "How often to poll WordPress for new comments.",
4343
},
4444
},
45+
hooks: {
46+
async activate() {
47+
await this.db.set("lastCommentId", null);
48+
await this.run({ $ : this.wordpress._mock$() });
49+
},
50+
},
51+
52+
4553
async run({ $ }) {
4654
const warnings = [];
4755

@@ -77,12 +85,6 @@ export default {
7785
console.log("No comments found on first run. Source initialized with no cursor.");
7886
return;
7987
}
80-
81-
const newest = comments[0]?.ID;
82-
if (!newest) {
83-
throw new Error("Failed to initialize: The latest comment does not have a valid ID.");
84-
}
85-
8688
await db.set("lastCommentId", newest);
8789
console.log(`Initialized lastCommentId on first run with comment ID ${newest}.`);
8890
return;

components/wordpress_com/sources/new-follower/new-follower.mjs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ export default {
5454
console.log("No followers found on first run. Source initialized with no cursor.");
5555
return;
5656
}
57-
58-
const newest = followers[0]?.ID;
59-
if (!newest) {
60-
throw new Error("Failed to initialize: The latest follower does not have a valid ID.");
61-
}
62-
6357
await db.set("lastFollowerId", newest);
6458
console.log(`Initialized lastFollowerId on first run with follower ID ${newest}.`);
6559
return;

components/wordpress_com/sources/new-post/new-post.mjs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,41 @@ export default {
5151
description: "How often to poll WordPress for new posts.",
5252
},
5353
},
54+
methods: {
55+
getWordpressPosts($){
56+
57+
let response;
58+
response = this.wordpress.getWordpressPosts({
59+
$,
60+
site: this.site,
61+
type: this.type,
62+
number: this.number,
63+
});
64+
65+
return response;
66+
}
67+
},
68+
hooks: {
69+
async activate() {
70+
71+
const {
72+
wordpress,
73+
db,
74+
type,
75+
} = this;
76+
77+
await this.db.set("lastCommentId", null);
78+
79+
const response = await this.getWordpressPosts(this.wordpress._mock$);
80+
81+
const posts = (type === "attachment")
82+
? (response.media || [])
83+
: (response.posts || []);
84+
85+
await wordpress.initialize(posts, db, "lastPostId");
86+
},
87+
},
88+
5489
async run({ $ }) {
5590
const warnings = [];
5691

@@ -59,46 +94,20 @@ export default {
5994
db,
6095
site,
6196
type,
62-
number,
6397
} = this;
6498

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

67-
let response;
68-
try {
69-
response = await wordpress.getWordpressPosts({
70-
$,
71-
site,
72-
type,
73-
number,
74-
});
75-
76-
} catch (error) {
77-
wordpress.throwCustomError("Failed to fetch posts from WordPress:", error, warnings);
78-
}
101+
const response = await this.getWordpressPosts($);
79102

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

85-
// First run: Initialize cursor
86-
if (!lastPostId) {
87-
if (!posts.length) {
88-
console.log("No posts found on first run. Source initialized with no cursor.");
89-
return;
90-
}
91-
92-
const newest = posts[0]?.ID;
93-
if (!newest) {
94-
throw new Error("Failed to initialize: The latest post does not have a valid ID.");
95-
}
96-
97-
await db.set("lastPostId", newest);
98-
console.log(`Initialized lastPostId on first run with post ID ${newest}.`);
99-
return;
100-
}
101-
108+
// Initialize if not already set
109+
if (!lastPostId) await initialize(posts, db, "lastPostId");
110+
102111
let maxPostIdTracker = lastPostId;
103112

104113
const newPosts = [];

components/wordpress_com/wordpress_com.app.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export default {
4545
},
4646
methods: {
4747
...methods,
48+
_mock$(){
49+
return new Proxy({}, {
50+
get() {
51+
return (...args) => console.log(...args);
52+
},
53+
});
54+
},
4855
_baseUrl() {
4956
return "https://public-api.wordpress.com/rest/v1.1";
5057
},
@@ -155,5 +162,16 @@ export default {
155162
...opts,
156163
});
157164
},
165+
async initialize(subject, db, dbName){
166+
if (!subject.length) {
167+
console.log("No ID found on first run. Source initialized with no cursor.");
168+
return false;
169+
}
170+
const newest = subject[0]?.ID;
171+
172+
await db.set(dbName, newest);
173+
console.log(`Initialized ${dbName} on first run with ID ${newest}.`);
174+
return true ;
175+
}
158176
},
159177
};

pnpm-lock.yaml

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)