Merge branch 'master' of github.com:revoltchat/backend into webhooks

This commit is contained in:
Zomatree
2023-01-18 21:03:15 +00:00
25 changed files with 314 additions and 79 deletions

View File

@@ -1,10 +1,10 @@
name: Rust build and test
name: Rust build, test, and generate specification
on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
env:
CARGO_TERM_COLOR: always
@@ -18,16 +18,58 @@ jobs:
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Run cargo build
uses: actions-rs/cargo@v1
with:
command: build
- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
- name: Copy .env.example
if: github.event_name != 'pull_request'
run: |
cp .env.example .env
- name: Run services in background
if: github.event_name != 'pull_request'
run: |
docker-compose -f docker-compose.db.yml up -d
- name: Start API in background
if: github.event_name != 'pull_request'
run: |
cargo run --bin revolt-delta &
- name: Wait for API to go up
if: github.event_name != 'pull_request'
uses: nev7n/wait_for_response@v1
with:
url: "http://localhost:8000/"
- name: Checkout API repository
if: github.event_name != 'pull_request'
uses: actions/checkout@v3
with:
repository: revoltchat/api
path: api
- name: Download OpenAPI specification
if: github.event_name != 'pull_request'
run: curl http://localhost:8000/openapi.json -o api/OpenAPI.json
- name: Commit changes
if: github.event_name != 'pull_request'
uses: EndBug/add-and-commit@v4
with:
cwd: "api"
add: "*.json"
message: "chore: generate OpenAPI specification"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

183
Cargo.lock generated
View File

