Skip to content

Add reply_root event #20

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/bot/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class Bot extends EventEmitter {
this.eventEmitter.on("error", (error) => this.emit("error", error));
this.eventEmitter.on("close", () => this.emit("close"));
this.eventEmitter.on("reply", (event) => this.emit("reply", event));
this.eventEmitter.on("reply_root", (event) => this.emit("reply_root", event));
this.eventEmitter.on("quote", (event) => this.emit("quote", event));
this.eventEmitter.on("mention", (event) => this.emit("mention", event));
this.eventEmitter.on("repost", (event) => this.emit("repost", event));
Expand Down Expand Up @@ -1522,6 +1523,8 @@ export class Bot extends EventEmitter {
override on(event: "close", listener: () => void): this;
/** Emitted when the bot receives a reply. */
override on(event: "reply", listener: (post: Post) => void): this;
/** Emitted when the bot receives a reply inside a post thread initiated by the bot. */
override on(event: "reply_root", listener: (post: Post) => void): this;
/** Emitted when the bot receives a quote post. */
override on(event: "quote", listener: (post: Post) => void): this;
/** Emitted when the bot is mentioned. */
Expand Down Expand Up @@ -1573,6 +1576,7 @@ export class Bot extends EventEmitter {
override addListener(event: "error", listener: (error: unknown) => void): this;
override addListener(event: "close", listener: () => void): this;
override addListener(event: "reply", listener: (post: Post) => void): this;
override addListener(event: "reply_root", listener: (post: Post) => void): this;
override addListener(event: "quote", listener: (post: Post) => void): this;
override addListener(event: "mention", listener: (post: Post) => void): this;
override addListener(
Expand Down
25 changes: 15 additions & 10 deletions src/bot/BotEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,18 @@ export class BotEventEmitter extends EventEmitter {
"app.bsky.feed.post",
async ({ commit: { record, rkey }, did }) => {
const uri = `at://${did}/app.bsky.feed.post/${rkey}`;
if (record.reply?.parent?.uri?.includes(`at://${this.bot.profile.did}`)) {
if (record.reply?.parent?.uri?.startsWith(`at://${this.bot.profile.did}/`)) {
this.emit("reply", await this.bot.getPost(uri));
} else if (record.reply?.root?.uri?.startsWith(`at://${this.bot.profile.did}/`)) {
this.emit("reply_root", await this.bot.getPost(uri));
} else if (
is("app.bsky.embed.record", record.embed)
&& record.embed.record.uri.includes(`at://${this.bot.profile.did}`)
&& record.embed.record.uri.startsWith(`at://${this.bot.profile.did}/`)
) {
this.emit("quote", await this.bot.getPost(uri));
} else if (
is("app.bsky.embed.recordWithMedia", record.embed)
&& record.embed.record.record.uri.includes(`at://${this.bot.profile.did}`)
&& record.embed.record.record.uri.startsWith(`at://${this.bot.profile.did}/`)
) {
this.emit("quote", await this.bot.getPost(uri));
} else if (
Expand All @@ -282,7 +284,7 @@ export class BotEventEmitter extends EventEmitter {
"app.bsky.feed.repost",
async ({ commit: { record, rkey }, did }) => {
const uri = `at://${did}/app.bsky.feed.repost/${rkey}`;
if (record.subject?.uri?.includes(`at://${this.bot.profile.did}`)) {
if (record.subject?.uri?.startsWith(`at://${this.bot.profile.did}/`)) {
this.emit("repost", {
post: await this.bot.getPost(uri),
user: await this.bot.getProfile(did),
Expand All @@ -296,7 +298,7 @@ export class BotEventEmitter extends EventEmitter {
"app.bsky.feed.like",
async ({ commit: { record, rkey }, did }) => {
const uri = `at://${did}/app.bsky.feed.like/${rkey}`;
if (record.subject?.uri?.includes(`at://${this.bot.profile.did}`)) {
if (record.subject?.uri?.startsWith(`at://${this.bot.profile.did}/`)) {
const { collection, host } = parseAtUri(record.subject.uri);
let subject: Post | FeedGenerator | Labeler | undefined;
switch (collection) {
Expand Down Expand Up @@ -386,20 +388,23 @@ export class BotEventEmitter extends EventEmitter {
emitInvalidRecordError(notification);
break;
}
let isRootReply = false;
if (notification.record.reply) {
try {
const { host } = parseAtUri(notification.record.reply.parent.uri);
if (host !== this.bot.profile.did) {
// Ignore replies that aren't direct replies to the bot
break;
const { host: parentHost } = parseAtUri(notification.record.reply.parent.uri);
const { host: rootHost } = parseAtUri(notification.record.reply.parent.uri);
if (parentHost !== this.bot.profile.did && rootHost === this.bot.profile.did) {
isRootReply = true; // This reply is only a root-reply
} else if (parentHost !== this.bot.profile.did) {
break; // Ignore non-parent & non-root replies
}
} catch (e) {
// Ignore invalid AT URI
break;
}
}
const reply = await this.bot.getPost(notification.uri);
if (reply) this.emit("reply", reply);
if (reply) this.emit(isRootReply ? "reply_root" : "reply", reply);
break;
}
case "quote": {
Expand Down