Compare commits
16
Commits
20230304-2
...
20230421-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32542a822e | ||
|
|
1df90ff53b | ||
|
|
487b979f0d | ||
|
|
b9d813d8f0 | ||
|
|
36dd128459 | ||
|
|
144f0d39c6 | ||
|
|
32a294a64a | ||
|
|
b7b70346b4 | ||
|
|
59832e6ba2 | ||
|
|
f309218573 | ||
|
|
0321eff62b | ||
|
|
4c8ea31d98 | ||
|
|
92ac86a6bd | ||
|
|
13ed69c82a | ||
|
|
b83f6da648 | ||
|
|
89f1167239 |
@@ -20,47 +20,52 @@ on:
|
|||||||
- "Dockerfile"
|
- "Dockerfile"
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
base:
|
base:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Build base image (amd64)
|
name: Build base image
|
||||||
steps:
|
steps:
|
||||||
# Configure build environment
|
# Configure build environment
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
# Authenticate with GHCR
|
# Authenticate with GHCR
|
||||||
- name: Login to Github Container Registry
|
- name: Login to Github Container Registry
|
||||||
uses: docker/login-action@v1
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
# Build all projects and cache
|
- name: Build base image
|
||||||
- name: Build Base Image
|
uses: docker/build-push-action@v4
|
||||||
uses: docker/build-push-action@v3
|
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
tags: ghcr.io/revoltchat/base:latest
|
platforms: linux/amd64,linux/arm64
|
||||||
cache-from: type=gha
|
tags: ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
cache-to: type=gha,mode=max
|
cache-from: type=gha,scope=buildx-base-multi-arch
|
||||||
|
cache-to: type=gha,scope=buildx-base-multi-arch,mode=max
|
||||||
|
|
||||||
publish_amd64:
|
publish:
|
||||||
needs: [base]
|
needs: [base]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request'
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
project: [delta, bonfire]
|
project: [delta, bonfire]
|
||||||
name: Build ${{ matrix.project }} image (amd64)
|
name: Build ${{ matrix.project }} image
|
||||||
steps:
|
steps:
|
||||||
# Configure build environment
|
# Configure build environment
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
@@ -68,10 +73,11 @@ jobs:
|
|||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
|
registry: docker.io
|
||||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
- name: Login to Github Container Registry
|
- name: Login to Github Container Registry
|
||||||
uses: docker/login-action@v1
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
@@ -86,11 +92,11 @@ jobs:
|
|||||||
{
|
{
|
||||||
"delta": {
|
"delta": {
|
||||||
"path": "crates/delta",
|
"path": "crates/delta",
|
||||||
"tag": "revoltchat/server"
|
"tag": "${{ github.repository_owner }}/server"
|
||||||
},
|
},
|
||||||
"bonfire": {
|
"bonfire": {
|
||||||
"path": "crates/bonfire",
|
"path": "crates/bonfire",
|
||||||
"tag": "revoltchat/bonfire"
|
"tag": "${{ github.repository_owner }}/bonfire"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export_to: output
|
export_to: output
|
||||||
@@ -98,19 +104,21 @@ jobs:
|
|||||||
# Configure metadata
|
# Configure metadata
|
||||||
- name: Docker meta
|
- name: Docker meta
|
||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@v3
|
uses: docker/metadata-action@v4
|
||||||
with:
|
with:
|
||||||
images: ${{ steps.export.outputs.tag }}, ghcr.io/${{ steps.export.outputs.tag }}
|
images: |
|
||||||
|
docker.io/${{ steps.export.outputs.tag }}
|
||||||
|
ghcr.io/${{ steps.export.outputs.tag }}
|
||||||
|
|
||||||
# Build crate image
|
# Build crate image
|
||||||
- name: Publish
|
- name: Publish
|
||||||
uses: docker/build-push-action@v3
|
uses: docker/build-push-action@v4
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64,linux/arm64
|
||||||
file: ${{ steps.export.outputs.path }}/Dockerfile
|
file: ${{ steps.export.outputs.path }}/Dockerfile
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
|
|||||||
Generated
+6
-7
@@ -2106,9 +2106,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.13.0"
|
version = "1.17.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
|
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "opaque-debug"
|
name = "opaque-debug"
|
||||||
@@ -2792,7 +2792,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt-bonfire"
|
name = "revolt-bonfire"
|
||||||
version = "0.5.15"
|
version = "0.5.18"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-std",
|
"async-std",
|
||||||
"async-tungstenite",
|
"async-tungstenite",
|
||||||
@@ -2808,7 +2808,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt-delta"
|
name = "revolt-delta"
|
||||||
version = "0.5.15"
|
version = "0.5.18"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-channel",
|
"async-channel",
|
||||||
"async-std",
|
"async-std",
|
||||||
@@ -2819,7 +2819,6 @@ dependencies = [
|
|||||||
"env_logger",
|
"env_logger",
|
||||||
"futures",
|
"futures",
|
||||||
"impl_ops",
|
"impl_ops",
|
||||||
"lazy_static",
|
|
||||||
"lettre",
|
"lettre",
|
||||||
"linkify 0.6.0",
|
"linkify 0.6.0",
|
||||||
"log",
|
"log",
|
||||||
@@ -2848,7 +2847,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt-quark"
|
name = "revolt-quark"
|
||||||
version = "0.5.15"
|
version = "0.5.18"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-lock",
|
"async-lock",
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
@@ -2866,7 +2865,6 @@ dependencies = [
|
|||||||
"impl_ops",
|
"impl_ops",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"iso8601-timestamp",
|
"iso8601-timestamp",
|
||||||
"lazy_static",
|
|
||||||
"linkify 0.8.1",
|
"linkify 0.8.1",
|
||||||
"log",
|
"log",
|
||||||
"lru",
|
"lru",
|
||||||
@@ -2876,6 +2874,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"optional_struct",
|
"optional_struct",
|
||||||
"pretty_env_logger",
|
"pretty_env_logger",
|
||||||
|
"rand 0.8.5",
|
||||||
"redis-kiss",
|
"redis-kiss",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
|||||||
+20
-5
@@ -1,12 +1,27 @@
|
|||||||
# Build Stage
|
# Build Stage
|
||||||
FROM rustlang/rust:nightly-slim AS builder
|
FROM --platform="${BUILDPLATFORM}" rustlang/rust:nightly-slim
|
||||||
USER 0:0
|
USER 0:0
|
||||||
WORKDIR /home/rust/src
|
WORKDIR /home/rust/src
|
||||||
|
|
||||||
# Install build requirements
|
ARG TARGETARCH
|
||||||
RUN apt-get update && apt-get install -y libssl-dev pkg-config
|
|
||||||
|
|
||||||
# Build all crates
|
# Install build requirements
|
||||||
|
RUN dpkg --add-architecture "${TARGETARCH}"
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y \
|
||||||
|
make \
|
||||||
|
pkg-config \
|
||||||
|
libssl-dev:"${TARGETARCH}"
|
||||||
|
COPY scripts/build-image-layer.sh /tmp/
|
||||||
|
RUN sh /tmp/build-image-layer.sh tools
|
||||||
|
|
||||||
|
# Build all dependencies
|
||||||
COPY Cargo.toml Cargo.lock ./
|
COPY Cargo.toml Cargo.lock ./
|
||||||
|
COPY crates/bonfire/Cargo.toml ./crates/bonfire/
|
||||||
|
COPY crates/delta/Cargo.toml ./crates/delta/
|
||||||
|
COPY crates/quark/Cargo.toml ./crates/quark/
|
||||||
|
RUN sh /tmp/build-image-layer.sh deps
|
||||||
|
|
||||||
|
# Build all apps
|
||||||
COPY crates ./crates
|
COPY crates ./crates
|
||||||
RUN cargo build --locked --release
|
RUN sh /tmp/build-image-layer.sh apps
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt-bonfire"
|
name = "revolt-bonfire"
|
||||||
version = "0.5.15"
|
version = "0.5.18"
|
||||||
license = "AGPL-3.0-or-later"
|
license = "AGPL-3.0-or-later"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
# Build Stage
|
# Build Stage
|
||||||
FROM ghcr.io/revoltchat/base:latest AS builder
|
FROM ghcr.io/revoltchat/base:latest AS builder
|
||||||
RUN cargo install --locked --path crates/bonfire
|
|
||||||
|
|
||||||
# Bundle Stage
|
# Bundle Stage
|
||||||
FROM debian:buster-slim
|
FROM debian:buster-slim
|
||||||
RUN apt-get update && apt-get install -y ca-certificates
|
RUN apt-get update && \
|
||||||
COPY --from=builder /usr/local/cargo/bin/revolt-bonfire ./
|
apt-get install -y ca-certificates && \
|
||||||
|
apt-get clean
|
||||||
|
COPY --from=builder /home/rust/src/target/release/revolt-bonfire ./
|
||||||
EXPOSE 9000
|
EXPOSE 9000
|
||||||
CMD ["./revolt-bonfire"]
|
CMD ["./revolt-bonfire"]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt-delta"
|
name = "revolt-delta"
|
||||||
version = "0.5.15"
|
version = "0.5.18"
|
||||||
license = "AGPL-3.0-or-later"
|
license = "AGPL-3.0-or-later"
|
||||||
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
@@ -15,9 +15,8 @@ log = "0.4.11"
|
|||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
dashmap = "5.2.0"
|
dashmap = "5.2.0"
|
||||||
linkify = "0.6.0"
|
linkify = "0.6.0"
|
||||||
once_cell = "1.4.1"
|
once_cell = "1.17.1"
|
||||||
env_logger = "0.7.1"
|
env_logger = "0.7.1"
|
||||||
lazy_static = "1.4.0"
|
|
||||||
|
|
||||||
# Lang. Utilities
|
# Lang. Utilities
|
||||||
regex = "1"
|
regex = "1"
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
# Build Stage
|
# Build Stage
|
||||||
FROM ghcr.io/revoltchat/base:latest AS builder
|
FROM ghcr.io/revoltchat/base:latest AS builder
|
||||||
RUN cargo install --locked --path crates/delta
|
|
||||||
|
|
||||||
# Bundle Stage
|
# Bundle Stage
|
||||||
FROM debian:buster-slim
|
FROM debian:buster-slim
|
||||||
RUN apt-get update && apt-get install -y ca-certificates
|
RUN apt-get update && \
|
||||||
COPY --from=builder /usr/local/cargo/bin/revolt-delta ./
|
apt-get install -y ca-certificates && \
|
||||||
|
apt-get clean
|
||||||
|
COPY --from=builder /home/rust/src/target/release/revolt-delta ./
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
ENV ROCKET_ADDRESS 0.0.0.0
|
ENV ROCKET_ADDRESS 0.0.0.0
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ fn main() {
|
|||||||
.output()
|
.output()
|
||||||
{
|
{
|
||||||
if let Ok(git_origin) = String::from_utf8(output.stdout) {
|
if let Ok(git_origin) = String::from_utf8(output.stdout) {
|
||||||
println!("cargo:rustc-env=GIT_ORIGIN_URL={}", git_origin);
|
println!("cargo:rustc-env=GIT_ORIGIN_URL={git_origin}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ extern crate rocket;
|
|||||||
extern crate revolt_rocket_okapi;
|
extern crate revolt_rocket_okapi;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
#[macro_use]
|
|
||||||
extern crate lazy_static;
|
|
||||||
|
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
use revolt_quark::{
|
||||||
|
models::{
|
||||||
|
message::{BulkMessageResponse, MessageQuery},
|
||||||
|
User,
|
||||||
|
},
|
||||||
|
Db, Error, Result,
|
||||||
|
};
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
/// # Globally Fetch Messages
|
||||||
|
///
|
||||||
|
/// This is a privileged route to globally fetch messages.
|
||||||
|
#[openapi(tag = "Admin")]
|
||||||
|
#[post("/messages", data = "<data>")]
|
||||||
|
pub async fn message_query(
|
||||||
|
db: &Db,
|
||||||
|
user: User,
|
||||||
|
data: Json<MessageQuery>,
|
||||||
|
) -> Result<Json<BulkMessageResponse>> {
|
||||||
|
// Must be privileged for this route
|
||||||
|
if !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch data using query
|
||||||
|
let data = data.into_inner();
|
||||||
|
let messages = db.fetch_messages(data).await?;
|
||||||
|
BulkMessageResponse::transform(db, None, messages, Some(true))
|
||||||
|
.await
|
||||||
|
.map(Json)
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
use revolt_rocket_okapi::revolt_okapi::openapi3::OpenApi;
|
||||||
|
use rocket::Route;
|
||||||
|
|
||||||
|
mod message_query;
|
||||||
|
mod stats;
|
||||||
|
|
||||||
|
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||||
|
openapi_get_routes_spec![stats::stats, message_query::message_query]
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ use rocket::serde::json::Json;
|
|||||||
/// # Query Stats
|
/// # Query Stats
|
||||||
///
|
///
|
||||||
/// Fetch various technical statistics.
|
/// Fetch various technical statistics.
|
||||||
#[openapi(tag = "Core")]
|
#[openapi(tag = "Admin")]
|
||||||
#[get("/stats")]
|
#[get("/stats")]
|
||||||
pub async fn stats(db: &Db) -> Result<Json<Stats>> {
|
pub async fn stats(db: &Db) -> Result<Json<Stats>> {
|
||||||
Ok(Json(db.generate_stats().await?))
|
Ok(Json(db.generate_stats().await?))
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use revolt_quark::{
|
use revolt_quark::{
|
||||||
models::{Channel, User},
|
get_relationship,
|
||||||
|
models::{user::RelationshipStatus, Channel, User},
|
||||||
perms, Db, EmptyResponse, Error, Permission, Ref, Result,
|
perms, Db, EmptyResponse, Error, Permission, Ref, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -22,6 +23,13 @@ pub async fn req(db: &Db, user: User, target: Ref, member: Ref) -> Result<EmptyR
|
|||||||
match &channel {
|
match &channel {
|
||||||
Channel::Group { .. } => {
|
Channel::Group { .. } => {
|
||||||
let member = member.as_user(db).await?;
|
let member = member.as_user(db).await?;
|
||||||
|
if !matches!(
|
||||||
|
get_relationship(&user, &member.id),
|
||||||
|
RelationshipStatus::Friend
|
||||||
|
) {
|
||||||
|
return Err(Error::NotFriends);
|
||||||
|
}
|
||||||
|
|
||||||
channel
|
channel
|
||||||
.add_user_to_group(db, &member.id, &user.id)
|
.add_user_to_group(db, &member.id, &user.id)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use revolt_quark::{
|
use revolt_quark::{
|
||||||
models::{
|
models::{
|
||||||
message::{BulkMessageResponse, MessageSort},
|
message::{
|
||||||
|
BulkMessageResponse, MessageFilter, MessageQuery, MessageSort, MessageTimePeriod,
|
||||||
|
},
|
||||||
User,
|
User,
|
||||||
},
|
},
|
||||||
perms, Db, Error, Permission, Ref, Result,
|
perms, Db, Error, Permission, Ref, Result,
|
||||||
@@ -68,14 +70,28 @@ pub async fn req(
|
|||||||
sort,
|
sort,
|
||||||
nearby,
|
nearby,
|
||||||
include_users,
|
include_users,
|
||||||
..
|
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
let messages = db
|
let messages = db
|
||||||
.fetch_messages(channel.id(), limit, before, after, sort, nearby)
|
.fetch_messages(MessageQuery {
|
||||||
|
filter: MessageFilter {
|
||||||
|
channel: Some(channel.id().to_string()),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
time_period: if let Some(nearby) = nearby {
|
||||||
|
MessageTimePeriod::Relative { nearby }
|
||||||
|
} else {
|
||||||
|
MessageTimePeriod::Absolute {
|
||||||
|
before,
|
||||||
|
after,
|
||||||
|
sort,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
limit,
|
||||||
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
BulkMessageResponse::transform(db, &channel, messages, include_users)
|
BulkMessageResponse::transform(db, Some(&channel), messages, include_users)
|
||||||
.await
|
.await
|
||||||
.map(Json)
|
.map(Json)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use revolt_quark::{
|
use revolt_quark::{
|
||||||
models::{
|
models::{
|
||||||
message::{BulkMessageResponse, MessageSort},
|
message::{
|
||||||
|
BulkMessageResponse, MessageFilter, MessageQuery, MessageSort, MessageTimePeriod,
|
||||||
|
},
|
||||||
User,
|
User,
|
||||||
},
|
},
|
||||||
perms, Db, Error, Permission, Ref, Result,
|
perms, Db, Error, Permission, Ref, Result,
|
||||||
@@ -30,7 +32,7 @@ pub struct OptionsMessageSearch {
|
|||||||
after: Option<String>,
|
after: Option<String>,
|
||||||
/// Message sort direction
|
/// Message sort direction
|
||||||
///
|
///
|
||||||
/// By default, it will be sorted by relevance.
|
/// By default, it will be sorted by latest.
|
||||||
#[serde(default = "MessageSort::default")]
|
#[serde(default = "MessageSort::default")]
|
||||||
sort: MessageSort,
|
sort: MessageSort,
|
||||||
/// Whether to include user (and member, if server channel) objects
|
/// Whether to include user (and member, if server channel) objects
|
||||||
@@ -73,10 +75,22 @@ pub async fn req(
|
|||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
let messages = db
|
let messages = db
|
||||||
.search_messages(channel.id(), &query, limit, before, after, sort)
|
.fetch_messages(MessageQuery {
|
||||||
|
filter: MessageFilter {
|
||||||
|
channel: Some(channel.id().to_string()),
|
||||||
|
query: Some(query),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
time_period: MessageTimePeriod::Absolute {
|
||||||
|
before,
|
||||||
|
after,
|
||||||
|
sort: Some(sort),
|
||||||
|
},
|
||||||
|
limit,
|
||||||
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
BulkMessageResponse::transform(db, &channel, messages, include_users)
|
BulkMessageResponse::transform(db, Some(&channel), messages, include_users)
|
||||||
.await
|
.await
|
||||||
.map(Json)
|
.map(Json)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use revolt_quark::{
|
|||||||
},
|
},
|
||||||
perms,
|
perms,
|
||||||
web::idempotency::IdempotencyKey,
|
web::idempotency::IdempotencyKey,
|
||||||
Db, Error, Permission, Ref, Result,
|
Db, Error, Permission, Ref, Result, variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT},
|
||||||
};
|
};
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@@ -15,6 +15,7 @@ use rocket::serde::json::Json;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
||||||
pub struct DataMessageSend {
|
pub struct DataMessageSend {
|
||||||
@@ -44,10 +45,7 @@ pub struct DataMessageSend {
|
|||||||
interactions: Option<Interactions>,
|
interactions: Option<Interactions>,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static RE_MENTION: Lazy<Regex> = Lazy::new(|| Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap());
|
||||||
// ignoring I L O and U is intentional
|
|
||||||
static ref RE_MENTION: Regex = Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Send Message
|
/// # Send Message
|
||||||
///
|
///
|
||||||
@@ -141,8 +139,8 @@ pub async fn message_send(
|
|||||||
// 4. Verify replies are valid.
|
// 4. Verify replies are valid.
|
||||||
let mut replies = HashSet::new();
|
let mut replies = HashSet::new();
|
||||||
if let Some(entries) = data.replies {
|
if let Some(entries) = data.replies {
|
||||||
if entries.len() > 5 {
|
if entries.len() > *MAX_REPLY_COUNT {
|
||||||
return Err(Error::TooManyReplies);
|
return Err(Error::TooManyReplies { max: *MAX_REPLY_COUNT });
|
||||||
}
|
}
|
||||||
|
|
||||||
for Reply { id, mention } in entries {
|
for Reply { id, mention } in entries {
|
||||||
@@ -193,8 +191,8 @@ pub async fn message_send(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ! FIXME: move this to app config
|
// ! FIXME: move this to app config
|
||||||
if ids.len() > 5 {
|
if ids.len() > *MAX_ATTACHMENT_COUNT {
|
||||||
return Err(Error::TooManyAttachments);
|
return Err(Error::TooManyAttachments { max: *MAX_ATTACHMENT_COUNT} );
|
||||||
}
|
}
|
||||||
|
|
||||||
for attachment_id in ids {
|
for attachment_id in ids {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use revolt_quark::models::emoji::EmojiParent;
|
use revolt_quark::models::emoji::EmojiParent;
|
||||||
use revolt_quark::models::{Emoji, File, User};
|
use revolt_quark::models::{Emoji, File, User};
|
||||||
|
use revolt_quark::variables::delta::MAX_EMOJI_COUNT;
|
||||||
use revolt_quark::{perms, Db, Error, Permission, Result};
|
use revolt_quark::{perms, Db, Error, Permission, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
@@ -55,8 +56,8 @@ pub async fn create_emoji(
|
|||||||
// Check that there are no more than 100 emoji
|
// Check that there are no more than 100 emoji
|
||||||
// ! FIXME: hardcoded upper limit
|
// ! FIXME: hardcoded upper limit
|
||||||
let emojis = db.fetch_emoji_by_parent_id(&server.id).await?;
|
let emojis = db.fetch_emoji_by_parent_id(&server.id).await?;
|
||||||
if emojis.len() > 99 {
|
if emojis.len() > *MAX_EMOJI_COUNT {
|
||||||
return Err(Error::TooManyEmoji);
|
return Err(Error::TooManyEmoji { max: *MAX_EMOJI_COUNT });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EmojiParent::Detached => return Err(Error::InvalidOperation),
|
EmojiParent::Detached => return Err(Error::InvalidOperation),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ pub use rocket::http::Status;
|
|||||||
pub use rocket::response::Redirect;
|
pub use rocket::response::Redirect;
|
||||||
use rocket::{Build, Rocket};
|
use rocket::{Build, Rocket};
|
||||||
|
|
||||||
|
mod admin;
|
||||||
mod bots;
|
mod bots;
|
||||||
mod channels;
|
mod channels;
|
||||||
mod customisation;
|
mod customisation;
|
||||||
@@ -12,7 +13,6 @@ mod push;
|
|||||||
mod root;
|
mod root;
|
||||||
mod safety;
|
mod safety;
|
||||||
mod servers;
|
mod servers;
|
||||||
mod stats;
|
|
||||||
mod sync;
|
mod sync;
|
||||||
mod users;
|
mod users;
|
||||||
|
|
||||||
@@ -22,7 +22,8 @@ pub fn mount(mut rocket: Rocket<Build>) -> Rocket<Build> {
|
|||||||
mount_endpoints_and_merged_docs! {
|
mount_endpoints_and_merged_docs! {
|
||||||
rocket, "/".to_owned(), settings,
|
rocket, "/".to_owned(), settings,
|
||||||
"/" => (vec![], custom_openapi_spec()),
|
"/" => (vec![], custom_openapi_spec()),
|
||||||
"" => openapi_get_routes_spec![root::root, stats::stats, root::ping],
|
"" => openapi_get_routes_spec![root::root, root::ping],
|
||||||
|
"/admin" => admin::routes(),
|
||||||
"/users" => users::routes(),
|
"/users" => users::routes(),
|
||||||
"/bots" => bots::routes(),
|
"/bots" => bots::routes(),
|
||||||
"/channels" => channels::routes(),
|
"/channels" => channels::routes(),
|
||||||
@@ -109,8 +110,9 @@ fn custom_openapi_spec() -> OpenApi {
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Platform Moderation",
|
"name": "Platform Administration",
|
||||||
"tags": [
|
"tags": [
|
||||||
|
"Admin",
|
||||||
"User Safety"
|
"User Safety"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use revolt_quark::events::client::EventV1;
|
use revolt_quark::events::client::EventV1;
|
||||||
|
use revolt_quark::models::message::{MessageFilter, MessageQuery, MessageSort, MessageTimePeriod};
|
||||||
use revolt_quark::models::report::{ReportStatus, ReportedContent};
|
use revolt_quark::models::report::{ReportStatus, ReportedContent};
|
||||||
use revolt_quark::models::snapshot::{Snapshot, SnapshotContent};
|
use revolt_quark::models::snapshot::{Snapshot, SnapshotContent};
|
||||||
use revolt_quark::models::{Report, User};
|
use revolt_quark::models::{Report, User};
|
||||||
@@ -55,26 +56,34 @@ pub async fn report_content(db: &Db, user: User, data: Json<DataReportContent>)
|
|||||||
|
|
||||||
// Collect prior context
|
// Collect prior context
|
||||||
let prior_context = db
|
let prior_context = db
|
||||||
.fetch_messages(
|
.fetch_messages(MessageQuery {
|
||||||
&message.channel,
|
filter: MessageFilter {
|
||||||
Some(15),
|
channel: Some(message.channel.to_string()),
|
||||||
Some(message.id.to_string()),
|
..Default::default()
|
||||||
None,
|
},
|
||||||
None,
|
limit: Some(15),
|
||||||
None,
|
time_period: MessageTimePeriod::Absolute {
|
||||||
)
|
before: Some(message.id.to_string()),
|
||||||
|
after: None,
|
||||||
|
sort: Some(MessageSort::Latest),
|
||||||
|
},
|
||||||
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Collect leading context
|
// Collect leading context
|
||||||
let leading_context = db
|
let leading_context = db
|
||||||
.fetch_messages(
|
.fetch_messages(MessageQuery {
|
||||||
&message.channel,
|
filter: MessageFilter {
|
||||||
Some(15),
|
channel: Some(message.channel.to_string()),
|
||||||
None,
|
..Default::default()
|
||||||
Some(message.id.to_string()),
|
},
|
||||||
None,
|
limit: Some(15),
|
||||||
None,
|
time_period: MessageTimePeriod::Absolute {
|
||||||
)
|
before: None,
|
||||||
|
after: Some(message.id.to_string()),
|
||||||
|
sort: Some(MessageSort::Oldest),
|
||||||
|
},
|
||||||
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use revolt_quark::{
|
use revolt_quark::{
|
||||||
models::{server::PartialServer, Channel, User},
|
models::{server::PartialServer, Channel, User},
|
||||||
perms, Db, Error, Permission, Ref, Result,
|
perms, Db, Error, Permission, Ref, Result, variables::delta::MAX_CHANNEL_COUNT,
|
||||||
};
|
};
|
||||||
|
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
@@ -11,20 +11,15 @@ use ulid::Ulid;
|
|||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
/// # Channel Type
|
/// # Channel Type
|
||||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
#[derive(Serialize, Deserialize, JsonSchema, Default)]
|
||||||
enum ChannelType {
|
enum ChannelType {
|
||||||
/// Text Channel
|
/// Text Channel
|
||||||
|
#[default]
|
||||||
Text,
|
Text,
|
||||||
/// Voice Channel
|
/// Voice Channel
|
||||||
Voice,
|
Voice,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ChannelType {
|
|
||||||
fn default() -> Self {
|
|
||||||
ChannelType::Text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Channel Data
|
/// # Channel Data
|
||||||
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
||||||
pub struct DataCreateChannel {
|
pub struct DataCreateChannel {
|
||||||
@@ -63,6 +58,10 @@ pub async fn req(
|
|||||||
.throw_permission(db, Permission::ManageChannel)
|
.throw_permission(db, Permission::ManageChannel)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
if server.channels.len() > *MAX_CHANNEL_COUNT {
|
||||||
|
return Err(Error::TooManyChannels { max: *MAX_CHANNEL_COUNT })
|
||||||
|
};
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
let mut channels = server.channels.clone();
|
let mut channels = server.channels.clone();
|
||||||
channels.push(id.clone());
|
channels.push(id.clone());
|
||||||
|
|||||||
@@ -11,10 +11,6 @@ use rocket::serde::json::Json;
|
|||||||
#[openapi(tag = "Server Members")]
|
#[openapi(tag = "Server Members")]
|
||||||
#[get("/<target>/invites")]
|
#[get("/<target>/invites")]
|
||||||
pub async fn req(db: &Db, user: User, target: Ref) -> Result<Json<Vec<Invite>>> {
|
pub async fn req(db: &Db, user: User, target: Ref) -> Result<Json<Vec<Invite>>> {
|
||||||
if user.bot.is_some() {
|
|
||||||
return Err(Error::IsBot);
|
|
||||||
}
|
|
||||||
|
|
||||||
let server = target.as_server(db).await?;
|
let server = target.as_server(db).await?;
|
||||||
perms(&user)
|
perms(&user)
|
||||||
.server(&server)
|
.server(&server)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use revolt_quark::{
|
use revolt_quark::{
|
||||||
models::{server::Role, User},
|
models::{server::Role, User},
|
||||||
perms, Db, Error, Permission, Ref, Result,
|
perms, Db, Error, Permission, Ref, Result, variables::delta::MAX_ROLE_COUNT,
|
||||||
};
|
};
|
||||||
|
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
@@ -50,6 +50,10 @@ pub async fn req(
|
|||||||
.throw_permission(db, Permission::ManageRole)
|
.throw_permission(db, Permission::ManageRole)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
if server.roles.len() > *MAX_ROLE_COUNT {
|
||||||
|
return Err(Error::TooManyRoles { max: *MAX_ROLE_COUNT })
|
||||||
|
};
|
||||||
|
|
||||||
let member_rank = permissions.get_member_rank();
|
let member_rank = permissions.get_member_rank();
|
||||||
let rank = if let Some(given_rank) = data.rank {
|
let rank = if let Some(given_rank) = data.rank {
|
||||||
if given_rank <= member_rank.unwrap_or(i64::MIN) {
|
if given_rank <= member_rank.unwrap_or(i64::MIN) {
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ pub async fn req(
|
|||||||
user: User,
|
user: User,
|
||||||
info: Json<DataCreateServer>,
|
info: Json<DataCreateServer>,
|
||||||
) -> Result<Json<CreateServerResponse>> {
|
) -> Result<Json<CreateServerResponse>> {
|
||||||
|
if user.bot.is_some() {
|
||||||
|
return Err(Error::IsBot);
|
||||||
|
}
|
||||||
|
|
||||||
let info = info.into_inner();
|
let info = info.into_inner();
|
||||||
info.validate()
|
info.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ pub struct DataEditServer {
|
|||||||
/// System message configuration
|
/// System message configuration
|
||||||
system_messages: Option<SystemMessageChannels>,
|
system_messages: Option<SystemMessageChannels>,
|
||||||
|
|
||||||
|
/// Bitfield of server flags
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub flags: Option<i32>,
|
||||||
|
|
||||||
// Whether this server is age-restricted
|
// Whether this server is age-restricted
|
||||||
// nsfw: Option<bool>,
|
// nsfw: Option<bool>,
|
||||||
/// Whether this server is public and should show up on [Revolt Discover](https://rvlt.gg)
|
/// Whether this server is public and should show up on [Revolt Discover](https://rvlt.gg)
|
||||||
@@ -92,6 +96,11 @@ pub async fn req(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check we are privileged if changing sensitive fields
|
||||||
|
if data.flags.is_some() && !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
if data.categories.is_some() {
|
if data.categories.is_some() {
|
||||||
permissions
|
permissions
|
||||||
.throw_permission(db, Permission::ManageChannel)
|
.throw_permission(db, Permission::ManageChannel)
|
||||||
@@ -105,6 +114,7 @@ pub async fn req(
|
|||||||
banner,
|
banner,
|
||||||
categories,
|
categories,
|
||||||
system_messages,
|
system_messages,
|
||||||
|
flags,
|
||||||
// nsfw,
|
// nsfw,
|
||||||
discoverable,
|
discoverable,
|
||||||
analytics,
|
analytics,
|
||||||
@@ -116,6 +126,7 @@ pub async fn req(
|
|||||||
description,
|
description,
|
||||||
categories,
|
categories,
|
||||||
system_messages,
|
system_messages,
|
||||||
|
flags,
|
||||||
// nsfw,
|
// nsfw,
|
||||||
discoverable,
|
discoverable,
|
||||||
analytics,
|
analytics,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use revolt_quark::models::user::{FieldsUser, PartialUser, User};
|
use revolt_quark::models::user::{FieldsUser, PartialUser, User};
|
||||||
use revolt_quark::models::File;
|
use revolt_quark::models::File;
|
||||||
use revolt_quark::{Database, Error, Result};
|
use revolt_quark::{Database, Error, Ref, Result};
|
||||||
|
|
||||||
use revolt_quark::models::user::UserStatus;
|
use revolt_quark::models::user::UserStatus;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
@@ -24,6 +24,10 @@ pub struct UserProfileData {
|
|||||||
/// # User Data
|
/// # User Data
|
||||||
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
|
||||||
pub struct DataEditUser {
|
pub struct DataEditUser {
|
||||||
|
/// Attachment Id for avatar
|
||||||
|
#[validate(length(min = 1, max = 128))]
|
||||||
|
avatar: Option<String>,
|
||||||
|
|
||||||
/// New user status
|
/// New user status
|
||||||
#[validate]
|
#[validate]
|
||||||
status: Option<UserStatus>,
|
status: Option<UserStatus>,
|
||||||
@@ -32,9 +36,14 @@ pub struct DataEditUser {
|
|||||||
/// This is applied as a partial.
|
/// This is applied as a partial.
|
||||||
#[validate]
|
#[validate]
|
||||||
profile: Option<UserProfileData>,
|
profile: Option<UserProfileData>,
|
||||||
/// Attachment Id for avatar
|
|
||||||
#[validate(length(min = 1, max = 128))]
|
/// Bitfield of user badges
|
||||||
avatar: Option<String>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
badges: Option<i32>,
|
||||||
|
/// Enum of user flags
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
flags: Option<i32>,
|
||||||
|
|
||||||
/// Fields to remove from user object
|
/// Fields to remove from user object
|
||||||
#[validate(length(min = 1))]
|
#[validate(length(min = 1))]
|
||||||
remove: Option<Vec<FieldsUser>>,
|
remove: Option<Vec<FieldsUser>>,
|
||||||
@@ -44,19 +53,36 @@ pub struct DataEditUser {
|
|||||||
///
|
///
|
||||||
/// Edit currently authenticated user.
|
/// Edit currently authenticated user.
|
||||||
#[openapi(tag = "User Information")]
|
#[openapi(tag = "User Information")]
|
||||||
#[patch("/@me", data = "<data>")]
|
#[patch("/<target>", data = "<data>")]
|
||||||
pub async fn req(
|
pub async fn req(
|
||||||
db: &State<Database>,
|
db: &State<Database>,
|
||||||
mut user: User,
|
mut user: User,
|
||||||
|
target: Ref,
|
||||||
data: Json<DataEditUser>,
|
data: Json<DataEditUser>,
|
||||||
) -> Result<Json<User>> {
|
) -> Result<Json<User>> {
|
||||||
let data = data.into_inner();
|
let data = data.into_inner();
|
||||||
data.validate()
|
data.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
// If we want to edit a different user than self, ensure we have
|
||||||
|
// permissions and subsequently replace the user in question
|
||||||
|
if target.id != "@me" {
|
||||||
|
if !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
user = target.as_user(db).await?;
|
||||||
|
// Otherwise, filter out invalid edit fields
|
||||||
|
} else if data.badges.is_some() || data.flags.is_some() {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit out early if nothing is changed
|
||||||
if data.status.is_none()
|
if data.status.is_none()
|
||||||
&& data.profile.is_none()
|
&& data.profile.is_none()
|
||||||
&& data.avatar.is_none()
|
&& data.avatar.is_none()
|
||||||
|
&& data.badges.is_none()
|
||||||
|
&& data.flags.is_none()
|
||||||
&& data.remove.is_none()
|
&& data.remove.is_none()
|
||||||
{
|
{
|
||||||
return Ok(Json(user));
|
return Ok(Json(user));
|
||||||
@@ -83,7 +109,11 @@ pub async fn req(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut partial: PartialUser = Default::default();
|
let mut partial: PartialUser = PartialUser {
|
||||||
|
badges: data.badges,
|
||||||
|
flags: data.flags,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
// 2. Apply new avatar
|
// 2. Apply new avatar
|
||||||
if let Some(avatar) = data.avatar {
|
if let Some(avatar) = data.avatar {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt-quark"
|
name = "revolt-quark"
|
||||||
version = "0.5.15"
|
version = "0.5.18"
|
||||||
license = "AGPL-3.0-or-later"
|
license = "AGPL-3.0-or-later"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
@@ -57,6 +57,7 @@ log = "0.4.14"
|
|||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
|
|
||||||
# Util
|
# Util
|
||||||
|
rand = "0.8.5"
|
||||||
ulid = "0.5.0"
|
ulid = "0.5.0"
|
||||||
regex = "1.5.5"
|
regex = "1.5.5"
|
||||||
nanoid = "0.4.0"
|
nanoid = "0.4.0"
|
||||||
@@ -67,8 +68,7 @@ impl_ops = "0.1.1"
|
|||||||
num_enum = "0.5.6"
|
num_enum = "0.5.6"
|
||||||
reqwest = "0.11.10"
|
reqwest = "0.11.10"
|
||||||
bitfield = "0.13.2"
|
bitfield = "0.13.2"
|
||||||
once_cell = "1.13.0"
|
once_cell = "1.17.1"
|
||||||
lazy_static = "1.4.0"
|
|
||||||
async-lock = "2.6.0"
|
async-lock = "2.6.0"
|
||||||
|
|
||||||
lru = { version = "0.7.6", optional = true }
|
lru = { version = "0.7.6", optional = true }
|
||||||
|
|||||||
@@ -557,7 +557,7 @@ impl EventV1 {
|
|||||||
|
|
||||||
/// Publish private event
|
/// Publish private event
|
||||||
pub async fn private(self, id: String) {
|
pub async fn private(self, id: String) {
|
||||||
self.p(format!("{}!", id)).await;
|
self.p(format!("{id}!")).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Publish internal global event
|
/// Publish internal global event
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::models::message::{AppendMessage, Message, MessageSort, PartialMessage};
|
use crate::models::message::{AppendMessage, Message, MessageQuery, PartialMessage};
|
||||||
use crate::{AbstractMessage, Result};
|
use crate::{AbstractMessage, Result};
|
||||||
|
|
||||||
use super::super::DummyDb;
|
use super::super::DummyDb;
|
||||||
@@ -41,28 +41,8 @@ impl AbstractMessage for DummyDb {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_messages(
|
async fn fetch_messages(&self, _query: MessageQuery) -> Result<Vec<Message>> {
|
||||||
&self,
|
Ok(vec![])
|
||||||
channel: &str,
|
|
||||||
_limit: Option<i64>,
|
|
||||||
_before: Option<String>,
|
|
||||||
_after: Option<String>,
|
|
||||||
_sort: Option<MessageSort>,
|
|
||||||
_nearby: Option<String>,
|
|
||||||
) -> Result<Vec<Message>> {
|
|
||||||
Ok(vec![self.fetch_message(channel).await.unwrap()])
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn search_messages(
|
|
||||||
&self,
|
|
||||||
channel: &str,
|
|
||||||
_query: &str,
|
|
||||||
_limit: Option<i64>,
|
|
||||||
_before: Option<String>,
|
|
||||||
_after: Option<String>,
|
|
||||||
_sort: MessageSort,
|
|
||||||
) -> Result<Vec<Message>> {
|
|
||||||
Ok(vec![self.fetch_message(channel).await.unwrap()])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new reaction to a message
|
/// Add a new reaction to a message
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ use crate::{
|
|||||||
Database, Error, Result,
|
Database, Error, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
lazy_static! {
|
static ALPHABET: [char; 54] = [
|
||||||
static ref ALPHABET: [char; 54] = [
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
||||||
'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
|
||||||
'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
|
];
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Invite {
|
impl Invite {
|
||||||
/// Get the invite code for this invite
|
/// Get the invite code for this invite
|
||||||
@@ -30,7 +28,7 @@ impl Invite {
|
|||||||
|
|
||||||
/// Create a new invite from given information
|
/// Create a new invite from given information
|
||||||
pub async fn create(db: &Database, creator: &User, target: &Channel) -> Result<Invite> {
|
pub async fn create(db: &Database, creator: &User, target: &Channel) -> Result<Invite> {
|
||||||
let code = nanoid!(8, &*ALPHABET);
|
let code = nanoid!(8, &ALPHABET);
|
||||||
let invite = match &target {
|
let invite = match &target {
|
||||||
Channel::Group { id, .. } => Ok(Invite::Group {
|
Channel::Group { id, .. } => Ok(Invite::Group {
|
||||||
code,
|
code,
|
||||||
|
|||||||
@@ -377,7 +377,7 @@ impl SendableEmbed {
|
|||||||
impl BulkMessageResponse {
|
impl BulkMessageResponse {
|
||||||
pub async fn transform(
|
pub async fn transform(
|
||||||
db: &Database,
|
db: &Database,
|
||||||
channel: &Channel,
|
channel: Option<&Channel>,
|
||||||
messages: Vec<Message>,
|
messages: Vec<Message>,
|
||||||
include_users: Option<bool>,
|
include_users: Option<bool>,
|
||||||
) -> Result<BulkMessageResponse> {
|
) -> Result<BulkMessageResponse> {
|
||||||
@@ -386,7 +386,8 @@ impl BulkMessageResponse {
|
|||||||
let users = User::fetch_foreign_users(db, &user_ids).await?;
|
let users = User::fetch_foreign_users(db, &user_ids).await?;
|
||||||
|
|
||||||
Ok(match channel {
|
Ok(match channel {
|
||||||
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
|
Some(Channel::TextChannel { server, .. })
|
||||||
|
| Some(Channel::VoiceChannel { server, .. }) => {
|
||||||
BulkMessageResponse::MessagesAndUsers {
|
BulkMessageResponse::MessagesAndUsers {
|
||||||
messages,
|
messages,
|
||||||
users,
|
users,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::{collections::HashSet, str::FromStr};
|
use std::{collections::HashSet, str::FromStr};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
@@ -8,13 +9,10 @@ use crate::{
|
|||||||
Database, Result,
|
Database, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
lazy_static! {
|
static PERMISSIBLE_EMOJIS: Lazy<HashSet<String>> = Lazy::new(|| include_str!(crate::asset!("emojis.txt"))
|
||||||
/// Permissible emojis
|
.split('\n')
|
||||||
static ref PERMISSIBLE_EMOJIS: HashSet<String> = include_str!(crate::asset!("emojis.txt"))
|
.map(|x| x.into())
|
||||||
.split('\n')
|
.collect());
|
||||||
.map(|x| x.into())
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Emoji {
|
impl Emoji {
|
||||||
/// Get parent id
|
/// Get parent id
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::time::Duration;
|
use std::{time::Duration, ops::BitXor};
|
||||||
|
|
||||||
use bson::{Bson, DateTime};
|
use bson::{Bson, DateTime};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
@@ -503,13 +503,8 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
|
|||||||
|
|
||||||
update.insert(
|
update.insert(
|
||||||
"default_permissions",
|
"default_permissions",
|
||||||
(*DEFAULT_PERMISSION_SERVER
|
// Remove Send Message permission if it wasn't originally granted
|
||||||
// Remove Send Message permission if it wasn't originally granted
|
DEFAULT_PERMISSION_SERVER.bitxor(if has_send { 0 } else { Permission::SendMessage as u64}) as i64,
|
||||||
^ (if has_send {
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
Permission::SendMessage as u64
|
|
||||||
})) as i64,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(Bson::Document(mut roles)) = document.remove("roles") {
|
if let Some(Bson::Document(mut roles)) = document.remove("roles") {
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ use bson::{to_bson, Document};
|
|||||||
use futures::try_join;
|
use futures::try_join;
|
||||||
use mongodb::options::FindOptions;
|
use mongodb::options::FindOptions;
|
||||||
|
|
||||||
use crate::models::message::{AppendMessage, Message, MessageSort, PartialMessage};
|
use crate::models::message::{
|
||||||
|
AppendMessage, Message, MessageQuery, MessageSort, MessageTimePeriod, PartialMessage,
|
||||||
|
};
|
||||||
use crate::r#impl::mongo::DocumentId;
|
use crate::r#impl::mongo::DocumentId;
|
||||||
use crate::{AbstractMessage, Error, Result};
|
use crate::{AbstractMessage, Error, Result};
|
||||||
|
|
||||||
@@ -139,145 +141,138 @@ impl AbstractMessage for MongoDb {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_messages(
|
async fn fetch_messages(&self, query: MessageQuery) -> Result<Vec<Message>> {
|
||||||
&self,
|
let mut filter = doc! {};
|
||||||
channel: &str,
|
|
||||||
limit: Option<i64>,
|
// 1. Apply message filters
|
||||||
before: Option<String>,
|
if let Some(channel) = query.filter.channel {
|
||||||
after: Option<String>,
|
filter.insert("channel", channel);
|
||||||
sort: Option<MessageSort>,
|
}
|
||||||
nearby: Option<String>,
|
|
||||||
) -> Result<Vec<Message>> {
|
if let Some(author) = query.filter.author {
|
||||||
let limit = limit.unwrap_or(50);
|
filter.insert("author", author);
|
||||||
Ok(if let Some(nearby) = nearby {
|
}
|
||||||
let (a, b) = try_join!(
|
|
||||||
self.find_with_options::<_, Message>(
|
let is_search_query = if let Some(query) = query.filter.query {
|
||||||
COL,
|
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! {
|
doc! {
|
||||||
"channel": channel,
|
"$lt": &nearby
|
||||||
"_id": {
|
|
||||||
"$gte": &nearby
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
FindOptions::builder()
|
);
|
||||||
.limit(limit / 2 + 1)
|
|
||||||
.sort(doc! {
|
newer_message_filter.insert(
|
||||||
"_id": 1_i32
|
"_id",
|
||||||
})
|
|
||||||
.build(),
|
|
||||||
),
|
|
||||||
self.find_with_options::<_, Message>(
|
|
||||||
COL,
|
|
||||||
doc! {
|
doc! {
|
||||||
"channel": channel,
|
"$gte": &nearby
|
||||||
"_id": {
|
|
||||||
"$lt": &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(),
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
|
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()
|
FindOptions::builder()
|
||||||
.limit(limit / 2)
|
.limit(limit)
|
||||||
.sort(doc! {
|
.sort(match sort.unwrap_or(MessageSort::Latest) {
|
||||||
"_id": -1_i32
|
// 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(),
|
.build(),
|
||||||
)
|
)
|
||||||
)?;
|
.await
|
||||||
|
|
||||||
[a, b].concat()
|
|
||||||
} else {
|
|
||||||
let mut query = doc! { "channel": channel };
|
|
||||||
if let Some(before) = before {
|
|
||||||
query.insert("_id", doc! { "$lt": before });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(after) = after {
|
|
||||||
query.insert("_id", doc! { "$gt": after });
|
|
||||||
}
|
|
||||||
|
|
||||||
let sort: i32 = if let MessageSort::Latest = sort.unwrap_or(MessageSort::Latest) {
|
|
||||||
-1
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
};
|
|
||||||
|
|
||||||
self.find_with_options::<_, Message>(
|
|
||||||
COL,
|
|
||||||
query,
|
|
||||||
FindOptions::builder()
|
|
||||||
.limit(limit)
|
|
||||||
.sort(doc! {
|
|
||||||
"_id": sort
|
|
||||||
})
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn search_messages(
|
|
||||||
&self,
|
|
||||||
channel: &str,
|
|
||||||
query: &str,
|
|
||||||
limit: Option<i64>,
|
|
||||||
before: Option<String>,
|
|
||||||
after: Option<String>,
|
|
||||||
sort: MessageSort,
|
|
||||||
) -> Result<Vec<Message>> {
|
|
||||||
let limit = limit.unwrap_or(50);
|
|
||||||
|
|
||||||
let mut filter = doc! {
|
|
||||||
"channel": channel,
|
|
||||||
"$text": {
|
|
||||||
"$search": query
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.find_with_options(
|
|
||||||
COL,
|
|
||||||
filter,
|
|
||||||
FindOptions::builder()
|
|
||||||
.projection(if let MessageSort::Relevance = &sort {
|
|
||||||
doc! {
|
|
||||||
"score": {
|
|
||||||
"$meta": "textScore"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
doc! {}
|
|
||||||
})
|
|
||||||
.limit(limit)
|
|
||||||
.sort(match &sort {
|
|
||||||
MessageSort::Relevance => doc! {
|
|
||||||
"score": {
|
|
||||||
"$meta": "textScore"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MessageSort::Latest => doc! {
|
|
||||||
"_id": -1_i32
|
|
||||||
},
|
|
||||||
MessageSort::Oldest => doc! {
|
|
||||||
"_id": 1_i32
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new reaction to a message
|
/// Add a new reaction to a message
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ impl AbstractAttachment for MongoDb {
|
|||||||
parent_type: &str,
|
parent_type: &str,
|
||||||
parent_id: &str,
|
parent_id: &str,
|
||||||
) -> Result<File> {
|
) -> Result<File> {
|
||||||
let key = format!("{}_id", parent_type);
|
let key = format!("{parent_type}_id");
|
||||||
match self
|
match self
|
||||||
.find_one::<File>(
|
.find_one::<File>(
|
||||||
COL,
|
COL,
|
||||||
|
|||||||
@@ -90,24 +90,25 @@ impl MongoDb {
|
|||||||
where
|
where
|
||||||
O: Into<Option<FindOptions>>,
|
O: Into<Option<FindOptions>>,
|
||||||
{
|
{
|
||||||
Ok(self
|
let result = self.col::<T>(collection).find(projection, options).await;
|
||||||
.col::<T>(collection)
|
Ok(if cfg!(debug_assertions) {
|
||||||
.find(projection, options)
|
result.unwrap()
|
||||||
.await
|
} else {
|
||||||
.map_err(|_| Error::DatabaseError {
|
result.map_err(|_| Error::DatabaseError {
|
||||||
operation: "find",
|
operation: "find",
|
||||||
with: collection,
|
with: collection,
|
||||||
})?
|
})?
|
||||||
.filter_map(|s| async {
|
}
|
||||||
if cfg!(debug_assertions) {
|
.filter_map(|s| async {
|
||||||
// Hard fail on invalid documents
|
if cfg!(debug_assertions) {
|
||||||
Some(s.unwrap())
|
// Hard fail on invalid documents
|
||||||
} else {
|
Some(s.unwrap())
|
||||||
s.ok()
|
} else {
|
||||||
}
|
s.ok()
|
||||||
})
|
}
|
||||||
.collect::<Vec<T>>()
|
})
|
||||||
.await)
|
.collect::<Vec<T>>()
|
||||||
|
.await)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find<T: DeserializeOwned + Unpin + Send + Sync>(
|
async fn find<T: DeserializeOwned + Unpin + Send + Sync>(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use bson::Document;
|
use bson::Document;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use mongodb::options::{Collation, CollationStrength, FindOneOptions, FindOptions};
|
use mongodb::options::{Collation, CollationStrength, FindOneOptions, FindOptions};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus, User};
|
use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus, User};
|
||||||
use crate::r#impl::mongo::IntoDocumentPath;
|
use crate::r#impl::mongo::IntoDocumentPath;
|
||||||
@@ -8,16 +9,14 @@ use crate::{AbstractUser, Error, Result};
|
|||||||
|
|
||||||
use super::super::MongoDb;
|
use super::super::MongoDb;
|
||||||
|
|
||||||
lazy_static! {
|
static FIND_USERNAME_OPTIONS: Lazy<FindOneOptions> = Lazy::new(|| FindOneOptions::builder()
|
||||||
static ref FIND_USERNAME_OPTIONS: FindOneOptions = FindOneOptions::builder()
|
.collation(
|
||||||
.collation(
|
Collation::builder()
|
||||||
Collation::builder()
|
.locale("en")
|
||||||
.locale("en")
|
.strength(CollationStrength::Secondary)
|
||||||
.strength(CollationStrength::Secondary)
|
.build()
|
||||||
.build()
|
)
|
||||||
)
|
.build());
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
static COL: &str = "users";
|
static COL: &str = "users";
|
||||||
|
|
||||||
@@ -265,7 +264,7 @@ impl AbstractUser for MongoDb {
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"_id": target_id,
|
"_id": target_id,
|
||||||
"status": format!("{:?}", relationship)
|
"status": format!("{relationship:?}")
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ extern crate impl_ops;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate optional_struct;
|
extern crate optional_struct;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate bitfield;
|
extern crate bitfield;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate bson;
|
extern crate bson;
|
||||||
|
|||||||
@@ -157,10 +157,11 @@ pub struct Message {
|
|||||||
/// # Message Sort
|
/// # Message Sort
|
||||||
///
|
///
|
||||||
/// Sort used for retrieving messages
|
/// Sort used for retrieving messages
|
||||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
#[derive(Serialize, Deserialize, JsonSchema, Debug, Default)]
|
||||||
#[cfg_attr(feature = "rocket_impl", derive(FromFormField))]
|
#[cfg_attr(feature = "rocket_impl", derive(FromFormField))]
|
||||||
pub enum MessageSort {
|
pub enum MessageSort {
|
||||||
/// Sort by the most relevant messages
|
/// Sort by the most relevant messages
|
||||||
|
#[default]
|
||||||
Relevance,
|
Relevance,
|
||||||
/// Sort by the newest messages first
|
/// Sort by the newest messages first
|
||||||
Latest,
|
Latest,
|
||||||
@@ -168,10 +169,54 @@ pub enum MessageSort {
|
|||||||
Oldest,
|
Oldest,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MessageSort {
|
/// # Message Time Period
|
||||||
fn default() -> MessageSort {
|
///
|
||||||
MessageSort::Relevance
|
/// Filter and sort messages by time
|
||||||
}
|
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum MessageTimePeriod {
|
||||||
|
Relative {
|
||||||
|
/// Message id to search around
|
||||||
|
///
|
||||||
|
/// Specifying 'nearby' ignores 'before', 'after' and 'sort'.
|
||||||
|
/// It will also take half of limit rounded as the limits to each side.
|
||||||
|
/// It also fetches the message ID specified.
|
||||||
|
nearby: String,
|
||||||
|
},
|
||||||
|
Absolute {
|
||||||
|
/// Message id before which messages should be fetched
|
||||||
|
before: Option<String>,
|
||||||
|
/// Message id after which messages should be fetched
|
||||||
|
after: Option<String>,
|
||||||
|
/// Message sort direction
|
||||||
|
sort: Option<MessageSort>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # Message Filter
|
||||||
|
#[derive(Serialize, Deserialize, JsonSchema, Default)]
|
||||||
|
pub struct MessageFilter {
|
||||||
|
/// Parent channel ID
|
||||||
|
pub channel: Option<String>,
|
||||||
|
/// Message author ID
|
||||||
|
pub author: Option<String>,
|
||||||
|
/// Search query
|
||||||
|
pub query: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # Message Query
|
||||||
|
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||||
|
pub struct MessageQuery {
|
||||||
|
/// Maximum number of messages to fetch
|
||||||
|
///
|
||||||
|
/// For fetching nearby messages, this is \`(limit + 1)\`.
|
||||||
|
pub limit: Option<i64>,
|
||||||
|
/// Filter to apply
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub filter: MessageFilter,
|
||||||
|
/// Time period to fetch
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub time_period: MessageTimePeriod,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Bulk Message Response
|
/// # Bulk Message Response
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Metadata associated with file
|
/// Metadata associated with file
|
||||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
|
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum Metadata {
|
pub enum Metadata {
|
||||||
/// File is just a generic uncategorised file
|
/// File is just a generic uncategorised file
|
||||||
|
#[default]
|
||||||
File,
|
File,
|
||||||
/// File contains textual data and should be displayed as such
|
/// File contains textual data and should be displayed as such
|
||||||
Text,
|
Text,
|
||||||
@@ -16,12 +17,6 @@ pub enum Metadata {
|
|||||||
Audio,
|
Audio,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Metadata {
|
|
||||||
fn default() -> Metadata {
|
|
||||||
Metadata::File
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Representation of a File on Revolt
|
/// Representation of a File on Revolt
|
||||||
/// Generated by Autumn
|
/// Generated by Autumn
|
||||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
|
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ pub struct Server {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub banner: Option<File>,
|
pub banner: Option<File>,
|
||||||
|
|
||||||
/// Enum of server flags
|
/// Bitfield of server flags
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub flags: Option<i32>,
|
pub flags: Option<i32>,
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use num_enum::TryFromPrimitive;
|
use num_enum::TryFromPrimitive;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::ops;
|
use once_cell::sync::Lazy;
|
||||||
|
use std::ops::{self, Add};
|
||||||
|
|
||||||
/// Permission value on Revolt
|
/// Permission value on Revolt
|
||||||
///
|
///
|
||||||
@@ -97,24 +98,18 @@ pub enum Permission {
|
|||||||
impl_op_ex!(+ |a: &Permission, b: &Permission| -> u64 { *a as u64 | *b as u64 });
|
impl_op_ex!(+ |a: &Permission, b: &Permission| -> u64 { *a as u64 | *b as u64 });
|
||||||
impl_op_ex_commutative!(+ |a: &u64, b: &Permission| -> u64 { *a | *b as u64 });
|
impl_op_ex_commutative!(+ |a: &u64, b: &Permission| -> u64 { *a | *b as u64 });
|
||||||
|
|
||||||
lazy_static! {
|
pub static ALLOW_IN_TIMEOUT: Lazy<u64> = Lazy::new(|| Permission::ViewChannel + Permission::ReadMessageHistory);
|
||||||
pub static ref ALLOW_IN_TIMEOUT: u64 = Permission::ViewChannel + Permission::ReadMessageHistory;
|
pub static DEFAULT_PERMISSION_VIEW_ONLY: Lazy<u64> = Lazy::new(|| Permission::ViewChannel + Permission::ReadMessageHistory);
|
||||||
pub static ref DEFAULT_PERMISSION_VIEW_ONLY: u64 =
|
pub static DEFAULT_PERMISSION: Lazy<u64> = Lazy::new(|| DEFAULT_PERMISSION_VIEW_ONLY.add(
|
||||||
Permission::ViewChannel + Permission::ReadMessageHistory;
|
Permission::SendMessage
|
||||||
pub static ref DEFAULT_PERMISSION: u64 = *DEFAULT_PERMISSION_VIEW_ONLY
|
+ Permission::InviteOthers
|
||||||
+ Permission::SendMessage
|
+ Permission::SendEmbeds
|
||||||
+ Permission::InviteOthers
|
+ Permission::UploadFiles
|
||||||
+ Permission::SendEmbeds
|
+ Permission::Connect
|
||||||
+ Permission::UploadFiles
|
+ Permission::Speak));
|
||||||
+ Permission::Connect
|
pub static DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = Permission::GrantAllSafe as u64;
|
||||||
+ Permission::Speak;
|
pub static DEFAULT_PERMISSION_DIRECT_MESSAGE: Lazy<u64> = Lazy::new(|| DEFAULT_PERMISSION.add(Permission::ManageChannel + Permission::React));
|
||||||
pub static ref DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = Permission::GrantAllSafe as u64;
|
pub static DEFAULT_PERMISSION_SERVER: Lazy<u64> = Lazy::new(|| DEFAULT_PERMISSION.add(Permission::React + Permission::ChangeNickname + Permission::ChangeAvatar));
|
||||||
pub static ref DEFAULT_PERMISSION_DIRECT_MESSAGE: u64 = *DEFAULT_PERMISSION
|
|
||||||
+ Permission::ManageChannel
|
|
||||||
+ Permission::React;
|
|
||||||
pub static ref DEFAULT_PERMISSION_SERVER: u64 =
|
|
||||||
*DEFAULT_PERMISSION + Permission::React + Permission::ChangeNickname + Permission::ChangeAvatar;
|
|
||||||
}
|
|
||||||
|
|
||||||
bitfield! {
|
bitfield! {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ async fn calculate_channel_permission(
|
|||||||
let value: PermissionValue = match channel {
|
let value: PermissionValue = match channel {
|
||||||
Channel::SavedMessages { user, .. } => {
|
Channel::SavedMessages { user, .. } => {
|
||||||
if user == &data.perspective.id {
|
if user == &data.perspective.id {
|
||||||
(*DEFAULT_PERMISSION_SAVED_MESSAGES).into()
|
DEFAULT_PERMISSION_SAVED_MESSAGES.into()
|
||||||
} else {
|
} else {
|
||||||
0_u64.into()
|
0_u64.into()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,64 +1,12 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
lazy_static! {
|
pub static REGION_ID: Lazy<u16> = Lazy::new(|| {
|
||||||
pub static ref REGION_ID: u16 = env::var("REGION_ID")
|
env::var("REGION_ID")
|
||||||
.unwrap_or_else(|_| "0".to_string())
|
.unwrap_or_else(|_| "0".to_string())
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap()
|
||||||
pub static ref REGION_KEY: String = format!("region{}", &*REGION_ID);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
/// Compact presence information for a user
|
pub static REGION_KEY: Lazy<String> = Lazy::new(|| format!("region{}", &*REGION_ID));
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct PresenceEntry {
|
|
||||||
/// Region this session exists in
|
|
||||||
///
|
|
||||||
/// We can have up to 65535 regions
|
|
||||||
pub region_id: u16,
|
|
||||||
|
|
||||||
/// Unique session ID
|
|
||||||
pub session_id: u8,
|
|
||||||
|
|
||||||
/// Known flags about session
|
|
||||||
pub flags: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PresenceEntry {
|
|
||||||
/// Create a new presence entry from a given session ID and known flags
|
|
||||||
pub fn from(session_id: u8, flags: u8) -> Self {
|
|
||||||
Self {
|
|
||||||
region_id: *REGION_ID,
|
|
||||||
session_id,
|
|
||||||
flags,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait PresenceOp {
|
|
||||||
/// Find next available session ID
|
|
||||||
fn find_next_id(&self) -> u8;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PresenceOp for Vec<PresenceEntry> {
|
|
||||||
fn find_next_id(&self) -> u8 {
|
|
||||||
// O(n^2) scan algorithm
|
|
||||||
// should be relatively fast at low numbers anyways
|
|
||||||
for i in 0..255 {
|
|
||||||
let mut found = false;
|
|
||||||
for entry in self {
|
|
||||||
if entry.session_id == i {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
255
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,87 +2,77 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
use redis_kiss::{get_connection, AsyncCommands};
|
use redis_kiss::{get_connection, AsyncCommands};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
mod entry;
|
mod entry;
|
||||||
mod operations;
|
mod operations;
|
||||||
|
|
||||||
use entry::{PresenceEntry, PresenceOp};
|
|
||||||
use operations::{
|
use operations::{
|
||||||
__add_to_set_sessions, __delete_key_presence_entry, __get_key_presence_entry,
|
__add_to_set_string, __add_to_set_u32, __delete_key, __get_set_members_as_string,
|
||||||
__get_set_sessions, __remove_from_set_sessions, __set_key_presence_entry,
|
__get_set_size, __remove_from_set_string, __remove_from_set_u32,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::presence::operations::__delete_set_sessions;
|
|
||||||
|
|
||||||
use self::entry::REGION_KEY;
|
use self::entry::REGION_KEY;
|
||||||
|
|
||||||
/// Create a new presence session, returns the ID of this session
|
/// Create a new presence session, returns the ID of this session
|
||||||
pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u8) {
|
pub async fn presence_create_session(user_id: &str, flags: u8) -> (bool, u32) {
|
||||||
info!("Creating a presence session for {user_id} with flags {flags}");
|
info!("Creating a presence session for {user_id} with flags {flags}");
|
||||||
|
|
||||||
// Try to find the presence entry for this user.
|
if let Ok(mut conn) = get_connection().await {
|
||||||
let mut conn = get_connection().await.unwrap();
|
// Check whether this is the first session
|
||||||
let mut entry: Vec<PresenceEntry> = __get_key_presence_entry(&mut conn, user_id)
|
let was_empty = __get_set_size(&mut conn, user_id).await == 0;
|
||||||
.await
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
// Return whether this was the first session.
|
// A session ID is comprised of random data and any flags ORed to the end
|
||||||
let was_empty = entry.is_empty();
|
let session_id = {
|
||||||
info!("User ID {} just came online.", &user_id);
|
let mut rng = rand::thread_rng();
|
||||||
|
(rng.gen::<u32>() ^ 1) | (flags as u32 & 1)
|
||||||
|
};
|
||||||
|
|
||||||
// Generate session ID and push new entry.
|
// Add session to user's sessions and to the region
|
||||||
let session_id = entry.find_next_id();
|
__add_to_set_u32(&mut conn, user_id, session_id).await;
|
||||||
entry.push(PresenceEntry::from(session_id, flags));
|
__add_to_set_string(&mut conn, ®ION_KEY, &format!("{user_id}:{session_id}")).await;
|
||||||
__set_key_presence_entry(&mut conn, user_id, entry).await;
|
info!("Created session for {user_id}, assigned them a session ID of {session_id}.");
|
||||||
|
|
||||||
// Add to region set in case of failure.
|
(was_empty, session_id)
|
||||||
__add_to_set_sessions(&mut conn, ®ION_KEY, user_id, session_id).await;
|
} else {
|
||||||
(was_empty, session_id)
|
// Fail through
|
||||||
|
(false, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete existing presence session
|
/// Delete existing presence session
|
||||||
pub async fn presence_delete_session(user_id: &str, session_id: u8) -> bool {
|
pub async fn presence_delete_session(user_id: &str, session_id: u32) -> bool {
|
||||||
presence_delete_session_internal(user_id, session_id, false).await
|
presence_delete_session_internal(user_id, session_id, false).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete existing presence session (but also choose whether to skip region)
|
/// Delete existing presence session (but also choose whether to skip region)
|
||||||
async fn presence_delete_session_internal(
|
async fn presence_delete_session_internal(
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
session_id: u8,
|
session_id: u32,
|
||||||
skip_region: bool,
|
skip_region: bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
info!("Deleting presence session for {user_id} with id {session_id}");
|
info!("Deleting presence session for {user_id} with id {session_id}");
|
||||||
|
|
||||||
// Return whether this was the last session.
|
if let Ok(mut conn) = get_connection().await {
|
||||||
let mut is_empty = false;
|
// Remove the session
|
||||||
|
__remove_from_set_u32(&mut conn, user_id, session_id).await;
|
||||||
|
|
||||||
// Only continue if we can actually find one.
|
// Remove from the region
|
||||||
let mut conn = get_connection().await.unwrap();
|
|
||||||
let entry: Option<Vec<PresenceEntry>> = __get_key_presence_entry(&mut conn, user_id).await;
|
|
||||||
if let Some(entry) = entry {
|
|
||||||
let entries = entry
|
|
||||||
.into_iter()
|
|
||||||
.filter(|x| x.session_id != session_id)
|
|
||||||
.collect::<Vec<PresenceEntry>>();
|
|
||||||
|
|
||||||
// If entry is empty, then just delete it.
|
|
||||||
if entries.is_empty() {
|
|
||||||
__delete_key_presence_entry(&mut conn, user_id).await;
|
|
||||||
is_empty = true;
|
|
||||||
} else {
|
|
||||||
__set_key_presence_entry(&mut conn, user_id, entries).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove from region set.
|
|
||||||
if !skip_region {
|
if !skip_region {
|
||||||
__remove_from_set_sessions(&mut conn, ®ION_KEY, user_id, session_id).await;
|
__remove_from_set_string(&mut conn, ®ION_KEY, &format!("{user_id}:{session_id}"))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if is_empty {
|
// Return whether this was the last session
|
||||||
info!("User ID {} just went offline.", &user_id);
|
let is_empty = __get_set_size(&mut conn, user_id).await == 0;
|
||||||
}
|
if is_empty {
|
||||||
|
info!("User ID {} just went offline.", &user_id);
|
||||||
|
}
|
||||||
|
|
||||||
is_empty
|
is_empty
|
||||||
|
} else {
|
||||||
|
// Fail through
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether a given user ID is online
|
/// Check whether a given user ID is online
|
||||||
@@ -102,6 +92,10 @@ pub async fn presence_filter_online(user_ids: &'_ [String]) -> HashSet<String> {
|
|||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: at the point that we need mobile indicators
|
||||||
|
// you can interpret the data here and return a new data
|
||||||
|
// structure like HashMap<String /* id */, u8 /* flags */>
|
||||||
|
|
||||||
// We need to handle a special case where only one is present
|
// We need to handle a special case where only one is present
|
||||||
// as for some reason or another, Redis does not like us sending
|
// as for some reason or another, Redis does not like us sending
|
||||||
// a list of just one ID to the server.
|
// a list of just one ID to the server.
|
||||||
@@ -136,7 +130,7 @@ pub async fn presence_clear_region(region_id: Option<&str>) {
|
|||||||
let region_id = region_id.unwrap_or(&*REGION_KEY);
|
let region_id = region_id.unwrap_or(&*REGION_KEY);
|
||||||
let mut conn = get_connection().await.expect("Redis connection");
|
let mut conn = get_connection().await.expect("Redis connection");
|
||||||
|
|
||||||
let sessions = __get_set_sessions(&mut conn, region_id).await;
|
let sessions = __get_set_members_as_string(&mut conn, region_id).await;
|
||||||
if !sessions.is_empty() {
|
if !sessions.is_empty() {
|
||||||
info!(
|
info!(
|
||||||
"Cleaning up {} sessions, this may take a while...",
|
"Cleaning up {} sessions, this may take a while...",
|
||||||
@@ -155,7 +149,7 @@ pub async fn presence_clear_region(region_id: Option<&str>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Then clear the set in Redis.
|
// Then clear the set in Redis.
|
||||||
__delete_set_sessions(&mut conn, region_id).await;
|
__delete_key(&mut conn, region_id).await;
|
||||||
|
|
||||||
info!("Clean up complete.");
|
info!("Clean up complete.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,42 @@
|
|||||||
use redis_kiss::{AsyncCommands, Conn};
|
use redis_kiss::{AsyncCommands, Conn};
|
||||||
|
|
||||||
use super::entry::PresenceEntry;
|
/// Add to set (string)
|
||||||
|
pub async fn __add_to_set_string(conn: &mut Conn, key: &str, value: &str) {
|
||||||
/// Set presence entry by given ID
|
let _: Option<()> = conn.sadd(key, value).await.ok();
|
||||||
pub async fn __set_key_presence_entry(conn: &mut Conn, id: &str, data: Vec<PresenceEntry>) {
|
|
||||||
let _: Option<()> = conn.set(id, bincode::serialize(&data).unwrap()).await.ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete presence entry by given ID
|
/// Add to set (u32)
|
||||||
pub async fn __delete_key_presence_entry(conn: &mut Conn, id: &str) {
|
pub async fn __add_to_set_u32(conn: &mut Conn, key: &str, value: u32) {
|
||||||
let _: Option<()> = conn.del(id).await.ok();
|
let _: Option<()> = conn.sadd(key, value).await.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get presence entry by given ID
|
/// Remove from set (string)
|
||||||
pub async fn __get_key_presence_entry(conn: &mut Conn, id: &str) -> Option<Vec<PresenceEntry>> {
|
pub async fn __remove_from_set_string(conn: &mut Conn, key: &str, value: &str) {
|
||||||
conn.get::<_, Option<Vec<u8>>>(id)
|
let _: Option<()> = conn.srem(key, value).await.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove from set (u32)
|
||||||
|
pub async fn __remove_from_set_u32(conn: &mut Conn, key: &str, value: u32) {
|
||||||
|
let _: Option<()> = conn.srem(key, value).await.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get set members as string
|
||||||
|
pub async fn __get_set_members_as_string(conn: &mut Conn, key: &str) -> Vec<String> {
|
||||||
|
conn.smembers::<_, Vec<String>>(key)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.expect("could not get set members as string")
|
||||||
.map(|entry| bincode::deserialize(&entry[..]).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add to region session set
|
/// Get set size
|
||||||
pub async fn __add_to_set_sessions(
|
pub async fn __get_set_size(conn: &mut Conn, id: &str) -> u32 {
|
||||||
conn: &mut Conn,
|
conn.scard::<_, u32>(id)
|
||||||
region_id: &str,
|
|
||||||
user_id: &str,
|
|
||||||
session_id: u8,
|
|
||||||
) {
|
|
||||||
let _: Option<()> = conn
|
|
||||||
.sadd(region_id, format!("{user_id}:{session_id}"))
|
|
||||||
.await
|
.await
|
||||||
.ok();
|
.expect("could not get set size")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove from region session set
|
/// Delete key by id
|
||||||
pub async fn __remove_from_set_sessions(
|
pub async fn __delete_key(conn: &mut Conn, id: &str) {
|
||||||
conn: &mut Conn,
|
conn.del::<_, ()>(id)
|
||||||
region_id: &str,
|
|
||||||
user_id: &str,
|
|
||||||
session_id: u8,
|
|
||||||
) {
|
|
||||||
let _: Option<()> = conn
|
|
||||||
.srem(region_id, format!("{user_id}:{session_id}"))
|
|
||||||
.await
|
.await
|
||||||
.ok();
|
.expect("could not delete key by id");
|
||||||
}
|
|
||||||
|
|
||||||
/// Get region session set as list
|
|
||||||
pub async fn __get_set_sessions(conn: &mut Conn, region_id: &str) -> Vec<String> {
|
|
||||||
conn.smembers::<_, Vec<String>>(region_id).await.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Delete region session set
|
|
||||||
pub async fn __delete_set_sessions(conn: &mut Conn, region_id: &str) {
|
|
||||||
conn.del::<_, ()>(region_id).await.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
use crate::Database;
|
use crate::Database;
|
||||||
|
|
||||||
use deadqueue::limited::Queue;
|
use deadqueue::limited::Queue;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use std::{collections::HashMap, time::Duration};
|
use std::{collections::HashMap, time::Duration};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use super::DelayedTask;
|
use super::DelayedTask;
|
||||||
|
|
||||||
@@ -38,9 +38,7 @@ struct Task {
|
|||||||
event: AckEvent,
|
event: AckEvent,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static Q: Lazy<Queue<Data>> = Lazy::new(|| Queue::new(10_000));
|
||||||
static ref Q: Queue<Data> = Queue::new(10_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Queue a new task for a worker
|
/// Queue a new task for a worker
|
||||||
pub async fn queue(channel: String, user: String, event: AckEvent) {
|
pub async fn queue(channel: String, user: String, event: AckEvent) {
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
use crate::{models::channel::PartialChannel, Database};
|
use crate::{models::channel::PartialChannel, Database};
|
||||||
|
|
||||||
use deadqueue::limited::Queue;
|
use deadqueue::limited::Queue;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use std::{collections::HashMap, time::Duration};
|
use std::{collections::HashMap, time::Duration};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use super::DelayedTask;
|
use super::DelayedTask;
|
||||||
|
|
||||||
@@ -26,9 +26,7 @@ struct Task {
|
|||||||
is_dm: bool,
|
is_dm: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static Q: Lazy<Queue<Data>> = Lazy::new(|| Queue::new(10_000));
|
||||||
static ref Q: Queue<Data> = Queue::new(10_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Queue a new task for a worker
|
/// Queue a new task for a worker
|
||||||
pub async fn queue(channel: String, id: String, is_dm: bool) {
|
pub async fn queue(channel: String, id: String, is_dm: bool) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use async_lock::Semaphore;
|
|||||||
use async_std::task::spawn;
|
use async_std::task::spawn;
|
||||||
use deadqueue::limited::Queue;
|
use deadqueue::limited::Queue;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
/// Task information
|
/// Task information
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -21,9 +22,8 @@ struct EmbedTask {
|
|||||||
content: String,
|
content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static Q: Lazy<Queue<EmbedTask>> = Lazy::new(|| Queue::new(10_000));
|
||||||
static ref Q: Queue<EmbedTask> = Queue::new(10_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Queue a new task for a worker
|
/// Queue a new task for a worker
|
||||||
pub async fn queue(channel: String, id: String, content: String) {
|
pub async fn queue(channel: String, id: String, content: String) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use crate::bson::doc;
|
|
||||||
use crate::util::variables::delta::VAPID_PRIVATE_KEY;
|
use crate::util::variables::delta::VAPID_PRIVATE_KEY;
|
||||||
|
|
||||||
use authifier::Database;
|
use authifier::Database;
|
||||||
use deadqueue::limited::Queue;
|
use deadqueue::limited::Queue;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use web_push::{
|
use web_push::{
|
||||||
ContentEncoding, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder, WebPushClient,
|
ContentEncoding, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder, WebPushClient,
|
||||||
WebPushMessageBuilder,
|
WebPushMessageBuilder,
|
||||||
@@ -17,9 +17,8 @@ struct PushTask {
|
|||||||
payload: String,
|
payload: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static Q: Lazy<Queue<PushTask>> = Lazy::new(|| Queue::new(10_000));
|
||||||
static ref Q: Queue<PushTask> = Queue::new(10_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Queue a new task for a worker
|
/// Queue a new task for a worker
|
||||||
pub async fn queue(recipients: Vec<String>, payload: String) {
|
pub async fn queue(recipients: Vec<String>, payload: String) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::models::message::{AppendMessage, Message, MessageSort, PartialMessage};
|
use crate::models::message::{AppendMessage, Message, MessageQuery, PartialMessage};
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@@ -21,27 +21,8 @@ pub trait AbstractMessage: Sync + Send {
|
|||||||
/// Delete messages from a channel by their ids and corresponding channel id
|
/// 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: Vec<String>) -> Result<()>;
|
||||||
|
|
||||||
/// Fetch multiple messages
|
/// Fetch multiple messages by given query
|
||||||
async fn fetch_messages(
|
async fn fetch_messages(&self, query: MessageQuery) -> Result<Vec<Message>>;
|
||||||
&self,
|
|
||||||
channel: &str,
|
|
||||||
limit: Option<i64>,
|
|
||||||
before: Option<String>,
|
|
||||||
after: Option<String>,
|
|
||||||
sort: Option<MessageSort>,
|
|
||||||
nearby: Option<String>,
|
|
||||||
) -> Result<Vec<Message>>;
|
|
||||||
|
|
||||||
/// Search for messages
|
|
||||||
async fn search_messages(
|
|
||||||
&self,
|
|
||||||
channel: &str,
|
|
||||||
query: &str,
|
|
||||||
limit: Option<i64>,
|
|
||||||
before: Option<String>,
|
|
||||||
after: Option<String>,
|
|
||||||
sort: MessageSort,
|
|
||||||
) -> Result<Vec<Message>>;
|
|
||||||
|
|
||||||
/// Add a new reaction to a message
|
/// Add a new reaction to a message
|
||||||
async fn add_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()>;
|
async fn add_reaction(&self, id: &str, emoji: &str, user: &str) -> Result<()>;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use linkify::{LinkFinder, LinkKind};
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::HashSet, sync::Arc};
|
use std::{collections::HashSet, sync::Arc};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::{models::attachment::File, Error, Result};
|
use crate::{models::attachment::File, Error, Result};
|
||||||
|
|
||||||
@@ -175,6 +176,9 @@ pub enum Embed {
|
|||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static RE_CODE: Lazy<Regex> = Lazy::new(|| Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap());
|
||||||
|
static RE_IGNORED: Lazy<Regex> = Lazy::new(|| Regex::new("(<http.+>)").unwrap());
|
||||||
|
|
||||||
impl Embed {
|
impl Embed {
|
||||||
/// Generate embeds from given content
|
/// Generate embeds from given content
|
||||||
pub async fn generate(
|
pub async fn generate(
|
||||||
@@ -183,10 +187,7 @@ impl Embed {
|
|||||||
max_embeds: usize,
|
max_embeds: usize,
|
||||||
semaphore: Arc<Semaphore>,
|
semaphore: Arc<Semaphore>,
|
||||||
) -> Result<Vec<Embed>> {
|
) -> Result<Vec<Embed>> {
|
||||||
lazy_static! {
|
|
||||||
static ref RE_CODE: Regex = Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap();
|
|
||||||
static ref RE_IGNORED: Regex = Regex::new("(<http.+>)").unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore code blocks.
|
// Ignore code blocks.
|
||||||
let content = RE_CODE.replace_all(&content, "");
|
let content = RE_CODE.replace_all(&content, "");
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ pub fn setup_logging(release: &'static str) -> sentry::ClientInitGuard {
|
|||||||
info!("Starting {release}");
|
info!("Starting {release}");
|
||||||
|
|
||||||
sentry::init((
|
sentry::init((
|
||||||
"https://62fd0e02c5354905b4e286757f4beb16@sentry.insert.moe/4",
|
"https://d1d2a6f15c6245a987c532bbbcb30a04@glitchtip.insert.moe/2",
|
||||||
sentry::ClientOptions {
|
sentry::ClientOptions {
|
||||||
release: Some(release.into()),
|
release: Some(release.into()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ use revolt_rocket_okapi::revolt_okapi::openapi3;
|
|||||||
use rocket::{
|
use rocket::{
|
||||||
http::{ContentType, Status},
|
http::{ContentType, Status},
|
||||||
response::{self, Responder},
|
response::{self, Responder},
|
||||||
serde::json::serde_json::json,
|
|
||||||
Request, Response,
|
Request, Response,
|
||||||
};
|
};
|
||||||
use schemars::schema::Schema;
|
use schemars::schema::Schema;
|
||||||
@@ -39,8 +38,15 @@ pub enum Error {
|
|||||||
UnknownMessage,
|
UnknownMessage,
|
||||||
CannotEditMessage,
|
CannotEditMessage,
|
||||||
CannotJoinCall,
|
CannotJoinCall,
|
||||||
TooManyAttachments,
|
TooManyAttachments {
|
||||||
TooManyReplies,
|
max: usize
|
||||||
|
},
|
||||||
|
TooManyReplies {
|
||||||
|
max: usize
|
||||||
|
},
|
||||||
|
TooManyChannels {
|
||||||
|
max: usize
|
||||||
|
},
|
||||||
EmptyMessage,
|
EmptyMessage,
|
||||||
PayloadTooLarge,
|
PayloadTooLarge,
|
||||||
CannotRemoveYourself,
|
CannotRemoveYourself,
|
||||||
@@ -57,7 +63,12 @@ pub enum Error {
|
|||||||
TooManyServers {
|
TooManyServers {
|
||||||
max: usize,
|
max: usize,
|
||||||
},
|
},
|
||||||
TooManyEmoji,
|
TooManyEmoji {
|
||||||
|
max: usize
|
||||||
|
},
|
||||||
|
TooManyRoles {
|
||||||
|
max: usize
|
||||||
|
},
|
||||||
|
|
||||||
// ? Bot related errors
|
// ? Bot related errors
|
||||||
ReachedMaximumBots,
|
ReachedMaximumBots,
|
||||||
@@ -151,8 +162,8 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
Error::UnknownAttachment => Status::BadRequest,
|
Error::UnknownAttachment => Status::BadRequest,
|
||||||
Error::CannotEditMessage => Status::Forbidden,
|
Error::CannotEditMessage => Status::Forbidden,
|
||||||
Error::CannotJoinCall => Status::BadRequest,
|
Error::CannotJoinCall => Status::BadRequest,
|
||||||
Error::TooManyAttachments => Status::BadRequest,
|
Error::TooManyAttachments { .. } => Status::BadRequest,
|
||||||
Error::TooManyReplies => Status::BadRequest,
|
Error::TooManyReplies { .. } => Status::BadRequest,
|
||||||
Error::EmptyMessage => Status::UnprocessableEntity,
|
Error::EmptyMessage => Status::UnprocessableEntity,
|
||||||
Error::PayloadTooLarge => Status::UnprocessableEntity,
|
Error::PayloadTooLarge => Status::UnprocessableEntity,
|
||||||
Error::CannotRemoveYourself => Status::BadRequest,
|
Error::CannotRemoveYourself => Status::BadRequest,
|
||||||
@@ -163,8 +174,11 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
Error::UnknownServer => Status::NotFound,
|
Error::UnknownServer => Status::NotFound,
|
||||||
Error::InvalidRole => Status::NotFound,
|
Error::InvalidRole => Status::NotFound,
|
||||||
Error::Banned => Status::Forbidden,
|
Error::Banned => Status::Forbidden,
|
||||||
Error::TooManyServers { .. } => Status::Forbidden,
|
|
||||||
Error::TooManyEmoji => Status::BadRequest,
|
Error::TooManyServers { .. } => Status::BadRequest,
|
||||||
|
Error::TooManyEmoji { .. } => Status::BadRequest,
|
||||||
|
Error::TooManyChannels { .. } => Status::BadRequest,
|
||||||
|
Error::TooManyRoles { .. } => Status::BadRequest,
|
||||||
|
|
||||||
Error::ReachedMaximumBots => Status::BadRequest,
|
Error::ReachedMaximumBots => Status::BadRequest,
|
||||||
Error::IsBot => Status::BadRequest,
|
Error::IsBot => Status::BadRequest,
|
||||||
@@ -193,7 +207,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Serialize the error data structure into JSON.
|
// Serialize the error data structure into JSON.
|
||||||
let string = json!(self).to_string();
|
let string = serde_json::to_string(&self).unwrap();
|
||||||
|
|
||||||
// Build and send the request.
|
// Build and send the request.
|
||||||
Response::build()
|
Response::build()
|
||||||
|
|||||||
@@ -1,73 +1,56 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
lazy_static! {
|
// Application Settings
|
||||||
// Application Settings
|
pub static PUBLIC_URL: Lazy<String> = Lazy::new(|| env::var("REVOLT_PUBLIC_URL").expect("Missing REVOLT_PUBLIC_URL environment variable."));
|
||||||
pub static ref PUBLIC_URL: String =
|
pub static APP_URL: Lazy<String> = Lazy::new(|| env::var("REVOLT_APP_URL").expect("Missing REVOLT_APP_URL environment variable."));
|
||||||
env::var("REVOLT_PUBLIC_URL").expect("Missing REVOLT_PUBLIC_URL environment variable.");
|
pub static EXTERNAL_WS_URL: Lazy<String> = Lazy::new(|| env::var("REVOLT_EXTERNAL_WS_URL").expect("Missing REVOLT_EXTERNAL_WS_URL environment variable."));
|
||||||
pub static ref APP_URL: String =
|
|
||||||
env::var("REVOLT_APP_URL").expect("Missing REVOLT_APP_URL environment variable.");
|
|
||||||
pub static ref EXTERNAL_WS_URL: String =
|
|
||||||
env::var("REVOLT_EXTERNAL_WS_URL").expect("Missing REVOLT_EXTERNAL_WS_URL environment variable.");
|
|
||||||
|
|
||||||
pub static ref AUTUMN_URL: String =
|
pub static AUTUMN_URL: Lazy<String> = Lazy::new(|| env::var("AUTUMN_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string()));
|
||||||
env::var("AUTUMN_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string());
|
pub static JANUARY_URL: Lazy<String> = Lazy::new(|| env::var("JANUARY_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string()));
|
||||||
pub static ref JANUARY_URL: String =
|
pub static JANUARY_CONCURRENT_CONNECTIONS: Lazy<usize> = Lazy::new(|| env::var("JANUARY_CONCURRENT_CONNECTIONS").map_or(50, |v| v.parse().unwrap()));
|
||||||
env::var("JANUARY_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string());
|
pub static VOSO_URL: Lazy<String> = Lazy::new(|| env::var("VOSO_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string()));
|
||||||
pub static ref JANUARY_CONCURRENT_CONNECTIONS: usize =
|
pub static VOSO_WS_HOST: Lazy<String> = Lazy::new(|| env::var("VOSO_WS_HOST").unwrap_or_else(|_| "wss://example.com".to_string()));
|
||||||
env::var("JANUARY_CONCURRENT_CONNECTIONS").map_or(50, |v| v.parse().unwrap());
|
pub static VOSO_MANAGE_TOKEN: Lazy<String> = Lazy::new(|| env::var("VOSO_MANAGE_TOKEN").unwrap_or_else(|_| "0".to_string()));
|
||||||
pub static ref VOSO_URL: String =
|
|
||||||
env::var("VOSO_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string());
|
|
||||||
pub static ref VOSO_WS_HOST: String =
|
|
||||||
env::var("VOSO_WS_HOST").unwrap_or_else(|_| "wss://example.com".to_string());
|
|
||||||
pub static ref VOSO_MANAGE_TOKEN: String =
|
|
||||||
env::var("VOSO_MANAGE_TOKEN").unwrap_or_else(|_| "0".to_string());
|
|
||||||
|
|
||||||
pub static ref HCAPTCHA_KEY: String =
|
pub static HCAPTCHA_KEY: Lazy<String> = Lazy::new(|| env::var("REVOLT_HCAPTCHA_KEY").unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string()));
|
||||||
env::var("REVOLT_HCAPTCHA_KEY").unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string());
|
pub static HCAPTCHA_SITEKEY: Lazy<String> = Lazy::new(|| env::var("REVOLT_HCAPTCHA_SITEKEY").unwrap_or_else(|_| "10000000-ffff-ffff-ffff-000000000001".to_string()));
|
||||||
pub static ref HCAPTCHA_SITEKEY: String =
|
pub static VAPID_PRIVATE_KEY: Lazy<String> = Lazy::new(|| env::var("REVOLT_VAPID_PRIVATE_KEY").expect("Missing REVOLT_VAPID_PRIVATE_KEY environment variable."));
|
||||||
env::var("REVOLT_HCAPTCHA_SITEKEY").unwrap_or_else(|_| "10000000-ffff-ffff-ffff-000000000001".to_string());
|
pub static VAPID_PUBLIC_KEY: Lazy<String> = Lazy::new(|| env::var("REVOLT_VAPID_PUBLIC_KEY").expect("Missing REVOLT_VAPID_PUBLIC_KEY environment variable."));
|
||||||
pub static ref VAPID_PRIVATE_KEY: String =
|
pub static AUTHIFIER_SHIELD_KEY: Lazy<Option<String>> = Lazy::new(|| env::var("REVOLT_AUTHIFIER_SHIELD_KEY").ok());
|
||||||
env::var("REVOLT_VAPID_PRIVATE_KEY").expect("Missing REVOLT_VAPID_PRIVATE_KEY environment variable.");
|
|
||||||
pub static ref VAPID_PUBLIC_KEY: String =
|
|
||||||
env::var("REVOLT_VAPID_PUBLIC_KEY").expect("Missing REVOLT_VAPID_PUBLIC_KEY environment variable.");
|
|
||||||
pub static ref AUTHIFIER_SHIELD_KEY: Option<String> =
|
|
||||||
env::var("REVOLT_AUTHIFIER_SHIELD_KEY").ok();
|
|
||||||
|
|
||||||
// Application Flags
|
// Application Flags
|
||||||
pub static ref INVITE_ONLY: bool = env::var("REVOLT_INVITE_ONLY").map_or(false, |v| v == "1");
|
pub static INVITE_ONLY: Lazy<bool> = Lazy::new(|| env::var("REVOLT_INVITE_ONLY").map_or(false, |v| v == "1"));
|
||||||
pub static ref USE_EMAIL: bool = env::var("REVOLT_USE_EMAIL_VERIFICATION").map_or(
|
pub static USE_EMAIL: Lazy<bool> = Lazy::new(|| env::var("REVOLT_USE_EMAIL_VERIFICATION").map_or(
|
||||||
env::var("REVOLT_SMTP_HOST").is_ok()
|
env::var("REVOLT_SMTP_HOST").is_ok()
|
||||||
&& env::var("REVOLT_SMTP_USERNAME").is_ok()
|
&& env::var("REVOLT_SMTP_USERNAME").is_ok()
|
||||||
&& env::var("REVOLT_SMTP_PASSWORD").is_ok()
|
&& env::var("REVOLT_SMTP_PASSWORD").is_ok()
|
||||||
&& env::var("REVOLT_SMTP_FROM").is_ok(),
|
&& env::var("REVOLT_SMTP_FROM").is_ok(),
|
||||||
|v| v == *"1"
|
|v| v == *"1"
|
||||||
);
|
));
|
||||||
pub static ref USE_HCAPTCHA: bool = env::var("REVOLT_HCAPTCHA_KEY").is_ok();
|
pub static USE_HCAPTCHA: Lazy<bool> = Lazy::new(|| env::var("REVOLT_HCAPTCHA_KEY").is_ok());
|
||||||
pub static ref USE_AUTUMN: bool = env::var("AUTUMN_PUBLIC_URL").is_ok();
|
pub static USE_AUTUMN: Lazy<bool> = Lazy::new(|| env::var("AUTUMN_PUBLIC_URL").is_ok());
|
||||||
pub static ref USE_JANUARY: bool = env::var("JANUARY_PUBLIC_URL").is_ok();
|
pub static USE_JANUARY: Lazy<bool> = Lazy::new(|| env::var("JANUARY_PUBLIC_URL").is_ok());
|
||||||
pub static ref USE_VOSO: bool = env::var("VOSO_PUBLIC_URL").is_ok() && env::var("VOSO_MANAGE_TOKEN").is_ok();
|
pub static USE_VOSO: Lazy<bool> = Lazy::new(|| env::var("VOSO_PUBLIC_URL").is_ok() && env::var("VOSO_MANAGE_TOKEN").is_ok());
|
||||||
|
|
||||||
// SMTP Settings
|
// SMTP Settings
|
||||||
pub static ref SMTP_HOST: String =
|
pub static SMTP_HOST: Lazy<String> = Lazy::new(|| env::var("REVOLT_SMTP_HOST").unwrap_or_else(|_| "".to_string()));
|
||||||
env::var("REVOLT_SMTP_HOST").unwrap_or_else(|_| "".to_string());
|
pub static SMTP_USERNAME: Lazy<String> = Lazy::new(|| env::var("REVOLT_SMTP_USERNAME").unwrap_or_else(|_| "".to_string()));
|
||||||
pub static ref SMTP_USERNAME: String =
|
pub static SMTP_PASSWORD: Lazy<String> = Lazy::new(|| env::var("REVOLT_SMTP_PASSWORD").unwrap_or_else(|_| "".to_string()));
|
||||||
env::var("REVOLT_SMTP_USERNAME").unwrap_or_else(|_| "".to_string());
|
pub static SMTP_FROM: Lazy<String> = Lazy::new(|| env::var("REVOLT_SMTP_FROM").unwrap_or_else(|_| "".to_string()));
|
||||||
pub static ref SMTP_PASSWORD: String =
|
|
||||||
env::var("REVOLT_SMTP_PASSWORD").unwrap_or_else(|_| "".to_string());
|
|
||||||
pub static ref SMTP_FROM: String = env::var("REVOLT_SMTP_FROM").unwrap_or_else(|_| "".to_string());
|
|
||||||
|
|
||||||
// Application Logic Settings
|
// Application Logic Settings
|
||||||
pub static ref MAX_GROUP_SIZE: usize =
|
pub static MAX_GROUP_SIZE: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap());
|
||||||
env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap();
|
pub static MAX_BOT_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_BOT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
|
||||||
pub static ref MAX_BOT_COUNT: usize =
|
pub static MAX_EMBED_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_EMBED_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
|
||||||
env::var("REVOLT_MAX_BOT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap();
|
pub static MAX_SERVER_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_SERVER_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap());
|
||||||
pub static ref MAX_EMBED_COUNT: usize =
|
pub static MAX_CHANNEL_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_CHANNEL_COUNT").unwrap_or_else(|_| "200".to_string()).parse().unwrap());
|
||||||
env::var("REVOLT_MAX_EMBED_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap();
|
pub static MAX_ROLE_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_ROLE_COUNT").unwrap_or_else(|_| "200".to_string()).parse().unwrap());
|
||||||
pub static ref MAX_SERVER_COUNT: usize =
|
pub static MAX_EMOJI_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_EMOJI_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap());
|
||||||
env::var("REVOLT_MAX_SERVER_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap();
|
pub static MAX_ATTACHMENT_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_ATTACHMENT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
|
||||||
pub static ref EARLY_ADOPTER_BADGE: i64 =
|
pub static MAX_REPLY_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_REPLY_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
|
||||||
env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap();
|
|
||||||
}
|
pub static EARLY_ADOPTER_BADGE: Lazy<i64> = Lazy::new(|| env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap());
|
||||||
|
|
||||||
pub fn preflight_checks() {
|
pub fn preflight_checks() {
|
||||||
format!("url = {}", *APP_URL);
|
format!("url = {}", *APP_URL);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use rocket::request::{FromRequest, Outcome};
|
|||||||
use schemars::schema::{InstanceType, SchemaObject, SingleOrVec};
|
use schemars::schema::{InstanceType, SchemaObject, SingleOrVec};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
#[derive(Validate, Serialize, Deserialize)]
|
#[derive(Validate, Serialize, Deserialize)]
|
||||||
pub struct IdempotencyKey {
|
pub struct IdempotencyKey {
|
||||||
@@ -16,9 +17,7 @@ pub struct IdempotencyKey {
|
|||||||
key: String,
|
key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static TOKEN_CACHE: Lazy<Mutex<lru::LruCache<String, ()>>> = Lazy::new(|| Mutex::new(lru::LruCache::new(100)));
|
||||||
static ref TOKEN_CACHE: Mutex<lru::LruCache<String, ()>> = Mutex::new(lru::LruCache::new(100));
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IdempotencyKey {
|
impl IdempotencyKey {
|
||||||
// Backwards compatibility.
|
// Backwards compatibility.
|
||||||
|
|||||||
@@ -22,17 +22,16 @@ use revolt_rocket_okapi::request::{OpenApiFromRequest, RequestHeaderInput};
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
/// Ratelimit Bucket
|
/// Ratelimit Bucket
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct Entry {
|
struct Entry {
|
||||||
used: u8,
|
used: u8,
|
||||||
reset: u128,
|
reset: u128,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static MAP: Lazy<DashMap<u64, Entry>> = Lazy::new(DashMap::new);
|
||||||
static ref MAP: DashMap<u64, Entry> = DashMap::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the current time from Unix Epoch as a Duration
|
/// Get the current time from Unix Epoch as a Duration
|
||||||
fn now() -> Duration {
|
fn now() -> Duration {
|
||||||
@@ -51,7 +50,7 @@ impl Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Deduct one unit from the bucket and save
|
/// Deduct one unit from the bucket and save
|
||||||
pub fn deduct(mut self) {
|
pub fn deduct(&mut self) {
|
||||||
let current_time = now().as_millis();
|
let current_time = now().as_millis();
|
||||||
if current_time > self.reset {
|
if current_time > self.reset {
|
||||||
self.used = 1;
|
self.used = 1;
|
||||||
@@ -193,7 +192,7 @@ impl Ratelimiter {
|
|||||||
|
|
||||||
let key = key.finish();
|
let key = key.finish();
|
||||||
let limit = resolve_bucket_limit(bucket);
|
let limit = resolve_bucket_limit(bucket);
|
||||||
let entry = Entry::from(key);
|
let mut entry = Entry::from(key);
|
||||||
|
|
||||||
let remaining = entry.get_remaining(limit);
|
let remaining = entry.get_remaining(limit);
|
||||||
if remaining > 0 {
|
if remaining > 0 {
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
version: "3.3"
|
version: "3.3"
|
||||||
services:
|
services:
|
||||||
|
# Redis
|
||||||
|
redis:
|
||||||
|
image: eqalpha/keydb
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
|
|
||||||
# MongoDB
|
# MongoDB
|
||||||
database:
|
database:
|
||||||
image: mongo
|
image: mongo
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
case "${TARGETARCH}" in
|
||||||
|
"amd64")
|
||||||
|
LINKER_NAME="x86_64-linux-gnu-gcc"
|
||||||
|
LINKER_PACKAGE="gcc-x86-64-linux-gnu"
|
||||||
|
BUILD_TARGET="x86_64-unknown-linux-gnu" ;;
|
||||||
|
"arm64")
|
||||||
|
LINKER_NAME="aarch64-linux-gnu-gcc"
|
||||||
|
LINKER_PACKAGE="gcc-aarch64-linux-gnu"
|
||||||
|
BUILD_TARGET="aarch64-unknown-linux-gnu" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
tools() {
|
||||||
|
apt-get install -y "${LINKER_PACKAGE}"
|
||||||
|
rustup target add "${BUILD_TARGET}"
|
||||||
|
}
|
||||||
|
|
||||||
|
deps() {
|
||||||
|
mkdir -p \
|
||||||
|
crates/bonfire/src \
|
||||||
|
crates/delta/src \
|
||||||
|
crates/quark/src
|
||||||
|
echo 'fn main() { panic!("stub"); }' |
|
||||||
|
tee crates/bonfire/src/main.rs |
|
||||||
|
tee crates/delta/src/main.rs
|
||||||
|
echo '' |
|
||||||
|
tee crates/quark/src/lib.rs
|
||||||
|
cargo build --locked --release --target "${BUILD_TARGET}"
|
||||||
|
}
|
||||||
|
|
||||||
|
apps() {
|
||||||
|
touch -am \
|
||||||
|
crates/bonfire/src/main.rs \
|
||||||
|
crates/delta/src/main.rs \
|
||||||
|
crates/quark/src/lib.rs
|
||||||
|
cargo build --locked --release --target "${BUILD_TARGET}"
|
||||||
|
mv target _target && mv _target/"${BUILD_TARGET}" target
|
||||||
|
}
|
||||||
|
|
||||||
|
export RUSTFLAGS="-C linker=${LINKER_NAME}"
|
||||||
|
export PKG_CONFIG_ALLOW_CROSS="1"
|
||||||
|
export PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig"
|
||||||
|
|
||||||
|
"$@"
|
||||||
Reference in New Issue
Block a user