Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Add import-emoji command
Browse files Browse the repository at this point in the history
  • Loading branch information
silverpill committed Jan 21, 2023
1 parent b958b8f commit e8500b9
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions docs/mitractl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions src/atom/feeds.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/bin/mitractl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
44 changes: 43 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -45,6 +50,7 @@ use crate::utils::{
generate_rsa_key,
serialize_private_key,
},
datetime::get_min_datetime,
files::remove_files,
passwords::hash_password,
};
Expand Down Expand Up @@ -72,6 +78,7 @@ pub enum SubCommand {
DeleteUnusedAttachments(DeleteUnusedAttachments),
DeleteOrphanedFiles(DeleteOrphanedFiles),
DeleteEmptyProfiles(DeleteEmptyProfiles),
ImportEmoji(ImportEmoji),
UpdateCurrentBlock(UpdateCurrentBlock),
ResetSubscriptions(ResetSubscriptions),
CreateMoneroWallet(CreateMoneroWallet),
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions src/models/emojis/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/models/emojis/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions src/utils/datetime.rs
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e8500b9

Please sign in to comment.