mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-08-31 03:32:30 +00:00
merge: branch 'insert/dev-branch'
This commit is contained in:
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"editor.formatOnSave": true,
|
||||
"rust-analyzer.checkOnSave.command": "clippy"
|
||||
}
|
||||
"editor.formatOnSave": true,
|
||||
"rust-analyzer.checkOnSave.command": "clippy",
|
||||
"nixEnvSelector.suggestion": false
|
||||
}
|
||||
|
||||
882
Cargo.lock
generated
882
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
16
clippy.toml
16
clippy.toml
@@ -3,13 +3,23 @@ disallowed-methods = [
|
||||
"revolt_database::models::bots::model::Bot::remove_field",
|
||||
|
||||
# Prefer to use Object::create()
|
||||
"revolt_database::models::safety_strikes::ops::AbstractAccountStrikes::insert_account_strike",
|
||||
"revolt_database::models::bots::ops::AbstractBots::insert_bot",
|
||||
"revolt_database::models::channel_invites::ops::AbstractChannelInvites::insert_invite",
|
||||
"revolt_database::models::channel_unreads::ops::AbstractChannelUnreads::acknowledge_message",
|
||||
"revolt_database::models::channel_webhooks::ops::AbstractWebhooks::insert_webhook",
|
||||
"revolt_database::models::channels::ops::AbstractChannels::insert_channel",
|
||||
"revolt_database::models::emojis::ops::AbstractEmojis::insert_emoji",
|
||||
"revolt_database::models::files::ops::AbstractAttachments::insert_attachment",
|
||||
"revolt_database::models::messages::ops::AbstractMessages::insert_message",
|
||||
"revolt_database::models::ratelimit_events::ops::AbstractRatelimitEvents::insert_ratelimit_event",
|
||||
"revolt_database::models::server_bans::ops::AbstractServerBans::insert_ban",
|
||||
"revolt_database::models::server_members::ops::AbstractServerMembers::insert_member",
|
||||
"revolt_database::models::servers::ops::AbstractServers::insert_server",
|
||||
"revolt_database::models::users::ops::AbstractUsers::insert_user",
|
||||
|
||||
# Prefer to use Object::update(&self)
|
||||
"revolt_database::models::bots::ops::AbstractBots::update_bot",
|
||||
"revolt_database::models::safety_strikes::ops::AbstractAccountStrikes::update_account_strike",
|
||||
|
||||
# Prefer to use Object::delete(&self)
|
||||
"revolt_database::models::bots::ops::AbstractBots::delete_bot",
|
||||
"revolt_database::models::safety_strikes::ops::AbstractAccountStrikes::delete_account_strike",
|
||||
]
|
||||
|
||||
26
crates/core/config/Cargo.toml
Normal file
26
crates/core/config/Cargo.toml
Normal file
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "revolt-config"
|
||||
version = "0.6.7"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0-or-later"
|
||||
authors = ["Paul Makles <me@insrt.uk>"]
|
||||
description = "Revolt Backend: Configuration"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
test = ["async-std"]
|
||||
default = ["test"]
|
||||
|
||||
[dependencies]
|
||||
# Utility
|
||||
config = "0.13.3"
|
||||
cached = "0.44.0"
|
||||
once_cell = "1.18.0"
|
||||
|
||||
# Serde
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
||||
# Async
|
||||
futures-locks = "0.7.1"
|
||||
async-std = { version = "1.8.0", features = ["attributes"], optional = true }
|
||||
64
crates/core/config/Revolt.toml
Normal file
64
crates/core/config/Revolt.toml
Normal file
@@ -0,0 +1,64 @@
|
||||
[database]
|
||||
mongodb = "mongodb://database"
|
||||
redis = "redis://redis/"
|
||||
|
||||
[hosts]
|
||||
app = "http://local.revolt.chat"
|
||||
api = "http://local.revolt.chat/api"
|
||||
events = "ws://local.revolt.chat/ws"
|
||||
autumn = "http://local.revolt.chat/autumn"
|
||||
january = "http://local.revolt.chat/january"
|
||||
voso_legacy = ""
|
||||
voso_legacy_ws = ""
|
||||
|
||||
[api]
|
||||
staging = false
|
||||
|
||||
[api.registration]
|
||||
invite_only = false
|
||||
|
||||
[api.smtp]
|
||||
host = ""
|
||||
username = ""
|
||||
password = ""
|
||||
from_address = ""
|
||||
|
||||
[api.vapid]
|
||||
private_key = "LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUJSUWpyTWxLRnBiVWhsUHpUbERvcEliYk1yeVNrNXpKYzVYVzIxSjJDS3hvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFWnkrQkg2TGJQZ2hEa3pEempXOG0rUXVPM3pCajRXT1phdkR6ZU00c0pqbmFwd1psTFE0WAp1ZDh2TzVodU94QWhMQlU3WWRldVovWHlBdFpWZmNyQi9BPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo"
|
||||
public_key = "BGcvgR-i2z4IQ5Mw841vJvkLjt8wY-FjmWrw83jOLCY52qcGZS0OF7nfLzuYbjsQISwVO2HXrmf18gLWVX3Kwfw="
|
||||
|
||||
[api.fcm]
|
||||
api_key = ""
|
||||
|
||||
[api.security]
|
||||
authifier_shield_key = ""
|
||||
voso_legacy_token = ""
|
||||
|
||||
[api.security.captcha]
|
||||
hcaptcha_key = ""
|
||||
hcaptcha_sitekey = ""
|
||||
|
||||
[api.workers]
|
||||
max_concurrent_connections = 50
|
||||
|
||||
[features]
|
||||
|
||||
[features.limits]
|
||||
|
||||
[features.limits.default]
|
||||
group_size = 100
|
||||
bots = 5
|
||||
message_replies = 5
|
||||
message_attachments = 5
|
||||
message_embeds = 5
|
||||
servers = 100
|
||||
server_emoji = 100
|
||||
server_roles = 200
|
||||
server_channels = 200
|
||||
|
||||
attachment_size = 20000000
|
||||
avatar_size = 4000000
|
||||
background_size = 6000000
|
||||
icon_size = 2500000
|
||||
banner_size = 6000000
|
||||
emoji_size = 500000
|
||||
162
crates/core/config/src/lib.rs
Normal file
162
crates/core/config/src/lib.rs
Normal file
@@ -0,0 +1,162 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use cached::proc_macro::cached;
|
||||
use config::{Config, File, FileFormat};
|
||||
use futures_locks::RwLock;
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::Deserialize;
|
||||
|
||||
static CONFIG_BUILDER: Lazy<RwLock<Config>> = Lazy::new(|| {
|
||||
RwLock::new({
|
||||
let mut builder = Config::builder().add_source(File::from_str(
|
||||
include_str!("../Revolt.toml"),
|
||||
FileFormat::Toml,
|
||||
));
|
||||
|
||||
if std::path::Path::new("revolt.toml").exists() {
|
||||
builder = builder.add_source(File::new("revolt.toml", FileFormat::Toml));
|
||||
}
|
||||
|
||||
builder.build().unwrap()
|
||||
})
|
||||
});
|
||||
|
||||
// https://gifbox.me/view/gT5mqxYKCZv-twilight-meow
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Database {
|
||||
pub mongodb: String,
|
||||
pub redis: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Hosts {
|
||||
pub app: String,
|
||||
pub api: String,
|
||||
pub events: String,
|
||||
pub autumn: String,
|
||||
pub january: String,
|
||||
pub voso_legacy: String,
|
||||
pub voso_legacy_ws: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiRegistration {
|
||||
pub invite_only: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiSmtp {
|
||||
pub host: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub from_address: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiVapid {
|
||||
pub private_key: String,
|
||||
pub public_key: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiFcm {
|
||||
pub api_key: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiSecurityCaptcha {
|
||||
pub hcaptcha_key: String,
|
||||
pub hcaptcha_sitekey: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiSecurity {
|
||||
pub authifier_shield_key: String,
|
||||
pub voso_legacy_token: String,
|
||||
pub captcha: ApiSecurityCaptcha,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ApiWorkers {
|
||||
pub max_concurrent_connections: usize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Api {
|
||||
pub staging: bool,
|
||||
pub registration: ApiRegistration,
|
||||
pub smtp: ApiSmtp,
|
||||
pub vapid: ApiVapid,
|
||||
pub fcm: ApiFcm,
|
||||
pub security: ApiSecurity,
|
||||
pub workers: ApiWorkers,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct FeaturesLimits {
|
||||
pub group_size: usize,
|
||||
pub bots: usize,
|
||||
pub message_replies: usize,
|
||||
pub message_attachments: usize,
|
||||
pub message_embeds: usize,
|
||||
pub servers: usize,
|
||||
pub server_emoji: usize,
|
||||
pub server_roles: usize,
|
||||
pub server_channels: usize,
|
||||
|
||||
pub attachment_size: usize,
|
||||
pub avatar_size: usize,
|
||||
pub background_size: usize,
|
||||
pub icon_size: usize,
|
||||
pub banner_size: usize,
|
||||
pub emoji_size: usize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct FeaturesLimitsCollection {
|
||||
pub default: FeaturesLimits,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub roles: HashMap<String, FeaturesLimits>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Features {
|
||||
pub limits: FeaturesLimitsCollection,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Settings {
|
||||
pub database: Database,
|
||||
pub hosts: Hosts,
|
||||
pub api: Api,
|
||||
pub features: Features,
|
||||
}
|
||||
|
||||
pub async fn init() {
|
||||
println!(
|
||||
":: Revolt Configuration ::\n\x1b[32m{:?}\x1b[0m",
|
||||
config().await
|
||||
);
|
||||
}
|
||||
|
||||
pub async fn read() -> Config {
|
||||
CONFIG_BUILDER.read().await.clone()
|
||||
}
|
||||
|
||||
#[cached(time = 30)]
|
||||
pub async fn config() -> Settings {
|
||||
read().await.try_deserialize::<Settings>().unwrap()
|
||||
}
|
||||
|
||||
#[cfg(feature = "test")]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::init;
|
||||
|
||||
#[async_std::test]
|
||||
async fn it_works() {
|
||||
init().await;
|
||||
}
|
||||
}
|
||||
@@ -13,15 +13,17 @@ description = "Revolt Backend: Database Implementation"
|
||||
mongodb = ["dep:mongodb", "bson"]
|
||||
|
||||
# ... Other
|
||||
tasks = ["isahc", "linkify", "url-escape"]
|
||||
async-std-runtime = ["async-std"]
|
||||
rocket-impl = ["rocket", "schemars"]
|
||||
rocket-impl = ["rocket", "schemars", "revolt_okapi", "revolt_rocket_okapi"]
|
||||
redis-is-patched = ["revolt-presence/redis-is-patched"]
|
||||
|
||||
# Default Features
|
||||
default = ["mongodb", "async-std-runtime"]
|
||||
default = ["mongodb", "async-std-runtime", "tasks"]
|
||||
|
||||
[dependencies]
|
||||
# Core
|
||||
revolt-config = { version = "0.6.7", path = "../config" }
|
||||
revolt-result = { version = "0.6.7", path = "../result" }
|
||||
revolt-models = { version = "0.6.7", path = "../models" }
|
||||
revolt-presence = { version = "0.6.7", path = "../presence" }
|
||||
@@ -32,11 +34,18 @@ revolt-permissions = { version = "0.6.7", path = "../permissions", features = [
|
||||
|
||||
# Utility
|
||||
log = "0.4"
|
||||
lru = "0.11.0"
|
||||
rand = "0.8.5"
|
||||
ulid = "1.0.0"
|
||||
nanoid = "0.4.0"
|
||||
base64 = "0.21.3"
|
||||
once_cell = "1.17"
|
||||
indexmap = "1.9.1"
|
||||
decancer = "1.6.2"
|
||||
deadqueue = "0.2.4"
|
||||
linkify = { optional = true, version = "0.8.1" }
|
||||
url-escape = { optional = true, version = "0.1.1" }
|
||||
isahc = { optional = true, version = "1.7", features = ["json"] }
|
||||
|
||||
# Serialisation
|
||||
serde_json = "1"
|
||||
@@ -57,6 +66,7 @@ regex = "1"
|
||||
|
||||
# Async Language Features
|
||||
futures = "0.3.19"
|
||||
async-lock = "2.8.0"
|
||||
async-trait = "0.1.51"
|
||||
async-recursion = "1.0.4"
|
||||
|
||||
@@ -68,6 +78,12 @@ schemars = { version = "0.8.8", optional = true }
|
||||
rocket = { version = "0.5.0-rc.2", default-features = false, features = [
|
||||
"json",
|
||||
], optional = true }
|
||||
revolt_okapi = { version = "0.9.1", optional = true }
|
||||
revolt_rocket_okapi = { version = "0.9.1", optional = true }
|
||||
|
||||
# Notifications
|
||||
fcm = "0.9.2"
|
||||
web-push = "0.10.0"
|
||||
|
||||
# Authifier
|
||||
authifier = { version = "1.0" }
|
||||
|
||||
@@ -65,3 +65,14 @@ impl DatabaseInfo {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Database> for authifier::Database {
|
||||
fn from(value: Database) -> Self {
|
||||
match value {
|
||||
Database::Reference(_) => Default::default(),
|
||||
Database::MongoDb(MongoDb(client, _)) => authifier::Database::MongoDb(
|
||||
authifier::database::MongoDb(client.database("revolt")),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,6 +238,6 @@ pub fn prefix_keys<T: Serialize>(t: &T, prefix: &str) -> HashMap<String, serde_j
|
||||
let v: HashMap<String, serde_json::Value> = serde_json::from_str(&v).unwrap();
|
||||
v.into_iter()
|
||||
.filter(|(_k, v)| !v.is_null())
|
||||
.map(|(k, v)| (prefix.to_owned() + &k, v))
|
||||
.map(|(k, v)| (format!("{}{}", prefix.to_owned(), k), v))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use futures::lock::Mutex;
|
||||
|
||||
use crate::{
|
||||
Bot, Channel, ChannelCompositeKey, ChannelUnread, Emoji, File, Invite, Member,
|
||||
MemberCompositeKey, Server, ServerBan, User, UserSettings, Webhook,
|
||||
MemberCompositeKey, Message, Server, ServerBan, User, UserSettings, Webhook,
|
||||
};
|
||||
|
||||
database_derived!(
|
||||
@@ -17,14 +17,14 @@ database_derived!(
|
||||
pub channel_unreads: Arc<Mutex<HashMap<ChannelCompositeKey, ChannelUnread>>>,
|
||||
pub channel_webhooks: Arc<Mutex<HashMap<String, Webhook>>>,
|
||||
pub emojis: Arc<Mutex<HashMap<String, Emoji>>>,
|
||||
pub files: Arc<Mutex<HashMap<String, File>>>,
|
||||
pub messages: Arc<Mutex<HashMap<String, Message>>>,
|
||||
pub user_settings: Arc<Mutex<HashMap<String, UserSettings>>>,
|
||||
pub users: Arc<Mutex<HashMap<String, User>>>,
|
||||
pub server_bans: Arc<Mutex<HashMap<MemberCompositeKey, ServerBan>>>,
|
||||
pub server_members: Arc<Mutex<HashMap<MemberCompositeKey, Member>>>,
|
||||
pub servers: Arc<Mutex<HashMap<String, Server>>>,
|
||||
pub files: Arc<Mutex<HashMap<String, File>>>,
|
||||
pub safety_reports: Arc<Mutex<HashMap<String, ()>>>,
|
||||
pub safety_snapshots: Arc<Mutex<HashMap<String, ()>>>,
|
||||
pub messages: Arc<Mutex<HashMap<String, ()>>>,
|
||||
}
|
||||
);
|
||||
|
||||
@@ -2,9 +2,10 @@ use authifier::AuthifierEvent;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use revolt_models::v0::{
|
||||
Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer, FieldsWebhook,
|
||||
MemberCompositeKey, PartialChannel, PartialMember, PartialRole, PartialServer, PartialWebhook,
|
||||
Server, UserSettings, Webhook,
|
||||
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
|
||||
FieldsUser, FieldsWebhook, MemberCompositeKey, Message, PartialChannel, PartialMember,
|
||||
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Server, UserSettings,
|
||||
Webhook,
|
||||
};
|
||||
use revolt_result::Error;
|
||||
|
||||
@@ -57,8 +58,7 @@ pub enum EventV1 {
|
||||
},
|
||||
|
||||
/// Ping response
|
||||
Pong { data: Ping },
|
||||
|
||||
Pong { data: Ping }, */
|
||||
/// New message
|
||||
Message(Message),
|
||||
|
||||
@@ -103,7 +103,8 @@ pub enum EventV1 {
|
||||
},
|
||||
|
||||
/// Bulk delete messages
|
||||
BulkMessageDelete { channel: String, ids: Vec<String> },*/
|
||||
BulkMessageDelete { channel: String, ids: Vec<String> },
|
||||
|
||||
/// New server
|
||||
ServerCreate {
|
||||
id: String,
|
||||
@@ -145,7 +146,7 @@ pub enum EventV1 {
|
||||
/// Server role deleted
|
||||
ServerRoleDelete { id: String, role_id: String },
|
||||
|
||||
/*/// Update existing user
|
||||
/// Update existing user
|
||||
UserUpdate {
|
||||
id: String,
|
||||
data: PartialUser,
|
||||
@@ -153,7 +154,7 @@ pub enum EventV1 {
|
||||
event_id: Option<String>,
|
||||
},
|
||||
|
||||
/// Relationship with another user changed
|
||||
/*/// Relationship with another user changed
|
||||
UserRelationship {
|
||||
id: String,
|
||||
user: User,
|
||||
|
||||
@@ -43,7 +43,7 @@ macro_rules! auto_derived {
|
||||
|
||||
macro_rules! auto_derived_partial {
|
||||
( $item:item, $name:expr ) => {
|
||||
#[derive(OptionalStruct, Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
|
||||
#[derive(OptionalStruct, Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
|
||||
#[optional_derive(Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
|
||||
#[optional_name = $name]
|
||||
#[opt_skip_serializing_none]
|
||||
@@ -81,6 +81,7 @@ pub mod util;
|
||||
pub use models::*;
|
||||
|
||||
pub mod events;
|
||||
pub mod tasks;
|
||||
|
||||
/// Utility function to check if a boolean value is false
|
||||
pub fn if_false(t: &bool) -> bool {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use revolt_result::Result;
|
||||
use ulid::Ulid;
|
||||
|
||||
use crate::Database;
|
||||
use crate::{BotInformation, Database, PartialUser, User};
|
||||
|
||||
auto_derived_partial!(
|
||||
/// Bot
|
||||
@@ -49,8 +50,71 @@ auto_derived!(
|
||||
}
|
||||
);
|
||||
|
||||
#[allow(clippy::derivable_impls)]
|
||||
impl Default for Bot {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Default::default(),
|
||||
owner: Default::default(),
|
||||
token: Default::default(),
|
||||
public: Default::default(),
|
||||
analytics: Default::default(),
|
||||
discoverable: Default::default(),
|
||||
interactions_url: Default::default(),
|
||||
terms_of_service_url: Default::default(),
|
||||
privacy_policy_url: Default::default(),
|
||||
flags: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl Bot {
|
||||
/// Create a new bot
|
||||
pub async fn create<D>(db: &Database, username: String, owner: &User, data: D) -> Result<Bot>
|
||||
where
|
||||
D: Into<Option<PartialBot>>,
|
||||
{
|
||||
if owner.bot.is_some() {
|
||||
return Err(create_error!(IsBot));
|
||||
}
|
||||
|
||||
// TODO: config
|
||||
let max_bot_count = 5;
|
||||
if db.get_number_of_bots_by_user(&owner.id).await? >= max_bot_count {
|
||||
return Err(create_error!(ReachedMaximumBots));
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
|
||||
User::create(
|
||||
db,
|
||||
username,
|
||||
Some(id.to_string()),
|
||||
Some(PartialUser {
|
||||
bot: Some(BotInformation {
|
||||
owner: id.to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut bot = Bot {
|
||||
id,
|
||||
owner: owner.id.to_string(),
|
||||
token: nanoid::nanoid!(64),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(data) = data.into() {
|
||||
bot.apply_options(data);
|
||||
}
|
||||
|
||||
db.insert_bot(&bot).await?;
|
||||
Ok(bot)
|
||||
}
|
||||
|
||||
/// Remove a field from this object
|
||||
pub fn remove_field(&mut self, field: &FieldsBot) {
|
||||
match field {
|
||||
@@ -96,27 +160,24 @@ mod tests {
|
||||
#[async_std::test]
|
||||
async fn crud() {
|
||||
database_test!(|db| async move {
|
||||
let bot_id = "bot";
|
||||
let user_id = "user";
|
||||
let token = "my_token";
|
||||
let owner = User::create(&db, "Owner".to_string(), None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let user = User {
|
||||
id: bot_id.to_string(),
|
||||
username: "Bot Name".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
let bot = Bot::create(
|
||||
&db,
|
||||
"Bot Name".to_string(),
|
||||
&owner,
|
||||
PartialBot {
|
||||
token: Some("my token".to_string()),
|
||||
interactions_url: Some("some url".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
db.insert_user(&user).await.unwrap();
|
||||
|
||||
let bot = Bot {
|
||||
id: bot_id.to_string(),
|
||||
owner: user_id.to_string(),
|
||||
token: token.to_string(),
|
||||
interactions_url: "some url".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
db.insert_bot(&bot).await.unwrap();
|
||||
assert!(!bot.interactions_url.is_empty());
|
||||
|
||||
let mut updated_bot = bot.clone();
|
||||
updated_bot
|
||||
@@ -131,9 +192,9 @@ mod tests {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
||||
let fetched_bot1 = db.fetch_bot(&bot.id).await.unwrap();
|
||||
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
||||
let fetched_bots = db.fetch_bots_by_user(user_id).await.unwrap();
|
||||
let fetched_bots = db.fetch_bots_by_user(&owner.id).await.unwrap();
|
||||
|
||||
assert!(!bot.public);
|
||||
assert!(fetched_bot1.public);
|
||||
@@ -143,12 +204,12 @@ mod tests {
|
||||
assert_eq!(updated_bot, fetched_bot1);
|
||||
assert_eq!(fetched_bot1, fetched_bot2);
|
||||
assert_eq!(fetched_bot1, fetched_bots[0]);
|
||||
assert_eq!(1, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
||||
assert_eq!(1, db.get_number_of_bots_by_user(&owner.id).await.unwrap());
|
||||
|
||||
bot.delete(&db).await.unwrap();
|
||||
assert!(db.fetch_bot(bot_id).await.is_err());
|
||||
assert_eq!(0, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
||||
assert_eq!(db.fetch_user(bot_id).await.unwrap().flags, Some(2))
|
||||
assert!(db.fetch_bot(&bot.id).await.is_err());
|
||||
assert_eq!(0, db.get_number_of_bots_by_user(&owner.id).await.unwrap());
|
||||
assert_eq!(db.fetch_user(&bot.id).await.unwrap().flags, Some(2))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ impl AbstractChannelUnreads for ReferenceDb {
|
||||
user: user_id.to_string(),
|
||||
};
|
||||
|
||||
if let Some(mut unread) = unreads.get_mut(&key) {
|
||||
if let Some(unread) = unreads.get_mut(&key) {
|
||||
unread.mentions = None;
|
||||
unread.last_id.replace(message_id.to_string());
|
||||
} else {
|
||||
@@ -41,6 +41,7 @@ impl AbstractChannelUnreads for ReferenceDb {
|
||||
async fn acknowledge_channels(&self, user_id: &str, channel_ids: &[String]) -> Result<()> {
|
||||
let current_time = Ulid::new().to_string();
|
||||
for channel_id in channel_ids {
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
self.acknowledge_message(channel_id, user_id, ¤t_time)
|
||||
.await?;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,20 @@ auto_derived!(
|
||||
}
|
||||
);
|
||||
|
||||
#[allow(clippy::derivable_impls)]
|
||||
impl Default for Webhook {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Default::default(),
|
||||
name: Default::default(),
|
||||
avatar: None,
|
||||
channel_id: Default::default(),
|
||||
permissions: Default::default(),
|
||||
token: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl Webhook {
|
||||
pub async fn create(&self, db: &Database) -> Result<()> {
|
||||
@@ -120,7 +134,7 @@ mod tests {
|
||||
id: webhook_id.to_string(),
|
||||
name: "Webhook Name".to_string(),
|
||||
channel_id: channel_id.to_string(),
|
||||
avatar: Some(Default::default()),
|
||||
avatar: None,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use revolt_models::v0::MessageAuthor;
|
||||
use revolt_permissions::OverrideField;
|
||||
use revolt_result::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{events::client::EventV1, Database, File, IntoDocumentPath};
|
||||
use crate::{events::client::EventV1, Database, File, IntoDocumentPath, SystemMessage, User};
|
||||
|
||||
auto_derived!(
|
||||
#[serde(tag = "channel_type")]
|
||||
pub enum Channel {
|
||||
/// Personal "Saved Notes" channel which allows users to save messages
|
||||
SavedMessages {
|
||||
@@ -164,6 +166,7 @@ auto_derived!(
|
||||
}
|
||||
);
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl Channel {
|
||||
/// Create a channel
|
||||
pub async fn create(&self, db: &Database) -> Result<()> {
|
||||
@@ -189,40 +192,48 @@ impl Channel {
|
||||
pub async fn add_user_to_group(
|
||||
&mut self,
|
||||
db: &Database,
|
||||
user_id: &str,
|
||||
_by_id: &str,
|
||||
user: &User,
|
||||
by_id: &str,
|
||||
) -> Result<()> {
|
||||
if let Channel::Group { recipients, .. } = self {
|
||||
if recipients.contains(&String::from(user_id)) {
|
||||
if recipients.contains(&String::from(&user.id)) {
|
||||
return Err(create_error!(AlreadyInGroup));
|
||||
}
|
||||
|
||||
recipients.push(String::from(user_id));
|
||||
recipients.push(String::from(&user.id));
|
||||
}
|
||||
|
||||
match &self {
|
||||
Channel::Group { id, .. } => {
|
||||
db.add_user_to_group(id, user_id).await?;
|
||||
db.add_user_to_group(id, &user.id).await?;
|
||||
|
||||
EventV1::ChannelGroupJoin {
|
||||
id: id.to_string(),
|
||||
user: user_id.to_string(),
|
||||
user: user.id.to_string(),
|
||||
}
|
||||
.p(id.to_string())
|
||||
.await;
|
||||
|
||||
EventV1::ChannelCreate(self.clone().into())
|
||||
.private(user_id.to_string())
|
||||
.private(user.id.to_string())
|
||||
.await;
|
||||
|
||||
/* TODO: SystemMessage::UserAdded {
|
||||
id: user.to_string(),
|
||||
by: by.to_string(),
|
||||
SystemMessage::UserAdded {
|
||||
id: user.id.to_string(),
|
||||
by: by_id.to_string(),
|
||||
}
|
||||
.into_message(id.to_string())
|
||||
.create(db, self, None)
|
||||
.send(
|
||||
db,
|
||||
MessageAuthor::System {
|
||||
username: &user.username,
|
||||
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
|
||||
},
|
||||
self,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.ok(); */
|
||||
.ok();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -468,19 +479,20 @@ impl Channel {
|
||||
pub async fn remove_user_from_group(
|
||||
&self,
|
||||
db: &Database,
|
||||
user_id: &str,
|
||||
_by_id: Option<&str>,
|
||||
user: &User,
|
||||
by_id: Option<&str>,
|
||||
silent: bool,
|
||||
) -> Result<()> {
|
||||
match &self {
|
||||
Channel::Group {
|
||||
id,
|
||||
name,
|
||||
owner,
|
||||
recipients,
|
||||
..
|
||||
} => {
|
||||
if user_id == owner {
|
||||
if let Some(new_owner) = recipients.iter().find(|x| *x != user_id) {
|
||||
if &user.id == owner {
|
||||
if let Some(new_owner) = recipients.iter().find(|x| *x != &user.id) {
|
||||
db.update_channel(
|
||||
id,
|
||||
&PartialChannel {
|
||||
@@ -491,14 +503,22 @@ impl Channel {
|
||||
)
|
||||
.await?;
|
||||
|
||||
/* TODO: SystemMessage::ChannelOwnershipChanged {
|
||||
SystemMessage::ChannelOwnershipChanged {
|
||||
from: owner.to_string(),
|
||||
to: new_owner.into(),
|
||||
to: new_owner.to_string(),
|
||||
}
|
||||
.into_message(id.to_string())
|
||||
.create(db, self, None)
|
||||
.send(
|
||||
db,
|
||||
MessageAuthor::System {
|
||||
username: name,
|
||||
avatar: None,
|
||||
},
|
||||
self,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.ok(); */
|
||||
.ok();
|
||||
} else {
|
||||
db.delete_channel(self).await?;
|
||||
return Ok(());
|
||||
@@ -507,26 +527,34 @@ impl Channel {
|
||||
|
||||
EventV1::ChannelGroupLeave {
|
||||
id: id.to_string(),
|
||||
user: user_id.to_string(),
|
||||
user: user.id.to_string(),
|
||||
}
|
||||
.p(id.to_string())
|
||||
.await;
|
||||
|
||||
if !silent {
|
||||
/* TODO: if let Some(_by) = by_id {
|
||||
if let Some(by) = by_id {
|
||||
SystemMessage::UserRemove {
|
||||
id: user_id.to_string(),
|
||||
id: user.id.to_string(),
|
||||
by: by.to_string(),
|
||||
}
|
||||
} else {
|
||||
SystemMessage::UserLeft {
|
||||
id: user_id.to_string(),
|
||||
id: user.id.to_string(),
|
||||
}
|
||||
}
|
||||
.into_message(id.to_string())
|
||||
.create(db, self, None)
|
||||
.send(
|
||||
db,
|
||||
MessageAuthor::System {
|
||||
username: &user.username,
|
||||
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
|
||||
},
|
||||
self,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.ok(); */
|
||||
.ok();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use iso8601_timestamp::Timestamp;
|
||||
use revolt_models::v0::{Embed, MessageSort, MessageWebhook};
|
||||
use revolt_models::v0::{Embed, MessageAuthor, MessageSort, MessageWebhook, PushNotification};
|
||||
use revolt_result::Result;
|
||||
use ulid::Ulid;
|
||||
|
||||
use crate::File;
|
||||
use crate::{
|
||||
events::client::EventV1,
|
||||
tasks::{self, ack::AckEvent},
|
||||
Channel, Database, File,
|
||||
};
|
||||
|
||||
auto_derived_partial!(
|
||||
/// Message
|
||||
@@ -165,33 +171,144 @@ auto_derived!(
|
||||
}
|
||||
);
|
||||
|
||||
#[allow(clippy::derivable_impls)]
|
||||
impl Default for Message {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Default::default(),
|
||||
nonce: None,
|
||||
channel: Default::default(),
|
||||
author: Default::default(),
|
||||
webhook: None,
|
||||
content: None,
|
||||
system: None,
|
||||
attachments: None,
|
||||
edited: None,
|
||||
embeds: None,
|
||||
mentions: None,
|
||||
replies: None,
|
||||
reactions: Default::default(),
|
||||
interactions: Default::default(),
|
||||
masquerade: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl Message {}
|
||||
|
||||
impl Interactions {
|
||||
/// Validate interactions info is correct
|
||||
/* pub async fn validate(
|
||||
&self,
|
||||
impl Message {
|
||||
/// Send a message without any notifications
|
||||
pub async fn send_without_notifications(
|
||||
&mut self,
|
||||
db: &Database,
|
||||
permissions: &mut PermissionCalculator<'_>,
|
||||
is_dm: bool,
|
||||
generate_embeds: bool,
|
||||
) -> Result<()> {
|
||||
if let Some(reactions) = &self.reactions {
|
||||
permissions.throw_permission(db, Permission::React).await?;
|
||||
db.insert_message(self).await?;
|
||||
|
||||
if reactions.len() > 20 {
|
||||
return Err(Error::InvalidOperation);
|
||||
// Fan out events
|
||||
EventV1::Message(self.clone().into())
|
||||
.p(self.channel.to_string())
|
||||
.await;
|
||||
|
||||
// Update last_message_id
|
||||
tasks::last_message_id::queue(self.channel.to_string(), self.id.to_string(), is_dm).await;
|
||||
|
||||
// Add mentions for affected users
|
||||
if let Some(mentions) = &self.mentions {
|
||||
for user in mentions {
|
||||
tasks::ack::queue(
|
||||
self.channel.to_string(),
|
||||
user.to_string(),
|
||||
AckEvent::AddMention {
|
||||
ids: vec![self.id.to_string()],
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
for reaction in reactions {
|
||||
if !Emoji::can_use(db, reaction).await? {
|
||||
return Err(Error::InvalidOperation);
|
||||
}
|
||||
// Generate embeds
|
||||
if generate_embeds {
|
||||
if let Some(content) = &self.content {
|
||||
tasks::process_embeds::queue(
|
||||
self.channel.to_string(),
|
||||
self.id.to_string(),
|
||||
content.clone(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}*/
|
||||
}
|
||||
|
||||
/// Send a message
|
||||
pub async fn send(
|
||||
&mut self,
|
||||
db: &Database,
|
||||
author: MessageAuthor<'_>,
|
||||
channel: &Channel,
|
||||
generate_embeds: bool,
|
||||
) -> Result<()> {
|
||||
self.send_without_notifications(
|
||||
db,
|
||||
matches!(channel, Channel::DirectMessage { .. }),
|
||||
generate_embeds,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Push out Web Push notifications
|
||||
crate::tasks::web_push::queue(
|
||||
{
|
||||
match channel {
|
||||
Channel::DirectMessage { recipients, .. }
|
||||
| Channel::Group { recipients, .. } => recipients.clone(),
|
||||
Channel::TextChannel { .. } => self.mentions.clone().unwrap_or_default(),
|
||||
_ => vec![],
|
||||
}
|
||||
},
|
||||
PushNotification::from(self.clone().into(), Some(author), &channel.id()).await,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Append content to message
|
||||
pub async fn append(
|
||||
db: &Database,
|
||||
id: String,
|
||||
channel: String,
|
||||
append: AppendMessage,
|
||||
) -> Result<()> {
|
||||
db.append_message(&id, &append).await?;
|
||||
|
||||
EventV1::MessageAppend {
|
||||
id,
|
||||
channel: channel.to_string(),
|
||||
append: append.into(),
|
||||
}
|
||||
.p(channel)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl SystemMessage {
|
||||
pub fn into_message(self, channel: String) -> Message {
|
||||
Message {
|
||||
id: Ulid::new().to_string(),
|
||||
channel,
|
||||
author: "00000000000000000000000000".to_string(),
|
||||
system: Some(self),
|
||||
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Interactions {
|
||||
/// Check if we can use a given emoji to react
|
||||
pub fn can_use(&self, emoji: &str) -> bool {
|
||||
if self.restrict_reactions {
|
||||
|
||||
@@ -2,8 +2,8 @@ use revolt_result::Result;
|
||||
|
||||
use crate::{AppendMessage, Message, MessageQuery, PartialMessage};
|
||||
|
||||
// mod mongodb;
|
||||
// mod reference;
|
||||
mod mongodb;
|
||||
mod reference;
|
||||
|
||||
#[async_trait]
|
||||
pub trait AbstractMessages: Sync + Send {
|
||||
@@ -35,5 +35,5 @@ pub trait AbstractMessages: Sync + Send {
|
||||
async fn delete_message(&self, id: &str) -> Result<()>;
|
||||
|
||||
/// Delete messages from a channel by their ids and corresponding channel id
|
||||
async fn delete_messages(&self, channel: &str, ids: Vec<String>) -> Result<()>;
|
||||
async fn delete_messages(&self, channel: &str, ids: &[String]) -> Result<()>;
|
||||
}
|
||||
|
||||
@@ -1,64 +1,214 @@
|
||||
use bson::Document;
|
||||
use bson::{to_bson, Document};
|
||||
use futures::try_join;
|
||||
use mongodb::options::FindOptions;
|
||||
use revolt_models::v0::MessageSort;
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::Emoji;
|
||||
use crate::MongoDb;
|
||||
use crate::{AppendMessage, Message, MessageQuery, MessageTimePeriod, MongoDb, PartialMessage};
|
||||
|
||||
use super::AbstractEmojis;
|
||||
use super::AbstractMessages;
|
||||
|
||||
static COL: &str = "emojis";
|
||||
static COL: &str = "messages";
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractEmojis for MongoDb {
|
||||
/// Insert emoji into database.
|
||||
async fn insert_emoji(&self, emoji: &Emoji) -> Result<()> {
|
||||
query!(self, insert_one, COL, &emoji).map(|_| ())
|
||||
impl AbstractMessages for MongoDb {
|
||||
/// Insert a new message into the database
|
||||
async fn insert_message(&self, message: &Message) -> Result<()> {
|
||||
query!(self, insert_one, COL, &message).map(|_| ())
|
||||
}
|
||||
|
||||
/// Fetch an emoji by its id
|
||||
async fn fetch_emoji(&self, id: &str) -> Result<Emoji> {
|
||||
/// Fetch a message by its id
|
||||
async fn fetch_message(&self, id: &str) -> Result<Message> {
|
||||
query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound))
|
||||
}
|
||||
|
||||
/// Fetch emoji by their parent id
|
||||
async fn fetch_emoji_by_parent_id(&self, parent_id: &str) -> Result<Vec<Emoji>> {
|
||||
query!(
|
||||
self,
|
||||
find_one,
|
||||
COL,
|
||||
doc! {
|
||||
"parent.id": parent_id
|
||||
}
|
||||
)?
|
||||
.ok_or_else(|| create_error!(NotFound))
|
||||
}
|
||||
/// Fetch multiple messages by given query
|
||||
async fn fetch_messages(&self, query: MessageQuery) -> Result<Vec<Message>> {
|
||||
let mut filter = doc! {};
|
||||
|
||||
/// Fetch emoji by their parent ids
|
||||
async fn fetch_emoji_by_parent_ids(&self, parent_ids: &[String]) -> Result<Vec<Emoji>> {
|
||||
query!(
|
||||
self,
|
||||
find,
|
||||
COL,
|
||||
doc! {
|
||||
"parent.id": {
|
||||
"$in": parent_ids
|
||||
// 1. Apply message filters
|
||||
if let Some(channel) = query.filter.channel {
|
||||
filter.insert("channel", channel);
|
||||
}
|
||||
|
||||
if let Some(author) = query.filter.author {
|
||||
filter.insert("author", author);
|
||||
}
|
||||
|
||||
let is_search_query = if let Some(query) = query.filter.query {
|
||||
filter.insert(
|
||||
"$text",
|
||||
doc! {
|
||||
"$search": query
|
||||
},
|
||||
);
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
// 2. Find query limit
|
||||
let limit = query.limit.unwrap_or(50);
|
||||
|
||||
// 3. Apply message time period
|
||||
match query.time_period {
|
||||
MessageTimePeriod::Relative { nearby } => {
|
||||
// 3.1. Prepare filters
|
||||
let mut older_message_filter = filter.clone();
|
||||
let mut newer_message_filter = filter;
|
||||
|
||||
older_message_filter.insert(
|
||||
"_id",
|
||||
doc! {
|
||||
"$lt": &nearby
|
||||
},
|
||||
);
|
||||
|
||||
newer_message_filter.insert(
|
||||
"_id",
|
||||
doc! {
|
||||
"$gte": &nearby
|
||||
},
|
||||
);
|
||||
|
||||
// 3.2. Execute in both directions
|
||||
let (a, b) = try_join!(
|
||||
self.find_with_options::<_, Message>(
|
||||
COL,
|
||||
newer_message_filter,
|
||||
FindOptions::builder()
|
||||
.limit(limit / 2 + 1)
|
||||
.sort(doc! {
|
||||
"_id": 1_i32
|
||||
})
|
||||
.build(),
|
||||
),
|
||||
self.find_with_options::<_, Message>(
|
||||
COL,
|
||||
older_message_filter,
|
||||
FindOptions::builder()
|
||||
.limit(limit / 2)
|
||||
.sort(doc! {
|
||||
"_id": -1_i32
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
)
|
||||
.map_err(|_| create_database_error!("find", COL))?;
|
||||
|
||||
Ok([a, b].concat())
|
||||
}
|
||||
MessageTimePeriod::Absolute {
|
||||
before,
|
||||
after,
|
||||
sort,
|
||||
} => {
|
||||
// 3.1. Apply message ID filter
|
||||
if let Some(doc) = match (before, after) {
|
||||
(Some(before), Some(after)) => Some(doc! {
|
||||
"$lt": before,
|
||||
"$gt": after
|
||||
}),
|
||||
(Some(before), _) => Some(doc! {
|
||||
"$lt": before
|
||||
}),
|
||||
(_, Some(after)) => Some(doc! {
|
||||
"$gt": after
|
||||
}),
|
||||
_ => None,
|
||||
} {
|
||||
filter.insert("_id", doc);
|
||||
}
|
||||
|
||||
// 3.2. Execute with given message sort
|
||||
self.find_with_options(
|
||||
COL,
|
||||
filter,
|
||||
FindOptions::builder()
|
||||
.limit(limit)
|
||||
.sort(match sort.unwrap_or(MessageSort::Latest) {
|
||||
// Sort by relevance, fallback to latest
|
||||
MessageSort::Relevance => {
|
||||
if is_search_query {
|
||||
doc! {
|
||||
"score": {
|
||||
"$meta": "textScore"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
doc! {
|
||||
"_id": -1_i32
|
||||
}
|
||||
}
|
||||
}
|
||||
// Sort by latest first
|
||||
MessageSort::Latest => doc! {
|
||||
"_id": -1_i32
|
||||
},
|
||||
// Sort by oldest first
|
||||
MessageSort::Oldest => doc! {
|
||||
"_id": 1_i32
|
||||
},
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| create_database_error!("find", COL))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Detach an emoji by its id
|
||||
async fn detach_emoji(&self, emoji: &Emoji) -> Result<()> {
|
||||
/// Update a given message with new information
|
||||
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
|
||||
query!(self, update_one_by_id, COL, id, message, vec![], None).map(|_| ())
|
||||
}
|
||||
|
||||
/// Append information to a given message
|
||||
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()> {
|
||||
let mut query = doc! {};
|
||||
|
||||
if let Some(embeds) = &append.embeds {
|
||||
if !embeds.is_empty() {
|
||||
query.insert(
|
||||
"$push",
|
||||
doc! {
|
||||
"embeds": {
|
||||
"$each": to_bson(embeds)
|
||||
.map_err(|_| create_database_error!("to_bson", "embeds"))?
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if query.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.col::<Document>(COL)
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": &emoji.id
|
||||
"_id": id
|
||||
},
|
||||
query,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|_| create_database_error!("update_one", COL))
|
||||
}
|
||||
|
||||
/// Add a new reaction to a message
|
||||
async fn add_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()> {
|
||||
self.col::<Document>(COL)
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": id
|
||||
},
|
||||
doc! {
|
||||
"$set": {
|
||||
"parent": {
|
||||
"type": "Detached"
|
||||
}
|
||||
"$addToSet": {
|
||||
format!("reactions.{emoji}"): user
|
||||
}
|
||||
},
|
||||
None,
|
||||
@@ -67,4 +217,64 @@ impl AbstractEmojis for MongoDb {
|
||||
.map(|_| ())
|
||||
.map_err(|_| create_database_error!("update_one", COL))
|
||||
}
|
||||
|
||||
/// Remove a reaction from a message
|
||||
async fn remove_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()> {
|
||||
self.col::<Document>(COL)
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": id
|
||||
},
|
||||
doc! {
|
||||
"$pull": {
|
||||
format!("reactions.{emoji}"): user
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|_| create_database_error!("update_one", COL))
|
||||
}
|
||||
|
||||
/// Remove reaction from a message
|
||||
async fn clear_reaction(&self, id: &str, emoji: &str) -> Result<()> {
|
||||
self.col::<Document>(COL)
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": id
|
||||
},
|
||||
doc! {
|
||||
"$unset": {
|
||||
format!("reactions.{emoji}"): 1
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|_| create_database_error!("update_one", COL))
|
||||
}
|
||||
|
||||
/// Delete a message from the database by its id
|
||||
async fn delete_message(&self, id: &str) -> Result<()> {
|
||||
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
||||
}
|
||||
|
||||
/// Delete messages from a channel by their ids and corresponding channel id
|
||||
async fn delete_messages(&self, channel: &str, ids: &[String]) -> Result<()> {
|
||||
self.col::<Document>(COL)
|
||||
.delete_many(
|
||||
doc! {
|
||||
"channel": channel,
|
||||
"_id": {
|
||||
"$in": ids
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|_| create_database_error!("delete_many", COL))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +1,272 @@
|
||||
use indexmap::IndexSet;
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::Emoji;
|
||||
use crate::EmojiParent;
|
||||
use crate::ReferenceDb;
|
||||
use crate::{AppendMessage, Message, MessageQuery, PartialMessage, ReferenceDb};
|
||||
|
||||
use super::AbstractEmojis;
|
||||
use super::AbstractMessages;
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractEmojis for ReferenceDb {
|
||||
/// Insert emoji into database.
|
||||
async fn insert_emoji(&self, emoji: &Emoji) -> Result<()> {
|
||||
let mut emojis = self.emojis.lock().await;
|
||||
if emojis.contains_key(&emoji.id) {
|
||||
Err(create_database_error!("insert", "emoji"))
|
||||
impl AbstractMessages for ReferenceDb {
|
||||
/// Insert a new message into the database
|
||||
async fn insert_message(&self, message: &Message) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if messages.contains_key(&message.id) {
|
||||
Err(create_database_error!("insert", "message"))
|
||||
} else {
|
||||
emojis.insert(emoji.id.to_string(), emoji.clone());
|
||||
messages.insert(message.id.to_string(), message.clone());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch an emoji by its id
|
||||
async fn fetch_emoji(&self, id: &str) -> Result<Emoji> {
|
||||
let emojis = self.emojis.lock().await;
|
||||
emojis
|
||||
/// Fetch a message by its id
|
||||
async fn fetch_message(&self, id: &str) -> Result<Message> {
|
||||
let messages = self.messages.lock().await;
|
||||
messages
|
||||
.get(id)
|
||||
.cloned()
|
||||
.ok_or_else(|| create_error!(NotFound))
|
||||
}
|
||||
|
||||
/// Fetch emoji by their parent id
|
||||
async fn fetch_emoji_by_parent_id(&self, parent_id: &str) -> Result<Vec<Emoji>> {
|
||||
let emojis = self.emojis.lock().await;
|
||||
Ok(emojis
|
||||
/// Fetch multiple messages by given query
|
||||
async fn fetch_messages(&self, query: MessageQuery) -> Result<Vec<Message>> {
|
||||
let messages = self.messages.lock().await;
|
||||
let matched_messages = messages
|
||||
.values()
|
||||
.filter(|emoji| match &emoji.parent {
|
||||
EmojiParent::Server { id } => id == parent_id,
|
||||
_ => false,
|
||||
.filter(|message| {
|
||||
if let Some(channel) = &query.filter.channel {
|
||||
if &message.channel != channel {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(author) = &query.filter.author {
|
||||
if &message.author != author {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(query) = &query.filter.query {
|
||||
if let Some(content) = &message.content {
|
||||
if !content.to_lowercase().contains(query) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
})
|
||||
.cloned()
|
||||
.collect())
|
||||
.collect();
|
||||
|
||||
// TODO: sorting, etc
|
||||
|
||||
Ok(matched_messages)
|
||||
|
||||
/*
|
||||
// 2. Find query limit
|
||||
let limit = query.limit.unwrap_or(50);
|
||||
|
||||
// 3. Apply message time period
|
||||
match query.time_period {
|
||||
MessageTimePeriod::Relative { nearby } => {
|
||||
// 3.1. Prepare filters
|
||||
let mut older_message_filter = filter.clone();
|
||||
let mut newer_message_filter = filter;
|
||||
|
||||
older_message_filter.insert(
|
||||
"_id",
|
||||
doc! {
|
||||
"$lt": &nearby
|
||||
},
|
||||
);
|
||||
|
||||
newer_message_filter.insert(
|
||||
"_id",
|
||||
doc! {
|
||||
"$gte": &nearby
|
||||
},
|
||||
);
|
||||
|
||||
// 3.2. Execute in both directions
|
||||
let (a, b) = try_join!(
|
||||
self.find_with_options::<_, Message>(
|
||||
COL,
|
||||
newer_message_filter,
|
||||
FindOptions::builder()
|
||||
.limit(limit / 2 + 1)
|
||||
.sort(doc! {
|
||||
"_id": 1_i32
|
||||
})
|
||||
.build(),
|
||||
),
|
||||
self.find_with_options::<_, Message>(
|
||||
COL,
|
||||
older_message_filter,
|
||||
FindOptions::builder()
|
||||
.limit(limit / 2)
|
||||
.sort(doc! {
|
||||
"_id": -1_i32
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
)
|
||||
.map_err(|_| create_database_error!("find", COL))?;
|
||||
|
||||
Ok([a, b].concat())
|
||||
}
|
||||
MessageTimePeriod::Absolute {
|
||||
before,
|
||||
after,
|
||||
sort,
|
||||
} => {
|
||||
// 3.1. Apply message ID filter
|
||||
if let Some(doc) = match (before, after) {
|
||||
(Some(before), Some(after)) => Some(doc! {
|
||||
"$lt": before,
|
||||
"$gt": after
|
||||
}),
|
||||
(Some(before), _) => Some(doc! {
|
||||
"$lt": before
|
||||
}),
|
||||
(_, Some(after)) => Some(doc! {
|
||||
"$gt": after
|
||||
}),
|
||||
_ => None,
|
||||
} {
|
||||
filter.insert("_id", doc);
|
||||
}
|
||||
|
||||
// 3.2. Execute with given message sort
|
||||
self.find_with_options(
|
||||
COL,
|
||||
filter,
|
||||
FindOptions::builder()
|
||||
.limit(limit)
|
||||
.sort(match sort.unwrap_or(MessageSort::Latest) {
|
||||
// Sort by relevance, fallback to latest
|
||||
MessageSort::Relevance => {
|
||||
if is_search_query {
|
||||
doc! {
|
||||
"score": {
|
||||
"$meta": "textScore"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
doc! {
|
||||
"_id": -1_i32
|
||||
}
|
||||
}
|
||||
}
|
||||
// Sort by latest first
|
||||
MessageSort::Latest => doc! {
|
||||
"_id": -1_i32
|
||||
},
|
||||
// Sort by oldest first
|
||||
MessageSort::Oldest => doc! {
|
||||
"_id": 1_i32
|
||||
},
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| create_database_error!("find", COL))
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
/// Fetch emoji by their parent ids
|
||||
async fn fetch_emoji_by_parent_ids(&self, parent_ids: &[String]) -> Result<Vec<Emoji>> {
|
||||
let emojis = self.emojis.lock().await;
|
||||
Ok(emojis
|
||||
.values()
|
||||
.filter(|emoji| match &emoji.parent {
|
||||
EmojiParent::Server { id } => parent_ids.contains(id),
|
||||
_ => false,
|
||||
})
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Detach an emoji by its id
|
||||
async fn detach_emoji(&self, emoji: &Emoji) -> Result<()> {
|
||||
let mut emojis = self.emojis.lock().await;
|
||||
if let Some(bot) = emojis.get_mut(&emoji.id) {
|
||||
bot.parent = EmojiParent::Detached;
|
||||
/// Update a given message with new information
|
||||
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if let Some(message_data) = messages.get_mut(id) {
|
||||
message_data.apply_options(message.to_owned());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Append information to a given message
|
||||
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if let Some(message_data) = messages.get_mut(id) {
|
||||
if let Some(embeds) = &append.embeds {
|
||||
if !embeds.is_empty() {
|
||||
if let Some(embeds_data) = &mut message_data.embeds {
|
||||
embeds_data.extend(embeds.clone());
|
||||
} else {
|
||||
message_data.embeds = Some(embeds.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a new reaction to a message
|
||||
async fn add_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if let Some(message) = messages.get_mut(id) {
|
||||
if let Some(users) = message.reactions.get_mut(emoji) {
|
||||
users.insert(user.to_string());
|
||||
} else {
|
||||
message
|
||||
.reactions
|
||||
.insert(emoji.to_string(), IndexSet::from([user.to_string()]));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a reaction from a message
|
||||
async fn remove_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if let Some(message) = messages.get_mut(id) {
|
||||
if let Some(users) = message.reactions.get_mut(emoji) {
|
||||
users.remove(&user.to_string());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove reaction from a message
|
||||
async fn clear_reaction(&self, id: &str, emoji: &str) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if let Some(message) = messages.get_mut(id) {
|
||||
message.reactions.remove(emoji);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a message from the database by its id
|
||||
async fn delete_message(&self, id: &str) -> Result<()> {
|
||||
let mut messages = self.messages.lock().await;
|
||||
if messages.remove(id).is_some() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete messages from a channel by their ids and corresponding channel id
|
||||
async fn delete_messages(&self, channel: &str, ids: &[String]) -> Result<()> {
|
||||
self.messages
|
||||
.lock()
|
||||
.await
|
||||
.retain(|id, message| message.channel != channel && !ids.contains(id));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ pub trait AbstractDatabase:
|
||||
+ channel_webhooks::AbstractWebhooks
|
||||
+ emojis::AbstractEmojis
|
||||
+ files::AbstractAttachments
|
||||
+ messages::AbstractMessages
|
||||
+ ratelimit_events::AbstractRatelimitEvents
|
||||
+ server_bans::AbstractServerBans
|
||||
+ server_members::AbstractServerMembers
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
use std::fmt;
|
||||
|
||||
use revolt_result::Result;
|
||||
use ulid::Ulid;
|
||||
|
||||
use crate::Database;
|
||||
|
||||
auto_derived!(
|
||||
/// Ratelimit Event
|
||||
pub struct RatelimitEvent {
|
||||
@@ -23,3 +28,20 @@ impl fmt::Display for RatelimitEventType {
|
||||
fmt::Debug::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl RatelimitEvent {
|
||||
/// Create ratelimit event
|
||||
pub async fn create(
|
||||
db: &Database,
|
||||
target_id: String,
|
||||
event_type: RatelimitEventType,
|
||||
) -> Result<()> {
|
||||
db.insert_ratelimit_event(&RatelimitEvent {
|
||||
id: Ulid::new().to_string(),
|
||||
target_id,
|
||||
event_type,
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use iso8601_timestamp::Timestamp;
|
||||
use revolt_result::Result;
|
||||
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
||||
use revolt_result::{create_error, Result};
|
||||
|
||||
use crate::{Database, File, Server};
|
||||
use crate::{
|
||||
events::client::EventV1, util::permissions::DatabasePermissionQuery, Database, File, Server,
|
||||
SystemMessage, User,
|
||||
};
|
||||
|
||||
auto_derived_partial!(
|
||||
/// Server Member
|
||||
@@ -11,8 +15,7 @@ auto_derived_partial!(
|
||||
pub id: MemberCompositeKey,
|
||||
|
||||
/// Time at which this user joined the server
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub joined_at: Option<Timestamp>,
|
||||
pub joined_at: Timestamp,
|
||||
|
||||
/// Member's nickname
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -57,7 +60,102 @@ auto_derived!(
|
||||
}
|
||||
);
|
||||
|
||||
impl Default for Member {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Default::default(),
|
||||
joined_at: Timestamp::now_utc(),
|
||||
nickname: None,
|
||||
avatar: None,
|
||||
roles: vec![],
|
||||
timeout: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl Member {
|
||||
/// Create a new member in a server
|
||||
pub async fn create(
|
||||
db: &Database,
|
||||
server: &Server,
|
||||
user: &User,
|
||||
// channels: Option<Vec<Channel>>,
|
||||
//) -> Result<Vec<Channel>> {
|
||||
) -> Result<()> {
|
||||
if db.fetch_ban(&server.id, &user.id).await.is_ok() {
|
||||
return Err(create_error!(Banned));
|
||||
}
|
||||
|
||||
if db.fetch_member(&server.id, &user.id).await.is_ok() {
|
||||
return Err(create_error!(AlreadyInServer));
|
||||
}
|
||||
|
||||
let member = Member {
|
||||
id: MemberCompositeKey {
|
||||
server: server.id.to_string(),
|
||||
user: user.id.to_string(),
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
db.insert_member(&member).await?;
|
||||
|
||||
let mut channels = vec![];
|
||||
|
||||
if true {
|
||||
let query = DatabasePermissionQuery::new(db, user).server(server);
|
||||
let existing_channels = db.fetch_channels(&server.channels).await?;
|
||||
|
||||
for channel in existing_channels {
|
||||
let mut channel_query = query.clone().channel(&channel);
|
||||
|
||||
if calculate_channel_permissions(&mut channel_query)
|
||||
.await
|
||||
.has_channel_permission(ChannelPermission::ViewChannel)
|
||||
{
|
||||
channels.push(channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EventV1::ServerMemberJoin {
|
||||
id: server.id.clone(),
|
||||
user: user.id.clone(),
|
||||
}
|
||||
.p(server.id.clone())
|
||||
.await;
|
||||
|
||||
EventV1::ServerCreate {
|
||||
id: server.id.clone(),
|
||||
server: server.clone().into(),
|
||||
channels: channels
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|channel| channel.into())
|
||||
.collect(),
|
||||
}
|
||||
.private(user.id.clone())
|
||||
.await;
|
||||
|
||||
if let Some(id) = server
|
||||
.system_messages
|
||||
.as_ref()
|
||||
.and_then(|x| x.user_joined.as_ref())
|
||||
{
|
||||
SystemMessage::UserJoined {
|
||||
id: user.id.clone(),
|
||||
}
|
||||
.into_message(id.to_string())
|
||||
.send_without_notifications(db, false, false)
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
// Ok(channels)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Update member data
|
||||
pub async fn update<'a>(
|
||||
&mut self,
|
||||
@@ -73,13 +171,13 @@ impl Member {
|
||||
|
||||
db.update_member(&self.id, &partial, remove.clone()).await?;
|
||||
|
||||
/* // TODO: EventV1::ServerMemberUpdate {
|
||||
id: self.id.clone(),
|
||||
data: partial,
|
||||
clear: remove,
|
||||
EventV1::ServerMemberUpdate {
|
||||
id: self.id.clone().into(),
|
||||
data: partial.into(),
|
||||
clear: remove.into_iter().map(|field| field.into()).collect(),
|
||||
}
|
||||
.p(self.id.server.clone())
|
||||
.await; */
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -128,6 +128,7 @@ auto_derived!(
|
||||
}
|
||||
);
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl Server {
|
||||
/// Create a server
|
||||
pub async fn create(&self, db: &Database) -> Result<()> {
|
||||
|
||||
@@ -2,8 +2,12 @@ mod model;
|
||||
mod ops;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
mod rocket;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
mod schema;
|
||||
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
pub use self::rocket::*;
|
||||
#[cfg(feature = "rocket-impl")]
|
||||
pub use self::schema::*;
|
||||
pub use model::*;
|
||||
pub use ops::*;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use std::collections::HashSet;
|
||||
use std::{collections::HashSet, time::Duration};
|
||||
|
||||
use crate::{Database, File};
|
||||
use crate::{events::client::EventV1, Database, File, RatelimitEvent};
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use revolt_result::{Error, ErrorType, Result};
|
||||
use rand::seq::SliceRandom;
|
||||
use revolt_result::{create_error, Error, ErrorType, Result};
|
||||
use ulid::Ulid;
|
||||
|
||||
auto_derived_partial!(
|
||||
/// # User
|
||||
@@ -49,6 +51,15 @@ auto_derived_partial!(
|
||||
);
|
||||
|
||||
auto_derived!(
|
||||
/// Optional fields on user object
|
||||
pub enum FieldsUser {
|
||||
Avatar,
|
||||
StatusText,
|
||||
StatusPresence,
|
||||
ProfileContent,
|
||||
ProfileBackground,
|
||||
}
|
||||
|
||||
/// User's relationship with another user (or themselves)
|
||||
pub enum RelationshipStatus {
|
||||
None,
|
||||
@@ -106,18 +117,202 @@ auto_derived!(
|
||||
/// Id of the owner of this bot
|
||||
pub owner: String,
|
||||
}
|
||||
|
||||
/// Optional fields on user object
|
||||
pub enum FieldsUser {
|
||||
Avatar,
|
||||
StatusText,
|
||||
StatusPresence,
|
||||
ProfileContent,
|
||||
ProfileBackground,
|
||||
}
|
||||
);
|
||||
|
||||
pub static DISCRIMINATOR_SEARCH_SPACE: Lazy<HashSet<String>> = Lazy::new(|| {
|
||||
let mut set = (2..9999)
|
||||
.map(|v| format!("{:0>4}", v))
|
||||
.collect::<HashSet<String>>();
|
||||
|
||||
for discrim in [
|
||||
123, 1234, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999,
|
||||
] {
|
||||
set.remove(&format!("{:0>4}", discrim));
|
||||
}
|
||||
|
||||
set.into_iter().collect()
|
||||
});
|
||||
|
||||
#[allow(clippy::derivable_impls)]
|
||||
impl Default for User {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Default::default(),
|
||||
username: Default::default(),
|
||||
discriminator: Default::default(),
|
||||
display_name: Default::default(),
|
||||
avatar: Default::default(),
|
||||
relations: Default::default(),
|
||||
badges: Default::default(),
|
||||
status: Default::default(),
|
||||
profile: Default::default(),
|
||||
flags: Default::default(),
|
||||
privileged: Default::default(),
|
||||
bot: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl User {
|
||||
/// Create a new user
|
||||
pub async fn create<I, D>(
|
||||
db: &Database,
|
||||
username: String,
|
||||
account_id: I,
|
||||
data: D,
|
||||
) -> Result<User>
|
||||
where
|
||||
I: Into<Option<String>>,
|
||||
D: Into<Option<PartialUser>>,
|
||||
{
|
||||
let username = User::validate_username(username)?;
|
||||
let mut user = User {
|
||||
id: account_id.into().unwrap_or_else(|| Ulid::new().to_string()),
|
||||
discriminator: User::find_discriminator(db, &username, None).await?,
|
||||
username,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(data) = data.into() {
|
||||
user.apply_options(data);
|
||||
}
|
||||
|
||||
db.insert_user(&user).await?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
/// Check whether two users have a mutual connection
|
||||
///
|
||||
/// This will check if user and user_b share a server or a group.
|
||||
pub async fn has_mutual_connection(&self, db: &Database, user_b: &str) -> Result<bool> {
|
||||
Ok(!db
|
||||
.fetch_mutual_server_ids(&self.id, user_b)
|
||||
.await?
|
||||
.is_empty()
|
||||
|| !db
|
||||
.fetch_mutual_channel_ids(&self.id, user_b)
|
||||
.await?
|
||||
.is_empty())
|
||||
}
|
||||
|
||||
/// Sanitise and validate a username can be used
|
||||
pub fn validate_username(username: String) -> Result<String> {
|
||||
// Copy the username for validation
|
||||
let username_lowercase = username.to_lowercase();
|
||||
|
||||
// Block homoglyphs
|
||||
if decancer::cure(&username_lowercase).into_str() != username_lowercase {
|
||||
return Err(create_error!(InvalidUsername));
|
||||
}
|
||||
|
||||
// Ensure the username itself isn't blocked
|
||||
const BLOCKED_USERNAMES: &[&str] = &["admin", "revolt"];
|
||||
|
||||
for username in BLOCKED_USERNAMES {
|
||||
if username_lowercase == *username {
|
||||
return Err(create_error!(InvalidUsername));
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure none of the following substrings show up in the username
|
||||
const BLOCKED_SUBSTRINGS: &[&str] = &["```"];
|
||||
|
||||
for substr in BLOCKED_SUBSTRINGS {
|
||||
if username_lowercase.contains(substr) {
|
||||
return Err(create_error!(InvalidUsername));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(username)
|
||||
}
|
||||
|
||||
// Find a free discriminator for a given username
|
||||
pub async fn find_discriminator(
|
||||
db: &Database,
|
||||
username: &str,
|
||||
preferred: Option<(String, String)>,
|
||||
) -> Result<String> {
|
||||
let search_space: &HashSet<String> = &DISCRIMINATOR_SEARCH_SPACE;
|
||||
let used_discriminators: HashSet<String> = db
|
||||
.fetch_discriminators_in_use(username)
|
||||
.await?
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let available_discriminators: Vec<&String> =
|
||||
search_space.difference(&used_discriminators).collect();
|
||||
|
||||
if available_discriminators.is_empty() {
|
||||
return Err(create_error!(UsernameTaken));
|
||||
}
|
||||
|
||||
if let Some((preferred, target_id)) = preferred {
|
||||
if available_discriminators.contains(&&preferred) {
|
||||
return Ok(preferred);
|
||||
} else {
|
||||
if db
|
||||
.has_ratelimited(
|
||||
&target_id,
|
||||
crate::RatelimitEventType::DiscriminatorChange,
|
||||
Duration::from_secs(60 * 60 * 24),
|
||||
1,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
return Err(create_error!(DiscriminatorChangeRatelimited));
|
||||
}
|
||||
|
||||
RatelimitEvent::create(
|
||||
db,
|
||||
target_id,
|
||||
crate::RatelimitEventType::DiscriminatorChange,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
Ok(available_discriminators
|
||||
.choose(&mut rng)
|
||||
.expect("we can assert this has an element")
|
||||
.to_string())
|
||||
}
|
||||
|
||||
/// Update a user's username
|
||||
pub async fn update_username(&mut self, db: &Database, username: String) -> Result<()> {
|
||||
let username = User::validate_username(username)?;
|
||||
if self.username.to_lowercase() == username.to_lowercase() {
|
||||
self.update(
|
||||
db,
|
||||
PartialUser {
|
||||
username: Some(username),
|
||||
..Default::default()
|
||||
},
|
||||
vec![],
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
self.update(
|
||||
db,
|
||||
PartialUser {
|
||||
discriminator: Some(
|
||||
User::find_discriminator(
|
||||
db,
|
||||
&username,
|
||||
Some((self.discriminator.to_string(), self.id.clone())),
|
||||
)
|
||||
.await?,
|
||||
),
|
||||
username: Some(username),
|
||||
..Default::default()
|
||||
},
|
||||
vec![],
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// Check whether a username is already in use by another user
|
||||
#[allow(dead_code)]
|
||||
async fn is_username_taken(db: &Database, username: &str) -> Result<bool> {
|
||||
@@ -145,13 +340,14 @@ impl User {
|
||||
self.apply_options(partial.clone());
|
||||
db.update_user(&self.id, &partial, remove.clone()).await?;
|
||||
|
||||
/* // TODO: EventV1::UserUpdate {
|
||||
EventV1::UserUpdate {
|
||||
id: self.id.clone(),
|
||||
data: partial,
|
||||
clear: remove,
|
||||
data: partial.into(),
|
||||
clear: remove.into_iter().map(|v| v.into()).collect(),
|
||||
event_id: Some(Ulid::new().to_string()),
|
||||
}
|
||||
.p_user(self.id.clone(), db)
|
||||
.await; */
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -203,17 +399,3 @@ impl User {
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub static DISCRIMINATOR_SEARCH_SPACE: Lazy<HashSet<String>> = Lazy::new(|| {
|
||||
let mut set = (2..9999)
|
||||
.map(|v| format!("{:0>4}", v))
|
||||
.collect::<HashSet<String>>();
|
||||
|
||||
for discrim in [
|
||||
123, 1234, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999,
|
||||
] {
|
||||
set.remove(&format!("{:0>4}", discrim));
|
||||
}
|
||||
|
||||
set.into_iter().collect()
|
||||
});
|
||||
|
||||
@@ -22,6 +22,9 @@ pub trait AbstractUsers: Sync + Send {
|
||||
/// Fetch multiple users by their ids
|
||||
async fn fetch_users<'a>(&self, ids: &'a [String]) -> Result<Vec<User>>;
|
||||
|
||||
/// Fetch all discriminators in use for a username
|
||||
async fn fetch_discriminators_in_use(&self, username: &str) -> Result<Vec<String>>;
|
||||
|
||||
/// Fetch ids of users that both users are friends with
|
||||
async fn fetch_mutual_user_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>>;
|
||||
|
||||
|
||||
@@ -87,6 +87,39 @@ impl AbstractUsers for MongoDb {
|
||||
.await)
|
||||
}
|
||||
|
||||
/// Fetch all discriminators in use for a username
|
||||
async fn fetch_discriminators_in_use(&self, username: &str) -> Result<Vec<String>> {
|
||||
#[derive(Deserialize)]
|
||||
struct UserDocument {
|
||||
discriminator: String,
|
||||
}
|
||||
|
||||
Ok(self
|
||||
.col::<UserDocument>(COL)
|
||||
.find(
|
||||
doc! {
|
||||
"username": username
|
||||
},
|
||||
FindOptions::builder()
|
||||
.collation(
|
||||
Collation::builder()
|
||||
.locale("en")
|
||||
.strength(CollationStrength::Secondary)
|
||||
.build(),
|
||||
)
|
||||
.projection(doc! { "_id": 0, "discriminator": 1 })
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| create_database_error!("find", COL))?
|
||||
.filter_map(|s| async { s.ok() })
|
||||
.collect::<Vec<UserDocument>>()
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|user| user.discriminator)
|
||||
.collect::<Vec<String>>())
|
||||
}
|
||||
|
||||
/// Fetch ids of users that both users are friends with
|
||||
async fn fetch_mutual_user_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
||||
Ok(self
|
||||
|
||||
@@ -56,6 +56,18 @@ impl AbstractUsers for ReferenceDb {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Fetch all discriminators in use for a username
|
||||
async fn fetch_discriminators_in_use(&self, username: &str) -> Result<Vec<String>> {
|
||||
let users = self.users.lock().await;
|
||||
let lowercase = username.to_lowercase();
|
||||
Ok(users
|
||||
.values()
|
||||
.filter(|user| user.username.to_lowercase() == lowercase)
|
||||
.map(|user| &user.discriminator)
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Fetch ids of users that both users are friends with
|
||||
async fn fetch_mutual_user_ids(&self, _user_a: &str, _user_b: &str) -> Result<Vec<String>> {
|
||||
todo!()
|
||||
|
||||
31
crates/core/database/src/models/users/schema.rs
Normal file
31
crates/core/database/src/models/users/schema.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use revolt_okapi::openapi3::{SecurityScheme, SecuritySchemeData};
|
||||
use revolt_rocket_okapi::{
|
||||
gen::OpenApiGenerator,
|
||||
request::{OpenApiFromRequest, RequestHeaderInput},
|
||||
};
|
||||
|
||||
use crate::User;
|
||||
|
||||
impl<'r> OpenApiFromRequest<'r> for User {
|
||||
fn from_request_input(
|
||||
_gen: &mut OpenApiGenerator,
|
||||
_name: String,
|
||||
_required: bool,
|
||||
) -> revolt_rocket_okapi::Result<RequestHeaderInput> {
|
||||
let mut requirements = schemars::Map::new();
|
||||
requirements.insert("Session Token".to_owned(), vec![]);
|
||||
|
||||
Ok(RequestHeaderInput::Security(
|
||||
"Session Token".to_owned(),
|
||||
SecurityScheme {
|
||||
data: SecuritySchemeData::ApiKey {
|
||||
name: "x-session-token".to_owned(),
|
||||
location: "header".to_owned(),
|
||||
},
|
||||
description: Some("Used to authenticate as a user.".to_owned()),
|
||||
extensions: schemars::Map::new(),
|
||||
},
|
||||
requirements,
|
||||
))
|
||||
}
|
||||
}
|
||||
122
crates/core/database/src/tasks/ack.rs
Normal file
122
crates/core/database/src/tasks/ack.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
// Queue Type: Debounced
|
||||
use crate::Database;
|
||||
|
||||
use deadqueue::limited::Queue;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
use super::DelayedTask;
|
||||
|
||||
/// Enumeration of possible events
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum AckEvent {
|
||||
/// Add mentions for a user in a channel
|
||||
AddMention {
|
||||
/// Message IDs
|
||||
ids: Vec<String>,
|
||||
},
|
||||
|
||||
/// Acknowledge message in a channel for a user
|
||||
AckMessage {
|
||||
/// Message ID
|
||||
id: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Task information
|
||||
struct Data {
|
||||
/// Channel to ack
|
||||
channel: String,
|
||||
/// User to ack for
|
||||
user: String,
|
||||
/// Event
|
||||
event: AckEvent,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Task {
|
||||
event: AckEvent,
|
||||
}
|
||||
|
||||
static Q: Lazy<Queue<Data>> = Lazy::new(|| Queue::new(10_000));
|
||||
|
||||
/// Queue a new task for a worker
|
||||
pub async fn queue(channel: String, user: String, event: AckEvent) {
|
||||
Q.try_push(Data {
|
||||
channel,
|
||||
user,
|
||||
event,
|
||||
})
|
||||
.ok();
|
||||
|
||||
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
|
||||
}
|
||||
|
||||
/// Start a new worker
|
||||
pub async fn worker(db: Database) {
|
||||
let mut tasks = HashMap::<(String, String), DelayedTask<Task>>::new();
|
||||
let mut keys = vec![];
|
||||
|
||||
loop {
|
||||
// Find due tasks.
|
||||
for (key, task) in &tasks {
|
||||
if task.should_run() {
|
||||
keys.push(key.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Commit any due tasks to the database.
|
||||
for key in &keys {
|
||||
if let Some(task) = tasks.remove(key) {
|
||||
let Task { event } = task.data;
|
||||
let (user, channel) = key;
|
||||
|
||||
if let Err(err) = match &event {
|
||||
#[allow(clippy::disallowed_methods)] // event is sent by higher level function
|
||||
AckEvent::AckMessage { id } => db.acknowledge_message(channel, user, id).await,
|
||||
AckEvent::AddMention { ids } => {
|
||||
db.add_mention_to_unread(channel, user, ids).await
|
||||
}
|
||||
} {
|
||||
error!("{err:?} for {event:?}. ({user}, {channel})");
|
||||
} else {
|
||||
info!("User {user} ack in {channel} with {event:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear keys
|
||||
keys.clear();
|
||||
|
||||
// Queue incoming tasks.
|
||||
while let Some(Data {
|
||||
channel,
|
||||
user,
|
||||
mut event,
|
||||
}) = Q.try_pop()
|
||||
{
|
||||
let key = (user, channel);
|
||||
if let Some(task) = tasks.get_mut(&key) {
|
||||
task.delay();
|
||||
|
||||
match &mut event {
|
||||
AckEvent::AddMention { ids } => {
|
||||
if let AckEvent::AddMention { ids: existing } = &mut task.data.event {
|
||||
existing.append(ids);
|
||||
} else {
|
||||
task.data.event = event;
|
||||
}
|
||||
}
|
||||
AckEvent::AckMessage { .. } => {
|
||||
task.data.event = event;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tasks.insert(key, DelayedTask::new(Task { event }));
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep for an arbitrary amount of time.
|
||||
async_std::task::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
87
crates/core/database/src/tasks/last_message_id.rs
Normal file
87
crates/core/database/src/tasks/last_message_id.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
// Queue Type: Debounced
|
||||
use deadqueue::limited::Queue;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
use crate::{Database, PartialChannel};
|
||||
|
||||
use super::DelayedTask;
|
||||
|
||||
/// Task information
|
||||
struct Data {
|
||||
/// Channel to update
|
||||
channel: String,
|
||||
/// Latest message ID
|
||||
id: String,
|
||||
/// Whether the channel is a DM
|
||||
is_dm: bool,
|
||||
}
|
||||
|
||||
/// Task information
|
||||
#[derive(Debug)]
|
||||
struct Task {
|
||||
/// Latest message ID
|
||||
id: String,
|
||||
/// Whether the channel is a DM
|
||||
is_dm: bool,
|
||||
}
|
||||
|
||||
static Q: Lazy<Queue<Data>> = Lazy::new(|| Queue::new(10_000));
|
||||
|
||||
/// Queue a new task for a worker
|
||||
pub async fn queue(channel: String, id: String, is_dm: bool) {
|
||||
Q.try_push(Data { channel, id, is_dm }).ok();
|
||||
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
|
||||
}
|
||||
|
||||
/// Start a new worker
|
||||
pub async fn worker(db: Database) {
|
||||
let mut tasks = HashMap::<String, DelayedTask<Task>>::new();
|
||||
let mut keys = vec![];
|
||||
|
||||
loop {
|
||||
// Find due tasks.
|
||||
for (key, task) in &tasks {
|
||||
if task.should_run() {
|
||||
keys.push(key.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Commit any due tasks to the database.
|
||||
for key in &keys {
|
||||
if let Some(task) = tasks.remove(key) {
|
||||
let Task { id, is_dm, .. } = task.data;
|
||||
|
||||
let mut channel = PartialChannel {
|
||||
last_message_id: Some(id.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if is_dm {
|
||||
channel.active = Some(true);
|
||||
}
|
||||
|
||||
match db.update_channel(key, &channel, vec![]).await {
|
||||
Ok(_) => info!("Updated last_message_id for {key} to {id}."),
|
||||
Err(err) => error!("Failed to update last_message_id with {err:?}!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear keys
|
||||
keys.clear();
|
||||
|
||||
// Queue incoming tasks.
|
||||
while let Some(Data { channel, id, is_dm }) = Q.try_pop() {
|
||||
if let Some(task) = tasks.get_mut(&channel) {
|
||||
task.data.id = id;
|
||||
task.delay();
|
||||
} else {
|
||||
tasks.insert(channel, DelayedTask::new(Task { id, is_dm }));
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep for an arbitrary amount of time.
|
||||
async_std::task::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
58
crates/core/database/src/tasks/mod.rs
Normal file
58
crates/core/database/src/tasks/mod.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
//! Semi-important background task management
|
||||
|
||||
use crate::Database;
|
||||
|
||||
use async_std::task;
|
||||
use std::time::Instant;
|
||||
|
||||
const WORKER_COUNT: usize = 5;
|
||||
|
||||
pub mod ack;
|
||||
pub mod last_message_id;
|
||||
pub mod process_embeds;
|
||||
pub mod web_push;
|
||||
|
||||
/// Spawn background workers
|
||||
pub async fn start_workers(db: Database) {
|
||||
for _ in 0..WORKER_COUNT {
|
||||
task::spawn(ack::worker(db.clone()));
|
||||
task::spawn(last_message_id::worker(db.clone()));
|
||||
task::spawn(process_embeds::worker(db.clone()));
|
||||
task::spawn(web_push::worker(db.clone().into()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Task with additional information on when it should run
|
||||
pub struct DelayedTask<T> {
|
||||
pub data: T,
|
||||
last_updated: Instant,
|
||||
first_seen: Instant,
|
||||
}
|
||||
|
||||
/// Commit to database every 30 seconds if the task is particularly active.
|
||||
static EXPIRE_CONSTANT: u64 = 30;
|
||||
|
||||
/// Otherwise, commit to database after 5 seconds.
|
||||
static SAVE_CONSTANT: u64 = 5;
|
||||
|
||||
impl<T> DelayedTask<T> {
|
||||
/// Create a new delayed task
|
||||
pub fn new(data: T) -> Self {
|
||||
DelayedTask {
|
||||
data,
|
||||
last_updated: Instant::now(),
|
||||
first_seen: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Push a task further back in time
|
||||
pub fn delay(&mut self) {
|
||||
self.last_updated = Instant::now()
|
||||
}
|
||||
|
||||
/// Check if a task should run yet
|
||||
pub fn should_run(&self) -> bool {
|
||||
self.first_seen.elapsed().as_secs() > EXPIRE_CONSTANT
|
||||
|| self.last_updated.elapsed().as_secs() > SAVE_CONSTANT
|
||||
}
|
||||
}
|
||||
170
crates/core/database/src/tasks/process_embeds.rs
Normal file
170
crates/core/database/src/tasks/process_embeds.rs
Normal file
@@ -0,0 +1,170 @@
|
||||
use crate::{models::Message, AppendMessage, Database};
|
||||
|
||||
use futures::future::join_all;
|
||||
use linkify::{LinkFinder, LinkKind};
|
||||
use regex::Regex;
|
||||
use revolt_config::config;
|
||||
use revolt_result::Result;
|
||||
|
||||
use async_lock::Semaphore;
|
||||
use async_std::task::spawn;
|
||||
use deadqueue::limited::Queue;
|
||||
use once_cell::sync::Lazy;
|
||||
use revolt_models::v0::Embed;
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
|
||||
use isahc::prelude::*;
|
||||
|
||||
/// Task information
|
||||
#[derive(Debug)]
|
||||
struct EmbedTask {
|
||||
/// Channel we're processing the event in
|
||||
channel: String,
|
||||
/// ID of the message we're processing
|
||||
id: String,
|
||||
/// Content of the message
|
||||
content: String,
|
||||
}
|
||||
|
||||
static Q: Lazy<Queue<EmbedTask>> = Lazy::new(|| Queue::new(10_000));
|
||||
|
||||
/// Queue a new task for a worker
|
||||
pub async fn queue(channel: String, id: String, content: String) {
|
||||
Q.try_push(EmbedTask {
|
||||
channel,
|
||||
id,
|
||||
content,
|
||||
})
|
||||
.ok();
|
||||
|
||||
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
|
||||
}
|
||||
|
||||
/// Start a new worker
|
||||
pub async fn worker(db: Database) {
|
||||
let semaphore = Arc::new(Semaphore::new(
|
||||
config().await.api.workers.max_concurrent_connections,
|
||||
));
|
||||
|
||||
loop {
|
||||
let task = Q.pop().await;
|
||||
let db = db.clone();
|
||||
let semaphore = semaphore.clone();
|
||||
|
||||
spawn(async move {
|
||||
let config = config().await;
|
||||
let embeds = generate(
|
||||
task.content,
|
||||
&config.hosts.january,
|
||||
config.features.limits.default.message_embeds,
|
||||
semaphore,
|
||||
)
|
||||
.await;
|
||||
|
||||
if let Ok(embeds) = embeds {
|
||||
if let Err(err) = Message::append(
|
||||
&db,
|
||||
task.id,
|
||||
task.channel,
|
||||
AppendMessage {
|
||||
embeds: Some(embeds),
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
error!("Encountered an error appending to message: {:?}", err);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static RE_CODE: Lazy<Regex> = Lazy::new(|| Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap());
|
||||
static RE_IGNORED: Lazy<Regex> = Lazy::new(|| Regex::new("(<http.+>)").unwrap());
|
||||
|
||||
pub async fn generate(
|
||||
content: String,
|
||||
host: &str,
|
||||
max_embeds: usize,
|
||||
semaphore: Arc<Semaphore>,
|
||||
) -> Result<Vec<Embed>> {
|
||||
// Ignore code blocks.
|
||||
let content = RE_CODE.replace_all(&content, "");
|
||||
|
||||
// Ignore all content between angle brackets starting with http.
|
||||
let content = RE_IGNORED.replace_all(&content, "");
|
||||
|
||||
let content = content
|
||||
// Ignore quoted lines.
|
||||
.split('\n')
|
||||
.map(|v| {
|
||||
if let Some(c) = v.chars().next() {
|
||||
if c == '>' {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
v
|
||||
})
|
||||
.collect::<Vec<&str>>()
|
||||
.join("\n");
|
||||
|
||||
let mut finder = LinkFinder::new();
|
||||
finder.kinds(&[LinkKind::Url]);
|
||||
|
||||
// Process all links, stripping anchors and
|
||||
// only taking up to `max_embeds` of links.
|
||||
let links: Vec<String> = finder
|
||||
.links(&content)
|
||||
.map(|x| {
|
||||
x.as_str()
|
||||
.chars()
|
||||
.take_while(|&ch| ch != '#')
|
||||
.collect::<String>()
|
||||
})
|
||||
.collect::<HashSet<String>>()
|
||||
.into_iter()
|
||||
.take(max_embeds)
|
||||
.collect();
|
||||
|
||||
// If no links, fail out.
|
||||
if links.is_empty() {
|
||||
return Err(create_error!(LabelMe));
|
||||
}
|
||||
|
||||
// ! FIXME: batch request to january
|
||||
let mut tasks = Vec::new();
|
||||
|
||||
for link in links {
|
||||
let semaphore = semaphore.clone();
|
||||
let host = host.to_string();
|
||||
tasks.push(spawn(async move {
|
||||
let guard = semaphore.acquire().await;
|
||||
|
||||
if let Ok(mut response) = isahc::get_async(format!(
|
||||
"{host}/embed?url={}",
|
||||
url_escape::encode_component(&link)
|
||||
))
|
||||
.await
|
||||
{
|
||||
drop(guard);
|
||||
response.json::<Embed>().await.ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
let embeds = join_all(tasks)
|
||||
.await
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect::<Vec<Embed>>();
|
||||
|
||||
// Prevent database update when no embeds are found.
|
||||
if !embeds.is_empty() {
|
||||
Ok(embeds)
|
||||
} else {
|
||||
Err(create_error!(LabelMe))
|
||||
}
|
||||
}
|
||||
162
crates/core/database/src/tasks/web_push.rs
Normal file
162
crates/core/database/src/tasks/web_push.rs
Normal file
@@ -0,0 +1,162 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use authifier::Database;
|
||||
use base64::{
|
||||
engine::{self},
|
||||
Engine as _,
|
||||
};
|
||||
use deadqueue::limited::Queue;
|
||||
use once_cell::sync::Lazy;
|
||||
use revolt_config::config;
|
||||
use revolt_models::v0::PushNotification;
|
||||
use revolt_presence::filter_online;
|
||||
use serde_json::json;
|
||||
use web_push::{
|
||||
ContentEncoding, IsahcWebPushClient, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder,
|
||||
WebPushClient, WebPushMessageBuilder,
|
||||
};
|
||||
|
||||
/// Task information
|
||||
#[derive(Debug)]
|
||||
struct PushTask {
|
||||
/// User IDs of the targets that are to receive this notification
|
||||
recipients: Vec<String>,
|
||||
/// Push Notification
|
||||
payload: PushNotification,
|
||||
}
|
||||
|
||||
static Q: Lazy<Queue<PushTask>> = Lazy::new(|| Queue::new(10_000));
|
||||
|
||||
/// Queue a new task for a worker
|
||||
pub async fn queue(recipients: Vec<String>, payload: PushNotification) {
|
||||
if recipients.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let online_ids = filter_online(&recipients).await;
|
||||
let recipients = (&recipients.into_iter().collect::<HashSet<String>>() - &online_ids)
|
||||
.into_iter()
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
Q.try_push(PushTask {
|
||||
recipients,
|
||||
payload,
|
||||
})
|
||||
.ok();
|
||||
|
||||
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
|
||||
}
|
||||
|
||||
/// Start a new worker
|
||||
pub async fn worker(db: Database) {
|
||||
let config = config().await;
|
||||
|
||||
let web_push_client = IsahcWebPushClient::new().unwrap();
|
||||
let fcm_client = if config.api.fcm.api_key.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(fcm::Client::new())
|
||||
};
|
||||
|
||||
let web_push_private_key = engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(config.api.vapid.private_key)
|
||||
.expect("valid `VAPID_PRIVATE_KEY`");
|
||||
|
||||
loop {
|
||||
let task = Q.pop().await;
|
||||
|
||||
if let Ok(sessions) = db.find_sessions_with_subscription(&task.recipients).await {
|
||||
for session in sessions {
|
||||
if let Some(sub) = session.subscription {
|
||||
if sub.endpoint == "fcm" {
|
||||
// Use Firebase Cloud Messaging
|
||||
if let Some(client) = &fcm_client {
|
||||
let PushNotification {
|
||||
author,
|
||||
icon,
|
||||
image: _,
|
||||
body,
|
||||
tag,
|
||||
timestamp: _,
|
||||
url: _,
|
||||
} = &task.payload;
|
||||
|
||||
let mut notification = fcm::NotificationBuilder::new();
|
||||
notification.title(author);
|
||||
notification.icon(icon);
|
||||
notification.body(body);
|
||||
notification.tag(tag);
|
||||
// TODO: expand support for fields
|
||||
let notification = notification.finalize();
|
||||
|
||||
let mut message_builder =
|
||||
fcm::MessageBuilder::new(&config.api.fcm.api_key, &sub.auth);
|
||||
message_builder.notification(notification);
|
||||
|
||||
if let Err(err) = client.send(message_builder.finalize()).await {
|
||||
error!("Failed to send FCM notification! {:?}", err);
|
||||
} else {
|
||||
info!("Sent FCM notification to {:?}.", session.id);
|
||||
}
|
||||
} else {
|
||||
info!("No FCM token was specified!");
|
||||
}
|
||||
} else {
|
||||
// Use Web Push Standard
|
||||
let subscription = SubscriptionInfo {
|
||||
endpoint: sub.endpoint,
|
||||
keys: SubscriptionKeys {
|
||||
auth: sub.auth,
|
||||
p256dh: sub.p256dh,
|
||||
},
|
||||
};
|
||||
|
||||
match VapidSignatureBuilder::from_pem(
|
||||
std::io::Cursor::new(&web_push_private_key),
|
||||
&subscription,
|
||||
) {
|
||||
Ok(sig_builder) => match sig_builder.build() {
|
||||
Ok(signature) => {
|
||||
let mut builder = WebPushMessageBuilder::new(&subscription);
|
||||
builder.set_vapid_signature(signature);
|
||||
|
||||
let payload = json!(task.payload).to_string();
|
||||
builder
|
||||
.set_payload(ContentEncoding::AesGcm, payload.as_bytes());
|
||||
|
||||
match builder.build() {
|
||||
Ok(msg) => match web_push_client.send(msg).await {
|
||||
Ok(_) => {
|
||||
info!(
|
||||
"Sent Web Push notification to {:?}.",
|
||||
session.id
|
||||
)
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Hit error sending Web Push! {:?}", err)
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
error!(
|
||||
"Failed to build message for {}! {:?}",
|
||||
session.user_id, err
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => error!(
|
||||
"Failed to build signature for {}! {:?}",
|
||||
session.user_id, err
|
||||
),
|
||||
},
|
||||
Err(err) => error!(
|
||||
"Failed to create signature builder for {}! {:?}",
|
||||
session.user_id, err
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,24 @@ impl From<crate::Bot> for Bot {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FieldsBot> for crate::FieldsBot {
|
||||
fn from(value: FieldsBot) -> Self {
|
||||
match value {
|
||||
FieldsBot::InteractionsURL => crate::FieldsBot::InteractionsURL,
|
||||
FieldsBot::Token => crate::FieldsBot::Token,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::FieldsBot> for FieldsBot {
|
||||
fn from(value: crate::FieldsBot) -> Self {
|
||||
match value {
|
||||
crate::FieldsBot::InteractionsURL => FieldsBot::InteractionsURL,
|
||||
crate::FieldsBot::Token => FieldsBot::Token,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::Invite> for Invite {
|
||||
fn from(value: crate::Invite) -> Self {
|
||||
match value {
|
||||
@@ -373,6 +391,14 @@ impl From<crate::Interactions> for Interactions {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::AppendMessage> for AppendMessage {
|
||||
fn from(value: crate::AppendMessage) -> Self {
|
||||
AppendMessage {
|
||||
embeds: value.embeds,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::Masquerade> for Masquerade {
|
||||
fn from(value: crate::Masquerade) -> Self {
|
||||
Masquerade {
|
||||
@@ -605,6 +631,83 @@ impl crate::User {
|
||||
id: self.id,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn into_self(self) -> User {
|
||||
User {
|
||||
username: self.username,
|
||||
discriminator: self.discriminator,
|
||||
display_name: self.display_name,
|
||||
avatar: self.avatar.map(|file| file.into()),
|
||||
relations: self
|
||||
.relations
|
||||
.map(|relationships| {
|
||||
relationships
|
||||
.into_iter()
|
||||
.map(|relationship| relationship.into())
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
badges: self.badges.unwrap_or_default() as u32,
|
||||
status: self.status.map(|status| status.into()),
|
||||
profile: self.profile.map(|profile| profile.into()),
|
||||
flags: self.flags.unwrap_or_default() as u32,
|
||||
privileged: self.privileged,
|
||||
bot: self.bot.map(|bot| bot.into()),
|
||||
relationship: RelationshipStatus::User,
|
||||
online: revolt_presence::is_online(&self.id).await,
|
||||
id: self.id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::PartialUser> for PartialUser {
|
||||
fn from(value: crate::PartialUser) -> Self {
|
||||
PartialUser {
|
||||
username: value.username,
|
||||
discriminator: value.discriminator,
|
||||
display_name: value.display_name,
|
||||
avatar: value.avatar.map(|file| file.into()),
|
||||
relations: value.relations.map(|relationships| {
|
||||
relationships
|
||||
.into_iter()
|
||||
.map(|relationship| relationship.into())
|
||||
.collect()
|
||||
}),
|
||||
badges: value.badges.map(|badges| badges as u32),
|
||||
status: value.status.map(|status| status.into()),
|
||||
profile: value.profile.map(|profile| profile.into()),
|
||||
flags: value.flags.map(|flags| flags as u32),
|
||||
privileged: value.privileged,
|
||||
bot: value.bot.map(|bot| bot.into()),
|
||||
relationship: None,
|
||||
online: None,
|
||||
id: value.id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FieldsUser> for crate::FieldsUser {
|
||||
fn from(value: FieldsUser) -> Self {
|
||||
match value {
|
||||
FieldsUser::Avatar => crate::FieldsUser::Avatar,
|
||||
FieldsUser::ProfileBackground => crate::FieldsUser::ProfileBackground,
|
||||
FieldsUser::ProfileContent => crate::FieldsUser::ProfileContent,
|
||||
FieldsUser::StatusPresence => crate::FieldsUser::StatusPresence,
|
||||
FieldsUser::StatusText => crate::FieldsUser::StatusText,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::FieldsUser> for FieldsUser {
|
||||
fn from(value: crate::FieldsUser) -> Self {
|
||||
match value {
|
||||
crate::FieldsUser::Avatar => FieldsUser::Avatar,
|
||||
crate::FieldsUser::ProfileBackground => FieldsUser::ProfileBackground,
|
||||
crate::FieldsUser::ProfileContent => FieldsUser::ProfileContent,
|
||||
crate::FieldsUser::StatusPresence => FieldsUser::StatusPresence,
|
||||
crate::FieldsUser::StatusText => FieldsUser::StatusText,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::RelationshipStatus> for RelationshipStatus {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use crate::{Error, Result};
|
||||
use std::num::NonZeroUsize;
|
||||
|
||||
use revolt_result::{create_error, Error, Result};
|
||||
|
||||
use async_std::sync::Mutex;
|
||||
use once_cell::sync::Lazy;
|
||||
use revolt_rocket_okapi::gen::OpenApiGenerator;
|
||||
use revolt_rocket_okapi::request::{OpenApiFromRequest, RequestHeaderInput};
|
||||
use revolt_rocket_okapi::revolt_okapi::openapi3::{Parameter, ParameterValue};
|
||||
@@ -8,16 +11,14 @@ use rocket::http::Status;
|
||||
use rocket::request::{FromRequest, Outcome};
|
||||
use schemars::schema::{InstanceType, SchemaObject, SingleOrVec};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct IdempotencyKey {
|
||||
#[validate(length(min = 1, max = 64))]
|
||||
key: String,
|
||||
}
|
||||
|
||||
static TOKEN_CACHE: Lazy<Mutex<lru::LruCache<String, ()>>> = Lazy::new(|| Mutex::new(lru::LruCache::new(100)));
|
||||
static TOKEN_CACHE: Lazy<Mutex<lru::LruCache<String, ()>>> =
|
||||
Lazy::new(|| Mutex::new(lru::LruCache::new(NonZeroUsize::new(1000).unwrap())));
|
||||
|
||||
impl IdempotencyKey {
|
||||
// Backwards compatibility.
|
||||
@@ -26,7 +27,7 @@ impl IdempotencyKey {
|
||||
if let Some(v) = v {
|
||||
let mut cache = TOKEN_CACHE.lock().await;
|
||||
if cache.get(&v).is_some() {
|
||||
return Err(Error::DuplicateNonce);
|
||||
return Err(create_error!(DuplicateNonce));
|
||||
}
|
||||
|
||||
cache.put(v.clone(), ());
|
||||
@@ -81,14 +82,19 @@ impl<'r> FromRequest<'r> for IdempotencyKey {
|
||||
.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 key.len() > 64 {
|
||||
return Outcome::Failure((
|
||||
Status::BadRequest,
|
||||
create_error!(FailedValidation {
|
||||
error: "idempotency key too long".to_string(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
let idempotency = IdempotencyKey { key };
|
||||
let mut cache = TOKEN_CACHE.lock().await;
|
||||
if cache.get(&idempotency.key).is_some() {
|
||||
return Outcome::Failure((Status::Conflict, Error::DuplicateNonce));
|
||||
return Outcome::Failure((Status::Conflict, create_error!(DuplicateNonce)));
|
||||
}
|
||||
|
||||
cache.put(idempotency.key.clone(), ());
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod bridge;
|
||||
pub mod idempotency;
|
||||
pub mod permissions;
|
||||
pub mod reference;
|
||||
|
||||
@@ -4,18 +4,19 @@ use revolt_permissions::{
|
||||
calculate_user_permissions, ChannelType, Override, PermissionQuery, RelationshipStatus,
|
||||
};
|
||||
|
||||
use crate::{Database, User};
|
||||
use crate::{Channel, Database, Member, Server, User};
|
||||
|
||||
/// Permissions calculator
|
||||
pub struct PermissionCalculator<'a> {
|
||||
#[derive(Clone)]
|
||||
pub struct DatabasePermissionQuery<'a> {
|
||||
#[allow(dead_code)]
|
||||
database: &'a Database,
|
||||
|
||||
perspective: &'a User,
|
||||
user: Option<Cow<'a, User>>,
|
||||
// pub channel: Cow<'a, Channel>,
|
||||
// pub server: Cow<'a, Server>,
|
||||
// pub member: Cow<'a, Member>,
|
||||
channel: Option<Cow<'a, Channel>>,
|
||||
server: Option<Cow<'a, Server>>,
|
||||
member: Option<Cow<'a, Member>>,
|
||||
|
||||
// flag_known_relationship: Option<&'a RelationshipStatus>,
|
||||
cached_user_permission: Option<u32>,
|
||||
@@ -23,7 +24,7 @@ pub struct PermissionCalculator<'a> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl PermissionQuery for PermissionCalculator<'_> {
|
||||
impl PermissionQuery for DatabasePermissionQuery<'_> {
|
||||
// * For calculating user permission
|
||||
|
||||
/// Is our perspective user privileged?
|
||||
@@ -81,85 +82,275 @@ impl PermissionQuery for PermissionCalculator<'_> {
|
||||
|
||||
/// Do we have a mutual connection with the currently selected user?
|
||||
async fn have_mutual_connection(&mut self) -> bool {
|
||||
// TODO: User::has_mutual_connection
|
||||
false
|
||||
if let Some(user) = &self.user {
|
||||
// TODO: cache result?
|
||||
matches!(
|
||||
self.perspective
|
||||
.has_mutual_connection(self.database, &user.id)
|
||||
.await,
|
||||
Ok(true)
|
||||
)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// * For calculating server permission
|
||||
|
||||
/// Is our perspective user the server's owner?
|
||||
async fn are_we_server_owner(&mut self) -> bool {
|
||||
todo!()
|
||||
if let Some(server) = &self.server {
|
||||
server.owner == self.perspective.id
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Is our perspective user a member of the server?
|
||||
async fn are_we_a_member(&mut self) -> bool {
|
||||
todo!()
|
||||
if let Some(server) = &self.server {
|
||||
if self.member.is_some() {
|
||||
true
|
||||
} else {
|
||||
self.database
|
||||
.fetch_member(&server.id, &self.perspective.id)
|
||||
.await
|
||||
.is_ok()
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Get default server permission
|
||||
async fn get_default_server_permissions(&mut self) -> u64 {
|
||||
todo!()
|
||||
if let Some(server) = &self.server {
|
||||
server.default_permissions as u64
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the ordered role overrides (from lowest to highest) for this member in this server
|
||||
async fn get_our_server_role_overrides(&mut self) -> Vec<Override> {
|
||||
todo!()
|
||||
if let Some(server) = &self.server {
|
||||
let member_roles = self
|
||||
.member
|
||||
.as_ref()
|
||||
.map(|member| member.roles.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut roles = server
|
||||
.roles
|
||||
.iter()
|
||||
.filter(|(id, _)| member_roles.contains(id))
|
||||
.map(|(_, role)| {
|
||||
let v: Override = role.permissions.into();
|
||||
(role.rank, v)
|
||||
})
|
||||
.collect::<Vec<(i64, Override)>>();
|
||||
|
||||
roles.sort_by(|a, b| b.0.cmp(&a.0));
|
||||
roles.into_iter().map(|(_, v)| v).collect()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
/// Is our perspective user timed out on this server?
|
||||
async fn are_we_timed_out(&mut self) -> bool {
|
||||
todo!()
|
||||
if let Some(member) = &self.member {
|
||||
member.in_timeout()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// * For calculating channel permission
|
||||
|
||||
/// Get the type of the channel
|
||||
async fn get_channel_type(&mut self) -> ChannelType {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::DirectMessage { .. })
|
||||
| Cow::Owned(Channel::DirectMessage { .. }) => ChannelType::DirectMessage,
|
||||
Cow::Borrowed(Channel::Group { .. }) | Cow::Owned(Channel::Group { .. }) => {
|
||||
ChannelType::Group
|
||||
}
|
||||
Cow::Borrowed(Channel::SavedMessages { .. })
|
||||
| Cow::Owned(Channel::SavedMessages { .. }) => ChannelType::SavedMessages,
|
||||
Cow::Borrowed(Channel::TextChannel { .. })
|
||||
| Cow::Owned(Channel::TextChannel { .. })
|
||||
| Cow::Borrowed(Channel::VoiceChannel { .. })
|
||||
| Cow::Owned(Channel::VoiceChannel { .. }) => ChannelType::ServerChannel,
|
||||
}
|
||||
} else {
|
||||
ChannelType::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the default channel permissions
|
||||
/// Group channel defaults should be mapped to an allow-only override
|
||||
async fn get_default_channel_permissions(&mut self) -> Override {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::Group { permissions, .. })
|
||||
| Cow::Owned(Channel::Group { permissions, .. }) => Override {
|
||||
allow: permissions.unwrap_or_default() as u64,
|
||||
deny: 0,
|
||||
},
|
||||
Cow::Borrowed(Channel::TextChannel {
|
||||
default_permissions,
|
||||
..
|
||||
})
|
||||
| Cow::Owned(Channel::TextChannel {
|
||||
default_permissions,
|
||||
..
|
||||
})
|
||||
| Cow::Borrowed(Channel::VoiceChannel {
|
||||
default_permissions,
|
||||
..
|
||||
})
|
||||
| Cow::Owned(Channel::VoiceChannel {
|
||||
default_permissions,
|
||||
..
|
||||
}) => default_permissions.unwrap_or_default().into(),
|
||||
_ => Default::default(),
|
||||
}
|
||||
} else {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the ordered role overrides (from lowest to highest) for this member in this channel
|
||||
async fn get_our_channel_role_overrides(&mut self) -> Vec<Override> {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::TextChannel {
|
||||
role_permissions, ..
|
||||
})
|
||||
| Cow::Owned(Channel::TextChannel {
|
||||
role_permissions, ..
|
||||
})
|
||||
| Cow::Borrowed(Channel::VoiceChannel {
|
||||
role_permissions, ..
|
||||
})
|
||||
| Cow::Owned(Channel::VoiceChannel {
|
||||
role_permissions, ..
|
||||
}) => {
|
||||
if let Some(server) = &self.server {
|
||||
let member_roles = self
|
||||
.member
|
||||
.as_ref()
|
||||
.map(|member| member.roles.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut roles = role_permissions
|
||||
.iter()
|
||||
.filter(|(id, _)| member_roles.contains(id))
|
||||
.filter_map(|(id, permission)| {
|
||||
server.roles.get(id).map(|role| {
|
||||
let v: Override = (*permission).into();
|
||||
(role.rank, v)
|
||||
})
|
||||
})
|
||||
.collect::<Vec<(i64, Override)>>();
|
||||
|
||||
roles.sort_by(|a, b| b.0.cmp(&a.0));
|
||||
roles.into_iter().map(|(_, v)| v).collect()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
_ => vec![],
|
||||
}
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
/// Do we own this group or saved messages channel if it is one of those?
|
||||
async fn do_we_own_the_channel(&mut self) -> bool {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::Group { owner, .. })
|
||||
| Cow::Owned(Channel::Group { owner, .. }) => owner == &self.perspective.id,
|
||||
Cow::Borrowed(Channel::SavedMessages { user, .. })
|
||||
| Cow::Owned(Channel::SavedMessages { user, .. }) => user == &self.perspective.id,
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Are we a recipient of this channel?
|
||||
async fn are_we_part_of_the_channel(&mut self) -> bool {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::DirectMessage { recipients, .. })
|
||||
| Cow::Owned(Channel::DirectMessage { recipients, .. })
|
||||
| Cow::Borrowed(Channel::Group { recipients, .. })
|
||||
| Cow::Owned(Channel::Group { recipients, .. }) => {
|
||||
recipients.contains(&self.perspective.id)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the current user as the recipient of this channel
|
||||
/// (this will only ever be called for DirectMessage channels, use unimplemented!() for other code paths)
|
||||
async fn set_recipient_as_user(&mut self) {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::DirectMessage { recipients, .. })
|
||||
| Cow::Owned(Channel::DirectMessage { recipients, .. }) => {
|
||||
let recipient_id = recipients
|
||||
.iter()
|
||||
.find(|recipient| recipient != &&self.perspective.id)
|
||||
.expect("Missing recipient for DM");
|
||||
|
||||
if let Ok(user) = self.database.fetch_user(recipient_id).await {
|
||||
self.user.replace(Cow::Owned(user));
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the current server as the server owning this channel
|
||||
/// (this will only ever be called for server channels, use unimplemented!() for other code paths)
|
||||
async fn set_server_from_channel(&mut self) {
|
||||
todo!()
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel {
|
||||
Cow::Borrowed(Channel::TextChannel { server, .. })
|
||||
| Cow::Owned(Channel::TextChannel { server, .. })
|
||||
| Cow::Borrowed(Channel::VoiceChannel { server, .. })
|
||||
| Cow::Owned(Channel::VoiceChannel { server, .. }) => {
|
||||
if let Ok(server) = self.database.fetch_server(server).await {
|
||||
self.server.replace(Cow::Owned(server));
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PermissionCalculator<'a> {
|
||||
impl<'a> DatabasePermissionQuery<'a> {
|
||||
/// Create a new permission calculator
|
||||
pub fn new(database: &'a Database, perspective: &'a User) -> PermissionCalculator<'a> {
|
||||
PermissionCalculator {
|
||||
pub fn new(database: &'a Database, perspective: &'a User) -> DatabasePermissionQuery<'a> {
|
||||
DatabasePermissionQuery {
|
||||
database,
|
||||
perspective,
|
||||
user: None,
|
||||
channel: None,
|
||||
server: None,
|
||||
member: None,
|
||||
|
||||
cached_user_permission: None,
|
||||
cached_permission: None,
|
||||
@@ -167,7 +358,7 @@ impl<'a> PermissionCalculator<'a> {
|
||||
}
|
||||
|
||||
/// Calculate the user permission value
|
||||
pub async fn calc_user(mut self) -> PermissionCalculator<'a> {
|
||||
pub async fn calc_user(mut self) -> DatabasePermissionQuery<'a> {
|
||||
if self.cached_user_permission.is_some() {
|
||||
return self;
|
||||
}
|
||||
@@ -176,14 +367,14 @@ impl<'a> PermissionCalculator<'a> {
|
||||
panic!("Expected `PermissionCalculator.user to exist.");
|
||||
}
|
||||
|
||||
PermissionCalculator {
|
||||
DatabasePermissionQuery {
|
||||
cached_user_permission: Some(calculate_user_permissions(&mut self).await),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the permission value
|
||||
pub async fn calc(self) -> PermissionCalculator<'a> {
|
||||
pub async fn calc(self) -> DatabasePermissionQuery<'a> {
|
||||
if self.cached_permission.is_some() {
|
||||
return self;
|
||||
}
|
||||
@@ -192,15 +383,39 @@ impl<'a> PermissionCalculator<'a> {
|
||||
}
|
||||
|
||||
/// Use user
|
||||
pub fn user(self, user: Cow<'a, User>) -> PermissionCalculator {
|
||||
PermissionCalculator {
|
||||
user: Some(user),
|
||||
pub fn user(self, user: &'a User) -> DatabasePermissionQuery {
|
||||
DatabasePermissionQuery {
|
||||
user: Some(Cow::Borrowed(user)),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Use channel
|
||||
pub fn channel(self, channel: &'a Channel) -> DatabasePermissionQuery {
|
||||
DatabasePermissionQuery {
|
||||
channel: Some(Cow::Borrowed(channel)),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Use server
|
||||
pub fn server(self, server: &'a Server) -> DatabasePermissionQuery {
|
||||
DatabasePermissionQuery {
|
||||
server: Some(Cow::Borrowed(server)),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Use member
|
||||
pub fn member(self, member: &'a Member) -> DatabasePermissionQuery {
|
||||
DatabasePermissionQuery {
|
||||
member: Some(Cow::Borrowed(member)),
|
||||
..self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Short-hand for creating a permission calculator
|
||||
pub fn perms<'a>(database: &'a Database, perspective: &'a User) -> PermissionCalculator<'a> {
|
||||
PermissionCalculator::new(database, perspective)
|
||||
pub fn perms<'a>(database: &'a Database, perspective: &'a User) -> DatabasePermissionQuery<'a> {
|
||||
DatabasePermissionQuery::new(database, perspective)
|
||||
}
|
||||
|
||||
@@ -18,10 +18,15 @@ default = ["serde", "partials"]
|
||||
|
||||
[dependencies]
|
||||
# Core
|
||||
revolt-config = { version = "0.6.7", path = "../config" }
|
||||
revolt-permissions = { version = "0.6.7", path = "../permissions" }
|
||||
|
||||
# Serialisation
|
||||
# Utility
|
||||
regex = "1"
|
||||
indexmap = "1.9.3"
|
||||
once_cell = "1.17.1"
|
||||
|
||||
# Serialisation
|
||||
revolt_optional_struct = { version = "0.2.0", optional = true }
|
||||
serde = { version = "1", features = ["derive"], optional = true }
|
||||
iso8601-timestamp = { version = "0.2.11", features = ["schema", "bson"] }
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use super::User;
|
||||
|
||||
use validator::Validate;
|
||||
|
||||
auto_derived!(
|
||||
/// Bot
|
||||
#[derive(Default)]
|
||||
pub struct Bot {
|
||||
/// Bot Id
|
||||
#[cfg_attr(feature = "serde", serde(rename = "_id"))]
|
||||
@@ -55,6 +58,12 @@ auto_derived!(
|
||||
pub flags: u32,
|
||||
}
|
||||
|
||||
/// Optional fields on bot object
|
||||
pub enum FieldsBot {
|
||||
Token,
|
||||
InteractionsURL,
|
||||
}
|
||||
|
||||
/// Flags that may be attributed to a bot
|
||||
#[repr(u32)]
|
||||
pub enum BotFlags {
|
||||
@@ -71,10 +80,16 @@ auto_derived!(
|
||||
/// Bot Username
|
||||
pub username: String,
|
||||
/// Profile Avatar
|
||||
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "String::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
serde(skip_serializing_if = "String::is_empty", default)
|
||||
)]
|
||||
pub avatar: String,
|
||||
/// Profile Description
|
||||
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "String::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
serde(skip_serializing_if = "String::is_empty", default)
|
||||
)]
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
@@ -85,4 +100,68 @@ auto_derived!(
|
||||
/// User object
|
||||
pub user: User,
|
||||
}
|
||||
|
||||
/// Bot Details
|
||||
#[derive(Default)]
|
||||
#[cfg_attr(feature = "validator", derive(Validate))]
|
||||
pub struct DataCreateBot {
|
||||
/// Bot username
|
||||
#[cfg_attr(
|
||||
feature = "validator",
|
||||
validate(length(min = 2, max = 32), regex = "super::RE_USERNAME")
|
||||
)]
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
/// New Bot Details
|
||||
#[derive(Default)]
|
||||
#[cfg_attr(feature = "validator", derive(Validate))]
|
||||
pub struct DataEditBot {
|
||||
/// Bot username
|
||||
#[cfg_attr(
|
||||
feature = "validator",
|
||||
validate(length(min = 2, max = 32), regex = "super::RE_USERNAME")
|
||||
)]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
/// Whether the bot can be added by anyone
|
||||
pub public: Option<bool>,
|
||||
/// Whether analytics should be gathered for this bot
|
||||
///
|
||||
/// Must be enabled in order to show up on [Revolt Discover](https://rvlt.gg).
|
||||
pub analytics: Option<bool>,
|
||||
/// Interactions URL
|
||||
#[cfg_attr(feature = "validator", validate(length(min = 1, max = 2048)))]
|
||||
pub interactions_url: Option<String>,
|
||||
/// Fields to remove from bot object
|
||||
#[cfg_attr(feature = "validator", validate(length(min = 1)))]
|
||||
pub remove: Option<Vec<FieldsBot>>,
|
||||
}
|
||||
|
||||
/// Where we are inviting a bot to
|
||||
#[serde(untagged)]
|
||||
pub enum InviteBotDestination {
|
||||
/// Invite to a server
|
||||
Server {
|
||||
/// Server Id
|
||||
server: String,
|
||||
},
|
||||
/// Invite to a group
|
||||
Group {
|
||||
/// Group Id
|
||||
group: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Owned Bots Response
|
||||
///
|
||||
/// Both lists are sorted by their IDs.
|
||||
///
|
||||
/// TODO: user should be in bot object
|
||||
pub struct OwnedBotsResponse {
|
||||
/// Bot objects
|
||||
pub bots: Vec<Bot>,
|
||||
/// User objects
|
||||
pub users: Vec<User>,
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::time::SystemTime;
|
||||
|
||||
use revolt_config::config;
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use iso8601_timestamp::Timestamp;
|
||||
|
||||
use super::{Embed, File, MessageWebhook};
|
||||
use super::{Embed, File, MessageWebhook, User, Webhook};
|
||||
|
||||
auto_derived_partial!(
|
||||
/// Message
|
||||
@@ -129,11 +133,136 @@ auto_derived!(
|
||||
/// Sort by the oldest messages first
|
||||
Oldest,
|
||||
}
|
||||
|
||||
/// Push Notification
|
||||
pub struct PushNotification {
|
||||
/// Known author name
|
||||
pub author: String,
|
||||
/// URL to author avatar
|
||||
pub icon: String,
|
||||
/// URL to first matching attachment
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub image: Option<String>,
|
||||
/// Message content or system message information
|
||||
pub body: String,
|
||||
/// Unique tag, usually the channel ID
|
||||
pub tag: String,
|
||||
/// Timestamp at which this notification was created
|
||||
pub timestamp: u64,
|
||||
/// URL to open when clicking notification
|
||||
pub url: String,
|
||||
}
|
||||
);
|
||||
|
||||
/// Message Author Abstraction
|
||||
pub enum MessageAuthor<'a> {
|
||||
User(&'a User),
|
||||
Webhook(&'a Webhook),
|
||||
System {
|
||||
username: &'a str,
|
||||
avatar: Option<&'a str>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Interactions {
|
||||
/// Check if default initialisation of fields
|
||||
pub fn is_default(&self) -> bool {
|
||||
!self.restrict_reactions && self.reactions.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> MessageAuthor<'a> {
|
||||
pub fn id(&self) -> &str {
|
||||
match self {
|
||||
MessageAuthor::User(user) => &user.id,
|
||||
MessageAuthor::Webhook(webhook) => &webhook.id,
|
||||
MessageAuthor::System { .. } => "00000000000000000000000000",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn avatar(&self) -> Option<&str> {
|
||||
match self {
|
||||
MessageAuthor::User(user) => user.avatar.as_ref().map(|file| file.id.as_str()),
|
||||
MessageAuthor::Webhook(webhook) => webhook.avatar.as_ref().map(|file| file.id.as_str()),
|
||||
MessageAuthor::System { avatar, .. } => *avatar,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn username(&self) -> &str {
|
||||
match self {
|
||||
MessageAuthor::User(user) => &user.username,
|
||||
MessageAuthor::Webhook(webhook) => &webhook.name,
|
||||
MessageAuthor::System { username, .. } => username,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SystemMessage> for String {
|
||||
fn from(s: SystemMessage) -> String {
|
||||
match s {
|
||||
SystemMessage::Text { content } => content,
|
||||
SystemMessage::UserAdded { .. } => "User added to the channel.".to_string(),
|
||||
SystemMessage::UserRemove { .. } => "User removed from the channel.".to_string(),
|
||||
SystemMessage::UserJoined { .. } => "User joined the channel.".to_string(),
|
||||
SystemMessage::UserLeft { .. } => "User left the channel.".to_string(),
|
||||
SystemMessage::UserKicked { .. } => "User kicked from the channel.".to_string(),
|
||||
SystemMessage::UserBanned { .. } => "User banned from the channel.".to_string(),
|
||||
SystemMessage::ChannelRenamed { .. } => "Channel renamed.".to_string(),
|
||||
SystemMessage::ChannelDescriptionChanged { .. } => {
|
||||
"Channel description changed.".to_string()
|
||||
}
|
||||
SystemMessage::ChannelIconChanged { .. } => "Channel icon changed.".to_string(),
|
||||
SystemMessage::ChannelOwnershipChanged { .. } => {
|
||||
"Channel ownership changed.".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PushNotification {
|
||||
/// Create a new notification from a given message, author and channel ID
|
||||
pub async fn from(msg: Message, author: Option<MessageAuthor<'_>>, channel_id: &str) -> Self {
|
||||
let config = config().await;
|
||||
|
||||
let icon = if let Some(author) = &author {
|
||||
if let Some(avatar) = author.avatar() {
|
||||
format!("{}/avatars/{}", config.hosts.autumn, avatar)
|
||||
} else {
|
||||
format!("{}/users/{}/default_avatar", config.hosts.api, author.id())
|
||||
}
|
||||
} else {
|
||||
format!("{}/assets/logo.png", config.hosts.app)
|
||||
};
|
||||
|
||||
let image = msg.attachments.and_then(|attachments| {
|
||||
attachments
|
||||
.first()
|
||||
.map(|v| format!("{}/attachments/{}", config.hosts.autumn, v.id))
|
||||
});
|
||||
|
||||
let body = if let Some(sys) = msg.system {
|
||||
sys.into()
|
||||
} else if let Some(text) = msg.content {
|
||||
text
|
||||
} else {
|
||||
"Empty Message".to_string()
|
||||
};
|
||||
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("Time went backwards")
|
||||
.as_secs();
|
||||
|
||||
Self {
|
||||
author: author
|
||||
.map(|x| x.username().to_string())
|
||||
.unwrap_or_else(|| "Revolt".to_string()),
|
||||
icon,
|
||||
image,
|
||||
body,
|
||||
tag: channel_id.to_string(),
|
||||
timestamp,
|
||||
url: format!("{}/channel/{}/{}", config.hosts.app, channel_id, msg.id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ auto_derived_partial!(
|
||||
pub id: MemberCompositeKey,
|
||||
|
||||
/// Time at which this user joined the server
|
||||
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
|
||||
pub joined_at: Option<Timestamp>,
|
||||
pub joined_at: Timestamp,
|
||||
|
||||
/// Member's nickname
|
||||
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
|
||||
use super::File;
|
||||
|
||||
auto_derived!(
|
||||
/// Regex for valid usernames
|
||||
///
|
||||
/// Block zero width space
|
||||
/// Block lookalike characters
|
||||
pub static RE_USERNAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\p{L}|[\d_.-])+$").unwrap());
|
||||
|
||||
auto_derived_partial!(
|
||||
/// User
|
||||
pub struct User {
|
||||
/// Unique Id
|
||||
@@ -56,6 +65,18 @@ auto_derived!(
|
||||
pub relationship: RelationshipStatus,
|
||||
/// Whether this user is currently online
|
||||
pub online: bool,
|
||||
},
|
||||
"PartialUser"
|
||||
);
|
||||
|
||||
auto_derived!(
|
||||
/// Optional fields on user object
|
||||
pub enum FieldsUser {
|
||||
Avatar,
|
||||
StatusText,
|
||||
StatusPresence,
|
||||
ProfileContent,
|
||||
ProfileBackground,
|
||||
}
|
||||
|
||||
/// User's relationship with another user (or themselves)
|
||||
|
||||
@@ -20,6 +20,9 @@ try-from-primitive = ["dep:num_enum"]
|
||||
async-std = { version = "1.8.0", features = ["attributes"] }
|
||||
|
||||
[dependencies]
|
||||
# Core
|
||||
revolt-result = { version = "0.6.5", path = "../result" }
|
||||
|
||||
# Utility
|
||||
auto_ops = "0.3.0"
|
||||
once_cell = "1.17"
|
||||
|
||||
@@ -118,6 +118,10 @@ pub async fn calculate_channel_permissions<P: PermissionQuery>(query: &mut P) ->
|
||||
permissions.restrict(*ALLOW_IN_TIMEOUT);
|
||||
}
|
||||
|
||||
if !permissions.has_channel_permission(ChannelPermission::ViewChannel) {
|
||||
permissions.revoke_all();
|
||||
}
|
||||
|
||||
permissions
|
||||
} else {
|
||||
0_u64.into()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use std::ops::Add;
|
||||
use std::{fmt, ops::Add};
|
||||
|
||||
/// Abstract channel type
|
||||
pub enum ChannelType {
|
||||
@@ -102,6 +102,12 @@ pub enum ChannelPermission {
|
||||
GrantAll = u64::MAX,
|
||||
}
|
||||
|
||||
impl fmt::Display for ChannelPermission {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u64 { *a as u64 | *b as u64 });
|
||||
impl_op_ex_commutative!(+ |a: &u64, b: &ChannelPermission| -> u64 { *a | *b as u64 });
|
||||
|
||||
@@ -136,4 +142,9 @@ pub static DEFAULT_PERMISSION_SERVER: Lazy<u64> = Lazy::new(|| {
|
||||
)
|
||||
});
|
||||
|
||||
pub static DEFAULT_WEBHOOK_PERMISSIONS: Lazy<u64> = Lazy::new(|| ChannelPermission::SendMessage + ChannelPermission::SendEmbeds + ChannelPermission::Masquerade + ChannelPermission::React);
|
||||
pub static DEFAULT_WEBHOOK_PERMISSIONS: Lazy<u64> = Lazy::new(|| {
|
||||
ChannelPermission::SendMessage
|
||||
+ ChannelPermission::SendEmbeds
|
||||
+ ChannelPermission::Masquerade
|
||||
+ ChannelPermission::React
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ mod server;
|
||||
mod user;
|
||||
|
||||
pub use channel::*;
|
||||
use revolt_result::{create_error, Result};
|
||||
pub use server::*;
|
||||
pub use user::*;
|
||||
|
||||
@@ -27,10 +28,39 @@ impl PermissionValue {
|
||||
self.0 &= !v;
|
||||
}
|
||||
|
||||
/// Revoke all permissions
|
||||
pub fn revoke_all(&mut self) {
|
||||
self.0 = 0;
|
||||
}
|
||||
|
||||
/// Restrict to given permissions
|
||||
pub fn restrict(&mut self, v: u64) {
|
||||
self.0 &= v;
|
||||
}
|
||||
|
||||
/// Check whether certain a permission has been granted
|
||||
pub fn has(&mut self, v: u64) -> bool {
|
||||
(self.0 & v) == v
|
||||
}
|
||||
|
||||
/// Check whether certain a channel permission has been granted
|
||||
pub fn has_channel_permission(&mut self, permission: ChannelPermission) -> bool {
|
||||
self.has(permission as u64)
|
||||
}
|
||||
|
||||
/// Throw if missing channel permission
|
||||
pub fn throw_if_lacking_channel_permission(
|
||||
&mut self,
|
||||
permission: ChannelPermission,
|
||||
) -> Result<()> {
|
||||
if self.has_channel_permission(permission) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(MissingPermission {
|
||||
permission: permission.to_string()
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for PermissionValue {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
use schemars::JsonSchema;
|
||||
|
||||
/// Representation of a single permission override
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[cfg_attr(feature = "schemas", derive(JsonSchema))]
|
||||
pub struct Override {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::fmt;
|
||||
|
||||
/// User's relationship with another user (or themselves)
|
||||
pub enum RelationshipStatus {
|
||||
None,
|
||||
@@ -21,5 +23,11 @@ pub enum UserPermission {
|
||||
Invite = 1 << 3,
|
||||
}
|
||||
|
||||
impl fmt::Display for UserPermission {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a as u32 | *b as u32 });
|
||||
impl_op_ex_commutative!(+ |a: &u32, b: &UserPermission| -> u32 { *a | *b as u32 });
|
||||
|
||||
@@ -43,6 +43,7 @@ pub enum ErrorType {
|
||||
// ? User related errors
|
||||
UsernameTaken,
|
||||
InvalidUsername,
|
||||
DiscriminatorChangeRatelimited,
|
||||
UnknownUser,
|
||||
AlreadyFriends,
|
||||
AlreadySentRequest,
|
||||
@@ -87,6 +88,7 @@ pub enum ErrorType {
|
||||
TooManyRoles {
|
||||
max: usize,
|
||||
},
|
||||
AlreadyInServer,
|
||||
|
||||
// ? Bot related errors
|
||||
ReachedMaximumBots,
|
||||
|
||||
@@ -19,6 +19,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
||||
ErrorType::UnknownUser => Status::NotFound,
|
||||
ErrorType::InvalidUsername => Status::BadRequest,
|
||||
ErrorType::UsernameTaken => Status::Conflict,
|
||||
ErrorType::DiscriminatorChangeRatelimited => Status::TooManyRequests,
|
||||
ErrorType::AlreadyFriends => Status::Conflict,
|
||||
ErrorType::AlreadySentRequest => Status::Conflict,
|
||||
ErrorType::Blocked => Status::Conflict,
|
||||
@@ -42,6 +43,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
||||
ErrorType::UnknownServer => Status::NotFound,
|
||||
ErrorType::InvalidRole => Status::NotFound,
|
||||
ErrorType::Banned => Status::Forbidden,
|
||||
ErrorType::AlreadyInServer => Status::Conflict,
|
||||
|
||||
ErrorType::TooManyServers { .. } => Status::BadRequest,
|
||||
ErrorType::TooManyEmoji { .. } => Status::BadRequest,
|
||||
|
||||
@@ -8,6 +8,10 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
# Test
|
||||
rand = "0.8.5"
|
||||
redis-kiss = "0.1.4"
|
||||
|
||||
# Utility
|
||||
lru = "0.7.0"
|
||||
url = "2.2.2"
|
||||
@@ -51,6 +55,7 @@ lettre = "0.10.0-alpha.4"
|
||||
rocket = { version = "0.5.0-rc.2", default-features = false, features = [
|
||||
"json",
|
||||
] }
|
||||
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", rev = "c17e8145baa4790319fdb6a473e465b960f55e7c" }
|
||||
rocket_empty = { version = "0.1.1", features = ["schema"] }
|
||||
rocket_authifier = { version = "1.0.7" }
|
||||
rocket_prometheus = "0.10.0-rc.3"
|
||||
|
||||
@@ -8,8 +8,11 @@ extern crate serde_json;
|
||||
pub mod routes;
|
||||
pub mod util;
|
||||
|
||||
use rocket::{Build, Rocket};
|
||||
use rocket_cors::{AllowedOrigins, CorsOptions};
|
||||
use rocket_prometheus::PrometheusMetrics;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::str::FromStr;
|
||||
|
||||
use async_std::channel::unbounded;
|
||||
use revolt_quark::authifier::{Authifier, AuthifierEvent};
|
||||
@@ -17,14 +20,7 @@ use revolt_quark::events::client::EventV1;
|
||||
use revolt_quark::DatabaseInfo;
|
||||
use rocket::data::ToByteUnit;
|
||||
|
||||
#[launch]
|
||||
async fn rocket() -> _ {
|
||||
// Configure logging and environment
|
||||
revolt_quark::configure!();
|
||||
|
||||
// Ensure environment variables are present
|
||||
revolt_quark::variables::delta::preflight_checks();
|
||||
|
||||
pub async fn web() -> Rocket<Build> {
|
||||
// Setup database
|
||||
let db = revolt_database::DatabaseInfo::Auto.connect().await.unwrap();
|
||||
db.migrate_database().await.unwrap();
|
||||
@@ -37,7 +33,7 @@ async fn rocket() -> _ {
|
||||
|
||||
// Setup Authifier
|
||||
let authifier = Authifier {
|
||||
database: legacy_db.clone().into(),
|
||||
database: db.clone().into(),
|
||||
config: revolt_quark::util::authifier::config(),
|
||||
event_channel: Some(sender),
|
||||
};
|
||||
@@ -59,10 +55,31 @@ async fn rocket() -> _ {
|
||||
});
|
||||
|
||||
// Launch background task workers
|
||||
async_std::task::spawn(revolt_database::tasks::start_workers(db.clone()));
|
||||
async_std::task::spawn(revolt_quark::tasks::start_workers(legacy_db.clone()));
|
||||
|
||||
// Configure CORS
|
||||
let cors = revolt_quark::web::cors::new();
|
||||
let cors = CorsOptions {
|
||||
allowed_origins: AllowedOrigins::All,
|
||||
allowed_methods: [
|
||||
"Get", "Put", "Post", "Delete", "Options", "Head", "Trace", "Connect", "Patch",
|
||||
]
|
||||
.iter()
|
||||
.map(|s| FromStr::from_str(s).unwrap())
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}
|
||||
.to_cors()
|
||||
.expect("Failed to create CORS.");
|
||||
|
||||
// Configure Swagger
|
||||
let swagger = revolt_rocket_okapi::swagger_ui::make_swagger_ui(
|
||||
&revolt_rocket_okapi::swagger_ui::SwaggerUIConfig {
|
||||
url: "../openapi.json".to_owned(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.into();
|
||||
|
||||
// Configure Rocket
|
||||
let rocket = rocket::build();
|
||||
@@ -71,14 +88,14 @@ async fn rocket() -> _ {
|
||||
routes::mount(rocket)
|
||||
.attach(prometheus.clone())
|
||||
.mount("/metrics", prometheus)
|
||||
.mount("/", revolt_quark::web::cors::catch_all_options_routes())
|
||||
.mount("/", revolt_quark::web::ratelimiter::routes())
|
||||
.mount("/swagger/", revolt_quark::web::swagger::routes())
|
||||
.mount("/", rocket_cors::catch_all_options_routes())
|
||||
.mount("/", util::ratelimiter::routes())
|
||||
.mount("/swagger/", swagger)
|
||||
.manage(authifier)
|
||||
.manage(db)
|
||||
.manage(legacy_db)
|
||||
.manage(cors.clone())
|
||||
.attach(revolt_quark::web::ratelimiter::RatelimitFairing)
|
||||
.attach(util::ratelimiter::RatelimitFairing)
|
||||
.attach(cors)
|
||||
.configure(rocket::Config {
|
||||
limits: rocket::data::Limits::default().limit("string", 5.megabytes()),
|
||||
@@ -86,3 +103,15 @@ async fn rocket() -> _ {
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
#[launch]
|
||||
async fn rocket() -> _ {
|
||||
// Configure logging and environment
|
||||
revolt_quark::configure!();
|
||||
|
||||
// Ensure environment variables are present
|
||||
revolt_quark::variables::delta::preflight_checks();
|
||||
|
||||
// Start web server
|
||||
web().await
|
||||
}
|
||||
|
||||
@@ -1,63 +1,59 @@
|
||||
use crate::util::regex::RE_USERNAME;
|
||||
|
||||
use nanoid::nanoid;
|
||||
use revolt_quark::{
|
||||
models::{user::BotInformation, Bot, User},
|
||||
variables::delta::MAX_BOT_COUNT,
|
||||
Db, Error, Result,
|
||||
};
|
||||
|
||||
use revolt_database::{Bot, Database, User};
|
||||
use revolt_models::v0;
|
||||
use revolt_result::{create_error, Result};
|
||||
use rocket::serde::json::Json;
|
||||
use serde::Deserialize;
|
||||
use ulid::Ulid;
|
||||
use rocket::State;
|
||||
use validator::Validate;
|
||||
|
||||
/// # Bot Details
|
||||
#[derive(Validate, Deserialize, JsonSchema)]
|
||||
pub struct DataCreateBot {
|
||||
/// Bot username
|
||||
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// # Create Bot
|
||||
///
|
||||
/// Create a new Revolt bot.
|
||||
#[openapi(tag = "Bots")]
|
||||
#[post("/create", data = "<info>")]
|
||||
pub async fn create_bot(db: &Db, user: User, info: Json<DataCreateBot>) -> Result<Json<Bot>> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
pub async fn create_bot(
|
||||
db: &State<Database>,
|
||||
user: User,
|
||||
info: Json<v0::DataCreateBot>,
|
||||
) -> Result<Json<v0::Bot>> {
|
||||
let info = info.into_inner();
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
info.validate().map_err(|error| {
|
||||
create_error!(FailedValidation {
|
||||
error: error.to_string()
|
||||
})
|
||||
})?;
|
||||
|
||||
if db.get_number_of_bots_by_user(&user.id).await? >= *MAX_BOT_COUNT {
|
||||
return Err(Error::ReachedMaximumBots);
|
||||
}
|
||||
let bot = Bot::create(db, info.name, &user, None).await?;
|
||||
Ok(Json(bot.into()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_models::v0;
|
||||
use rocket::http::{ContentType, Header, Status};
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn create_bot() {
|
||||
let harness = TestHarness::new().await;
|
||||
let (_, session, _) = harness.new_user().await;
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let username = User::validate_username(info.name)?;
|
||||
let bot_user = User {
|
||||
id: id.clone(),
|
||||
discriminator: User::find_discriminator(db, &username, None).await?,
|
||||
username,
|
||||
bot: Some(BotInformation {
|
||||
owner: user.id.clone(),
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
let response = harness
|
||||
.client
|
||||
.post("/bots/create")
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.header(ContentType::JSON)
|
||||
.body(
|
||||
json!(v0::DataCreateBot {
|
||||
name: TestHarness::rand_string(),
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
let bot = Bot {
|
||||
id,
|
||||
owner: user.id,
|
||||
token: nanoid!(64),
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
|
||||
db.insert_user(&bot_user).await?;
|
||||
db.insert_bot(&bot).await?;
|
||||
Ok(Json(bot))
|
||||
let bot: v0::Bot = response.into_json().await.expect("`Bot`");
|
||||
assert!(harness.db.fetch_bot(&bot.id).await.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,64 @@
|
||||
use revolt_quark::{models::User, Db, EmptyResponse, Error, Ref, Result};
|
||||
use revolt_database::{util::reference::Reference, Database, User};
|
||||
use revolt_result::{create_error, Result};
|
||||
use rocket::State;
|
||||
use rocket_empty::EmptyResponse;
|
||||
|
||||
/// # Delete Bot
|
||||
///
|
||||
/// Delete a bot by its id.
|
||||
#[openapi(tag = "Bots")]
|
||||
#[delete("/<target>")]
|
||||
pub async fn delete_bot(db: &Db, user: User, target: Ref) -> Result<EmptyResponse> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
pub async fn delete_bot(
|
||||
db: &State<Database>,
|
||||
user: User,
|
||||
target: Reference,
|
||||
) -> Result<EmptyResponse> {
|
||||
let bot = target.as_bot(db).await?;
|
||||
if bot.owner != user.id {
|
||||
return Err(Error::NotFound);
|
||||
return Err(create_error!(NotFound));
|
||||
}
|
||||
|
||||
bot.delete(db).await.map(|_| EmptyResponse)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_database::{events::client::EventV1, Bot};
|
||||
use rocket::http::{Header, Status};
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn delete_bot() {
|
||||
let mut harness = TestHarness::new().await;
|
||||
let (_, session, user) = harness.new_user().await;
|
||||
|
||||
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
let response = harness
|
||||
.client
|
||||
.delete(format!("/bots/{}", bot.id))
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), Status::NoContent);
|
||||
assert!(harness.db.fetch_bot(&bot.id).await.is_err());
|
||||
drop(response);
|
||||
|
||||
let event = harness
|
||||
.wait_for_event(|event| match event {
|
||||
EventV1::UserUpdate { id, .. } => id == &bot.id,
|
||||
_ => false,
|
||||
})
|
||||
.await;
|
||||
|
||||
match event {
|
||||
EventV1::UserUpdate { data, .. } => {
|
||||
assert_eq!(data.flags, Some(2));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +1,32 @@
|
||||
use crate::util::regex::RE_USERNAME;
|
||||
|
||||
use revolt_quark::{
|
||||
models::{
|
||||
bot::{FieldsBot, PartialBot},
|
||||
Bot, User,
|
||||
},
|
||||
Db, Error, Ref, Result,
|
||||
};
|
||||
use revolt_database::{util::reference::Reference, Database, PartialBot, User};
|
||||
use revolt_models::v0::{self, DataEditBot};
|
||||
use revolt_result::{create_error, Result};
|
||||
use rocket::State;
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
/// # Bot Details
|
||||
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
||||
pub struct DataEditBot {
|
||||
/// Bot username
|
||||
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
name: Option<String>,
|
||||
/// Whether the bot can be added by anyone
|
||||
public: Option<bool>,
|
||||
/// Whether analytics should be gathered for this bot
|
||||
///
|
||||
/// Must be enabled in order to show up on [Revolt Discover](https://rvlt.gg).
|
||||
analytics: Option<bool>,
|
||||
/// Interactions URL
|
||||
#[validate(length(min = 1, max = 2048))]
|
||||
interactions_url: Option<String>,
|
||||
/// Fields to remove from bot object
|
||||
#[validate(length(min = 1))]
|
||||
remove: Option<Vec<FieldsBot>>,
|
||||
}
|
||||
|
||||
/// # Edit Bot
|
||||
///
|
||||
/// Edit bot details by its id.
|
||||
#[openapi(tag = "Bots")]
|
||||
#[patch("/<target>", data = "<data>")]
|
||||
pub async fn edit_bot(
|
||||
db: &Db,
|
||||
db: &State<Database>,
|
||||
user: User,
|
||||
target: Ref,
|
||||
target: Reference,
|
||||
data: Json<DataEditBot>,
|
||||
) -> Result<Json<Bot>> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
) -> Result<Json<v0::Bot>> {
|
||||
let data = data.into_inner();
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
data.validate().map_err(|error| {
|
||||
create_error!(FailedValidation {
|
||||
error: error.to_string()
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut bot = target.as_bot(db).await?;
|
||||
if bot.owner != user.id {
|
||||
return Err(Error::NotFound);
|
||||
return Err(create_error!(NotFound));
|
||||
}
|
||||
|
||||
if let Some(name) = data.name {
|
||||
@@ -67,7 +39,7 @@ pub async fn edit_bot(
|
||||
&& data.interactions_url.is_none()
|
||||
&& data.remove.is_none()
|
||||
{
|
||||
return Ok(Json(bot));
|
||||
return Ok(Json(bot.into()));
|
||||
}
|
||||
|
||||
let DataEditBot {
|
||||
@@ -78,26 +50,63 @@ pub async fn edit_bot(
|
||||
..
|
||||
} = data;
|
||||
|
||||
let mut partial = PartialBot {
|
||||
let partial = PartialBot {
|
||||
public,
|
||||
analytics,
|
||||
interactions_url,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(remove) = &remove {
|
||||
for field in remove {
|
||||
bot.remove(field);
|
||||
}
|
||||
bot.update(
|
||||
db,
|
||||
partial,
|
||||
remove
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|v| v.into())
|
||||
.collect(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
if remove.iter().any(|x| x == &FieldsBot::Token) {
|
||||
partial.token = Some(bot.token.clone());
|
||||
}
|
||||
}
|
||||
Ok(Json(bot.into()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_database::Bot;
|
||||
use revolt_models::v0::{self, FieldsBot};
|
||||
use rocket::http::{ContentType, Header, Status};
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn edit_bot() {
|
||||
let harness = TestHarness::new().await;
|
||||
let (_, session, user) = harness.new_user().await;
|
||||
|
||||
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
let response = harness
|
||||
.client
|
||||
.patch(format!("/bots/{}", bot.id))
|
||||
.header(ContentType::JSON)
|
||||
.body(
|
||||
json!(v0::DataEditBot {
|
||||
public: Some(true),
|
||||
remove: Some(vec![FieldsBot::Token]),
|
||||
..Default::default()
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
db.update_bot(&bot.id, &partial, remove.unwrap_or_default())
|
||||
.await?;
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
|
||||
bot.apply_options(partial);
|
||||
Ok(Json(bot))
|
||||
let updated_bot: v0::Bot = response.into_json().await.expect("`Bot`");
|
||||
assert!(!bot.public);
|
||||
assert!(updated_bot.public);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use revolt_database::{util::reference::Reference, Database};
|
||||
use revolt_database::{util::reference::Reference, Database, User};
|
||||
use revolt_models::v0::FetchBotResponse;
|
||||
use revolt_quark::{models::User, Error, Result};
|
||||
use revolt_result::{create_error, Result};
|
||||
use rocket::{serde::json::Json, State};
|
||||
|
||||
/// # Fetch Bot
|
||||
@@ -14,21 +14,46 @@ pub async fn fetch_bot(
|
||||
bot: Reference,
|
||||
) -> Result<Json<FetchBotResponse>> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
return Err(create_error!(IsBot));
|
||||
}
|
||||
|
||||
let bot = bot.as_bot(db).await.map_err(Error::from_core)?;
|
||||
let bot = bot.as_bot(db).await?;
|
||||
if bot.owner != user.id {
|
||||
return Err(Error::NotFound);
|
||||
return Err(create_error!(NotFound));
|
||||
}
|
||||
|
||||
Ok(Json(FetchBotResponse {
|
||||
user: db
|
||||
.fetch_user(&bot.id)
|
||||
.await
|
||||
.map_err(Error::from_core)?
|
||||
.into(None)
|
||||
.await,
|
||||
user: db.fetch_user(&bot.id).await?.into(None).await,
|
||||
bot: bot.into(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_database::Bot;
|
||||
use revolt_models::v0;
|
||||
use rocket::http::{Header, Status};
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn fetch_bot() {
|
||||
let harness = TestHarness::new().await;
|
||||
let (_, session, user) = harness.new_user().await;
|
||||
|
||||
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
let response = harness
|
||||
.client
|
||||
.get(format!("/bots/{}", bot.id))
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
|
||||
let response: v0::FetchBotResponse = response.into_json().await.expect("`Bot`");
|
||||
assert_eq!(response.bot, bot.into());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,16 @@
|
||||
use revolt_quark::{
|
||||
models::{Bot, User},
|
||||
Db, Error, Result,
|
||||
};
|
||||
use futures::future::join_all;
|
||||
use revolt_database::{Database, User};
|
||||
use revolt_models::v0::OwnedBotsResponse;
|
||||
use revolt_result::Result;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::Serialize;
|
||||
|
||||
/// # Owned Bots Response
|
||||
///
|
||||
/// Both lists are sorted by their IDs.
|
||||
#[derive(Serialize, JsonSchema)]
|
||||
pub struct OwnedBotsResponse {
|
||||
/// Bot objects
|
||||
bots: Vec<Bot>,
|
||||
/// User objects
|
||||
users: Vec<User>,
|
||||
}
|
||||
use rocket::State;
|
||||
|
||||
/// # Fetch Owned Bots
|
||||
///
|
||||
/// Fetch all of the bots that you have control over.
|
||||
#[openapi(tag = "Bots")]
|
||||
#[get("/@me")]
|
||||
pub async fn fetch_owned_bots(db: &Db, user: User) -> Result<Json<OwnedBotsResponse>> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
pub async fn fetch_owned_bots(db: &State<Database>, user: User) -> Result<Json<OwnedBotsResponse>> {
|
||||
let mut bots = db.fetch_bots_by_user(&user.id).await?;
|
||||
let user_ids = bots
|
||||
.iter()
|
||||
@@ -38,5 +23,41 @@ pub async fn fetch_owned_bots(db: &Db, user: User) -> Result<Json<OwnedBotsRespo
|
||||
bots.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
users.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
|
||||
Ok(Json(OwnedBotsResponse { users, bots }))
|
||||
Ok(Json(OwnedBotsResponse {
|
||||
users: join_all(users.into_iter().map(|user| user.into_self())).await,
|
||||
bots: bots.into_iter().map(|bot| bot.into()).collect(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_database::Bot;
|
||||
use revolt_models::v0;
|
||||
use rocket::http::{Header, Status};
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn fetch_owned() {
|
||||
let harness = TestHarness::new().await;
|
||||
let (_, session, user) = harness.new_user().await;
|
||||
|
||||
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
let response = harness
|
||||
.client
|
||||
.get("/bots/@me")
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
|
||||
let resp: v0::OwnedBotsResponse = response.into_json().await.expect("`Vec<Bot>`");
|
||||
assert_eq!(resp.bots.len(), 1);
|
||||
assert_eq!(resp.users.len(), 1);
|
||||
assert_eq!(resp.bots[0], bot.into());
|
||||
assert_eq!(resp.bots[0].id, resp.users[0].id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use revolt_database::Database;
|
||||
use revolt_database::{util::reference::Reference, Database, User};
|
||||
use revolt_models::v0::PublicBot;
|
||||
use revolt_quark::{models::User, Error, Ref, Result};
|
||||
use revolt_result::{create_error, Result};
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::State;
|
||||
@@ -13,13 +13,51 @@ use rocket::State;
|
||||
pub async fn fetch_public_bot(
|
||||
db: &State<Database>,
|
||||
user: Option<User>,
|
||||
target: Ref,
|
||||
target: Reference,
|
||||
) -> Result<Json<PublicBot>> {
|
||||
let bot = db.fetch_bot(&target.id).await.map_err(Error::from_core)?;
|
||||
let bot = db.fetch_bot(&target.id).await?;
|
||||
if !bot.public && user.map_or(true, |x| x.id != bot.owner) {
|
||||
return Err(Error::NotFound);
|
||||
return Err(create_error!(NotFound));
|
||||
}
|
||||
|
||||
let user = db.fetch_user(&bot.id).await.map_err(Error::from_core)?;
|
||||
let user = db.fetch_user(&bot.id).await?;
|
||||
Ok(Json(bot.into_public_bot(user)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_database::{Bot, PartialBot};
|
||||
use revolt_models::v0;
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn fetch_public() {
|
||||
let harness = TestHarness::new().await;
|
||||
let (_, _, user) = harness.new_user().await;
|
||||
|
||||
let mut bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
bot.update(
|
||||
&harness.db,
|
||||
PartialBot {
|
||||
public: Some(true),
|
||||
..Default::default()
|
||||
},
|
||||
vec![],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let bot_user = harness.db.fetch_user(&bot.id).await.expect("`User`");
|
||||
let response = harness
|
||||
.client
|
||||
.get(format!("/bots/{}/invite", bot.id))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
let public_bot: v0::PublicBot = response.into_json().await.expect("`PublicBot`");
|
||||
assert_eq!(public_bot, bot.into_public_bot(bot_user));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
use revolt_quark::{models::User, perms, Db, EmptyResponse, Error, Permission, Ref, Result};
|
||||
use revolt_database::util::permissions::DatabasePermissionQuery;
|
||||
use revolt_database::Member;
|
||||
use revolt_database::{util::reference::Reference, Database, User};
|
||||
use revolt_models::v0;
|
||||
use revolt_permissions::{
|
||||
calculate_channel_permissions, calculate_server_permissions, ChannelPermission,
|
||||
};
|
||||
use revolt_result::{create_error, Result};
|
||||
use rocket::State;
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// # Invite Destination
|
||||
#[derive(Deserialize, JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum InviteBotDestination {
|
||||
/// Invite to a server
|
||||
Server {
|
||||
/// Server Id
|
||||
server: String,
|
||||
},
|
||||
/// Invite to a group
|
||||
Group {
|
||||
/// Group Id
|
||||
group: String,
|
||||
},
|
||||
}
|
||||
use rocket_empty::EmptyResponse;
|
||||
|
||||
/// # Invite Bot
|
||||
///
|
||||
@@ -25,47 +17,168 @@ pub enum InviteBotDestination {
|
||||
#[openapi(tag = "Bots")]
|
||||
#[post("/<target>/invite", data = "<dest>")]
|
||||
pub async fn invite_bot(
|
||||
db: &Db,
|
||||
db: &State<Database>,
|
||||
user: User,
|
||||
target: Ref,
|
||||
dest: Json<InviteBotDestination>,
|
||||
target: Reference,
|
||||
dest: Json<v0::InviteBotDestination>,
|
||||
) -> Result<EmptyResponse> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
return Err(create_error!(IsBot));
|
||||
}
|
||||
|
||||
let bot = target.as_bot(db).await?;
|
||||
if !bot.public && bot.owner != user.id {
|
||||
return Err(Error::BotIsPrivate);
|
||||
return Err(create_error!(BotIsPrivate));
|
||||
}
|
||||
|
||||
let bot_user = db.fetch_user(&bot.id).await?;
|
||||
|
||||
match dest.into_inner() {
|
||||
InviteBotDestination::Server { server } => {
|
||||
v0::InviteBotDestination::Server { server } => {
|
||||
let server = db.fetch_server(&server).await?;
|
||||
|
||||
perms(&user)
|
||||
.server(&server)
|
||||
.throw_permission(db, Permission::ManageServer)
|
||||
.await?;
|
||||
let mut query = DatabasePermissionQuery::new(db, &user).server(&server);
|
||||
calculate_server_permissions(&mut query)
|
||||
.await
|
||||
.throw_if_lacking_channel_permission(ChannelPermission::ManageServer)?;
|
||||
|
||||
let user = db.fetch_user(&bot.id).await?;
|
||||
server
|
||||
.create_member(db, user, None)
|
||||
Member::create(db, &server, &bot_user)
|
||||
.await
|
||||
.map(|_| EmptyResponse)
|
||||
}
|
||||
InviteBotDestination::Group { group } => {
|
||||
v0::InviteBotDestination::Group { group } => {
|
||||
let mut channel = db.fetch_channel(&group).await?;
|
||||
|
||||
perms(&user)
|
||||
.channel(&channel)
|
||||
.throw_permission_and_view_channel(db, Permission::InviteOthers)
|
||||
.await?;
|
||||
let mut query = DatabasePermissionQuery::new(db, &user).channel(&channel);
|
||||
calculate_channel_permissions(&mut query)
|
||||
.await
|
||||
.throw_if_lacking_channel_permission(ChannelPermission::InviteOthers)?;
|
||||
|
||||
channel
|
||||
.add_user_to_group(db, &bot.id, &user.id)
|
||||
.add_user_to_group(db, &bot_user, &user.id)
|
||||
.await
|
||||
.map(|_| EmptyResponse)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{rocket, util::test::TestHarness};
|
||||
use revolt_database::{events::client::EventV1, Bot, Channel, Server};
|
||||
use revolt_models::v0;
|
||||
use rocket::http::{ContentType, Header, Status};
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn invite_bot_to_group() {
|
||||
let mut harness = TestHarness::new().await;
|
||||
let (_, session, user) = harness.new_user().await;
|
||||
|
||||
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
// FIXME: Channel::create_group
|
||||
let group = Channel::Group {
|
||||
id: ulid::Ulid::new().to_string(),
|
||||
name: TestHarness::rand_string(),
|
||||
owner: user.id.to_string(),
|
||||
description: None,
|
||||
last_message_id: None,
|
||||
icon: None,
|
||||
nsfw: false,
|
||||
permissions: None,
|
||||
recipients: vec![user.id.to_string()],
|
||||
};
|
||||
|
||||
group.create(&harness.db).await.unwrap();
|
||||
|
||||
let response = harness
|
||||
.client
|
||||
.post(format!("/bots/{}/invite", bot.id))
|
||||
.header(ContentType::JSON)
|
||||
.body(json!(v0::InviteBotDestination::Group { group: group.id() }).to_string())
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), Status::NoContent);
|
||||
drop(response);
|
||||
|
||||
let event = harness
|
||||
.wait_for_event(|event| match event {
|
||||
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
|
||||
_ => false,
|
||||
})
|
||||
.await;
|
||||
|
||||
match event {
|
||||
EventV1::ChannelGroupJoin { user, .. } => {
|
||||
assert_eq!(bot.id, user);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn invite_bot_to_server() {
|
||||
let mut harness = TestHarness::new().await;
|
||||
let (_, session, user) = harness.new_user().await;
|
||||
|
||||
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
|
||||
.await
|
||||
.expect("`Bot`");
|
||||
|
||||
// FIXME: Server::create_server
|
||||
let server = Server {
|
||||
id: ulid::Ulid::new().to_string(),
|
||||
name: TestHarness::rand_string(),
|
||||
owner: user.id.to_string(),
|
||||
analytics: false,
|
||||
discoverable: false,
|
||||
nsfw: false,
|
||||
banner: None,
|
||||
icon: None,
|
||||
categories: None,
|
||||
channels: vec![],
|
||||
default_permissions: 0,
|
||||
description: None,
|
||||
flags: None,
|
||||
roles: Default::default(),
|
||||
system_messages: None,
|
||||
};
|
||||
|
||||
server.create(&harness.db).await.unwrap();
|
||||
|
||||
let response = harness
|
||||
.client
|
||||
.post(format!("/bots/{}/invite", bot.id))
|
||||
.header(ContentType::JSON)
|
||||
.body(
|
||||
json!(v0::InviteBotDestination::Server {
|
||||
server: server.id.to_string()
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.header(Header::new("x-session-token", session.token.to_string()))
|
||||
.dispatch()
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), Status::NoContent);
|
||||
drop(response);
|
||||
|
||||
let event = harness
|
||||
.wait_for_event(|event| match event {
|
||||
EventV1::ServerMemberJoin { id, .. } => id == &server.id,
|
||||
_ => false,
|
||||
})
|
||||
.await;
|
||||
|
||||
match event {
|
||||
EventV1::ServerMemberJoin { user, .. } => {
|
||||
assert_eq!(bot.id, user);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use revolt_database::util::idempotency::IdempotencyKey;
|
||||
use revolt_quark::{
|
||||
models::{message::DataMessageSend, Message, User},
|
||||
perms,
|
||||
types::push::MessageAuthor,
|
||||
web::idempotency::IdempotencyKey,
|
||||
Db, Error, Permission, Ref, Result,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use revolt_quark::models::emoji::EmojiParent;
|
||||
use revolt_quark::models::{Emoji, File, User};
|
||||
use revolt_quark::variables::delta::MAX_EMOJI_COUNT;
|
||||
@@ -5,10 +7,13 @@ use revolt_quark::{perms, Db, Error, Permission, Result};
|
||||
use serde::Deserialize;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::util::regex::RE_EMOJI;
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
|
||||
/// Regex for valid emoji names
|
||||
///
|
||||
/// Alphanumeric and underscores
|
||||
pub static RE_EMOJI: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z0-9_]+$").unwrap());
|
||||
|
||||
/// # Emoji Data
|
||||
#[derive(Validate, Deserialize, JsonSchema)]
|
||||
pub struct DataCreateEmoji {
|
||||
@@ -57,7 +62,9 @@ pub async fn create_emoji(
|
||||
// ! FIXME: hardcoded upper limit
|
||||
let emojis = db.fetch_emoji_by_parent_id(&server.id).await?;
|
||||
if emojis.len() > *MAX_EMOJI_COUNT {
|
||||
return Err(Error::TooManyEmoji { max: *MAX_EMOJI_COUNT });
|
||||
return Err(Error::TooManyEmoji {
|
||||
max: *MAX_EMOJI_COUNT,
|
||||
});
|
||||
}
|
||||
}
|
||||
EmojiParent::Detached => return Err(Error::InvalidOperation),
|
||||
|
||||
@@ -25,7 +25,7 @@ pub fn mount(mut rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
mount_endpoints_and_merged_docs! {
|
||||
rocket, "/".to_owned(), settings,
|
||||
"/" => (vec![], custom_openapi_spec()),
|
||||
"" => openapi_get_routes_spec![root::root, root::ping],
|
||||
"" => openapi_get_routes_spec![root::root],
|
||||
"/admin" => admin::routes(),
|
||||
"/users" => users::routes(),
|
||||
"/bots" => bots::routes(),
|
||||
@@ -46,7 +46,7 @@ pub fn mount(mut rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
mount_endpoints_and_merged_docs! {
|
||||
rocket, "/".to_owned(), settings,
|
||||
"/" => (vec![], custom_openapi_spec()),
|
||||
"" => openapi_get_routes_spec![root::root, root::ping],
|
||||
"" => openapi_get_routes_spec![root::root],
|
||||
"/admin" => admin::routes(),
|
||||
"/users" => users::routes(),
|
||||
"/bots" => bots::routes(),
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
use crate::util::regex::RE_USERNAME;
|
||||
use revolt_quark::{
|
||||
authifier::models::Session, models::User, Database, EmptyResponse, Error, Result,
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use revolt_database::{Database, User};
|
||||
use revolt_models::v0;
|
||||
use revolt_quark::authifier::models::Session;
|
||||
use revolt_result::{create_error, Result};
|
||||
|
||||
use rocket::{serde::json::Json, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
/// Regex for valid usernames
|
||||
///
|
||||
/// Block zero width space
|
||||
/// Block lookalike characters
|
||||
pub static RE_USERNAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\p{L}|[\d_.-])+$").unwrap());
|
||||
|
||||
/// # New User Data
|
||||
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
||||
pub struct DataOnboard {
|
||||
@@ -25,22 +33,22 @@ pub async fn req(
|
||||
session: Session,
|
||||
user: Option<User>,
|
||||
data: Json<DataOnboard>,
|
||||
) -> Result<EmptyResponse> {
|
||||
) -> Result<Json<v0::User>> {
|
||||
if user.is_some() {
|
||||
return Err(Error::AlreadyOnboarded);
|
||||
return Err(create_error!(AlreadyOnboarded));
|
||||
}
|
||||
|
||||
let data = data.into_inner();
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
data.validate().map_err(|error| {
|
||||
create_error!(FailedValidation {
|
||||
error: error.to_string()
|
||||
})
|
||||
})?;
|
||||
|
||||
let username = User::validate_username(data.username)?;
|
||||
let user = User {
|
||||
id: session.user_id,
|
||||
discriminator: User::find_discriminator(db, &username, None).await?,
|
||||
username,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
db.insert_user(&user).await.map(|_| EmptyResponse)
|
||||
Ok(Json(
|
||||
User::create(db, data.username, session.user_id, None)
|
||||
.await?
|
||||
.into_self()
|
||||
.await,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ use revolt_quark::variables::delta::{
|
||||
};
|
||||
use revolt_quark::Result;
|
||||
|
||||
use rocket::http::Status;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::Serialize;
|
||||
|
||||
@@ -138,9 +137,22 @@ pub async fn root() -> Result<Json<RevoltConfig>> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// Example endpoint.
|
||||
#[openapi(skip)]
|
||||
#[get("/ping")]
|
||||
pub async fn ping(/*_limitguard: Ratelimiter*/) -> Status {
|
||||
Status::Ok
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::rocket;
|
||||
use rocket::http::Status;
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn hello_world() {
|
||||
let harness = crate::util::test::TestHarness::new().await;
|
||||
let response = harness.client.get("/").dispatch().await;
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
}
|
||||
|
||||
#[rocket::async_test]
|
||||
async fn hello_world_concurrent() {
|
||||
let harness = crate::util::test::TestHarness::new().await;
|
||||
let response = harness.client.get("/").dispatch().await;
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
use crate::util::regex::RE_USERNAME;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use revolt_quark::{authifier::models::Account, models::User, Database, Error, Result};
|
||||
use rocket::{serde::json::Json, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
/// Regex for valid usernames
|
||||
///
|
||||
/// Block zero width space
|
||||
/// Block lookalike characters
|
||||
pub static RE_USERNAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\p{L}|[\d_.-])+$").unwrap());
|
||||
|
||||
/// # Username Information
|
||||
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
||||
pub struct DataChangeUsername {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use revolt_quark::models::user::{FieldsUser, PartialUser, User};
|
||||
use revolt_quark::models::File;
|
||||
use revolt_quark::{Database, Error, Ref, Result};
|
||||
@@ -8,7 +10,11 @@ use rocket::State;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
use crate::util::regex::RE_DISPLAY_NAME;
|
||||
/// Regex for valid display names
|
||||
///
|
||||
/// Block zero width space
|
||||
/// Block newline and carriage return
|
||||
pub static RE_DISPLAY_NAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[^\u200B\n\r]+$").unwrap());
|
||||
|
||||
/// # Profile Data
|
||||
#[derive(Validate, Serialize, Deserialize, Debug, JsonSchema)]
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use revolt_database::{util::reference::Reference, Database};
|
||||
use revolt_database::{
|
||||
util::{idempotency::IdempotencyKey, reference::Reference},
|
||||
Database,
|
||||
};
|
||||
use revolt_quark::{
|
||||
models::message::{DataMessageSend, Message},
|
||||
types::push::MessageAuthor,
|
||||
web::idempotency::IdempotencyKey,
|
||||
Db, Error, Result,
|
||||
};
|
||||
use rocket::{serde::json::Json, State};
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod regex;
|
||||
pub mod ratelimiter;
|
||||
pub mod test;
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
//! Pulled from lightspeed-tv/backend.
|
||||
//!
|
||||
//! This will be replaced again in the near future since
|
||||
//! I don't want duplication between two different projects.
|
||||
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::Hasher;
|
||||
use std::ops::Add;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::authifier::models::Session;
|
||||
use revolt_quark::authifier::models::Session;
|
||||
use rocket::fairing::{Fairing, Info, Kind};
|
||||
use rocket::http::uri::Origin;
|
||||
use rocket::http::{Method, Status};
|
||||
@@ -1,19 +0,0 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
|
||||
/// Regex for valid display names
|
||||
///
|
||||
/// Block zero width space
|
||||
/// Block newline and carriage return
|
||||
pub static RE_DISPLAY_NAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[^\u200B\n\r]+$").unwrap());
|
||||
|
||||
/// Regex for valid usernames
|
||||
///
|
||||
/// Block zero width space
|
||||
/// Block lookalike characters
|
||||
pub static RE_USERNAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\p{L}|[\d_.-])+$").unwrap());
|
||||
|
||||
/// Regex for valid emoji names
|
||||
///
|
||||
/// Alphanumeric and underscores
|
||||
pub static RE_EMOJI: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z0-9_]+$").unwrap());
|
||||
110
crates/delta/src/util/test.rs
Normal file
110
crates/delta/src/util/test.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
use futures::StreamExt;
|
||||
use rand::Rng;
|
||||
use redis_kiss::redis::aio::PubSub;
|
||||
use revolt_database::{events::client::EventV1, Database, DatabaseInfo, User};
|
||||
use revolt_quark::authifier::{
|
||||
models::{Account, Session},
|
||||
Authifier,
|
||||
};
|
||||
use rocket::local::asynchronous::Client;
|
||||
|
||||
pub struct TestHarness {
|
||||
pub client: Client,
|
||||
authifier: Authifier,
|
||||
pub db: Database,
|
||||
sub: PubSub,
|
||||
event_buffer: Vec<EventV1>,
|
||||
}
|
||||
|
||||
impl TestHarness {
|
||||
pub async fn new() -> TestHarness {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let client = Client::tracked(crate::web().await)
|
||||
.await
|
||||
.expect("valid rocket instance");
|
||||
|
||||
let db = DatabaseInfo::Auto.connect().await.expect("`Database`");
|
||||
let mut sub = redis_kiss::open_pubsub_connection()
|
||||
.await
|
||||
.expect("`PubSub`");
|
||||
|
||||
sub.psubscribe("*").await.unwrap();
|
||||
|
||||
TestHarness {
|
||||
client,
|
||||
authifier: Authifier {
|
||||
database: db.clone().into(),
|
||||
config: revolt_quark::util::authifier::config(),
|
||||
event_channel: None,
|
||||
},
|
||||
db,
|
||||
sub,
|
||||
event_buffer: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rand_string() -> String {
|
||||
let mut rng = rand::thread_rng();
|
||||
(&mut rng)
|
||||
.sample_iter(rand::distributions::Alphanumeric)
|
||||
.take(20)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn new_user(&self) -> (Account, Session, User) {
|
||||
let account = Account::new(
|
||||
&self.authifier,
|
||||
format!("{}@revolt.chat", TestHarness::rand_string()),
|
||||
"password".to_string(),
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.expect("`Account`");
|
||||
|
||||
let session = account
|
||||
.create_session(&self.authifier, String::new())
|
||||
.await
|
||||
.expect("`Session`");
|
||||
|
||||
let user = User::create(
|
||||
&self.db,
|
||||
TestHarness::rand_string(),
|
||||
account.id.to_string(),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("`User`");
|
||||
|
||||
(account, session, user)
|
||||
}
|
||||
|
||||
pub async fn wait_for_event<F>(&mut self, predicate: F) -> EventV1
|
||||
where
|
||||
F: Fn(&EventV1) -> bool,
|
||||
{
|
||||
for event in &self.event_buffer {
|
||||
if predicate(event) {
|
||||
// does not remove from buffer
|
||||
return event.clone();
|
||||
}
|
||||
}
|
||||
|
||||
let mut stream = self.sub.on_message();
|
||||
while let Some(item) = stream.next().await {
|
||||
let payload: EventV1 = redis_kiss::decode_payload(&item.unwrap()).unwrap();
|
||||
|
||||
if predicate(&payload) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
self.event_buffer.push(payload);
|
||||
}
|
||||
|
||||
// WARNING: if predicate is never satisfied, this will never return
|
||||
// should add a timeout for events so tests can fail gracefully
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@ pub mod servers {
|
||||
}
|
||||
|
||||
pub mod users {
|
||||
pub mod bot;
|
||||
pub mod user;
|
||||
pub mod user_settings;
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
use crate::models::bot::{Bot, FieldsBot, PartialBot};
|
||||
use crate::{AbstractBot, Result};
|
||||
|
||||
use super::super::DummyDb;
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractBot for DummyDb {
|
||||
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
||||
Ok(Bot {
|
||||
id: id.into(),
|
||||
owner: "user".into(),
|
||||
token: "token".into(),
|
||||
public: true,
|
||||
analytics: true,
|
||||
discoverable: true,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
async fn fetch_bot_by_token(&self, _token: &str) -> Result<Bot> {
|
||||
self.fetch_bot("bot").await
|
||||
}
|
||||
|
||||
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
|
||||
info!("Insert {bot:?}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()> {
|
||||
info!("Update {id} with {bot:?} and remove {remove:?}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_bot(&self, id: &str) -> Result<()> {
|
||||
info!("Delete {id}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
||||
Ok(vec![self.fetch_bot(user_id).await.unwrap()])
|
||||
}
|
||||
|
||||
async fn get_number_of_bots_by_user(&self, _user_id: &str) -> Result<usize> {
|
||||
Ok(1)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use revolt_database::util::idempotency::IdempotencyKey;
|
||||
use ulid::Ulid;
|
||||
|
||||
use crate::{
|
||||
@@ -11,8 +12,7 @@ use crate::{
|
||||
},
|
||||
tasks::{ack::AckEvent, process_embeds},
|
||||
types::push::MessageAuthor,
|
||||
variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT, MAX_EMBED_COUNT},
|
||||
web::idempotency::IdempotencyKey,
|
||||
variables::delta::{MAX_ATTACHMENT_COUNT, MAX_EMBED_COUNT, MAX_REPLY_COUNT},
|
||||
Database, Error, OverrideField, Ref, Result,
|
||||
};
|
||||
|
||||
@@ -413,7 +413,10 @@ impl Channel {
|
||||
) -> Result<Message> {
|
||||
Message::validate_sum(&data.content, data.embeds.as_deref().unwrap_or_default())?;
|
||||
|
||||
idempotency.consume_nonce(data.nonce).await?;
|
||||
idempotency
|
||||
.consume_nonce(data.nonce)
|
||||
.await
|
||||
.map_err(|_| Error::InvalidOperation)?;
|
||||
|
||||
// Check the message is not empty
|
||||
if (data.content.as_ref().map_or(true, |v| v.is_empty()))
|
||||
@@ -497,16 +500,24 @@ impl Channel {
|
||||
|
||||
// Add attachments to message.
|
||||
let mut attachments = vec![];
|
||||
if data.attachments.as_ref().is_some_and(|v| v.len() > *MAX_ATTACHMENT_COUNT) {
|
||||
if data
|
||||
.attachments
|
||||
.as_ref()
|
||||
.is_some_and(|v| v.len() > *MAX_ATTACHMENT_COUNT)
|
||||
{
|
||||
return Err(Error::TooManyAttachments {
|
||||
max: *MAX_ATTACHMENT_COUNT,
|
||||
});
|
||||
}
|
||||
|
||||
if data.embeds.as_ref().is_some_and(|v| v.len() > *MAX_EMBED_COUNT) {
|
||||
if data
|
||||
.embeds
|
||||
.as_ref()
|
||||
.is_some_and(|v| v.len() > *MAX_EMBED_COUNT)
|
||||
{
|
||||
return Err(Error::TooManyEmbeds {
|
||||
max: *MAX_EMBED_COUNT
|
||||
})
|
||||
max: *MAX_EMBED_COUNT,
|
||||
});
|
||||
}
|
||||
|
||||
for attachment_id in data.attachments.as_deref().unwrap_or_default() {
|
||||
|
||||
@@ -19,7 +19,6 @@ pub mod servers {
|
||||
}
|
||||
|
||||
pub mod users {
|
||||
pub mod bot;
|
||||
pub mod user;
|
||||
pub mod user_settings;
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
use nanoid::nanoid;
|
||||
|
||||
use crate::{
|
||||
models::{bot::FieldsBot, Bot},
|
||||
Database, Result,
|
||||
};
|
||||
|
||||
impl Bot {
|
||||
/// Remove a field from this object
|
||||
pub fn remove(&mut self, field: &FieldsBot) {
|
||||
match field {
|
||||
FieldsBot::Token => self.token = nanoid!(64),
|
||||
FieldsBot::InteractionsURL => {
|
||||
self.interactions_url.take();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete this bot
|
||||
pub async fn delete(&self, db: &Database) -> Result<()> {
|
||||
db.fetch_user(&self.id).await?.mark_deleted(db).await?;
|
||||
db.delete_bot(&self.id).await
|
||||
}
|
||||
}
|
||||
@@ -243,6 +243,8 @@ impl User {
|
||||
return Err(Error::DiscriminatorChangeRatelimited);
|
||||
}
|
||||
|
||||
// FIXME: don't access directly?
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
rvdb.insert_ratelimit_event(&revolt_database::RatelimitEvent {
|
||||
id: ulid::Ulid::new().to_string(),
|
||||
target_id,
|
||||
@@ -469,7 +471,17 @@ impl User {
|
||||
#[async_recursion]
|
||||
pub async fn from_token(db: &Database, token: &str, hint: UserHint) -> Result<User> {
|
||||
match hint {
|
||||
UserHint::Bot => db.fetch_user(&db.fetch_bot_by_token(token).await?.id).await,
|
||||
UserHint::Bot => {
|
||||
let rvdb: revolt_database::Database = db.clone().into();
|
||||
db.fetch_user(
|
||||
&rvdb
|
||||
.fetch_bot_by_token(token)
|
||||
.await
|
||||
.map_err(|_| Error::InternalError)?
|
||||
.id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
UserHint::User => db.fetch_user_by_token(token).await,
|
||||
UserHint::Any => {
|
||||
if let Ok(user) = User::from_token(db, token, UserHint::User).await {
|
||||
|
||||
@@ -34,7 +34,6 @@ pub mod servers {
|
||||
}
|
||||
|
||||
pub mod users {
|
||||
pub mod bot;
|
||||
pub mod user;
|
||||
pub mod user_settings;
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
use crate::models::bot::{Bot, FieldsBot, PartialBot};
|
||||
use crate::r#impl::mongo::IntoDocumentPath;
|
||||
use crate::{AbstractBot, Result};
|
||||
|
||||
use super::super::MongoDb;
|
||||
|
||||
static COL: &str = "bots";
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractBot for MongoDb {
|
||||
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
||||
self.find_one_by_id(COL, id).await
|
||||
}
|
||||
|
||||
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot> {
|
||||
self.find_one(
|
||||
COL,
|
||||
doc! {
|
||||
"token": token
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
|
||||
self.insert_one(COL, &bot).await.map(|_| ())
|
||||
}
|
||||
|
||||
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()> {
|
||||
self.update_one_by_id(
|
||||
COL,
|
||||
id,
|
||||
bot,
|
||||
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
async fn delete_bot(&self, id: &str) -> Result<()> {
|
||||
self.delete_one_by_id(COL, id).await.map(|_| ())
|
||||
}
|
||||
|
||||
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
||||
self.find(
|
||||
COL,
|
||||
doc! {
|
||||
"owner": user_id
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize> {
|
||||
// ! FIXME: move this to generic?
|
||||
self.fetch_bots_by_user(user_id).await.map(|x| x.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoDocumentPath for FieldsBot {
|
||||
fn as_path(&self) -> Option<&'static str> {
|
||||
match self {
|
||||
FieldsBot::InteractionsURL => Some("interactions_url"),
|
||||
FieldsBot::Token => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ mod servers {
|
||||
}
|
||||
|
||||
mod users {
|
||||
pub mod bot;
|
||||
pub mod user;
|
||||
pub mod user_settings;
|
||||
}
|
||||
@@ -40,7 +39,6 @@ pub use servers::*;
|
||||
pub use users::*;
|
||||
|
||||
pub use attachment::File;
|
||||
pub use bot::Bot;
|
||||
pub use channel::Channel;
|
||||
pub use channel_invite::Invite;
|
||||
pub use channel_unread::ChannelUnread;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::models::{channel::{Channel, FieldsChannel, PartialChannel}};
|
||||
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
||||
use crate::{OverrideField, Result};
|
||||
|
||||
#[async_trait]
|
||||
@@ -13,9 +13,6 @@ pub trait AbstractChannel: Sync + Send {
|
||||
async fn insert_channel(&self, channel: &Channel) -> Result<()>;
|
||||
|
||||
/// Update an existing channel using some data
|
||||
/// ! TODO: we need separate Channel::update which also sends out the relevant events
|
||||
/// ! also applies to other methods I guess, try to restrict event bound methods to
|
||||
/// ! the models themselves instead of the abstract database
|
||||
async fn update_channel(
|
||||
&self,
|
||||
id: &str,
|
||||
|
||||
@@ -21,7 +21,6 @@ mod servers {
|
||||
}
|
||||
|
||||
mod users {
|
||||
pub mod bot;
|
||||
pub mod user;
|
||||
pub mod user_settings;
|
||||
}
|
||||
@@ -45,7 +44,6 @@ pub use servers::server::AbstractServer;
|
||||
pub use servers::server_ban::AbstractServerBan;
|
||||
pub use servers::server_member::AbstractServerMember;
|
||||
|
||||
pub use users::bot::AbstractBot;
|
||||
pub use users::user::AbstractUser;
|
||||
pub use users::user_settings::AbstractUserSettings;
|
||||
|
||||
@@ -65,7 +63,6 @@ pub trait AbstractDatabase:
|
||||
+ AbstractServer
|
||||
+ AbstractServerBan
|
||||
+ AbstractServerMember
|
||||
+ AbstractBot
|
||||
+ AbstractUser
|
||||
+ AbstractUserSettings
|
||||
+ AbstractReport
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
use crate::models::bot::{Bot, FieldsBot, PartialBot};
|
||||
use crate::Result;
|
||||
|
||||
#[async_trait]
|
||||
pub trait AbstractBot: Sync + Send {
|
||||
/// Fetch a bot by its id
|
||||
async fn fetch_bot(&self, id: &str) -> Result<Bot>;
|
||||
|
||||
/// Fetch a bot by its token
|
||||
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot>;
|
||||
|
||||
/// Insert new bot into the database
|
||||
async fn insert_bot(&self, bot: &Bot) -> Result<()>;
|
||||
|
||||
/// Update bot with new information
|
||||
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()>;
|
||||
|
||||
/// Delete a bot from the database
|
||||
async fn delete_bot(&self, id: &str) -> Result<()>;
|
||||
|
||||
/// Fetch bots owned by a user
|
||||
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>>;
|
||||
|
||||
/// Get the number of bots owned by a user
|
||||
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize>;
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models::{
|
||||
Bot, Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User,
|
||||
Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User,
|
||||
};
|
||||
use crate::{Database, Error, Result};
|
||||
|
||||
@@ -56,11 +56,6 @@ impl Ref {
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
/// Fetch bot from Ref
|
||||
pub async fn as_bot(&self, db: &Database) -> Result<Bot> {
|
||||
db.fetch_bot(&self.id).await
|
||||
}
|
||||
|
||||
/// Fetch invite from Ref
|
||||
pub async fn as_invite(&self, db: &Database) -> Result<Invite> {
|
||||
Invite::find(db, &self.id).await
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
pub use rocket_cors::catch_all_options_routes;
|
||||
use rocket_cors::{AllowedOrigins, Cors};
|
||||
|
||||
pub fn new() -> Cors {
|
||||
rocket_cors::CorsOptions {
|
||||
allowed_origins: AllowedOrigins::All,
|
||||
allowed_methods: [
|
||||
"Get", "Put", "Post", "Delete", "Options", "Head", "Trace", "Connect", "Patch",
|
||||
]
|
||||
.iter()
|
||||
.map(|s| FromStr::from_str(s).unwrap())
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}
|
||||
.to_cors()
|
||||
.expect("Failed to create CORS.")
|
||||
}
|
||||
@@ -1,10 +1,5 @@
|
||||
use crate::Database;
|
||||
use rocket::State;
|
||||
|
||||
pub mod cors;
|
||||
pub mod idempotency;
|
||||
pub mod ratelimiter;
|
||||
pub mod swagger;
|
||||
|
||||
pub use rocket_empty::EmptyResponse;
|
||||
pub type Db = State<Database>;
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
revolt_rocket_okapi::swagger_ui::make_swagger_ui(&revolt_rocket_okapi::swagger_ui::SwaggerUIConfig {
|
||||
url: "../openapi.json".to_owned(),
|
||||
..Default::default()
|
||||
})
|
||||
.into()
|
||||
}
|
||||
33
default.nix
Normal file
33
default.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
let
|
||||
# Pinned nixpkgs, deterministic. Last updated: 11-08-2023.
|
||||
pkgs = import (fetchTarball("https://github.com/NixOS/nixpkgs/archive/bb9707ef2ea4a5b749b362d5cf81ada3ded2c53f.tar.gz")) {};
|
||||
|
||||
# Rolling updates, not deterministic.
|
||||
# pkgs = import (fetchTarball("channel:nixpkgs-unstable")) {};
|
||||
in pkgs.mkShell {
|
||||
name = "revoltEnv";
|
||||
|
||||
# LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
|
||||
# pkgs.gcc-unwrapped
|
||||
# pkgs.zlib
|
||||
# pkgs.glib
|
||||
# pkgs.libGL
|
||||
# ];
|
||||
|
||||
buildInputs = [
|
||||
# Tools
|
||||
pkgs.git
|
||||
|
||||
# Database
|
||||
# pkgs.mongodb
|
||||
|
||||
# Rust
|
||||
pkgs.cargo
|
||||
pkgs.rustc
|
||||
pkgs.clippy
|
||||
pkgs.pkgconfig
|
||||
pkgs.openssl.dev
|
||||
];
|
||||
|
||||
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||
}
|
||||
Reference in New Issue
Block a user