pub async fn add_user_to_blacklist(
collection: &Collection<User>,
user_id: &str,
) -> Result<()>Expand description
⚙️ Adds a user to the blacklist.
This asynchronous function inserts a new document into a MongoDB collection to blacklist a user.
The document includes the user_id, username, and a timestamp indicating when the user was added to the blacklist.
§Parameters:
collection: A reference to a MongoDB collection ofBlackListdocuments.user_id: A string slice representing the ID of the user to blacklist.username: A string slice representing the username of the user to blacklist.
§Returns:
Result<(), mongodb::error::Error>: ReturnsOk(())if the user is successfully blacklisted. On failure, returns anErrorfrom MongoDB detailing the issue.
§Example:
add_user_to_blacklist(&collection, "123456789012345678", "JohnDoe").await?;
println!("User has been blacklisted.");§⚠️ Notes:
- The function creates a new
BlackListentry with the provideduser_idandusername. - The
created_atfield is set to the current timestamp in seconds since the UNIX epoch.
§Related Structures:
BlackList: Represents the structure of a blacklisted user in the database.
§Dependencies:
- Requires a MongoDB collection containing
BlackListdocuments. - Uses the
chronocrate to generate a timestamp for thecreated_atfield.