24
Cargo.lock
generated
24
Cargo.lock
generated
@@ -27,6 +27,17 @@ version = "1.0.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ahash"
|
||||||
|
version = "0.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom 0.2.3",
|
||||||
|
"once_cell",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
version = "0.7.18"
|
version = "0.7.18"
|
||||||
@@ -1180,6 +1191,9 @@ name = "hashbrown"
|
|||||||
version = "0.11.2"
|
version = "0.11.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
|
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
@@ -1600,6 +1614,15 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lru"
|
||||||
|
version = "0.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6c748cfe47cb8da225c37595b3108bea1c198c84aaae8ea0ba76d01dda9fc803"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lru-cache"
|
name = "lru-cache"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
@@ -2786,6 +2809,7 @@ dependencies = [
|
|||||||
"lettre",
|
"lettre",
|
||||||
"linkify",
|
"linkify",
|
||||||
"log",
|
"log",
|
||||||
|
"lru",
|
||||||
"many-to-many",
|
"many-to-many",
|
||||||
"mobc",
|
"mobc",
|
||||||
"mobc-redis",
|
"mobc-redis",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# Utility
|
# Utility
|
||||||
|
lru = "0.7.0"
|
||||||
url = "2.2.2"
|
url = "2.2.2"
|
||||||
log = "0.4.11"
|
log = "0.4.11"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ pub enum Channel {
|
|||||||
Group {
|
Group {
|
||||||
#[serde(rename = "_id")]
|
#[serde(rename = "_id")]
|
||||||
id: String,
|
id: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
nonce: Option<String>,
|
|
||||||
|
|
||||||
name: String,
|
name: String,
|
||||||
owner: String,
|
owner: String,
|
||||||
@@ -57,8 +55,6 @@ pub enum Channel {
|
|||||||
#[serde(rename = "_id")]
|
#[serde(rename = "_id")]
|
||||||
id: String,
|
id: String,
|
||||||
server: String,
|
server: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
nonce: Option<String>,
|
|
||||||
|
|
||||||
name: String,
|
name: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
@@ -81,8 +77,6 @@ pub enum Channel {
|
|||||||
#[serde(rename = "_id")]
|
#[serde(rename = "_id")]
|
||||||
id: String,
|
id: String,
|
||||||
server: String,
|
server: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
nonce: Option<String>,
|
|
||||||
|
|
||||||
name: String,
|
name: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
|||||||
@@ -181,6 +181,20 @@ impl Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn publish(self, channel: &Channel, process_embeds: bool) -> Result<()> {
|
pub async fn publish(self, channel: &Channel, process_embeds: bool) -> Result<()> {
|
||||||
|
// construct message and publish
|
||||||
|
// commit message to database
|
||||||
|
|
||||||
|
// spawn task_queue ( update last_message_id )
|
||||||
|
// spawn task_queue ( process embeds )
|
||||||
|
|
||||||
|
// if mentions {
|
||||||
|
// spawn task_queue ( update channel_unreads )
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (channel => DM | Group) | mentions {
|
||||||
|
// spawn task_queue ( web push )
|
||||||
|
// }
|
||||||
|
|
||||||
get_collection("messages")
|
get_collection("messages")
|
||||||
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
|
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -85,8 +85,6 @@ pub enum RemoveMember {
|
|||||||
pub struct Server {
|
pub struct Server {
|
||||||
#[serde(rename = "_id")]
|
#[serde(rename = "_id")]
|
||||||
pub id: String,
|
pub id: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub nonce: Option<String>,
|
|
||||||
pub owner: String,
|
pub owner: String,
|
||||||
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|||||||
@@ -342,6 +342,36 @@ pub async fn run_migrations(revision: i32) -> i32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if revision <= 10 {
|
||||||
|
info!("Running migration [revision 10 / 2021-11-01]: Remove nonce values on channels and servers.");
|
||||||
|
|
||||||
|
get_collection("servers")
|
||||||
|
.update_many(
|
||||||
|
doc! {},
|
||||||
|
doc! {
|
||||||
|
"$unset": {
|
||||||
|
"nonce": 1,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
get_collection("channels")
|
||||||
|
.update_many(
|
||||||
|
doc! {},
|
||||||
|
doc! {
|
||||||
|
"$unset": {
|
||||||
|
"nonce": 1,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
|
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
|
||||||
|
|
||||||
// Reminder to update LATEST_REVISION when adding new migrations.
|
// Reminder to update LATEST_REVISION when adding new migrations.
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
use crate::util::idempotency::IdempotencyKey;
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
use crate::util::variables::MAX_GROUP_SIZE;
|
use crate::util::variables::MAX_GROUP_SIZE;
|
||||||
|
|
||||||
@@ -16,16 +17,13 @@ pub struct Data {
|
|||||||
name: String,
|
name: String,
|
||||||
#[validate(length(min = 0, max = 1024))]
|
#[validate(length(min = 0, max = 1024))]
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
|
||||||
#[validate(length(min = 1, max = 36))]
|
|
||||||
nonce: String,
|
|
||||||
users: Vec<String>,
|
users: Vec<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
nsfw: Option<bool>
|
nsfw: Option<bool>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/create", data = "<info>")]
|
#[post("/create", data = "<info>")]
|
||||||
pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
|
pub async fn req(_idempotency: IdempotencyKey, user: User, info: Json<Data>) -> Result<Value> {
|
||||||
if user.bot.is_some() {
|
if user.bot.is_some() {
|
||||||
return Err(Error::IsBot)
|
return Err(Error::IsBot)
|
||||||
}
|
}
|
||||||
@@ -52,27 +50,9 @@ pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if get_collection("channels")
|
|
||||||
.find_one(
|
|
||||||
doc! {
|
|
||||||
"nonce": &info.nonce
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "find_one",
|
|
||||||
with: "channel",
|
|
||||||
})?
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
Err(Error::DuplicateNonce)?
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
let channel = Channel::Group {
|
let channel = Channel::Group {
|
||||||
id,
|
id,
|
||||||
nonce: Some(info.nonce),
|
|
||||||
name: info.name,
|
name: info.name,
|
||||||
description: info.description,
|
description: info.description,
|
||||||
owner: user.id,
|
owner: user.id,
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
use crate::util::idempotency::IdempotencyKey;
|
||||||
use crate::util::ratelimit::{Ratelimiter, RatelimitResponse};
|
use crate::util::ratelimit::{Ratelimiter, RatelimitResponse};
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
use mongodb::{bson::doc, options::FindOneOptions};
|
use mongodb::bson::doc;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rocket::serde::json::{Json, Value};
|
use rocket::serde::json::{Json, Value};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -21,22 +22,22 @@ pub struct Reply {
|
|||||||
pub struct Data {
|
pub struct Data {
|
||||||
#[validate(length(min = 0, max = 2000))]
|
#[validate(length(min = 0, max = 2000))]
|
||||||
content: String,
|
content: String,
|
||||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
|
||||||
#[validate(length(min = 1, max = 36))]
|
|
||||||
nonce: String,
|
|
||||||
#[validate(length(min = 1, max = 128))]
|
#[validate(length(min = 1, max = 128))]
|
||||||
attachments: Option<Vec<String>>,
|
attachments: Option<Vec<String>>,
|
||||||
|
nonce: Option<String>,
|
||||||
replies: Option<Vec<Reply>>,
|
replies: Option<Vec<Reply>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
// ignoring I L O and U is intentional
|
// ignoring I L O and U is intentional
|
||||||
static ref RE_ULID: Regex = Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap();
|
static ref RE_MENTION: Regex = Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<target>/messages", data = "<message>")]
|
#[post("/<target>/messages", data = "<message>")]
|
||||||
pub async fn message_send(user: User, _r: Ratelimiter, target: Ref, message: Json<Data>) -> Result<RatelimitResponse<Value>> {
|
pub async fn message_send(user: User, _r: Ratelimiter, mut idempotency: IdempotencyKey, target: Ref, message: Json<Data>) -> Result<RatelimitResponse<Value>> {
|
||||||
let message = message.into_inner();
|
let message = message.into_inner();
|
||||||
|
idempotency.consume_nonce(message.nonce.clone());
|
||||||
|
|
||||||
message
|
message
|
||||||
.validate()
|
.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
@@ -59,29 +60,8 @@ pub async fn message_send(user: User, _r: Ratelimiter, target: Ref, message: Jso
|
|||||||
return Err(Error::MissingPermission)
|
return Err(Error::MissingPermission)
|
||||||
}
|
}
|
||||||
|
|
||||||
if get_collection("messages")
|
|
||||||
.find_one(
|
|
||||||
doc! {
|
|
||||||
"nonce": &message.nonce
|
|
||||||
},
|
|
||||||
FindOneOptions::builder()
|
|
||||||
.projection(doc! { "_id": 1 })
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "find_one",
|
|
||||||
with: "message",
|
|
||||||
})?
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
Err(Error::DuplicateNonce)?
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
|
||||||
|
|
||||||
let mut mentions = HashSet::new();
|
let mut mentions = HashSet::new();
|
||||||
for capture in RE_ULID.captures_iter(&message.content) {
|
for capture in RE_MENTION.captures_iter(&message.content) {
|
||||||
if let Some(mention) = capture.get(1) {
|
if let Some(mention) = capture.get(1) {
|
||||||
mentions.insert(mention.as_str().to_string());
|
mentions.insert(mention.as_str().to_string());
|
||||||
}
|
}
|
||||||
@@ -90,7 +70,7 @@ pub async fn message_send(user: User, _r: Ratelimiter, target: Ref, message: Jso
|
|||||||
let mut replies = HashSet::new();
|
let mut replies = HashSet::new();
|
||||||
if let Some(entries) = message.replies {
|
if let Some(entries) = message.replies {
|
||||||
// ! FIXME: move this to app config
|
// ! FIXME: move this to app config
|
||||||
if entries.len() >= 5 {
|
if entries.len() > 5 {
|
||||||
return Err(Error::TooManyReplies)
|
return Err(Error::TooManyReplies)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,14 +87,16 @@ pub async fn message_send(user: User, _r: Ratelimiter, target: Ref, message: Jso
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let id = Ulid::new().to_string();
|
||||||
let mut attachments = vec![];
|
let mut attachments = vec![];
|
||||||
|
|
||||||
if let Some(ids) = &message.attachments {
|
if let Some(ids) = &message.attachments {
|
||||||
if ids.len() > 0 && !perm.get_upload_files() {
|
if ids.len() > 0 && !perm.get_upload_files() {
|
||||||
return Err(Error::MissingPermission)
|
return Err(Error::MissingPermission)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ! FIXME: move this to app config
|
// ! FIXME: move this to app config
|
||||||
if ids.len() >= 5 {
|
if ids.len() > 5 {
|
||||||
return Err(Error::TooManyAttachments)
|
return Err(Error::TooManyAttachments)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +112,7 @@ pub async fn message_send(user: User, _r: Ratelimiter, target: Ref, message: Jso
|
|||||||
author: user.id,
|
author: user.id,
|
||||||
|
|
||||||
content: Content::Text(message.content.clone()),
|
content: Content::Text(message.content.clone()),
|
||||||
nonce: Some(message.nonce.clone()),
|
nonce: Some(idempotency.key),
|
||||||
edited: None,
|
edited: None,
|
||||||
embeds: None,
|
embeds: None,
|
||||||
|
|
||||||
@@ -148,6 +130,5 @@ pub async fn message_send(user: User, _r: Ratelimiter, target: Ref, message: Jso
|
|||||||
};
|
};
|
||||||
|
|
||||||
msg.clone().publish(&target, perm.get_embed_links()).await?;
|
msg.clone().publish(&target, perm.get_embed_links()).await?;
|
||||||
|
|
||||||
Ok(RatelimitResponse(json!(msg)))
|
Ok(RatelimitResponse(json!(msg)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
use crate::util::idempotency::IdempotencyKey;
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
@@ -29,15 +30,12 @@ pub struct Data {
|
|||||||
name: String,
|
name: String,
|
||||||
#[validate(length(min = 0, max = 1024))]
|
#[validate(length(min = 0, max = 1024))]
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
|
||||||
#[validate(length(min = 1, max = 36))]
|
|
||||||
nonce: String,
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
nsfw: Option<bool>,
|
nsfw: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<target>/channels", data = "<info>")]
|
#[post("/<target>/channels", data = "<info>")]
|
||||||
pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<Value> {
|
pub async fn req(_idempotency: IdempotencyKey, user: User, target: Ref, info: Json<Data>) -> Result<Value> {
|
||||||
let info = info.into_inner();
|
let info = info.into_inner();
|
||||||
info.validate()
|
info.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
@@ -52,29 +50,11 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<Value> {
|
|||||||
Err(Error::MissingPermission)?
|
Err(Error::MissingPermission)?
|
||||||
}
|
}
|
||||||
|
|
||||||
if get_collection("channels")
|
|
||||||
.find_one(
|
|
||||||
doc! {
|
|
||||||
"nonce": &info.nonce
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "find_one",
|
|
||||||
with: "channel",
|
|
||||||
})?
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
Err(Error::DuplicateNonce)?
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
let channel = match info.channel_type {
|
let channel = match info.channel_type {
|
||||||
ChannelType::Text => Channel::TextChannel {
|
ChannelType::Text => Channel::TextChannel {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
server: target.id.clone(),
|
server: target.id.clone(),
|
||||||
nonce: Some(info.nonce),
|
|
||||||
|
|
||||||
name: info.name,
|
name: info.name,
|
||||||
description: info.description,
|
description: info.description,
|
||||||
@@ -89,7 +69,6 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<Value> {
|
|||||||
ChannelType::Voice => Channel::VoiceChannel {
|
ChannelType::Voice => Channel::VoiceChannel {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
server: target.id.clone(),
|
server: target.id.clone(),
|
||||||
nonce: Some(info.nonce),
|
|
||||||
|
|
||||||
name: info.name,
|
name: info.name,
|
||||||
description: info.description,
|
description: info.description,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
use crate::util::idempotency::IdempotencyKey;
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
use crate::util::variables::MAX_SERVER_COUNT;
|
use crate::util::variables::MAX_SERVER_COUNT;
|
||||||
|
|
||||||
@@ -16,15 +17,12 @@ pub struct Data {
|
|||||||
name: String,
|
name: String,
|
||||||
#[validate(length(min = 0, max = 1024))]
|
#[validate(length(min = 0, max = 1024))]
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
|
||||||
#[validate(length(min = 1, max = 36))]
|
|
||||||
nonce: String,
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
nsfw: Option<bool>
|
nsfw: Option<bool>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/create", data = "<info>")]
|
#[post("/create", data = "<info>")]
|
||||||
pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
|
pub async fn req(_idempotency: IdempotencyKey, user: User, info: Json<Data>) -> Result<Value> {
|
||||||
if user.bot.is_some() {
|
if user.bot.is_some() {
|
||||||
return Err(Error::IsBot)
|
return Err(Error::IsBot)
|
||||||
}
|
}
|
||||||
@@ -39,29 +37,11 @@ pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
|
|||||||
info.validate()
|
info.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
if get_collection("servers")
|
|
||||||
.find_one(
|
|
||||||
doc! {
|
|
||||||
"nonce": &info.nonce
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "find_one",
|
|
||||||
with: "server",
|
|
||||||
})?
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
Err(Error::DuplicateNonce)?
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
let cid = Ulid::new().to_string();
|
let cid = Ulid::new().to_string();
|
||||||
|
|
||||||
let server = Server {
|
let server = Server {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
nonce: Some(info.nonce.clone()),
|
|
||||||
owner: user.id.clone(),
|
owner: user.id.clone(),
|
||||||
|
|
||||||
name: info.name,
|
name: info.name,
|
||||||
@@ -92,7 +72,6 @@ pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
|
|||||||
Channel::TextChannel {
|
Channel::TextChannel {
|
||||||
id: cid,
|
id: cid,
|
||||||
server: id,
|
server: id,
|
||||||
nonce: Some(info.nonce),
|
|
||||||
name: "general".to_string(),
|
name: "general".to_string(),
|
||||||
description: None,
|
description: None,
|
||||||
|
|
||||||
|
|||||||
56
src/util/idempotency.rs
Normal file
56
src/util/idempotency.rs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
use crate::util::result::Error;
|
||||||
|
|
||||||
|
use mongodb::bson::doc;
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::request::{FromRequest, Outcome};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use validator::Validate;
|
||||||
|
|
||||||
|
#[derive(Validate, Serialize, Deserialize)]
|
||||||
|
pub struct IdempotencyKey {
|
||||||
|
#[validate(length(min = 1, max = 64))]
|
||||||
|
pub key: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdempotencyKey {
|
||||||
|
// Backwards compatibility.
|
||||||
|
// Issue #109
|
||||||
|
pub fn consume_nonce(&mut self, v: Option<String>) {
|
||||||
|
if let Some(v) = v {
|
||||||
|
self.key = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref TOKEN_CACHE: std::sync::Mutex<lru::LruCache<String, ()>> = std::sync::Mutex::new(lru::LruCache::new(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<'r> FromRequest<'r> for IdempotencyKey {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
async fn from_request(request: &'r rocket::Request<'_>) -> Outcome<Self, Self::Error> {
|
||||||
|
if let Some(key) = request
|
||||||
|
.headers()
|
||||||
|
.get("Idempotency-Key")
|
||||||
|
.next()
|
||||||
|
.map(|k| k.to_string()) {
|
||||||
|
let idempotency = IdempotencyKey { key };
|
||||||
|
if let Err(error) = idempotency.validate() {
|
||||||
|
return Outcome::Failure((Status::BadRequest, Error::FailedValidation { error }));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(mut cache) = TOKEN_CACHE.lock() {
|
||||||
|
if cache.get(&idempotency.key).is_some() {
|
||||||
|
return Outcome::Failure((Status::Conflict, Error::DuplicateNonce));
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.put(idempotency.key.clone(), ());
|
||||||
|
return Outcome::Success(idempotency);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Outcome::Success(IdempotencyKey { key: ulid::Ulid::new().to_string() })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,3 +2,4 @@ pub mod result;
|
|||||||
pub mod variables;
|
pub mod variables;
|
||||||
pub mod ratelimit;
|
pub mod ratelimit;
|
||||||
pub mod regex;
|
pub mod regex;
|
||||||
|
pub mod idempotency;
|
||||||
Reference in New Issue
Block a user