Add import-emoji command

This commit is contained in:
silverpill 2023-01-17 00:09:09 +00:00
parent b958b8fb4c
commit e8500b982b
9 changed files with 81 additions and 9 deletions

View file

@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Save sizes of media attachments and other files to database.
- Added `import-emoji` command.
### Security

View file

@ -74,6 +74,12 @@ Delete empty remote profiles:
mitractl delete-empty-profiles 100
```
Import custom emoji from another instance:
```shell
mitractl import-emoji emojiname example.org
```
Generate ethereum address:
```shell

View file

@ -1,20 +1,16 @@
use ammonia::clean_text;
use chrono::{DateTime, NaiveDateTime, Utc};
use crate::activitypub::identifiers::{local_actor_id, local_object_id};
use crate::config::Instance;
use crate::models::posts::types::Post;
use crate::models::profiles::types::DbActorProfile;
use crate::utils::html::clean_html_all;
use crate::utils::{
datetime::get_min_datetime,
html::clean_html_all,
};
const ENTRY_TITLE_MAX_LENGTH: usize = 75;
fn get_min_datetime() -> DateTime<Utc> {
let native = NaiveDateTime::from_timestamp_opt(0, 0)
.expect("0 should be a valid argument");
DateTime::from_utc(native, Utc)
}
fn make_entry(
instance_url: &str,
post: &Post,

View file

@ -34,6 +34,7 @@ async fn main() {
SubCommand::DeleteUnusedAttachments(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::DeleteOrphanedFiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::DeleteEmptyProfiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::ImportEmoji(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::ResetSubscriptions(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::CreateMoneroWallet(cmd) => cmd.execute(&config).await.unwrap(),

View file

@ -19,7 +19,12 @@ use crate::ethereum::{
use crate::models::{
attachments::queries::delete_unused_attachments,
cleanup::find_orphaned_files,
emojis::queries::delete_emoji,
emojis::queries::{
create_emoji,
delete_emoji,
get_emoji_by_name_and_hostname,
},
emojis::validators::EMOJI_LOCAL_MAX_SIZE,
oauth::queries::delete_oauth_tokens,
posts::queries::{delete_post, find_extraneous_posts, get_post_by_id},
profiles::queries::{
@ -45,6 +50,7 @@ use crate::utils::{
generate_rsa_key,
serialize_private_key,
},
datetime::get_min_datetime,
files::remove_files,
passwords::hash_password,
};
@ -72,6 +78,7 @@ pub enum SubCommand {
DeleteUnusedAttachments(DeleteUnusedAttachments),
DeleteOrphanedFiles(DeleteOrphanedFiles),
DeleteEmptyProfiles(DeleteEmptyProfiles),
ImportEmoji(ImportEmoji),
UpdateCurrentBlock(UpdateCurrentBlock),
ResetSubscriptions(ResetSubscriptions),
CreateMoneroWallet(CreateMoneroWallet),
@ -373,6 +380,41 @@ impl DeleteEmptyProfiles {
}
}
/// Import custom emoji from another instance
#[derive(Parser)]
pub struct ImportEmoji {
emoji_name: String,
hostname: String,
}
impl ImportEmoji {
pub async fn execute(
&self,
_config: &Config,
db_client: &impl DatabaseClient,
) -> Result<(), Error> {
let emoji = get_emoji_by_name_and_hostname(
db_client,
&self.emoji_name,
&self.hostname,
).await?;
if emoji.image.file_size > EMOJI_LOCAL_MAX_SIZE {
println!("emoji is too big");
return Ok(());
};
create_emoji(
db_client,
&emoji.emoji_name,
None,
emoji.image,
None,
&get_min_datetime(),
).await?;
println!("added emoji to local collection");
Ok(())
}
}
/// Update blockchain synchronization starting block
#[derive(Parser)]
pub struct UpdateCurrentBlock {

View file

@ -76,6 +76,23 @@ pub async fn update_emoji(
Ok(emoji)
}
pub async fn get_emoji_by_name_and_hostname(
db_client: &impl DatabaseClient,
emoji_name: &str,
hostname: &str,
) -> Result<DbEmoji, DatabaseError> {
let maybe_row = db_client.query_opt(
"
SELECT emoji
FROM emoji WHERE emoji_name = $1 AND hostname = $2
",
&[&emoji_name, &hostname],
).await?;
let row = maybe_row.ok_or(DatabaseError::NotFound("emoji"))?;
let emoji = row.try_get("emoji")?;
Ok(emoji)
}
pub async fn get_emoji_by_remote_object_id(
db_client: &impl DatabaseClient,
object_id: &str,

View file

@ -4,6 +4,7 @@ use crate::errors::ValidationError;
const EMOJI_NAME_RE: &str = r"^[\w.]+$";
pub const EMOJI_MAX_SIZE: usize = 250 * 1000; // 250 kB
pub const EMOJI_LOCAL_MAX_SIZE: usize = 50 * 1000; // 50 kB
pub const EMOJI_MEDIA_TYPES: [&str; 2] = [
"image/gif",
"image/png",

7
src/utils/datetime.rs Normal file
View file

@ -0,0 +1,7 @@
use chrono::{DateTime, NaiveDateTime, Utc};
pub fn get_min_datetime() -> DateTime<Utc> {
let native = NaiveDateTime::from_timestamp_opt(0, 0)
.expect("0 should be a valid argument");
DateTime::from_utc(native, Utc)
}

View file

@ -2,6 +2,7 @@ pub mod caip2;
pub mod canonicalization;
pub mod crypto_rsa;
pub mod currencies;
pub mod datetime;
pub mod files;
pub mod html;
pub mod id;