Skip to content

Commit e2be15a

Browse files
committed
Fix sources
1 parent 1b6788e commit e2be15a

File tree

4 files changed

+101
-57
lines changed

4 files changed

+101
-57
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "wordpress_com-create-post",
55
name: "Create New Post",
66
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/)",
7-
version: "0.0.6",
7+
version: "0.0.8",
88
type: "action",
99
props: {
1010
wordpress,

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ export default {
77
version: "0.0.2",
88
type: "source",
99
dedupe: "unique",
10+
methods: {
11+
getWordpressComments($) {
12+
return this.wordpress.getWordpressComments({
13+
$,
14+
site: this.site,
15+
postId: this.postId,
16+
number: this.number,
17+
});
18+
},
19+
},
1020
props: {
1121
wordpress,
1222
db: "$.service.db",
@@ -44,33 +54,46 @@ export default {
4454
},
4555
hooks: {
4656
async activate() {
47-
await this.db.set("lastCommentId", null);
48-
await this.run({ $ : this.wordpress._mock$() });
57+
58+
const warnings = [];
59+
60+
const {
61+
wordpress,
62+
db,
63+
site,
64+
} = this;
65+
66+
warnings.push(...wordpress.checkDomainOrId(site));
67+
68+
if (warnings.length > 0) {
69+
console.log("Warnings:\n- " + warnings.join("\n- "));
70+
}
71+
72+
await this.db.set("lastCommentId", null); //reset
73+
74+
const response = await this.getWordpressComments(this.wordpress._mock$);
75+
76+
const comments = response.comments || [];
77+
78+
await wordpress.initialize(comments, db, "lastCommentId");
4979
},
5080
},
5181

52-
5382
async run({ $ }) {
5483
const warnings = [];
5584

5685
const {
5786
wordpress,
5887
db,
5988
site,
60-
postId,
61-
number,
6289
} = this;
6390

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

6693
let response;
6794
try {
68-
response = await wordpress.getWordpressComments({
69-
$,
70-
site,
71-
postId,
72-
number,
73-
});
95+
96+
response = await this.getWordpressComments($);
7497

7598
} catch (error) {
7699
wordpress.throwCustomError("Failed to fetch comments from WordPress:", error, warnings);
@@ -79,16 +102,7 @@ export default {
79102
const comments = response.comments || [];
80103
const lastCommentId = Number(await db.get("lastCommentId"));
81104

82-
// First run: Initialize cursor
83-
if (!lastCommentId) {
84-
if (!comments.length) {
85-
console.log("No comments found on first run. Source initialized with no cursor.");
86-
return;
87-
}
88-
await db.set("lastCommentId", newest);
89-
console.log(`Initialized lastCommentId on first run with comment ID ${newest}.`);
90-
return;
91-
}
105+
if (!lastCommentId) await wordpress.initialize(comments, db, "lastCommentId");
92106

93107
let maxCommentIdTracker = lastCommentId;
94108
const newComments = [];

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ export default {
77
version: "0.0.2",
88
type: "source",
99
dedupe: "unique",
10+
methods: {
11+
getWordpressFollowers($) {
12+
13+
return this.wordpress.getWordpressFollowers({
14+
$,
15+
site: this.site,
16+
});
17+
18+
},
19+
},
1020
props: {
1121
wordpress,
1222
db: "$.service.db",
@@ -22,6 +32,33 @@ export default {
2232
description: "How often to poll WordPress for new followers.",
2333
},
2434
},
35+
hooks: {
36+
async activate() {
37+
38+
const warnings = [];
39+
40+
const {
41+
wordpress,
42+
db,
43+
site,
44+
} = this;
45+
46+
warnings.push(...wordpress.checkDomainOrId(site));
47+
48+
if (warnings.length > 0) {
49+
console.log("Warnings:\n- " + warnings.join("\n- "));
50+
}
51+
52+
await this.db.set("lastFollowerId", null); //reset
53+
54+
const response = await this.getWordpressFollowers(this.wordpress._mock$);
55+
56+
const followers = response.subscribers || [];
57+
58+
await wordpress.initialize(followers, db, "lastFollowerId");
59+
},
60+
},
61+
2562
async run({ $ }) {
2663
const warnings = [];
2764

@@ -35,10 +72,7 @@ export default {
3572

3673
let response;
3774
try {
38-
response = await wordpress.getWordpressFollowers({
39-
$,
40-
site,
41-
});
75+
response = await this.getWordpressFollowers($);
4276

4377
} catch (error) {
4478
wordpress.throwCustomError("Failed to fetch followers from WordPress:", error, warnings);
@@ -48,16 +82,7 @@ export default {
4882

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

51-
// First run: Initialize cursor
52-
if (!lastFollowerId) {
53-
if (!followers.length) {
54-
console.log("No followers found on first run. Source initialized with no cursor.");
55-
return;
56-
}
57-
await db.set("lastFollowerId", newest);
58-
console.log(`Initialized lastFollowerId on first run with follower ID ${newest}.`);
59-
return;
60-
}
85+
if (!lastFollowerId) await wordpress.initialize(followers, db, "lastPostId");
6186

6287
let maxFollowerIdTracker = lastFollowerId;
6388
const newFollowers = [];

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "wordpress_com-new-post",
55
name: "New Post",
66
description: "Emit new event for each new post published since the last run. If no new posts, emit nothing.",
7-
version: "0.0.2",
7+
version: "0.0.3",
88
type: "source",
99
dedupe: "unique",
1010
props: {
@@ -52,32 +52,38 @@ export default {
5252
},
5353
},
5454
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-
}
55+
getWordpressPosts($) {
56+
57+
return this.wordpress.getWordpressPosts({
58+
$,
59+
site: this.site,
60+
type: this.type,
61+
number: this.number,
62+
});
63+
64+
},
6765
},
6866
hooks: {
6967
async activate() {
7068

71-
const {
69+
const warnings = []
70+
; const {
7271
wordpress,
7372
db,
7473
type,
74+
site,
7575
} = this;
7676

77-
await this.db.set("lastCommentId", null);
77+
warnings.push(...wordpress.checkDomainOrId(site));
78+
79+
if (warnings.length > 0) {
80+
console.log("Warnings:\n- " + warnings.join("\n- "));
81+
}
82+
83+
await this.db.set("lastPostId", null); // reset
84+
85+
const response = await this.getWordpressPosts(this.wordpress._mock$);
7886

79-
const response = await this.getWordpressPosts(this.wordpress._mock$);
80-
8187
const posts = (type === "attachment")
8288
? (response.media || [])
8389
: (response.posts || []);
@@ -98,16 +104,15 @@ export default {
98104

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

101-
const response = await this.getWordpressPosts($);
107+
const response = await this.getWordpressPosts($);
102108

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

108-
// Initialize if not already set
109-
if (!lastPostId) await initialize(posts, db, "lastPostId");
110-
114+
if (!lastPostId) await wordpress.initialize(posts, db, "lastPostId");
115+
111116
let maxPostIdTracker = lastPostId;
112117

113118
const newPosts = [];

0 commit comments

Comments
 (0)