Skip to content

Commit a00e55a

Browse files
committed
feat(bot): render marketplace listings
1 parent 3326d31 commit a00e55a

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

apps/bot/src/lib/builder.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ function buildMediaEmbed(media: NormalizedPost["media"], spoiler?: EmbedFlags["S
5959

6060
const gallery = new MediaGalleryBuilder();
6161
gallery.addItems(
62-
media.slice(0, MAX_GALLERY_ITEMS).map((m) => ({ media: { url: m.url }, spoiler })),
62+
media.slice(0, MAX_GALLERY_ITEMS).map((m) => ({
63+
media: { url: m.url },
64+
description: m.description?.slice(0, 1024) || undefined,
65+
spoiler,
66+
})),
6367
);
6468

6569
return gallery.toJSON();
@@ -79,14 +83,21 @@ function addPostComponents(embed: ContainerBuilder, post: PostData, headingPrefi
7983
post.author.handle && post.author.url
8084
? `(${hyperlink(`@${post.author.handle}`, post.author.url)})`
8185
: "";
82-
const authorHeading = [headingPrefix, post.author.name, authorHandle].filter(Boolean).join(" ");
86+
const authorName =
87+
post.platform === "FacebookMarketplace"
88+
? truncate(escapeMarkdown(post.author.name), 250)
89+
: post.author.name;
90+
const authorHeading = [headingPrefix, authorName, authorHandle].filter(Boolean).join(" ");
8391

8492
embed.addSectionComponents((section) => {
8593
section
8694
.setThumbnailAccessory((thumbnail) => thumbnail.setURL(post.author.avatar))
8795
.addTextDisplayComponents((author) =>
8896
author.setContent(heading(authorHeading, HeadingLevel.Three)),
8997
);
98+
if (post.platform === "FacebookMarketplace" && post.price) {
99+
section.addTextDisplayComponents((price) => price.setContent(escapeMarkdown(post.price)));
100+
}
90101
if (post.text && post.text.length > 0) {
91102
section.addTextDisplayComponents((text) =>
92103
text.setContent(
@@ -121,6 +132,30 @@ function addPostComponents(embed: ContainerBuilder, post: PostData, headingPrefi
121132
if (post.media.length > 0) {
122133
embed.addMediaGalleryComponents(buildMediaEmbed(post.media)!);
123134
}
135+
if (post.platform === "FacebookMarketplace") {
136+
if (post.location) {
137+
embed.addTextDisplayComponents((location) =>
138+
location.setContent(`${escapeMarkdown(post.location)} · Location is approximate`),
139+
);
140+
}
141+
if (post.map) {
142+
embed.addMediaGalleryComponents((gallery) =>
143+
gallery.addItems({
144+
media: { url: post.map },
145+
description: (post.location
146+
? `Map showing approximate listing location in ${post.location}.`
147+
: "Map showing approximate listing location."
148+
).slice(0, 1024),
149+
}),
150+
);
151+
}
152+
embed.addTextDisplayComponents((footer) =>
153+
footer.setContent(
154+
`${time(post.timestamp, TimestampStyles.RelativeTime)}${hyperlink("View on Facebook Marketplace", post.url)}`,
155+
),
156+
);
157+
return;
158+
}
124159
embed
125160
.addSeparatorComponents((sep) => sep.setDivider(false).setSpacing(SeparatorSpacingSize.Small))
126161
.addTextDisplayComponents(
@@ -140,6 +175,7 @@ function addPostComponents(embed: ContainerBuilder, post: PostData, headingPrefi
140175
}
141176

142177
export function buildEmbed(post: PostData, flags?: Partial<EmbedFlags>) {
178+
if (post.platform === "FacebookMarketplace" && post.media.length === 0) return null;
143179
if (flags?.MediaOnly) {
144180
return buildMediaEmbed(post.media, flags?.Spoiler);
145181
}

0 commit comments

Comments
 (0)