Skip to content

Commit

Permalink
smart attachments list (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
circuitsacul authored Jan 26, 2023
1 parent e2564f5 commit 8c63326
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
26 changes: 20 additions & 6 deletions src/core/embedder/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl BuiltStarboardEmbed {
pub async fn build(
handle: &Embedder,
force_partial: bool,
files_uploaded: bool,
watermark: bool,
) -> StarboardResult<Self> {
if let MessageResult::Ok(orig) = &handle.orig_message {
Expand All @@ -54,7 +55,8 @@ impl BuiltStarboardEmbed {

let built = Self::Full(FullBuiltStarboardEmbed {
top_content: Self::build_top_content(handle),
embeds: Self::build_embeds(handle, orig, &parsed, watermark).await?,
embeds: Self::build_embeds(handle, orig, &parsed, files_uploaded, watermark)
.await?,
upload_attachments: parsed.upload_attachments,
components: Self::build_components(handle),
});
Expand Down Expand Up @@ -133,14 +135,18 @@ impl BuiltStarboardEmbed {
handle: &Embedder,
orig: &CachedMessage,
parsed: &ParsedMessage,
files_uploaded: bool,
watermark: bool,
) -> StarboardResult<Vec<Embed>> {
let mut embeds = Vec::new();

if let Some(e) = Self::build_replied_embed(handle).await? {
embeds.push(e);
}
if let Some(e) = Self::build_primary_embed(handle, orig, parsed, watermark, false).await? {
if let Some(e) =
Self::build_primary_embed(handle, orig, parsed, files_uploaded, watermark, false)
.await?
{
embeds.push(e);
}

Expand All @@ -159,13 +165,14 @@ impl BuiltStarboardEmbed {
Some(msg) => msg,
};
let reply_parsed = ParsedMessage::parse(ref_msg);
Self::build_primary_embed(handle, ref_msg, &reply_parsed, false, true).await
Self::build_primary_embed(handle, ref_msg, &reply_parsed, false, false, true).await
}

pub async fn build_primary_embed(
handle: &Embedder,
orig: &CachedMessage,
parsed: &ParsedMessage,
files_uploaded: bool,
watermark: bool,
is_reply: bool,
) -> StarboardResult<Option<Embed>> {
Expand Down Expand Up @@ -288,11 +295,18 @@ impl BuiltStarboardEmbed {
}

// attachments list
if (handle.config.resolved.attachments_list || is_reply) && !parsed.url_list.is_empty() {
let mut urls = Vec::<&str>::new();
if !files_uploaded || is_reply {
urls.extend(parsed.urls.uploaded.iter().map(|url| url.as_str()));
}
if !handle.config.resolved.extra_embeds || is_reply {
urls.extend(parsed.urls.embedded.iter().map(|url| url.as_str()));
}

if (handle.config.resolved.attachments_list || is_reply) && !urls.is_empty() {
let mut field = String::new();
let url_list = parsed.url_list.iter();

for next in url_list {
for next in urls {
if field.len() + next.len() + 10 > 1_024 {
field.push_str("...");
break;
Expand Down
10 changes: 7 additions & 3 deletions src/core/embedder/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ impl Embedder {
pub async fn build(
&self,
force_partial: bool,
files_uploaded: bool,
watermark: bool,
) -> StarboardResult<BuiltStarboardEmbed> {
BuiltStarboardEmbed::build(self, force_partial, watermark).await
BuiltStarboardEmbed::build(self, force_partial, files_uploaded, watermark).await
}

pub async fn send(
Expand All @@ -44,7 +45,7 @@ impl Embedder {
let is_prem = is_guild_premium(bot, self.config.starboard.guild_id).await?;

let built = match self
.build(false, self.config.resolved.use_webhook && !is_prem)
.build(false, is_prem, self.config.resolved.use_webhook && !is_prem)
.await?
{
BuiltStarboardEmbed::Full(built) => built,
Expand Down Expand Up @@ -203,7 +204,10 @@ impl Embedder {

let is_prem = is_guild_premium(bot, self.config.starboard.guild_id).await?;

match self.build(force_partial, wh.is_some() && !is_prem).await? {
match self
.build(force_partial, is_prem, wh.is_some() && !is_prem)
.await?
{
BuiltStarboardEmbed::Full(built) => {
if let Some(wh) = wh {
let mut ud = bot
Expand Down
21 changes: 14 additions & 7 deletions src/core/embedder/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ use super::{

pub type StickerNames = String;
pub type PrimaryImage = ImageSource;
pub type UrlList = Vec<String>;
pub type Embeds = Vec<Embed>;
pub type UploadAttachments = Vec<AttachmentHandle>;

#[derive(Default)]
pub struct AttachmentUrls {
pub embedded: Vec<String>,
pub uploaded: Vec<String>,
}

pub struct ParsedMessage {
pub sticker_names_str: Option<String>,
// attachments
pub url_list: Vec<String>,
pub urls: AttachmentUrls,
pub primary_image: Option<ImageSource>,
pub embeds: Vec<Embed>,
pub upload_attachments: Vec<AttachmentHandle>,
Expand All @@ -36,7 +41,7 @@ impl ParsedMessage {
Self {
sticker_names_str,
primary_image,
url_list,
urls: url_list,
embeds,
upload_attachments,
}
Expand All @@ -47,29 +52,31 @@ impl ParsedMessage {
) -> (
Option<StickerNames>,
Option<PrimaryImage>,
UrlList,
AttachmentUrls,
Embeds,
UploadAttachments,
) {
let mut primary_image = None;
let mut embeds = Vec::new();
let mut upload_attachments = Vec::new();
let mut url_list = Vec::new();
let mut urls = AttachmentUrls::default();

for attachment in &orig.attachments {
let handle = AttachmentHandle::from_attachment(attachment);
url_list.push(handle.url_list_item());

if primary_image.is_none() {
if let Some(image) = handle.embedable_image() {
urls.embedded.push(handle.url_list_item());
primary_image.replace(image);
continue;
}
} else if let Some(embed) = handle.as_embed() {
urls.embedded.push(handle.url_list_item());
embeds.push(embed);
continue;
}

urls.uploaded.push(handle.url_list_item());
upload_attachments.push(handle);
}

Expand Down Expand Up @@ -173,7 +180,7 @@ impl ParsedMessage {
(
sticker_names_str,
primary_image,
url_list,
urls,
embeds,
upload_attachments,
)
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/moststarred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async fn scrolling_paginator(
continue;
};

let built = embedder.build(false, false).await?;
let built = embedder.build(false, false, false).await?;
let BuiltStarboardEmbed::Full(built) = built else {
unreachable!("didn't get full embed");
};
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl RandomPost {
.await?
.unwrap();

let built = embedder.build(false, false).await?;
let built = embedder.build(false, false, false).await?;
let built = match built {
BuiltStarboardEmbed::Partial(_) => unreachable!("didn't get full embed"),
BuiltStarboardEmbed::Full(built) => built,
Expand Down

0 comments on commit 8c63326

Please sign in to comment.