-
-
Notifications
You must be signed in to change notification settings - Fork 884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow empty string to clear URL-type DB fields. #4780
Changes from 4 commits
013e392
9eb5d34
c6579d8
9b4b253
8721985
e6bc8f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -340,15 +340,15 @@ async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Lemm | |||||
|
||||||
/// When adding a new avatar, banner or similar image, delete the old one. | ||||||
pub async fn replace_image( | ||||||
new_image: &Option<String>, | ||||||
new_image: &Option<Option<DbUrl>>, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as here. |
||||||
old_image: &Option<DbUrl>, | ||||||
context: &Data<LemmyContext>, | ||||||
) -> LemmyResult<()> { | ||||||
if let (Some(new_image), Some(old_image)) = (new_image, old_image) { | ||||||
if let (Some(Some(new_image)), Some(old_image)) = (new_image, old_image) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the following should work:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might work, but seems less clear to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair. Personally, I find this a bit easier than needing to parse nested |
||||||
// Note: Oftentimes front ends will include the current image in the form. | ||||||
// In this case, deleting `old_image` would also be deletion of `new_image`, | ||||||
// so the deletion must be skipped for the image to be kept. | ||||||
if new_image != old_image.as_str() { | ||||||
if new_image != old_image { | ||||||
// Ignore errors because image may be stored externally. | ||||||
let image = LocalImage::delete_by_url(&mut context.pool(), old_image) | ||||||
.await | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1004,26 +1004,25 @@ pub(crate) async fn proxy_image_link(link: Url, context: &LemmyContext) -> Lemmy | |
} | ||
|
||
pub async fn proxy_image_link_opt_api( | ||
link: &Option<String>, | ||
link: Option<Option<DbUrl>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything preventing us from using a single layer of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These ones I left as helper functions, because of the internal async call, and not being able to use async map. The one right below this uses the single option version. |
||
context: &LemmyContext, | ||
) -> LemmyResult<Option<Option<DbUrl>>> { | ||
proxy_image_link_api(link, context).await.map(Some) | ||
if let Some(Some(link)) = link { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried, but at least for this one, it would've forced me to use a clone and deref with it, in this case its cleaner to just do |
||
proxy_image_link(link.into(), context) | ||
.await | ||
.map(Some) | ||
.map(Some) | ||
} else { | ||
Ok(link) | ||
} | ||
} | ||
|
||
pub async fn proxy_image_link_api( | ||
link: &Option<String>, | ||
link: Option<DbUrl>, | ||
context: &LemmyContext, | ||
) -> LemmyResult<Option<DbUrl>> { | ||
let link: Option<DbUrl> = match link.as_ref().map(String::as_str) { | ||
// An empty string is an erase | ||
Some("") => None, | ||
Some(str_url) => Url::parse(str_url) | ||
.map(|u| Some(u.into())) | ||
.with_lemmy_type(LemmyErrorType::InvalidUrl)?, | ||
None => None, | ||
}; | ||
if let Some(l) = link { | ||
proxy_image_link(l.into(), context).await.map(Some) | ||
if let Some(link) = link { | ||
proxy_image_link(link.into(), context).await.map(Some) | ||
} else { | ||
Ok(link) | ||
} | ||
|
@@ -1130,29 +1129,4 @@ mod tests { | |
.is_ok() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
#[serial] | ||
async fn test_diesel_option_overwrite_to_url() { | ||
let context = LemmyContext::init_test_context().await; | ||
|
||
assert!(matches!( | ||
proxy_image_link_api(&None, &context).await, | ||
Ok(None) | ||
)); | ||
assert!(matches!( | ||
proxy_image_link_opt_api(&Some(String::new()), &context).await, | ||
Ok(Some(None)) | ||
)); | ||
assert!( | ||
proxy_image_link_opt_api(&Some("invalid_url".to_string()), &context) | ||
.await | ||
.is_err() | ||
); | ||
let example_url = "https://lemmy-alpha/image.png"; | ||
assert!(matches!( | ||
proxy_image_link_opt_api(&Some(example_url.to_string()), &context).await, | ||
Ok(Some(Some(url))) if url == Url::parse(example_url).unwrap().into() | ||
)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test here to make sure that it isn't clearing the avatar, when that field is missing.