Skip to content

Commit

Permalink
refactor: rm like when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristuker committed Sep 13, 2024
1 parent e665bbd commit 3b07384
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Record } from "./interfaces/notifications";
import cron from "node-cron";
import { initServer } from "./server";
import "dotenv/config";
import { shouldProcessMention } from "./shouldProcessMention";

connectRedis();

Expand All @@ -31,13 +32,17 @@ export async function main() {
console.log("Processing another mention");
const record = mention.record as Record;
const taggedPost = mention.uri;
const shouldProcess = await shouldProcessMention(taggedPost)
if (shouldProcess) {
console.log(`Already sended ${taggedPost}`);
continue;
}

const convo = await listConvo(mention.author.did, agent);
const url = getUrlFromUri(record.reply.root.uri);
const message = await messageBuilder(url, record.text);
await message.detectFacets(agent);
await sendMessage(convo.id, message, agent, taggedPost);
await saveWrongMessage(mention.uri);
await agent.like(mention.uri, mention.cid);
console.log("Process ended");
} catch (error) {
console.error("Error:", error);
Expand Down
32 changes: 10 additions & 22 deletions src/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import AtpAgent, { RichText } from "@atproto/api";
import { messageExists, saveMessage, messageWrongExists } from "./redis";
import { saveMessage } from "./redis";

export const sendMessage = async (
convoId: string,
message: RichText,
agent: AtpAgent,
taggedPost: string
) => {
try {
const [sended, wrongMessage] = await Promise.all([
messageExists(taggedPost),
messageWrongExists(taggedPost),
]);
if (sended || wrongMessage) {
console.log(`Already sended ${taggedPost}`);
return;
}
const proxy = agent.withProxy("bsky_chat", "did:web:api.bsky.chat");
await proxy.chat.bsky.convo.sendMessage({
convoId: convoId,
message: {
text: message.text,
facets: message.facets,
},
});
await saveMessage(taggedPost);
} catch (error) {
console.log("Error on send message:", error);
}
const proxy = agent.withProxy("bsky_chat", "did:web:api.bsky.chat");
await proxy.chat.bsky.convo.sendMessage({
convoId: convoId,
message: {
text: message.text,
facets: message.facets,
},
});
await saveMessage(taggedPost);
};
9 changes: 9 additions & 0 deletions src/shouldProcessMention.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { messageExists, messageWrongExists } from "./redis";

export const shouldProcessMention = async (taggedPost: string) => {
const [sended, wrongMessage] = await Promise.all([
messageExists(taggedPost),
messageWrongExists(taggedPost),
]);
return sended || wrongMessage;
};

0 comments on commit 3b07384

Please sign in to comment.