-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (111 loc) · 4.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { WebhookClient } = require("discord.js")
const { AtpAgent } = require("@atproto/api")
const { IdResolver } = require("@atproto/identity")
const { WebSocket } = require("partysocket")
const config = require("./config.json")
const blueskyClient = new AtpAgent({ service: config.bluesky.service })
async function sendWebhook(embed) {
for (var i = 0; i < config.webhookURLs.length; i++) {
const webhook = new WebhookClient({ url: config.webhookURLs[i] })
await webhook.send({
embeds: [embed]
})
}
}
async function runBluesky() {
const idResolver = new IdResolver();
// Resolve handles to DIDs
accountDIDs = await Promise.all(
config.bluesky.accountHandles.map(async (handle) => {
try {
const res = await idResolver.handle.resolve(handle)
console.log(`Resolved ${handle} to ${res}`)
return res
} catch (err) {
console.log(`Failed to resolve handle ${handle}:`, err);
return null;
}
})
)
const wantedDids = accountDIDs.join('&');
let urlIndex = 0;
const urlProvider = () => {
const baseUrl = config.bluesky.jetstreamServices[urlIndex++ % config.bluesky.jetstreamServices.length];
const queryParams = `wantedCollections=app.bsky.feed.post&wantedDids=${wantedDids}`;
console.log(`Trying wss://${baseUrl}/subscribe?${queryParams}`)
return `wss://${baseUrl}/subscribe?${queryParams}`;
};
const jetstreamSocket = new WebSocket(urlProvider);
jetstreamSocket.addEventListener("open", () => {
console.log("Bluesky Jetstream websocket client connected");
});
jetstreamSocket.addEventListener("error", (error) => {
console.log("Websocket connection error: " + error.error);
});
jetstreamSocket.addEventListener("close", (event) => {
console.log("Websocket connection closed: " + event.code);
});
jetstreamSocket.addEventListener("message", async (event) => {
try {
const data = JSON.parse(event.data);
// Extra sanity check just in case the websocket starts sending posts from other accounts
if (!accountDIDs.includes(data.did)) {
return;
}
if (data.commit?.operation == "create") {
if (data.commit?.record?.reply) {
return;
}
const profileInfo = await blueskyClient.app.bsky.actor.getProfile({
actor: data.did
});
const accountName = profileInfo.data.displayName || profileInfo.data.handle;
let embed = {
"title": `New Bluesky post from ${accountName}`,
"description": data.commit.record.text,
"url": `https://bsky.app/profile/${profileInfo.data.handle}/post/${data.commit.rkey}`,
"color": 0x156cc7,
"timestamp": (new Date(data.commit.record.createdAt)),
"author": {
"name": accountName,
"icon_url": profileInfo.data.avatar
}
};
// Check for embed and images
if (data.commit.record.embed && data.commit.record.embed.images && data.commit.record.embed.images.length > 0) {
// Get the CID of the first image
const firstImage = data.commit.record.embed.images[0];
const imageCID = firstImage.image.ref.$link.toString();
embed.image = {
url: `https://cdn.bsky.app/img/feed_fullsize/plain/${data.did}/${imageCID}@jpeg`
};
}
if (data.commit.record.embed && data.commit.record.embed.video) {
embed.image = {
url: `https://video.bsky.app/watch/${data.did}/${data.commit.record.embed.video.ref.$link}/thumbnail.jpg`
}
}
sendWebhook(embed)
}
} catch (error) {
console.error("Error parsing message or fetching profile:", error);
}
});
}
async function main() {
const configErrors = [];
if (config.bluesky.accountHandles.length === 0) {
configErrors.push("No Bluesky accounts specified in config.")
}
if (config.webhookURLs.length === 0) {
configErrors.push("No webhook URLs specified in config.")
}
if (configErrors.length > 0) {
console.log("Error(s) found in configuration:");
configErrors.forEach(configWarning => console.log(`- ${configWarning}`));
process.exit(1);
}
console.log("\nInitialising BlueskyDiscordFeed!")
runBluesky()
}
main()