Refactor embed module functions and fix lint error
This commit is contained in:
		
							parent
							
								
									5af13e5a07
								
							
						
					
					
						commit
						f335c0cbcf
					
				
					 2 changed files with 45 additions and 43 deletions
				
			
		| 
						 | 
				
			
			@ -124,53 +124,55 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
 | 
			
		|||
        let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap();
 | 
			
		||||
 | 
			
		||||
        for url in urls {
 | 
			
		||||
            if let Ok(req) = reqwest_client.get(url).send().await {
 | 
			
		||||
                if let Ok(res) = req.text().await {
 | 
			
		||||
                    // beware, dirty HTML parsing code
 | 
			
		||||
                    let metadata = parse_metadata(&res);
 | 
			
		||||
                    warn!("Ran fn parse_metadata after: '{:#?}'", fn_start.elapsed());
 | 
			
		||||
            match reqwest_client.get(url).send().await {
 | 
			
		||||
                Err(e) => warn!("Failed to fetch metadata for '{}' with error: '{:?}'", &url, e),
 | 
			
		||||
                Ok(req) => {
 | 
			
		||||
                    match req.text().await {
 | 
			
		||||
                        Err(e) => warn!("Failed to parse HTML for URL '{}' with error: '{:?}'", &url, e),
 | 
			
		||||
                        Ok(resp) => {
 | 
			
		||||
                            // beware, dirty HTML parsing code
 | 
			
		||||
                            let metadata = parse_metadata(&resp);
 | 
			
		||||
                            warn!("Ran fn parse_metadata after: '{:#?}'", fn_start.elapsed());
 | 
			
		||||
 | 
			
		||||
                    // Build and send our message reply
 | 
			
		||||
                    if metadata.is_some() {
 | 
			
		||||
                        let embed = metadata.unwrap();
 | 
			
		||||
                        let bot_reply = RoomMessageEventContent::text_html(
 | 
			
		||||
                            &embed.title,
 | 
			
		||||
                            format!(
 | 
			
		||||
                                "<blockquote>
 | 
			
		||||
                                <h4>{}</h4>
 | 
			
		||||
                                <p>{}</p>
 | 
			
		||||
                                </blockquote>",
 | 
			
		||||
                                &embed.title, &embed.description
 | 
			
		||||
                            ),
 | 
			
		||||
                        )
 | 
			
		||||
                        .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
 | 
			
		||||
                            // Build and send our message reply
 | 
			
		||||
                            if metadata.is_some() {
 | 
			
		||||
                                let embed = metadata.unwrap();
 | 
			
		||||
                                let bot_reply = RoomMessageEventContent::text_html(
 | 
			
		||||
                                    &embed.title,
 | 
			
		||||
                                    format!(
 | 
			
		||||
                                        "<blockquote>
 | 
			
		||||
                                        <h4>{}</h4>
 | 
			
		||||
                                        <p>{}</p>
 | 
			
		||||
                                        </blockquote>",
 | 
			
		||||
                                        &embed.title, &embed.description
 | 
			
		||||
                                    ),
 | 
			
		||||
                                )
 | 
			
		||||
                                .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
 | 
			
		||||
 | 
			
		||||
                        // Finally send the reply to the room
 | 
			
		||||
                        warn!("Sending embed for URL: '{}'", &url);
 | 
			
		||||
                        if room.send(bot_reply).await.is_err() {
 | 
			
		||||
                            warn!("Failed to send embed for URL: '{}'", &url);
 | 
			
		||||
                                // Finally send the reply to the room
 | 
			
		||||
                                warn!("Sending embed for URL: '{}'", &url);
 | 
			
		||||
                                if room.send(bot_reply).await.is_err() {
 | 
			
		||||
                                    warn!("Failed to send embed for URL: '{}'", &url);
 | 
			
		||||
                                }
 | 
			
		||||
                                warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
 | 
			
		||||
                            // If we didn't get any metadata send a generic "No metadata" response
 | 
			
		||||
                            } else {
 | 
			
		||||
                                let bot_reply = RoomMessageEventContent::text_html(
 | 
			
		||||
                                    "Couldn't parse metadata for URL",
 | 
			
		||||
                                    "<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
 | 
			
		||||
                                )
 | 
			
		||||
                                .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
 | 
			
		||||
                                // Send the reply to the room
 | 
			
		||||
                                warn!("Sending 'No metadata' embed for URL: '{}'", &url);
 | 
			
		||||
                                if room.send(bot_reply).await.is_err() {
 | 
			
		||||
                                    warn!("Failed to send embed for URL: '{}'", &url);
 | 
			
		||||
                                }
 | 
			
		||||
                                warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
 | 
			
		||||
                    // If we didn't get any metadata send a generic "No metadata" response
 | 
			
		||||
                    } else {
 | 
			
		||||
                        let bot_reply = RoomMessageEventContent::text_html(
 | 
			
		||||
                            "Couldn't parse metadata for URL",
 | 
			
		||||
                            "<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
 | 
			
		||||
                        )
 | 
			
		||||
                        .make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
 | 
			
		||||
                        // Send the reply to the room
 | 
			
		||||
                        warn!("Sending 'No metadata' embed for URL: '{}'", &url);
 | 
			
		||||
                        if room.send(bot_reply).await.is_err() {
 | 
			
		||||
                            warn!("Failed to send embed for URL: '{}'", &url);
 | 
			
		||||
                        }
 | 
			
		||||
                        warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    warn!("Failed to parse HTML for URL: '{}'", &url);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                warn!("Failed to fetch metadata for '{}'", &url);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -76,7 +76,7 @@ pub async fn delete_old_encryption_devices(client: &Client, config: &Config) ->
 | 
			
		|||
                uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()),
 | 
			
		||||
                config.password.clone(),
 | 
			
		||||
            );
 | 
			
		||||
            password.session = info.session.clone();
 | 
			
		||||
            password.session.clone_from(&info.session);
 | 
			
		||||
            client
 | 
			
		||||
                .delete_devices(&old_devices, Some(uiaa::AuthData::Password(password)))
 | 
			
		||||
                .await?;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue