add default reaction

This commit is contained in:
naskya 2024-06-27 09:23:18 +09:00
parent c3377cb076
commit 81f4fd2976
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
6 changed files with 68 additions and 4 deletions

34
Cargo.lock generated
View file

@ -460,6 +460,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "emojis"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f619a926616ae7149a0d82610b051134a0d6c4ae2962d990c06c847a445c5d9"
dependencies = [
"phf",
]
[[package]] [[package]]
name = "enum-iterator" name = "enum-iterator"
version = "2.1.0" version = "2.1.0"
@ -526,6 +535,7 @@ dependencies = [
"chrono", "chrono",
"clap", "clap",
"color-print", "color-print",
"emojis",
"enum-iterator", "enum-iterator",
"macros", "macros",
"redis", "redis",
@ -1104,6 +1114,24 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "phf"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_shared"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.14" version = "0.2.14"
@ -1444,6 +1472,12 @@ dependencies = [
"rand_core", "rand_core",
] ]
[[package]]
name = "siphasher"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.9" version = "0.4.9"

View file

@ -8,6 +8,7 @@ macros = { path = "macros" }
chrono = "0.4" chrono = "0.4"
clap = "4.5" clap = "4.5"
color-print = "0.3" color-print = "0.3"
emojis = "0.6"
enum-iterator = "2.1" enum-iterator = "2.1"
quote = "1.0" quote = "1.0"
redis = "0.25" redis = "0.25"

View file

@ -12,6 +12,7 @@ macros = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
clap = { workspace = true, features = ["derive"] } clap = { workspace = true, features = ["derive"] }
color-print = { workspace = true } color-print = { workspace = true }
emojis = { workspace = true }
enum-iterator = { workspace = true } enum-iterator = { workspace = true }
redis = { workspace = true, features = ["tokio-comp"] } redis = { workspace = true, features = ["tokio-comp"] }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }

View file

@ -363,13 +363,14 @@ fn create_new_server_config(
let mut server_config = server::Config { let mut server_config = server::Config {
config_revision: Revision::V1, config_revision: Revision::V1,
info: Some(server::Info { custom: Some(server::Custom {
name: meta.name.to_owned(), name: meta.name.to_owned(),
description: meta.description.to_owned(), description: meta.description.to_owned(),
maintainer_name: meta.maintainer_name.to_owned(), maintainer_name: meta.maintainer_name.to_owned(),
contact_info: meta.maintainer_email.to_owned(), contact_info: meta.maintainer_email.to_owned(),
open_registrations: !meta.disable_registration, open_registrations: !meta.disable_registration,
repository_url, repository_url,
default_reaction: meta.default_reaction.to_owned(),
}), }),
timeline: Some(server::Timeline { timeline: Some(server::Timeline {
local: !meta.disable_local_timeline, local: !meta.disable_local_timeline,

View file

@ -7,7 +7,7 @@
use crate::config::{ensure_latest_revision, Revision}; use crate::config::{ensure_latest_revision, Revision};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use validator::Validate; use validator::{Validate, ValidationError};
#[derive(Deserialize, Serialize, Validate, Debug, Clone)] #[derive(Deserialize, Serialize, Validate, Debug, Clone)]
pub struct Config { pub struct Config {
@ -16,7 +16,7 @@ pub struct Config {
#[validate(nested)] #[validate(nested)]
pub network: Network, pub network: Network,
#[validate(nested)] #[validate(nested)]
pub info: Option<Info>, pub custom: Option<Custom>,
#[validate(nested)] #[validate(nested)]
pub timeline: Option<Timeline>, pub timeline: Option<Timeline>,
#[validate(nested)] #[validate(nested)]
@ -63,7 +63,7 @@ pub struct Proxy {
} }
#[derive(Deserialize, Serialize, Validate, Debug, Clone)] #[derive(Deserialize, Serialize, Validate, Debug, Clone)]
pub struct Info { pub struct Custom {
/// Server name /// Server name
pub name: Option<String>, pub name: Option<String>,
/// Server description /// Server description
@ -76,6 +76,9 @@ pub struct Info {
pub open_registrations: bool, pub open_registrations: bool,
/// Repository URL /// Repository URL
pub repository_url: Option<String>, pub repository_url: Option<String>,
#[validate(custom(function = "validate_emoji"))]
/// Default reaction emoji
pub default_reaction: String,
} }
#[derive(Deserialize, Serialize, Validate, Debug, Clone)] #[derive(Deserialize, Serialize, Validate, Debug, Clone)]
@ -157,3 +160,26 @@ pub enum Captcha {
#[serde(rename = "reCAPTCHA")] #[serde(rename = "reCAPTCHA")]
ReCaptcha, ReCaptcha,
} }
fn validate_emoji(value: &str) -> Result<(), ValidationError> {
let error = Err(ValidationError::new("not a Unicode emoji or :emoji_code:"));
// check if `value` is a Unicode emoji
if emojis::get(value).is_some() {
return Ok(());
}
// check if `value` is a custom emoji
// "+", "-", "_" are allowed in emoji shortcodes in Misskey variants
// ref: <https://github.com/misskey-dev/mfm.js/blob/6aaf68089023c6adebe44123eebbc4dcd75955e0/docs/syntax.md?plain=1#L583>
if value.len() > 2 {
if value[1..value.len() - 1]
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '_')
{
return Ok(());
}
}
error
}

View file

@ -0,0 +1 @@