1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use serde::{Serialize, Deserialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
pub use buckets_core::model::*;
#[derive(Serialize, Deserialize)]
pub struct CustomEmoji {
pub id: usize,
pub created: usize,
pub owner: usize,
pub community: usize,
pub upload_id: usize,
pub name: String,
pub is_animated: bool,
}
pub type EmojiParserResult = Vec<(String, usize, String)>;
impl CustomEmoji {
/// Create a new [`CustomEmoji`].
pub fn new(
owner: usize,
community: usize,
upload_id: usize,
name: String,
is_animated: bool,
) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
owner,
community,
upload_id,
name,
is_animated,
}
}
/// Replace emojis in the given input string.
pub fn replace(input: &str) -> String {
let res = Self::parse(input);
let mut out = input.to_string();
for emoji in res {
if emoji.1 == 0 {
out = out.replace(
&emoji.0,
match emoji.2.as_str() {
"100" => "💯",
"thumbs_up" => "👍",
"thumbs_down" => "👎",
_ => match emojis::get_by_shortcode(&emoji.2) {
Some(e) => e.as_str(),
None => &emoji.0,
},
},
);
} else {
out = out.replace(
&emoji.0,
&format!(
"<img class=\"emoji\" src=\"/api/v1/communities/{}/emojis/{}\" />",
emoji.1, emoji.2
),
);
}
}
out
}
/// Parse text for emojis.
///
/// Another "great" parser, just like the mentions parser.
///
/// # Returns
/// `(capture, community id, emoji name)`
pub fn parse(input: &str) -> EmojiParserResult {
let mut out = Vec::new();
let mut buffer: String = String::new();
let mut escape: bool = false;
let mut in_emoji: bool = false;
let mut chars = input.chars();
while let Some(char) = chars.next() {
if char == '\\' && !escape {
escape = true;
continue;
} else if char == ':' && !escape {
let mut community_id: String = String::new();
let mut accepting_community_id_chars: bool = true;
let mut emoji_name: String = String::new();
for (char_count, char) in (0_u32..).zip(chars.by_ref()) {
if (char == ':') | (char == ' ') {
in_emoji = false;
break;
}
if char.is_ascii_digit() && accepting_community_id_chars {
community_id.push(char);
} else if char == '.' {
// the period closes the community id
accepting_community_id_chars = false;
} else {
emoji_name.push(char);
}
if char_count >= 4 && community_id.is_empty() {
accepting_community_id_chars = false;
}
}
out.push((
format!(
":{}{emoji_name}:",
if !community_id.is_empty() {
format!("{community_id}.")
} else {
String::new()
}
),
community_id.parse::<usize>().unwrap_or(0),
emoji_name,
));
continue;
} else if in_emoji {
buffer.push(char);
}
escape = false;
}
out
}
}