Skip to content

Commit d3e1afd

Browse files
authored
Merge pull request #255 from Rustmail/212-snippets
feat(snippets): add commands for snippet feature
2 parents f166d3a + 97a3518 commit d3e1afd

27 files changed

Lines changed: 1632 additions & 18 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ config.toml
33
config.toml.backup
44
node_modules
55
.env
6-
db/
6+
/db/
77
.idea
88
.vscode/
99
package-lock.json

.sqlx/query-0d8e91df51026f5a3400697b0797c60424475d42fec68d891852cecc41e15c0a.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-2b110957c98cd482afcc93eba2762c7229a0d4b04387acf03179c7449054a58a.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-68cdb069424b9fdd6468b28d8cdf9b5c2aa49cc2bb3ac886496adb0e220bf431.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-6cf54c26731b1dd92650445bac0850548928cbfa6501788cd1c3b6d8b3fd6b19.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-8b8bfcba2e57300a0f448095a1739a46668ffc4cda6d9bec0fa1965317123c4e.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Add migration script here
2+
CREATE TABLE IF NOT EXISTS "snippets" (
3+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
4+
"key" TEXT NOT NULL UNIQUE,
5+
"content" TEXT NOT NULL,
6+
"created_by" TEXT NOT NULL,
7+
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
8+
"updated_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
9+
);
10+
11+
CREATE INDEX IF NOT EXISTS "idx_snippets_key" ON "snippets"("key");

rustmail/src/bot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub async fn run_bot(
144144
registry.register_command(TakeCommand);
145145
registry.register_command(ReleaseCommand);
146146
registry.register_command(PingCommand);
147+
registry.register_command(SnippetCommand);
147148

148149
let registry = Arc::new(registry);
149150

rustmail/src/commands/anonreply/text_command/anonreply.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,29 @@ pub async fn anonreply(
2121
.as_ref()
2222
.ok_or_else(database_connection_failed)?;
2323

24-
let content = extract_reply_content(&msg.content, &config.command.prefix, &["anonreply", "ar"]);
24+
let mut content =
25+
extract_reply_content(&msg.content, &config.command.prefix, &["anonreply", "ar"]);
26+
27+
if let Some(text) = &content {
28+
if let Some(stripped) = text.strip_prefix("{{").and_then(|s| s.strip_suffix("}}")) {
29+
let snippet_key = stripped.trim();
30+
match get_snippet_by_key(snippet_key, db_pool).await? {
31+
Some(snippet) => {
32+
content = Some(snippet.content);
33+
}
34+
None => {
35+
return Err(ModmailError::Command(CommandError::SnippetNotFound(
36+
snippet_key.to_string(),
37+
)));
38+
}
39+
}
40+
}
41+
}
42+
2543
let intent = extract_intent(content, &msg.attachments).await;
2644

2745
let Some(intent) = intent else {
28-
MessageBuilder::system_message(&ctx, config)
29-
.translated_content(
30-
"reply.missing_content",
31-
None,
32-
Some(msg.author.id),
33-
msg.guild_id.map(|g| g.get()),
34-
)
35-
.await
36-
.color(0xFF0000)
37-
.reply_to(msg.clone())
38-
.send_and_forget()
39-
.await;
40-
41-
return Err(validation_failed("Missing content"));
46+
return Err(ModmailError::Message(MessageError::MessageEmpty));
4247
};
4348

4449
let thread = fetch_thread(db_pool, &msg.channel_id.to_string()).await?;

rustmail/src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub mod release;
2828
pub mod remove_reminder;
2929
pub mod remove_staff;
3030
pub mod reply;
31+
pub mod snippet;
3132
pub mod take;
3233

3334
pub use add_reminder::*;
@@ -49,6 +50,7 @@ pub use release::*;
4950
pub use remove_reminder::*;
5051
pub use remove_staff::*;
5152
pub use reply::*;
53+
pub use snippet::*;
5254
pub use take::*;
5355

5456
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

0 commit comments

Comments
 (0)