|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use linkify::{Link, LinkFinder, LinkKind}; |
| 4 | +use twilight_http::Client; |
| 5 | +use twilight_model::{ |
| 6 | + application::{ |
| 7 | + command::CommandType, |
| 8 | + interaction::{application_command::CommandOptionValue, InteractionData}, |
| 9 | + }, |
| 10 | + channel::message::MessageFlags, |
| 11 | + gateway::payload::incoming::InteractionCreate, |
| 12 | + http::interaction::{InteractionResponse, InteractionResponseType}, |
| 13 | + id::{marker::ApplicationMarker, Id}, |
| 14 | +}; |
| 15 | +use twilight_util::builder::InteractionResponseDataBuilder; |
| 16 | +use url::Url; |
| 17 | + |
| 18 | +pub async fn handle_fix_twitter_link( |
| 19 | + interaction: &InteractionCreate, |
| 20 | + application_id: Id<ApplicationMarker>, |
| 21 | + http_client: Arc<Client>, |
| 22 | +) -> Result<(), anyhow::Error> { |
| 23 | + let (original_message, twitter_links) = parse_twitter_links(interaction); |
| 24 | + let interaction_client = http_client.interaction(application_id); |
| 25 | + |
| 26 | + if twitter_links.is_empty() { |
| 27 | + let interaction_response_data = InteractionResponseDataBuilder::new() |
| 28 | + .content("Sorry, there are no Twitter links in this message.") |
| 29 | + .flags(MessageFlags::EPHEMERAL) |
| 30 | + .build(); |
| 31 | + interaction_client |
| 32 | + .create_response( |
| 33 | + interaction.id, |
| 34 | + &interaction.token, |
| 35 | + &InteractionResponse { |
| 36 | + kind: InteractionResponseType::ChannelMessageWithSource, |
| 37 | + data: Some(interaction_response_data), |
| 38 | + }, |
| 39 | + ) |
| 40 | + .await?; |
| 41 | + return Ok(()); |
| 42 | + } else { |
| 43 | + let interaction_response_data = InteractionResponseDataBuilder::new() |
| 44 | + .content("Fixing Twitter links...") |
| 45 | + .build(); |
| 46 | + interaction_client |
| 47 | + .create_response( |
| 48 | + interaction.id, |
| 49 | + &interaction.token, |
| 50 | + &InteractionResponse { |
| 51 | + kind: InteractionResponseType::DeferredChannelMessageWithSource, |
| 52 | + data: Some(interaction_response_data), |
| 53 | + }, |
| 54 | + ) |
| 55 | + .await?; |
| 56 | + } |
| 57 | + |
| 58 | + let new_message_content = fix_twitter_links_in_place(original_message.clone(), twitter_links); |
| 59 | + |
| 60 | + interaction_client |
| 61 | + .update_response(&interaction.token) |
| 62 | + .content(Some(&new_message_content))? |
| 63 | + .await?; |
| 64 | + |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +/// Returns (message_content, link_information). Does not handle more than one message. |
| 69 | +fn parse_twitter_links(interaction: &InteractionCreate) -> (String, Vec<(Url, Link)>) { |
| 70 | + let Some(message_content) = message_content_from_interaction(interaction) else { |
| 71 | + return (String::new(), Vec::new()); |
| 72 | + }; |
| 73 | + let parsed_link_information = parse_twitter_links_inner(message_content); |
| 74 | + |
| 75 | + (message_content.to_string(), parsed_link_information) |
| 76 | +} |
| 77 | + |
| 78 | +// We need to handle interactions coming from both slash commands and message |
| 79 | +// commands. Those require some different plumbing to get to the input message, |
| 80 | +// which we take care of here. |
| 81 | +fn message_content_from_interaction(interaction: &InteractionCreate) -> Option<&str> { |
| 82 | + let Some(InteractionData::ApplicationCommand(command_data)) = &interaction.data else { |
| 83 | + return None; |
| 84 | + }; |
| 85 | + |
| 86 | + match command_data.kind { |
| 87 | + CommandType::ChatInput => command_data |
| 88 | + .options |
| 89 | + .iter() |
| 90 | + .next() |
| 91 | + .map(|opt| &opt.value) |
| 92 | + .and_then(|value| match value { |
| 93 | + CommandOptionValue::String(value) => Some(value.as_str()), |
| 94 | + _ => None, |
| 95 | + }), |
| 96 | + CommandType::Message => command_data |
| 97 | + .resolved |
| 98 | + .as_ref() |
| 99 | + .and_then(|resolved_command_data| { |
| 100 | + resolved_command_data |
| 101 | + .messages |
| 102 | + .iter() |
| 103 | + .next() |
| 104 | + .map(|(_, m)| m) |
| 105 | + .map(|m| m.content.as_str()) |
| 106 | + }), |
| 107 | + _ => None, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +fn parse_twitter_links_inner(message_content: &str) -> Vec<(Url, Link)> { |
| 112 | + let finder = LinkFinder::new(); |
| 113 | + let links_iterator = finder |
| 114 | + .links(message_content) |
| 115 | + .filter(|l| l.kind() == &LinkKind::Url) |
| 116 | + .filter_map(|l| Url::parse(l.as_str()).ok().zip(Some(l))) |
| 117 | + .filter(|(u, _)| { |
| 118 | + if let Some(host_str) = u.host_str() { |
| 119 | + host_str == "twitter.com" |
| 120 | + || host_str == "mobile.twitter.com" |
| 121 | + || host_str == "x.com" |
| 122 | + || host_str == "mobile.x.com" |
| 123 | + } else { |
| 124 | + false |
| 125 | + } |
| 126 | + }); |
| 127 | + links_iterator.collect() |
| 128 | +} |
| 129 | + |
| 130 | +fn fix_twitter_links_in_place( |
| 131 | + original_message: String, |
| 132 | + twitter_links: Vec<(Url, Link<'_>)>, |
| 133 | +) -> String { |
| 134 | + let mut new_message_content = original_message; |
| 135 | + |
| 136 | + for (mut parsed_url, link_info) in twitter_links.into_iter().rev() { |
| 137 | + let Some(host) = parsed_url.host_str() else { |
| 138 | + continue; |
| 139 | + }; |
| 140 | + |
| 141 | + let new_host = match host { |
| 142 | + "twitter.com" => "fxtwitter.com", |
| 143 | + "mobile.twitter.com" => "fxtwitter.com", |
| 144 | + "x.com" => "fixupx.com", |
| 145 | + "mobile.x.com" => "fixupx.com", |
| 146 | + _ => "fxtwitter.com", |
| 147 | + }; |
| 148 | + if let Err(_) = parsed_url.set_host(Some(new_host)) { |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + new_message_content.replace_range(link_info.start()..link_info.end(), parsed_url.as_str()); |
| 153 | + } |
| 154 | + new_message_content |
| 155 | +} |
| 156 | + |
| 157 | +#[cfg(test)] |
| 158 | +mod tests { |
| 159 | + use super::*; |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn test_fix_twitter_links_in_place() { |
| 163 | + let original_message = r#" |
| 164 | + This is a message that contains a twitter link (https://twitter.com/test/test), a |
| 165 | + x.com link (https://x.com/test/test), a mobile.twitter.com link |
| 166 | + (https://mobile.twitter.com/test/test), a mobile.x.com link |
| 167 | + (https://mobile.x.com/test/test), an unrelated link |
| 168 | + (https://otherwebsite/test/test), and a weird twitter link |
| 169 | + (https://weird.link.twitter.com/test/test). |
| 170 | + "#; |
| 171 | + let twitter_links = parse_twitter_links_inner(&original_message); |
| 172 | + |
| 173 | + let new_message = fix_twitter_links_in_place(original_message.to_string(), twitter_links); |
| 174 | + assert_eq!( |
| 175 | + new_message, |
| 176 | + r#" |
| 177 | + This is a message that contains a twitter link (https://fxtwitter.com/test/test), a |
| 178 | + x.com link (https://fixupx.com/test/test), a mobile.twitter.com link |
| 179 | + (https://fxtwitter.com/test/test), a mobile.x.com link |
| 180 | + (https://fixupx.com/test/test), an unrelated link |
| 181 | + (https://otherwebsite/test/test), and a weird twitter link |
| 182 | + (https://weird.link.twitter.com/test/test). |
| 183 | + "# |
| 184 | + ); |
| 185 | + } |
| 186 | +} |
0 commit comments