@@ -81,6 +81,12 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "anyhow"
version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
[[package]]
name = "arrayref"
version = "0.3.6"
@@ -517,6 +523,9 @@ name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
dependencies = [
"jobserver",
]
[[package]]
name = "cfg-if"
@@ -606,7 +615,7 @@ dependencies = [
"rand 0.8.5",
"sha2",
"subtle",
"time 0.3.9",
"time 0.3.17",
"version_check",
]
@@ -875,6 +884,26 @@ dependencies = [
"syn 1.0.96",
]
[[package]]
name = "enum-iterator"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91a4ec26efacf4aeff80887a175a419493cb6f8b5480d26387eb0bd038976187"
dependencies = [
"enum-iterator-derive",
]
[[package]]
name = "enum-iterator-derive"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "828de45d0ca18782232dfb8f3ea9cc428e8ced380eb26a520baaacfc70de39ce"
dependencies = [
"proc-macro2",
"quote 1.0.18",
"syn 1.0.96",
]
[[package]]
name = "env_logger"
version = "0.7.1"
@@ -1138,6 +1167,18 @@ dependencies = [
"wasi 0.10.0+wasi-snapshot-preview1",
]
[[package]]
name = "getset"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e45727250e75cc04ff2846a66397da8ef2b3db8e40e0cef4df67950a07621eb9"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote 1.0.18",
"syn 1.0.96",
]
[[package]]
name = "ghash"
version = "0.4.4"
@@ -1154,6 +1195,19 @@ version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4"
[[package]]
name = "git2"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2994bee4a3a6a51eb90c218523be382fd7ea09b16380b9312e9dbe955ff7c7d1"
dependencies = [
"bitflags",
"libc",
"libgit2-sys",
"log",
"url",
]
[[package]]
name = "glob"
version = "0.3.0"
@@ -1514,7 +1568,7 @@ dependencies = [
"generic-array 0.14.5",
"schemars",
"serde",
"time 0.3.9",
"time 0.3.17",
]
[[package]]
@@ -1529,6 +1583,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "jobserver"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b"
dependencies = [
"libc",
]
[[package]]
name = "js-sys"
version = "0.3.57"
@@ -1590,6 +1653,30 @@ version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "libgit2-sys"
version = "0.14.1+1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a07fb2692bc3593bda59de45a502bb3071659f2c515e28c71e728306b038e17"
dependencies = [
"cc",
"libc",
"libz-sys",
"pkg-config",
]
[[package]]
name = "libz-sys"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "linked-hash-map"
version = "0.5.4"
@@ -1914,6 +2001,15 @@ dependencies = [
"minimal-lexical",
]
[[package]]
name = "ntapi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc51db7b362b205941f71232e56c625156eb9a929f8cf74a428fd5bc094a4afc"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@@ -1964,15 +2060,6 @@ dependencies = [
"syn 1.0.96",
]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.28.4"
@@ -2765,6 +2852,7 @@ dependencies = [
"ulid 0.4.1",
"url",
"validator 0.14.0",
"vergen",
]
[[package]]
@@ -2881,7 +2969,7 @@ dependencies = [
"serde_json",
"state",
"tempfile",
"time 0.3.9",
"time 0.3.17",
"tokio 1.18.2",
"tokio-stream",
"tokio-util 0.7.2",
@@ -2953,7 +3041,7 @@ dependencies = [
"smallvec",
"stable-pattern",
"state",
"time 0.3.9",
"time 0.3.17",
"tokio 1.18.2",
"uncased",
]
@@ -3069,9 +3157,9 @@ dependencies = [
[[package]]
name = "rustversion"
version = "1.0.6"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f"
checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70"
[[package]]
name = "ryu"
@@ -3261,7 +3349,7 @@ dependencies = [
"serde",
"serde_json",
"thiserror",
"time 0.3.9",
"time 0.3.17",
"url",
"uuid",
]
@@ -3545,6 +3633,20 @@ dependencies = [
"unicode-xid 0.0.4",
]
[[package]]
name = "sysinfo"
version = "0.27.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c215311383d25d03753375c53ab9fad8fc0cf46953c409211e065edeabf577ee"
dependencies = [
"cfg-if 1.0.0",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"winapi 0.3.9",
]
[[package]]
name = "take_mut"
version = "0.2.2"
@@ -3576,18 +3678,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.31"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a"
checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.31"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a"
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
dependencies = [
"proc-macro2",
"quote 1.0.18",
@@ -3616,21 +3718,30 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.9"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376"
dependencies = [
"itoa 1.0.2",
"libc",
"num_threads",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-macros"
version = "0.2.4"
name = "time-core"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
[[package]]
name = "time-macros"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d967f99f534ca7e495c575c62638eebc2898a8c84c119b89e250477bc4ba16b2"
dependencies = [
"time-core",
]
[[package]]
name = "tinyvec"
@@ -4204,6 +4315,24 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vergen"
version = "7.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "571b69f690c855821462709b6f41d42ceccc316fbd17b60bd06d06928cfe6a99"
dependencies = [
"anyhow",
"cfg-if 1.0.0",
"enum-iterator",
"getset",
"git2",
"rustc_version 0.4.0",
"rustversion",
"sysinfo",
"thiserror",
"time 0.3.17",
]
[[package]]
name = "version_check"
version = "0.9.4"

View File

@@ -61,3 +61,6 @@ rocket_okapi = { git = "https://github.com/insertish/okapi", rev = "a1048d0c8cd7
# quark
revolt-quark = { path = "../quark" }
[build-dependencies]
vergen = "7.5.0"

16
crates/delta/build.rs Normal file
View File

@@ -0,0 +1,16 @@
use vergen::{vergen, Config};
use std::process::Command;
fn main() {
if let Ok(output) = Command::new("git")
.args(["config", "--get", "remote.origin.url"])
.output()
{
if let Ok(git_origin) = String::from_utf8(output.stdout) {
println!("cargo:rustc-env=GIT_ORIGIN_URL={}", git_origin);
}
}
vergen(Config::default()).unwrap();
}

View File

@@ -23,6 +23,9 @@ pub enum InviteResponse {
/// Attachment for server banner
#[serde(skip_serializing_if = "Option::is_none")]
server_banner: Option<File>,
/// Enum of server flags
#[serde(skip_serializing_if = "Option::is_none")]
server_flags: Option<i32>,
/// Id of server channel
channel_id: String,
/// Name of server channel
@@ -94,6 +97,7 @@ pub async fn req(db: &Db, target: Ref) -> Result<Json<InviteResponse>> {
server_name: server.name,
server_icon: server.icon,
server_banner: server.banner,
server_flags: server.flags,
channel_id: id,
channel_name: name,
channel_description: description,

View File

@@ -22,7 +22,7 @@ pub async fn req(
) -> Result<EmptyResponse> {
session.subscription = Some(data.into_inner());
session
.save(&rauth)
.save(rauth)
.await
.map(|_| EmptyResponse)
.map_err(|_| Error::DatabaseError {

View File

@@ -13,7 +13,7 @@ use rocket::State;
pub async fn req(rauth: &State<RAuth>, mut session: Session) -> Result<EmptyResponse> {
session.subscription = None;
session
.save(&rauth)
.save(rauth)
.await
.map(|_| EmptyResponse)
.map_err(|_| Error::DatabaseError {

View File

@@ -54,6 +54,21 @@ pub struct RevoltFeatures {
pub voso: VoiceFeature,
}
/// # Build Information
#[derive(Serialize, JsonSchema, Debug)]
pub struct BuildInformation {
/// Commit Hash
pub commit_sha: String,
/// Commit Timestamp
pub commit_timestamp: String,
/// Git Semver
pub semver: String,
/// Git Origin URL
pub origin_url: String,
/// Build Timestamp
pub timestamp: String,
}
/// # Server Configuration
#[derive(Serialize, JsonSchema, Debug)]
pub struct RevoltConfig {
@@ -67,6 +82,8 @@ pub struct RevoltConfig {
pub app: String,
/// Web Push VAPID public key
pub vapid: String,
/// Build information
pub build: BuildInformation,
}
/// # Query Node
@@ -101,6 +118,13 @@ pub async fn root() -> Result<Json<RevoltConfig>> {
ws: EXTERNAL_WS_URL.to_string(),
app: APP_URL.to_string(),
vapid: VAPID_PUBLIC_KEY.to_string(),
build: BuildInformation {
commit_sha: env!("VERGEN_GIT_SHA").to_string(),
commit_timestamp: env!("VERGEN_GIT_COMMIT_TIMESTAMP").to_string(),
semver: env!("VERGEN_GIT_SEMVER").to_string(),
origin_url: env!("GIT_ORIGIN_URL", "<missing>").to_string(),
timestamp: env!("VERGEN_BUILD_TIMESTAMP").to_string(),
},
}))
}

View File

@@ -1,8 +1,6 @@
use revolt_quark::{
models::{Member, User},
perms,
presence::presence_filter_online,
Db, Ref, Result,
perms, Db, Ref, Result,
};
use rocket::serde::json::Json;
@@ -47,16 +45,7 @@ pub async fn req(
user_ids.push(member.id.user.clone());
}
let online_ids = presence_filter_online(&user_ids).await;
let mut users = db
.fetch_users(&user_ids)
.await?
.into_iter()
.map(|mut user| {
user.online = Some(online_ids.contains(&user.id));
user.foreign()
})
.collect::<Vec<User>>();
let mut users = User::fetch_foreign_users(db, &user_ids).await?;
// Ensure the lists match up exactly.
members.sort_by(|a, b| a.id.user.cmp(&b.id.user));

View File

@@ -23,11 +23,7 @@ impl Cache {
pub async fn can_view_channel(&self, db: &Database, channel: &Channel) -> bool {
match &channel {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
let member = self
.members
.iter()
.map(|(_, x)| x)
.find(|x| &x.id.server == server);
let member = self.members.values().find(|x| &x.id.server == server);
let server = self.servers.get(server);
let mut perms = perms(self.users.get(&self.user_id).unwrap()).channel(channel);

View File

@@ -395,7 +395,7 @@ impl BulkMessageResponse {
) -> Result<BulkMessageResponse> {
if let Some(true) = include_users {
let user_ids = messages.get_user_ids();
let users = db.fetch_users(&user_ids).await?;
let users = User::fetch_foreign_users(db, &user_ids).await?;
Ok(match channel {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {

View File

@@ -4,6 +4,7 @@ use crate::models::user::{
};
use crate::permissions::defn::UserPerms;
use crate::permissions::r#impl::user::get_relationship;
use crate::presence::presence_filter_online;
use crate::{perms, Database, Error, Result};
use futures::try_join;
@@ -67,6 +68,7 @@ impl User {
}
/// Mutate the user object to remove redundant information
#[must_use]
pub fn foreign(mut self) -> User {
self.profile = None;
self.relations = None;
@@ -94,6 +96,21 @@ impl User {
self
}
/// Fetch foreign users by a list of IDs
pub async fn fetch_foreign_users(db: &Database, user_ids: &[String]) -> Result<Vec<User>> {
let online_ids = presence_filter_online(user_ids).await;
Ok(db
.fetch_users(user_ids)
.await?
.into_iter()
.map(|mut user| {
user.online = Some(online_ids.contains(&user.id));
user.foreign()
})
.collect::<Vec<User>>())
}
/// Mutate the user object to include relationship (if it does not already exist)
#[must_use]
pub fn with_relationship(self, perspective: &User) -> User {

View File

@@ -161,7 +161,7 @@ pub struct PartialChannel {
}
/// Optional fields on channel object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
pub enum FieldsChannel {
Description,
Icon,

View File

@@ -81,7 +81,7 @@ pub struct Masquerade {
pub name: Option<String>,
/// Replace the avatar shown on this message (URL to image file)
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(length(min = 1, max = 128))]
#[validate(length(min = 1, max = 256))]
pub avatar: Option<String>,
/// Replace the display role colour shown on this message
///

View File

@@ -134,7 +134,7 @@ pub struct Server {
}
/// Optional fields on server object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
pub enum FieldsServer {
Description,
Categories,
@@ -144,7 +144,7 @@ pub enum FieldsServer {
}
/// Optional fields on server object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
pub enum FieldsRole {
Colour,
}

View File

@@ -42,7 +42,7 @@ pub struct Member {
}
/// Optional fields on server member object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
pub enum FieldsMember {
Nickname,
Avatar,

View File

@@ -56,7 +56,7 @@ pub struct Bot {
}
/// Optional fields on bot object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq)]
pub enum FieldsBot {
Token,
InteractionsURL,

View File

@@ -10,7 +10,7 @@ pub fn if_false(t: &bool) -> bool {
}
/// User's relationship with another user (or themselves)
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq, Eq)]
pub enum RelationshipStatus {
None,
User,
@@ -30,7 +30,7 @@ pub struct Relationship {
}
/// Presence status
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq, Eq)]
pub enum Presence {
/// User is online
Online,
@@ -163,7 +163,7 @@ pub struct User {
}
/// Optional fields on user object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
pub enum FieldsUser {
Avatar,
StatusText,

View File

@@ -97,7 +97,7 @@ impl From<i64> for PermissionValue {
impl From<u64> for PermissionValue {
fn from(v: u64) -> Self {
Self(v as u64)
Self(v)
}
}

View File

@@ -24,7 +24,7 @@ pub enum Permission {
/// Manage server customisation (includes emoji)
ManageCustomisation = 1 << 4,
// % 1 bits reserved
// % 1 bit reserved
// * Member permissions
/// Kick other members below their ranking
@@ -109,10 +109,11 @@ lazy_static! {
+ Permission::Connect
+ Permission::Speak;
pub static ref DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = Permission::GrantAllSafe as u64;
pub static ref DEFAULT_PERMISSION_DIRECT_MESSAGE: u64 =
*DEFAULT_PERMISSION + Permission::ManageChannel;
pub static ref DEFAULT_PERMISSION_DIRECT_MESSAGE: u64 = *DEFAULT_PERMISSION
+ Permission::ManageChannel
+ Permission::React;
pub static ref DEFAULT_PERMISSION_SERVER: u64 =
*DEFAULT_PERMISSION + Permission::ChangeNickname + Permission::ChangeAvatar;
*DEFAULT_PERMISSION + Permission::React + Permission::ChangeNickname + Permission::ChangeAvatar;
}
bitfield! {

View File

@@ -35,7 +35,7 @@ pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u8) {
__set_key_presence_entry(&mut conn, user_id, entry).await;
// Add to region set in case of failure.
__add_to_set_sessions(&mut conn, &*REGION_KEY, user_id, session_id).await;
__add_to_set_sessions(&mut conn, &REGION_KEY, user_id, session_id).await;
(was_empty, session_id)
}
@@ -74,7 +74,7 @@ async fn presence_delete_session_internal(
// Remove from region set.
if !skip_region {
__remove_from_set_sessions(&mut conn, &*REGION_KEY, user_id, session_id).await;
__remove_from_set_sessions(&mut conn, &REGION_KEY, user_id, session_id).await;
}
}
@@ -147,7 +147,7 @@ pub async fn presence_clear_region(region_id: Option<&str>) {
// also send out any relevant events.
for session in sessions {
let parts = session.split(':').collect::<Vec<&str>>();
if let (Some(user_id), Some(session_id)) = (parts.get(0), parts.get(1)) {
if let (Some(user_id), Some(session_id)) = (parts.first(), parts.get(1)) {
if let Ok(session_id) = session_id.parse() {
presence_delete_session_internal(user_id, session_id, true).await;
}

View File

@@ -53,5 +53,5 @@ pub async fn __get_set_sessions(conn: &mut Conn, region_id: &str) -> Vec<String>
/// Delete region session set
pub async fn __delete_set_sessions(conn: &mut Conn, region_id: &str) {
let _: () = conn.del(region_id).await.unwrap();
conn.del::<_, ()>(region_id).await.unwrap();
}

View File

@@ -38,7 +38,7 @@ pub async fn queue(channel: String, id: String, content: String) {
pub async fn worker(db: Database) {
loop {
let task = Q.pop().await;
if let Ok(embeds) = Embed::generate(task.content, &*JANUARY_URL, *MAX_EMBED_COUNT).await {
if let Ok(embeds) = Embed::generate(task.content, &JANUARY_URL, *MAX_EMBED_COUNT).await {
if let Err(err) = Message::append(
&db,
task.id,

View File

@@ -51,7 +51,7 @@ impl Entry {
}
/// Deduct one unit from the bucket and save
pub fn deduct(mut self, key: u64) {
pub fn deduct(mut self) {
let current_time = now().as_millis();
if current_time > self.reset {
self.used = 1;
@@ -59,7 +59,10 @@ impl Entry {
} else {
self.used += 1;
}
}
/// Save information
pub fn save(self, key: u64) {
MAP.insert(key, self);
}
@@ -189,10 +192,12 @@ impl Ratelimiter {
let entry = Entry::from(key);
let remaining = entry.get_remaining(limit);
let reset = entry.left_until_reset();
if remaining > 0 {
entry.deduct(key);
entry.deduct();
let reset = entry.left_until_reset();
entry.save(key);
Ok(Ratelimiter {
key,
limit,
@@ -200,7 +205,7 @@ impl Ratelimiter {
reset,
})
} else {
Err(reset)
Err(entry.left_until_reset())
}
}
}

9
docker-compose.db.yml Normal file
View File

@@ -0,0 +1,9 @@
version: "3.3"
services:
# MongoDB
database:
image: mongo
ports:
- "27017:27017"
volumes:
- ./.data/db:/data/db