Skip to content
Merged
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
23 changes: 23 additions & 0 deletions src/commands/unfollow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Command from "#utils/Command.js"
import { ApplicationCommandOptionTypes } from "oceanic.js";
import interactWithBlueskyNow from "#utils/interact-with-bluesky-now.js";

const unlikeNowCommand = new Command({
name: "unfollow",
description: "Unfollow an account on Bluesky.",
options: [
{
type: ApplicationCommandOptionTypes.STRING,
name: "handle",
description: "What's the name of the account?",
required: true
}
],
async action(interaction) {

return await interactWithBlueskyNow(interaction, "unfollow", "deleteFollow");

}
});

export default unlikeNowCommand;
5 changes: 3 additions & 2 deletions src/utils/interact-with-bluesky-now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { authenticator } from "otplib";
import IncorrectDecryptionKeyError from "./errors/IncorrectDecryptionKeyError.js";
import MFAIncorrectCodeError from "./errors/MFAIncorrectCodeError.js";

async function interactWithBlueskyNow(interaction: CommandInteraction | ComponentInteraction | ModalSubmitInteraction, customIDPrefix: string, action: "follow" | "deleteRepost" | "like" | "deleteLike" | "repost") {
async function interactWithBlueskyNow(interaction: CommandInteraction | ComponentInteraction | ModalSubmitInteraction, customIDPrefix: string, action: "deleteFollow" | "follow" | "deleteRepost" | "like" | "deleteLike" | "repost") {

const guildID = getGuildIDFromInteraction(interaction);

Expand All @@ -26,7 +26,8 @@ async function interactWithBlueskyNow(interaction: CommandInteraction | Componen
like: "💖",
deleteLike: "💔",
deleteRepost: "🗑️",
follow: "➕"
follow: "➕",
deleteFollow: "➖"
};
await interaction.editOriginal({
content: responses[action],
Expand Down
43 changes: 25 additions & 18 deletions src/utils/interact-with-bluesky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import blueskyClient from "./bluesky-client.js";
import { Agent } from "@atproto/api";
import { isThreadViewPost } from "@atproto/api/dist/client/types/app/bsky/feed/defs.js";

async function interactWithBluesky(source: {interaction?: ModalSubmitInteraction | ComponentInteraction, rkey?: string, targetHandle?: string, actorDID?: string, guildID: string, decryptionKey?: string}, action: "follow" | "deletePost" | "deleteLike" | "like" | "deleteRepost" | "repost") {
async function interactWithBluesky(source: {interaction?: ModalSubmitInteraction | ComponentInteraction, rkey?: string, targetHandle?: string, actorDID?: string, guildID: string, decryptionKey?: string}, action: "deleteFollow" | "follow" | "deletePost" | "deleteLike" | "like" | "deleteRepost" | "repost") {

let {interaction, rkey, targetHandle, actorDID} = source;

const isTargetAccount = action === "deleteFollow" || action === "follow";
if (interaction && !targetHandle) {

// Get the rkey of the post.
const originalMessage = await interaction.getOriginal();
const originalEmbed = originalMessage.embeds[0];
const value = originalEmbed?.footer?.text;

if (action === "follow") {
if (isTargetAccount) {

targetHandle = value;

Expand All @@ -30,7 +31,7 @@ async function interactWithBluesky(source: {interaction?: ModalSubmitInteraction

}

if (!targetHandle || !actorDID || (action !== "follow" && !rkey)) throw new Error();
if (!targetHandle || !actorDID || (!isTargetAccount && !rkey)) throw new Error();

// Get the CID of the post if necessary.
const session = await blueskyClient.restore(actorDID, "auto", {guildID: source.guildID, decryptionKey: source.decryptionKey});
Expand All @@ -39,10 +40,16 @@ async function interactWithBluesky(source: {interaction?: ModalSubmitInteraction
let cid;
let uri;
if (!targetDID) throw new Error();
if (action !== "follow") {
if (action === "deleteFollow") {

const profileResponse = await agent.getProfile({actor: targetDID});
uri = profileResponse.data.viewer?.following;
if (!uri) return;

} else if (action !== "follow") {

if (!rkey) throw new Error();

const recordResponse = await agent.com.atproto.repo.getRecord({
collection: "app.bsky.feed.post",
repo: targetDID,
Expand All @@ -52,20 +59,20 @@ async function interactWithBluesky(source: {interaction?: ModalSubmitInteraction
cid = recordResponse.data.cid;

if (!cid) throw new Error();

// Get the URI we need.
uri = `at://${targetDID}/app.bsky.feed.post/${rkey}`;
if (action === "deleteLike" || action === "deleteRepost") {

const response = await agent.getPostThread({uri});
if (isThreadViewPost(response.data.thread)) {

const possibleURI = response.data.thread.post.viewer?.[action === "deleteLike" ? "like" : "repost"];
if (!possibleURI) return;
uri = possibleURI;

}


}

// Get the URI we need.
if (action === "deleteLike" || action === "deleteRepost") {

const response = await agent.getPostThread({uri: uri!});
if (isThreadViewPost(response.data.thread)) {

const possibleURI = response.data.thread.post.viewer?.[action === "deleteLike" ? "like" : "repost"];
if (!possibleURI) return;
uri = possibleURI;

}

}
Expand Down
Loading