feat: clean up post parsing & parse quote posts
This commit is contained in:
parent
ce1fbe60ae
commit
abb6b8b719
4 changed files with 85 additions and 32 deletions
|
|
@ -19,7 +19,7 @@ try {
|
||||||
await bot.setChatPreference(IncomingChatPreference.All);
|
await bot.setChatPreference(IncomingChatPreference.All);
|
||||||
bot.on("message", messages.handler);
|
bot.on("message", messages.handler);
|
||||||
|
|
||||||
logger.success("Registered events (reply, mention, quote)");
|
logger.success("Registered events (message)");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error("Failure to log-in: ", e);
|
logger.error("Failure to log-in: ", e);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|
|
||||||
12
src/types.ts
Normal file
12
src/types.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
export type ParsedPost = {
|
||||||
|
thread?: {
|
||||||
|
ancestors: ParsedPost[];
|
||||||
|
};
|
||||||
|
author: string;
|
||||||
|
text: string;
|
||||||
|
images?: {
|
||||||
|
index: number;
|
||||||
|
alt: string;
|
||||||
|
}[];
|
||||||
|
quotePost?: ParsedPost;
|
||||||
|
};
|
||||||
|
|
@ -9,7 +9,7 @@ import { conversations, messages } from "../db/schema";
|
||||||
import { and, eq } from "drizzle-orm";
|
import { and, eq } from "drizzle-orm";
|
||||||
import { env } from "../env";
|
import { env } from "../env";
|
||||||
import { bot, MAX_GRAPHEMES } from "../core";
|
import { bot, MAX_GRAPHEMES } from "../core";
|
||||||
import { parsePostImages, traverseThread } from "./post";
|
import { parsePost, parsePostImages, traverseThread } from "./post";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Utilities
|
Utilities
|
||||||
|
|
@ -126,37 +126,31 @@ export async function parseConversation(convo: Conversation) {
|
||||||
const post = await bot.getPost(row.postUri);
|
const post = await bot.getPost(row.postUri);
|
||||||
const convoMessages = await getRelevantMessages(row!);
|
const convoMessages = await getRelevantMessages(row!);
|
||||||
|
|
||||||
const thread = await traverseThread(post);
|
let parseResult = null;
|
||||||
|
try {
|
||||||
|
parseResult = yaml.dump({
|
||||||
|
post: await parsePost(post, true),
|
||||||
|
messages: convoMessages.map((message) => {
|
||||||
|
const profile = resolveDid(convo, message.did);
|
||||||
|
|
||||||
return yaml.dump({
|
return {
|
||||||
post: {
|
user: profile.displayName
|
||||||
thread: {
|
? `${profile.displayName} (${profile.handle})`
|
||||||
ancestors: thread.map((post) => ({
|
: `Handle: ${profile.handle}`,
|
||||||
author: post.author.displayName
|
text: message.text,
|
||||||
? `${post.author.displayName} (${post.author.handle})`
|
};
|
||||||
: `Handle: ${post.author.handle}`,
|
}),
|
||||||
text: post.text,
|
});
|
||||||
})),
|
} catch (e) {
|
||||||
},
|
convo.sendMessage({
|
||||||
author: post.author.displayName
|
text:
|
||||||
? `${post.author.displayName} (${post.author.handle})`
|
"Sorry, I ran into an issue analyzing that post. Please try again.",
|
||||||
: `Handle: ${post.author.handle}`,
|
});
|
||||||
text: post.text,
|
|
||||||
images: parsePostImages(post),
|
|
||||||
likes: post.likeCount || 0,
|
|
||||||
replies: post.replyCount || 0,
|
|
||||||
},
|
|
||||||
messages: convoMessages.map((message) => {
|
|
||||||
const profile = resolveDid(convo, message.did);
|
|
||||||
|
|
||||||
return {
|
throw new Error("Failed to parse conversation");
|
||||||
user: profile.displayName
|
}
|
||||||
? `${profile.displayName} (${profile.handle})`
|
|
||||||
: `Handle: ${profile.handle}`,
|
return parseResult;
|
||||||
text: message.text,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,53 @@
|
||||||
import { EmbedImage, Post } from "@skyware/bot";
|
import {
|
||||||
|
EmbedImage,
|
||||||
|
Post,
|
||||||
|
PostEmbed,
|
||||||
|
RecordEmbed,
|
||||||
|
RecordWithMediaEmbed,
|
||||||
|
} from "@skyware/bot";
|
||||||
import * as c from "../core";
|
import * as c from "../core";
|
||||||
import * as yaml from "js-yaml";
|
import * as yaml from "js-yaml";
|
||||||
|
import type { ParsedPost } from "../types";
|
||||||
|
|
||||||
|
export async function parsePost(
|
||||||
|
post: Post,
|
||||||
|
includeThread: boolean,
|
||||||
|
): Promise<ParsedPost> {
|
||||||
|
const [images, quotePost, ancestorPosts] = await Promise.all([
|
||||||
|
parsePostImages(post),
|
||||||
|
parseQuote(post),
|
||||||
|
includeThread ? traverseThread(post) : Promise.resolve(null),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
author: post.author.displayName
|
||||||
|
? `${post.author.displayName} (${post.author.handle})`
|
||||||
|
: `Handle: ${post.author.handle}`,
|
||||||
|
text: post.text,
|
||||||
|
...(images && { images }),
|
||||||
|
...(quotePost && { quotePost }),
|
||||||
|
...(ancestorPosts && {
|
||||||
|
thread: {
|
||||||
|
ancestors: await Promise.all(
|
||||||
|
ancestorPosts.map((ancestor) => parsePost(ancestor, false)),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function parseQuote(post: Post) {
|
||||||
|
if (
|
||||||
|
!post.embed || (!post.embed.isRecord() && !post.embed.isRecordWithMedia())
|
||||||
|
) return undefined;
|
||||||
|
|
||||||
|
const record = (post.embed as RecordEmbed || RecordWithMediaEmbed).record;
|
||||||
|
console.log("embed: ", post.embed);
|
||||||
|
console.log("record: ", record);
|
||||||
|
const embedPost = await c.bot.getPost(record.uri);
|
||||||
|
|
||||||
|
return await parsePost(embedPost, false);
|
||||||
|
}
|
||||||
|
|
||||||
export function parsePostImages(post: Post) {
|
export function parsePostImages(post: Post) {
|
||||||
if (!post.embed) return [];
|
if (!post.embed) return [];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue