Skip to content
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

Adding admin purging of DB items and pictures. #904 #1331 #1809

Merged
merged 17 commits into from
Jun 13, 2022
Merged

Conversation

dessalines
Copy link
Member

No description provided.

crates/api/src/site.rs Outdated Show resolved Hide resolved
@dessalines dessalines changed the title First pass at adding admin purge. #904 #1331 Adding admin purging of DB items and pictures. #904 #1331 Jun 1, 2022
@dessalines
Copy link
Member Author

This is getting close, I just need to build out the front end for it.

/// Set a custom pictrs API key. ( Required for deleting images )
#[default("API_KEY")]
#[doku(example = "API_KEY")]
pub api_key: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should put an actual (testing) api key here, to show how it looks. And not specify any default i guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the default, but I'm not sure what would be better than just API_KEY as a test.

Once this is merged, in lemmy-ansible, I plan to generate this key in the same way the postgres password is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not meant to be a test, but an example, so it should look similar to a real value. With API_KEY, people might think that they need an env var for this.

/// Address where pictrs is available (for image hosting)
#[default("http://pictrs:8080")]
#[doku(example = "http://localhost:8080")]
pub url: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its enough to specify only default in this case.

struct PictrsPurgeParams {
file: Option<String>,
alias: Option<String>,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to make this work a bit more cleanly using enum.

https://serde.rs/enum-representations.html

Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
}

fn pictrs_config(settings: Settings) -> Result<PictrsConfig, LemmyError> {
Copy link
Member

@Nutomic Nutomic Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this method into impl settings, and then probably make the member private.

.set(form)
.get_result::<Self>(conn)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to use some macro to generate these crud methods, and avoid all the copy-paste. For example https://github.com/shssoichiro/diesel-derives-extra (but looks unmaintained). Anyway, not necessarily in this pr.

actor.id,
context.pool(),
&context.settings(),
context.client(),
Copy link
Member

@Nutomic Nutomic Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, just pass in context. I would start passing in context directly as soon as there are 2 or more params from it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that'd require lemmy_utils pulling in lemmy_websocket, since that's where LemmyContext lives.

blocked_person.id,
context.pool(),
&context.settings(),
context.client(),
Copy link
Member

@Nutomic Nutomic Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pass in context.

@@ -505,13 +506,99 @@ pub async fn check_private_instance_and_federation_enabled(
Ok(())
}

pub async fn remove_user_data(banned_person_id: PersonId, pool: &DbPool) -> Result<(), LemmyError> {
pub async fn purge_image_posts_for_person(
Copy link
Member

@Nutomic Nutomic Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is already quite big, so please put all the purge/remove related stuff into a new file. Or turn utils into a folder with multiple files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do that in a separate PR, I'll make an issue for it. Utils touches almost every file so that'd make this PR huge.

"Could not purge Pictrs image with incorrect domain: {}",
image_url.as_str()
)));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check necessary? If something is wrong, pictrs will return an error on its own. But if someone changes the instance url, this would prevent image purges for no reason.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're probably right, it's not necessary.

settings: &Settings,
image_url: &Url,
) -> Result<(), LemmyError> {
if let Some(pictrs_config) = settings.pictrs_config.to_owned() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the pictrs_config function you defined in lemmy_routes (after moving it to lemmy_utils).


let post_id = comment.post_id;

// TODO read comments for pictrs images and purge them
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is about inline images in comments, right? And similarly for inline images in post body, community/user profile. Those are generally tricky to manage. Easiest option would be not to permit inline images, otherwise we will have to add some functionality to retrieve them sooner or later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. We could have something that scans through every comment, post body, bio, community sidebar... ( there's probably a few others I'm not thinking of), does a regex search for pictrs URLs, and tries to remove them. IMO that's a little excessive, but it might be necessary later on. I'll just leave that TODO in there to remind us.

Vec::new(),
Vec::new(),
Vec::new(),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work with simply Default::Default()

@dessalines dessalines requested a review from Nutomic June 3, 2022 17:15
@dessalines dessalines marked this pull request as ready for review June 3, 2022 17:15
@dessalines
Copy link
Member Author

I have the front end for this done also, everything works.

@dessalines dessalines marked this pull request as draft June 3, 2022 18:02
@dessalines
Copy link
Member Author

Actually something in the merge from main broke image uploads, I'm tracing it down.

@dessalines
Copy link
Member Author

@Nutomic This line breaks pictrs functionality. All uploads and such return the error:

Middleware error: Request object is not clonable. Are you passing a streaming body?

I think the only option is for me to create a separate pictrs reqwest client without that middleware.

@dessalines dessalines marked this pull request as ready for review June 3, 2022 23:12
@Nutomic
Copy link
Member

Nutomic commented Jun 6, 2022

Yes it should be pretty straightforward to define a Lazy<Client> in images.rs. Also we should open an issue in the upstream repo to get this fixed.

https://github.com/TrueLayer/reqwest-middleware

@dessalines
Copy link
Member Author

Yes it should be pretty straightforward to define a Lazy<Client> in images.rs. Also we should open an issue in the upstream repo to get this fixed.

I did this in main.rs, a special pictrs client. That way it can be reused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants