Skip to main content

nexus_watcher/events/
moderation.rs

1use std::path::PathBuf;
2
3use crate::events::handlers;
4use nexus_common::types::DynError;
5use pubky_app_specs::{ParsedUri, PubkyAppTag, PubkyId, Resource};
6use tracing::info;
7
8pub struct Moderation {
9    // Moderator trusted user id
10    pub id: PubkyId,
11    // Tags to be moderated (tagged content is deleted)
12    pub tags: Vec<String>,
13}
14
15impl Moderation {
16    pub async fn should_delete(&self, tag: &PubkyAppTag, tagger_id: PubkyId) -> bool {
17        tagger_id == self.id && self.tags.contains(&tag.label)
18    }
19
20    pub async fn apply_moderation(
21        moderator_tag: PubkyAppTag,
22        files_path: PathBuf,
23    ) -> Result<(), DynError> {
24        // Parse the embeded URI to extract author_id and post_id using parse_tagged_post_uri
25        let parsed_uri = ParsedUri::try_from(moderator_tag.uri.as_str())?;
26        let user_id = parsed_uri.user_id;
27
28        match parsed_uri.resource {
29            Resource::Post(post_id) => {
30                // Delete the post and return the result
31                info!(
32                    "Moderation tag '{}' detected. Deleting post {}:{}",
33                    moderator_tag.label, user_id, post_id
34                );
35                handlers::post::sync_del(user_id, post_id).await
36            }
37            Resource::Tag(tag_id) => {
38                // Delete the tag and return the result
39                info!(
40                    "Moderation tag '{}' detected. Deleting tag {}:{}",
41                    moderator_tag.label, user_id, tag_id
42                );
43                handlers::tag::del(user_id, tag_id).await
44            }
45            Resource::User => {
46                // Delete the user profile and return the result
47                info!(
48                    "Moderation tag '{}' detected. Deleting user profile {}",
49                    moderator_tag.label, user_id
50                );
51                handlers::user::del(user_id).await
52            }
53            Resource::File(file_id) => {
54                // Delete the file and return the result
55                info!(
56                    "Moderation tag '{}' detected. Deleting file {}:{}",
57                    moderator_tag.label, user_id, file_id
58                );
59                handlers::file::del(&user_id, file_id, files_path).await
60            }
61            _ => Ok(()),
62        }
63    }
64}