forked from jmug/stoatchat
Compare commits
265
Commits
20240611-2
...
20250818-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f9015a6ff | ||
|
|
ed22b3a5ce | ||
|
|
65fbd36624 | ||
|
|
050ba16d4a | ||
|
|
65bc6c8fc6 | ||
|
|
6ad3da5f35 | ||
|
|
947eb15771 | ||
|
|
f4ee35fb09 | ||
|
|
6048587d34 | ||
|
|
80cf8fc4e8 | ||
|
|
4f54227495 | ||
|
|
aab1734615 | ||
|
|
40a41ffd64 | ||
|
|
d30ceea373 | ||
|
|
3e8a401077 | ||
|
|
99f400bc7b | ||
|
|
73b576a75f | ||
|
|
4e4e598daf | ||
|
|
77daf82b94 | ||
|
|
e00603f276 | ||
|
|
1b2c7b2fa1 | ||
|
|
c526095d4f | ||
|
|
8cc4bbea4d | ||
|
|
911ffc767e | ||
|
|
1690df998d | ||
|
|
519d3c08a8 | ||
|
|
9846d8aac2 | ||
|
|
c74b6255dd | ||
|
|
df91b8c990 | ||
|
|
c4728c696d | ||
|
|
8153f5f17a | ||
|
|
99a80f723d | ||
|
|
4f13f5899b | ||
|
|
5362e84730 | ||
|
|
be89e62d54 | ||
|
|
a72134f4ad | ||
|
|
69ae767175 | ||
|
|
2aff76c369 | ||
|
|
01e0f9e558 | ||
|
|
8ea8f50fb8 | ||
|
|
68bcfcb626 | ||
|
|
9632a0dc1b | ||
|
|
a4a0934475 | ||
|
|
2061794305 | ||
|
|
3a7ebad883 | ||
|
|
6daa8223b2 | ||
|
|
4a58d6eae0 | ||
|
|
a8a2e491b0 | ||
|
|
f98f09a5ea | ||
|
|
7a4e12a732 | ||
|
|
aa8f8575bc | ||
|
|
70d6a5ac08 | ||
|
|
7f85ad5e7b | ||
|
|
c785ae615d | ||
|
|
9c8a362e9f | ||
|
|
e708c612cf | ||
|
|
f75d635c81 | ||
|
|
29b6ac3acf | ||
|
|
a1b0e4767a | ||
|
|
f4281c7050 | ||
|
|
d2e83c94f3 | ||
|
|
2540860129 | ||
|
|
93a5de769d | ||
|
|
717d5e9a88 | ||
|
|
18f06467bb | ||
|
|
8d88ea9963 | ||
|
|
e3e1cab02e | ||
|
|
00bd56b687 | ||
|
|
a7727bba5c | ||
|
|
6b8005f530 | ||
|
|
714fba7ea6 | ||
|
|
ac05fb385c | ||
|
|
537c147c4b | ||
|
|
7bee609b84 | ||
|
|
859c0214ec | ||
|
|
5eea0bbc9a | ||
|
|
15dec656a8 | ||
|
|
bcf6561c0c | ||
|
|
b177a3e201 | ||
|
|
d1236bf8f2 | ||
|
|
01490f5723 | ||
|
|
3ceefb840b | ||
|
|
eb5f5f91cd | ||
|
|
52e1f0ddde | ||
|
|
defc9ec79b | ||
|
|
7026961df4 | ||
|
|
078380d305 | ||
|
|
add3f40b23 | ||
|
|
7216e9909b | ||
|
|
a1e6a19210 | ||
|
|
5f39403ce7 | ||
|
|
e525ffe5e4 | ||
|
|
e3723d647e | ||
|
|
5f84daa9db | ||
|
|
e957af4ca3 | ||
|
|
439bacf067 | ||
|
|
fa55e88dd9 | ||
|
|
71e7fe3086 | ||
|
|
03f2e3b1bf | ||
|
|
8bbb579d23 | ||
|
|
6448af071b | ||
|
|
c2a0ab71df | ||
|
|
7a17165c24 | ||
|
|
d7213fa409 | ||
|
|
479f0402ca | ||
|
|
b62eeef80c | ||
|
|
7b15006a50 | ||
|
|
ac731e547d | ||
|
|
443f374f23 | ||
|
|
42367f477c | ||
|
|
3cb91151ca | ||
|
|
4c46054bff | ||
|
|
f01794af93 | ||
|
|
49f7f9549c | ||
|
|
72e3129557 | ||
|
|
75a07a84c0 | ||
|
|
b55765d7c7 | ||
|
|
ed5ded5e45 | ||
|
|
249749e14d | ||
|
|
4c00a7dfb7 | ||
|
|
acc4317246 | ||
|
|
b9ae333b02 | ||
|
|
bf0fc504a9 | ||
|
|
705e517871 | ||
|
|
6f99ac2160 | ||
|
|
2fcc714546 | ||
|
|
1e72f7bc77 | ||
|
|
af0d24c7c6 | ||
|
|
ab58177dfa | ||
|
|
df07426019 | ||
|
|
c30d9a2620 | ||
|
|
7b44317705 | ||
|
|
876068a37e | ||
|
|
397b9878e1 | ||
|
|
d9deadc65a | ||
|
|
58d3c5cc2e | ||
|
|
520fb02fb6 | ||
|
|
2cb12a3d59 | ||
|
|
18888eae5f | ||
|
|
f31020fb6e | ||
|
|
bb202079e0 | ||
|
|
bb6bcda8bd | ||
|
|
68099bd2b7 | ||
|
|
a8db1cb40d | ||
|
|
efa7ba78ed | ||
|
|
25fc692dc1 | ||
|
|
530d68fe89 | ||
|
|
afd8c906ba | ||
|
|
c596dd5458 | ||
|
|
ed78b253ff | ||
|
|
66c84e0ad9 | ||
|
|
21335b3297 | ||
|
|
8fc791f81a | ||
|
|
1689ee5ddc | ||
|
|
7a061bb3c6 | ||
|
|
6c3b8eaa92 | ||
|
|
5e1b2e165f | ||
|
|
e270b5df6a | ||
|
|
cbf9e81256 | ||
|
|
3080ec1f5a | ||
|
|
4fd66b2719 | ||
|
|
916f47e2f5 | ||
|
|
a3c5b1bf87 | ||
|
|
ace6c30ba5 | ||
|
|
f4104612b2 | ||
|
|
6209bc7152 | ||
|
|
1a6a8a809b | ||
|
|
5485781e7c | ||
|
|
8b21825ffe | ||
|
|
530ff4f9c7 | ||
|
|
2eb596fe99 | ||
|
|
0bbb9e7072 | ||
|
|
2433fa661a | ||
|
|
70bdeecf77 | ||
|
|
24dc96f80f | ||
|
|
ebbbb5e174 | ||
|
|
78757ac7f1 | ||
|
|
01368960f3 | ||
|
|
ae1d5d07e3 | ||
|
|
5ad72abca7 | ||
|
|
c1b92ef56e | ||
|
|
acbc1b8956 | ||
|
|
bce24c8b1b | ||
|
|
31262315e7 | ||
|
|
d8acaa1107 | ||
|
|
730039eda6 | ||
|
|
79e1388000 | ||
|
|
5c8ece0727 | ||
|
|
ab6b92aa2a | ||
|
|
239710951a | ||
|
|
28960979ec | ||
|
|
1938ebc3fa | ||
|
|
1fb4032d9e | ||
|
|
ae2194cce8 | ||
|
|
949a0cf649 | ||
|
|
f6c57b23b4 | ||
|
|
12ae781621 | ||
|
|
a52d610e6c | ||
|
|
f513a9cb5e | ||
|
|
535f01604b | ||
|
|
f8ec6ba5ff | ||
|
|
b8fdc07508 | ||
|
|
5ce7530206 | ||
|
|
b36bac3225 | ||
|
|
beef06ebe3 | ||
|
|
4fc46f765b | ||
|
|
b45ae2cd4d | ||
|
|
0a8089787b | ||
|
|
475fa6f2e6 | ||
|
|
96bf73cb57 | ||
|
|
87a9fb79f2 | ||
|
|
c50435d499 | ||
|
|
d2f4823ceb | ||
|
|
39230c559a | ||
|
|
32d1d5df2e | ||
|
|
7547fbe245 | ||
|
|
9f13cb6f47 | ||
|
|
d94571fbf9 | ||
|
|
389ecc0e5c | ||
|
|
412f4a99d7 | ||
|
|
caa0795d07 | ||
|
|
5c82343bb3 | ||
|
|
e17af1c064 | ||
|
|
a9e309395e | ||
|
|
8468ce19cb | ||
|
|
27f15f7b02 | ||
|
|
78b9fa168b | ||
|
|
e290d168ac | ||
|
|
78cd89ec32 | ||
|
|
f9d9059e73 | ||
|
|
0954d71f51 | ||
|
|
9ea2bd9f2f | ||
|
|
d6bcb844db | ||
|
|
ce20e689cc | ||
|
|
d9cfc9d0e1 | ||
|
|
64a07d09f4 | ||
|
|
00e881799f | ||
|
|
fa598dd6f8 | ||
|
|
ac4e2cb10b | ||
|
|
ec578ab0ef | ||
|
|
ea6ba59841 | ||
|
|
f903f716e3 | ||
|
|
c5f4b94aa5 | ||
|
|
1d5dae4751 | ||
|
|
ac20b6bc99 | ||
|
|
6ec8007e4e | ||
|
|
e5eea267cf | ||
|
|
de5add09d0 | ||
|
|
1ec8f46c1d | ||
|
|
80666848cc | ||
|
|
e8e9613169 | ||
|
|
eda36436a8 | ||
|
|
93e05e9f18 | ||
|
|
090d8cba71 | ||
|
|
f16e72e329 | ||
|
|
8248a4a5b8 | ||
|
|
cf0b4274c8 | ||
|
|
36ecb48c7b | ||
|
|
ba411fef43 | ||
|
|
4055363cff | ||
|
|
d179d1e695 | ||
|
|
fc9ceb7540 | ||
|
|
a31a3e2e65 | ||
|
|
b12e728514 | ||
|
|
962c7d62c7 |
@@ -1,2 +1,2 @@
|
|||||||
[profile.default]
|
[profile.default]
|
||||||
slow-timeout = { period = "3s", terminate-after = 2 }
|
slow-timeout = { period = "5s", terminate-after = 10 }
|
||||||
|
|||||||
-107
@@ -1,107 +0,0 @@
|
|||||||
# MongoDB URI
|
|
||||||
MONGODB=mongodb://localhost
|
|
||||||
AUTUMN_MONGO_URI=mongodb://database
|
|
||||||
REDIS_URI=redis://localhost/
|
|
||||||
|
|
||||||
# URL to where the Revolt app is publicly accessible
|
|
||||||
REVOLT_APP_URL=http://local.revolt.chat:5000
|
|
||||||
|
|
||||||
# URL to where the API is publicly accessible
|
|
||||||
REVOLT_PUBLIC_URL=http://local.revolt.chat:8000
|
|
||||||
VITE_API_URL=http://local.revolt.chat:8000
|
|
||||||
|
|
||||||
# URL to where the WebSocket server is publicly accessible
|
|
||||||
REVOLT_EXTERNAL_WS_URL=ws://local.revolt.chat:9000
|
|
||||||
|
|
||||||
# URL to where Autumn is publicly available
|
|
||||||
AUTUMN_PUBLIC_URL=http://local.revolt.chat:3000
|
|
||||||
|
|
||||||
# URL to where January is publicly available
|
|
||||||
JANUARY_PUBLIC_URL=http://local.revolt.chat:7000
|
|
||||||
|
|
||||||
# URL to where Vortex is publicly available
|
|
||||||
# VOSO_PUBLIC_URL=https://voso.revolt.chat
|
|
||||||
|
|
||||||
|
|
||||||
##
|
|
||||||
## hCaptcha Settings
|
|
||||||
##
|
|
||||||
|
|
||||||
# If you are sure that you don't want to use hCaptcha, set to 1.
|
|
||||||
REVOLT_UNSAFE_NO_CAPTCHA=1
|
|
||||||
|
|
||||||
# hCaptcha API key
|
|
||||||
# REVOLT_HCAPTCHA_KEY=0x0000000000000000000000000000000000000000
|
|
||||||
|
|
||||||
# hCaptcha site key
|
|
||||||
# REVOLT_HCAPTCHA_SITEKEY=10000000-ffff-ffff-ffff-000000000001
|
|
||||||
|
|
||||||
|
|
||||||
##
|
|
||||||
## Email Settings
|
|
||||||
##
|
|
||||||
|
|
||||||
# If you are sure that you don't want to use email verification, set to 1.
|
|
||||||
REVOLT_UNSAFE_NO_EMAIL=1
|
|
||||||
|
|
||||||
# SMTP host
|
|
||||||
# REVOLT_SMTP_HOST=smtp.example.com
|
|
||||||
|
|
||||||
# SMTP username
|
|
||||||
# REVOLT_SMTP_USERNAME=noreply@example.com
|
|
||||||
|
|
||||||
# SMTP password
|
|
||||||
# REVOLT_SMTP_PASSWORD=CHANGEME
|
|
||||||
|
|
||||||
# SMTP From header
|
|
||||||
# REVOLT_SMTP_FROM=Revolt <noreply@example.com>
|
|
||||||
|
|
||||||
|
|
||||||
##
|
|
||||||
## Application Settings
|
|
||||||
##
|
|
||||||
|
|
||||||
# Whether to enable staging only features
|
|
||||||
REVOLT_IS_STAGING=1
|
|
||||||
|
|
||||||
# Whether to only allow users to sign up if they have an invite code
|
|
||||||
REVOLT_INVITE_ONLY=0
|
|
||||||
|
|
||||||
# Maximum number of people that can be in a group chat
|
|
||||||
REVOLT_MAX_GROUP_SIZE=150
|
|
||||||
|
|
||||||
# VAPID keys for push notifications
|
|
||||||
# Generate using this guide: https://gitlab.insrt.uk/revolt/delta/-/wikis/vapid
|
|
||||||
# --> Please replace these keys before going into production! <--
|
|
||||||
REVOLT_VAPID_PRIVATE_KEY=LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUJSUWpyTWxLRnBiVWhsUHpUbERvcEliYk1yeVNrNXpKYzVYVzIxSjJDS3hvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFWnkrQkg2TGJQZ2hEa3pEempXOG0rUXVPM3pCajRXT1phdkR6ZU00c0pqbmFwd1psTFE0WAp1ZDh2TzVodU94QWhMQlU3WWRldVovWHlBdFpWZmNyQi9BPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=
|
|
||||||
REVOLT_VAPID_PUBLIC_KEY=BGcvgR-i2z4IQ5Mw841vJvkLjt8wY-FjmWrw83jOLCY52qcGZS0OF7nfLzuYbjsQISwVO2HXrmf18gLWVX3Kwfw=
|
|
||||||
|
|
||||||
|
|
||||||
##
|
|
||||||
## Vortex configuration
|
|
||||||
##
|
|
||||||
|
|
||||||
# VOSO_MANAGE_TOKEN=CHANGEME
|
|
||||||
|
|
||||||
|
|
||||||
##
|
|
||||||
## Autumn configuration
|
|
||||||
##
|
|
||||||
|
|
||||||
# S3 Region
|
|
||||||
AUTUMN_S3_REGION=minio
|
|
||||||
|
|
||||||
# S3 Endpoint
|
|
||||||
AUTUMN_S3_ENDPOINT=http://minio:9000
|
|
||||||
|
|
||||||
# MinIO Root User
|
|
||||||
MINIO_ROOT_USER=minioautumn
|
|
||||||
|
|
||||||
# MinIO Root Password
|
|
||||||
MINIO_ROOT_PASSWORD=minioautumn
|
|
||||||
|
|
||||||
# AWS Access Key ID
|
|
||||||
AWS_ACCESS_KEY_ID=minioautumn
|
|
||||||
|
|
||||||
# AWS Secret Key
|
|
||||||
AWS_SECRET_ACCESS_KEY=minioautumn
|
|
||||||
+121
-53
@@ -2,24 +2,13 @@ name: Docker Test & Publish
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
|
||||||
- "main"
|
|
||||||
tags:
|
tags:
|
||||||
- "*"
|
- "*"
|
||||||
paths-ignore:
|
|
||||||
- ".github/**"
|
|
||||||
- "!.github/workflows/docker.yml"
|
|
||||||
- ".vscode/**"
|
|
||||||
- "doc/**"
|
|
||||||
- ".gitignore"
|
|
||||||
- "LICENSE"
|
|
||||||
- "README"
|
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- "main"
|
- "main"
|
||||||
paths:
|
paths:
|
||||||
- "Dockerfile"
|
- "Dockerfile"
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
@@ -27,8 +16,9 @@ permissions:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
base:
|
base:
|
||||||
|
name: Test base image build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Build base image
|
if: github.event_name == 'pull_request'
|
||||||
steps:
|
steps:
|
||||||
# Configure build environment
|
# Configure build environment
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
@@ -36,33 +26,21 @@ jobs:
|
|||||||
- 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
|
# Build the image
|
||||||
- name: Login to Github Container Registry
|
|
||||||
if: ${{ github.event_name != 'pull_request' }}
|
|
||||||
uses: docker/login-action@v2
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Build base image
|
- name: Build base image
|
||||||
uses: docker/build-push-action@v4
|
uses: docker/build-push-action@v4
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
push: false
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
tags: ghcr.io/${{ github.repository_owner }}/base:latest
|
tags: ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
cache-from: type=gha,scope=buildx-base-multi-arch
|
cache-from: type=gha,scope=buildx-base-multi-arch
|
||||||
cache-to: type=gha,scope=buildx-base-multi-arch,mode=max
|
cache-to: type=gha,scope=buildx-base-multi-arch,mode=max
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
needs: [base]
|
runs-on: self-hosted
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request'
|
||||||
strategy:
|
name: Publish Docker images
|
||||||
matrix:
|
|
||||||
project: [delta, bonfire]
|
|
||||||
name: Build ${{ matrix.project }} image
|
|
||||||
steps:
|
steps:
|
||||||
# Configure build environment
|
# Configure build environment
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
@@ -77,6 +55,7 @@ jobs:
|
|||||||
registry: docker.io
|
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@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
@@ -84,42 +63,131 @@ jobs:
|
|||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
# Resolve the correct project
|
# Build the image
|
||||||
- uses: kanga333/variable-mapper@master
|
- name: Build base image
|
||||||
id: export
|
uses: docker/build-push-action@v4
|
||||||
with:
|
with:
|
||||||
key: "${{ matrix.project }}"
|
context: .
|
||||||
map: |
|
push: true
|
||||||
{
|
platforms: linux/amd64,linux/arm64
|
||||||
"delta": {
|
tags: ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
"path": "crates/delta",
|
|
||||||
"tag": "${{ github.repository_owner }}/server"
|
|
||||||
},
|
|
||||||
"bonfire": {
|
|
||||||
"path": "crates/bonfire",
|
|
||||||
"tag": "${{ github.repository_owner }}/bonfire"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export_to: output
|
|
||||||
|
|
||||||
# Configure metadata
|
# revoltchat/server
|
||||||
- name: Docker meta
|
- name: Docker meta
|
||||||
id: meta
|
id: meta-delta
|
||||||
uses: docker/metadata-action@v4
|
uses: docker/metadata-action@v4
|
||||||
with:
|
with:
|
||||||
images: |
|
images: |
|
||||||
docker.io/${{ steps.export.outputs.tag }}
|
docker.io/revoltchat/server
|
||||||
ghcr.io/${{ steps.export.outputs.tag }}
|
ghcr.io/revoltchat/server
|
||||||
|
|
||||||
# Build crate image
|
|
||||||
- name: Publish
|
- name: Publish
|
||||||
uses: docker/build-push-action@v4
|
uses: docker/build-push-action@v4
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
file: ${{ steps.export.outputs.path }}/Dockerfile
|
file: crates/delta/Dockerfile
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta-delta.outputs.tags }}
|
||||||
build-args: |
|
build-args: |
|
||||||
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta-delta.outputs.labels }}
|
||||||
|
|
||||||
|
# revoltchat/bonfire
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta-bonfire
|
||||||
|
uses: docker/metadata-action@v4
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
docker.io/revoltchat/bonfire
|
||||||
|
ghcr.io/revoltchat/bonfire
|
||||||
|
- name: Publish
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: crates/bonfire/Dockerfile
|
||||||
|
tags: ${{ steps.meta-bonfire.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
|
labels: ${{ steps.meta-bonfire.outputs.labels }}
|
||||||
|
|
||||||
|
# revoltchat/autumn
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta-autumn
|
||||||
|
uses: docker/metadata-action@v4
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
docker.io/revoltchat/autumn
|
||||||
|
ghcr.io/revoltchat/autumn
|
||||||
|
- name: Publish
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: crates/services/autumn/Dockerfile
|
||||||
|
tags: ${{ steps.meta-autumn.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
|
labels: ${{ steps.meta-autumn.outputs.labels }}
|
||||||
|
|
||||||
|
# revoltchat/january
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta-january
|
||||||
|
uses: docker/metadata-action@v4
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
docker.io/revoltchat/january
|
||||||
|
ghcr.io/revoltchat/january
|
||||||
|
- name: Publish
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: crates/services/january/Dockerfile
|
||||||
|
tags: ${{ steps.meta-january.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
|
labels: ${{ steps.meta-january.outputs.labels }}
|
||||||
|
|
||||||
|
# revoltchat/crond
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta-crond
|
||||||
|
uses: docker/metadata-action@v4
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
docker.io/revoltchat/crond
|
||||||
|
ghcr.io/revoltchat/crond
|
||||||
|
- name: Publish
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: crates/daemons/crond/Dockerfile
|
||||||
|
tags: ${{ steps.meta-crond.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
|
labels: ${{ steps.meta-crond.outputs.labels }}
|
||||||
|
|
||||||
|
# revoltchat/pushd
|
||||||
|
- name: Docker meta
|
||||||
|
id: meta-pushd
|
||||||
|
uses: docker/metadata-action@v4
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
docker.io/revoltchat/pushd
|
||||||
|
ghcr.io/revoltchat/pushd
|
||||||
|
- name: Publish
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
file: crates/daemons/pushd/Dockerfile
|
||||||
|
tags: ${{ steps.meta-pushd.outputs.tags }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=ghcr.io/${{ github.repository_owner }}/base:latest
|
||||||
|
labels: ${{ steps.meta-pushd.outputs.labels }}
|
||||||
|
|||||||
@@ -41,11 +41,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Run services in background
|
- name: Run services in background
|
||||||
run: |
|
run: |
|
||||||
docker-compose -f docker-compose.db.yml up -d
|
docker compose -f compose.yml up -d
|
||||||
|
|
||||||
- name: Copy .env.example
|
|
||||||
run: |
|
|
||||||
cp .env.example .env
|
|
||||||
|
|
||||||
- name: Run cargo test
|
- name: Run cargo test
|
||||||
env:
|
env:
|
||||||
@@ -71,7 +67,7 @@ jobs:
|
|||||||
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
||||||
uses: nev7n/wait_for_response@v1
|
uses: nev7n/wait_for_response@v1
|
||||||
with:
|
with:
|
||||||
url: "http://localhost:8000/"
|
url: "http://localhost:14702/"
|
||||||
|
|
||||||
- name: Checkout API repository
|
- name: Checkout API repository
|
||||||
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
||||||
@@ -83,7 +79,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Download OpenAPI specification
|
- name: Download OpenAPI specification
|
||||||
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
||||||
run: curl http://localhost:8000/openapi.json -o api/OpenAPI.json
|
run: curl http://localhost:14702/openapi.json -o api/OpenAPI.json
|
||||||
|
|
||||||
- name: Commit changes
|
- name: Commit changes
|
||||||
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
if: github.event_name != 'pull_request' && github.ref_name == 'main'
|
||||||
|
|||||||
+6
-1
@@ -1,9 +1,14 @@
|
|||||||
Rocket.toml
|
Rocket.toml
|
||||||
Revolt.toml
|
Revolt.*.toml
|
||||||
|
compose.override.yml
|
||||||
|
|
||||||
target
|
target
|
||||||
.data
|
.data
|
||||||
.env
|
.env
|
||||||
|
.venv/
|
||||||
|
venv/
|
||||||
|
|
||||||
.vercel
|
.vercel
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
.idea
|
||||||
Vendored
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"rust-analyzer.checkOnSave.command": "clippy",
|
"rust-analyzer.check.command": "clippy",
|
||||||
"nixEnvSelector.suggestion": false,
|
"nixEnvSelector.suggestion": false,
|
||||||
"nixEnvSelector.nixFile": "${workspaceRoot}/default.nix"
|
"nixEnvSelector.nixFile": "${workspaceFolder}/default.nix"
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+5770
-1522
File diff suppressed because it is too large
Load Diff
+18
-6
@@ -1,10 +1,22 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = ["crates/delta", "crates/bonfire", "crates/core/*"]
|
|
||||||
|
members = [
|
||||||
|
"crates/delta",
|
||||||
|
"crates/bonfire",
|
||||||
|
"crates/core/*",
|
||||||
|
"crates/services/*",
|
||||||
|
"crates/daemons/*",
|
||||||
|
]
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
# mobc-redis = { git = "https://github.com/insertish/mobc", rev = "8b880bb59f2ba80b4c7bc40c649c113d8857a186" }
|
redis23 = { package = "redis", version = "0.23.3", git = "https://github.com/revoltchat/redis-rs", rev = "523b2937367e17bd0073722bf6e23d06042cb4e4" }
|
||||||
redis22 = { package = "redis", version = "0.22.3", git = "https://github.com/revoltchat/redis-rs", rev = "1a41faf356fd21aebba71cea7eb7eb2653e5f0ef" }
|
#authifier = { package = "authifier", version = "1.0.10", path = "../authifier/crates/authifier" }
|
||||||
redis23 = { package = "redis", version = "0.23.1", git = "https://github.com/revoltchat/redis-rs", rev = "f8ca28ab85da59d2ccde526b4d2fb390eff5a5f9" }
|
#rocket_authifier = { package = "rocket_authifier", version = "1.0.10", path = "../authifier/crates/rocket_authifier" }
|
||||||
# authifier = { package = "authifier", version = "1.0.8", path = "../authifier/crates/authifier" }
|
|
||||||
# rocket_authifier = { package = "rocket_authifier", version = "1.0.8", path = "../authifier/crates/rocket_authifier" }
|
# I'm 99% sure this is overloading the GitHub worker
|
||||||
|
# hence builds have been failing since, let's just
|
||||||
|
# disable it for now. In the future, we could use this
|
||||||
|
# if we were rolling our own CI (that is now).
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
|||||||
+7
-1
@@ -1,5 +1,5 @@
|
|||||||
# Build Stage
|
# Build Stage
|
||||||
FROM --platform="${BUILDPLATFORM}" rust:1.70.0-slim
|
FROM --platform="${BUILDPLATFORM}" rust:1.86.0-slim-bookworm
|
||||||
USER 0:0
|
USER 0:0
|
||||||
WORKDIR /home/rust/src
|
WORKDIR /home/rust/src
|
||||||
|
|
||||||
@@ -21,10 +21,16 @@ COPY crates/bonfire/Cargo.toml ./crates/bonfire/
|
|||||||
COPY crates/delta/Cargo.toml ./crates/delta/
|
COPY crates/delta/Cargo.toml ./crates/delta/
|
||||||
COPY crates/core/config/Cargo.toml ./crates/core/config/
|
COPY crates/core/config/Cargo.toml ./crates/core/config/
|
||||||
COPY crates/core/database/Cargo.toml ./crates/core/database/
|
COPY crates/core/database/Cargo.toml ./crates/core/database/
|
||||||
|
COPY crates/core/files/Cargo.toml ./crates/core/files/
|
||||||
COPY crates/core/models/Cargo.toml ./crates/core/models/
|
COPY crates/core/models/Cargo.toml ./crates/core/models/
|
||||||
|
COPY crates/core/parser/Cargo.toml ./crates/core/parser/
|
||||||
COPY crates/core/permissions/Cargo.toml ./crates/core/permissions/
|
COPY crates/core/permissions/Cargo.toml ./crates/core/permissions/
|
||||||
COPY crates/core/presence/Cargo.toml ./crates/core/presence/
|
COPY crates/core/presence/Cargo.toml ./crates/core/presence/
|
||||||
COPY crates/core/result/Cargo.toml ./crates/core/result/
|
COPY crates/core/result/Cargo.toml ./crates/core/result/
|
||||||
|
COPY crates/services/autumn/Cargo.toml ./crates/services/autumn/
|
||||||
|
COPY crates/services/january/Cargo.toml ./crates/services/january/
|
||||||
|
COPY crates/daemons/crond/Cargo.toml ./crates/daemons/crond/
|
||||||
|
COPY crates/daemons/pushd/Cargo.toml ./crates/daemons/pushd/
|
||||||
RUN sh /tmp/build-image-layer.sh deps
|
RUN sh /tmp/build-image-layer.sh deps
|
||||||
|
|
||||||
# Build all apps
|
# Build all apps
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Build Stage
|
# Build Stage
|
||||||
FROM rust:1.70.0-slim
|
FROM rust:1.86.0-slim-bookworm
|
||||||
USER 0:0
|
USER 0:0
|
||||||
WORKDIR /home/rust/src
|
WORKDIR /home/rust/src
|
||||||
|
|
||||||
@@ -17,10 +17,16 @@ COPY crates/bonfire/Cargo.toml ./crates/bonfire/
|
|||||||
COPY crates/delta/Cargo.toml ./crates/delta/
|
COPY crates/delta/Cargo.toml ./crates/delta/
|
||||||
COPY crates/core/config/Cargo.toml ./crates/core/config/
|
COPY crates/core/config/Cargo.toml ./crates/core/config/
|
||||||
COPY crates/core/database/Cargo.toml ./crates/core/database/
|
COPY crates/core/database/Cargo.toml ./crates/core/database/
|
||||||
|
COPY crates/core/files/Cargo.toml ./crates/core/files/
|
||||||
COPY crates/core/models/Cargo.toml ./crates/core/models/
|
COPY crates/core/models/Cargo.toml ./crates/core/models/
|
||||||
|
COPY crates/core/parser/Cargo.toml ./crates/core/parser/
|
||||||
COPY crates/core/permissions/Cargo.toml ./crates/core/permissions/
|
COPY crates/core/permissions/Cargo.toml ./crates/core/permissions/
|
||||||
COPY crates/core/presence/Cargo.toml ./crates/core/presence/
|
COPY crates/core/presence/Cargo.toml ./crates/core/presence/
|
||||||
COPY crates/core/result/Cargo.toml ./crates/core/result/
|
COPY crates/core/result/Cargo.toml ./crates/core/result/
|
||||||
|
COPY crates/services/autumn/Cargo.toml ./crates/services/autumn/
|
||||||
|
COPY crates/services/january/Cargo.toml ./crates/services/january/
|
||||||
|
COPY crates/daemons/crond/Cargo.toml ./crates/daemons/crond/
|
||||||
|
COPY crates/daemons/pushd/Cargo.toml ./crates/daemons/pushd/
|
||||||
RUN sh /tmp/build-image-layer.sh deps
|
RUN sh /tmp/build-image-layer.sh deps
|
||||||
|
|
||||||
# Build all apps
|
# Build all apps
|
||||||
|
|||||||
@@ -1,26 +1,47 @@
|
|||||||
# Revolt Backend
|
<div align="center">
|
||||||
|
<h1>
|
||||||
|
Revolt Backend
|
||||||
|
|
||||||
|
[](https://github.com/revoltchat/backend/stargazers)
|
||||||
|
[](https://github.com/revoltchat/backend/network/members)
|
||||||
|
[](https://github.com/revoltchat/backend/pulls)
|
||||||
|
[](https://github.com/revoltchat/backend/issues)
|
||||||
|
[](https://github.com/revoltchat/backend/graphs/contributors)
|
||||||
|
[](https://github.com/revoltchat/backend/blob/main/LICENSE)
|
||||||
|
</h1>
|
||||||
|
The services and libraries that power the Revolt service.<br/>
|
||||||
|
<br/>
|
||||||
|
|
||||||
This is a monorepo for the Revolt backend.
|
| Crate | Path | Description | |
|
||||||
|
| ------------------ | -------------------------------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| `core/config` | [crates/core/config](crates/core/config) | Core: Configuration |     |
|
||||||
|
| `core/database` | [crates/core/database](crates/core/database) | Core: Database Implementation |     |
|
||||||
|
| `core/files` | [crates/core/files](crates/core/files) | Core: S3 and encryption subroutines |     |
|
||||||
|
| `core/models` | [crates/core/models](crates/core/models) | Core: API Models |     |
|
||||||
|
| `core/permissions` | [crates/core/permissions](crates/core/permissions) | Core: Permission Logic |     |
|
||||||
|
| `core/presence` | [crates/core/presence](crates/core/presence) | Core: User Presence |     |
|
||||||
|
| `core/result` | [crates/core/result](crates/core/result) | Core: Result and Error types |     |
|
||||||
|
| `delta` | [crates/delta](crates/delta) | REST API server |  |
|
||||||
|
| `bonfire` | [crates/bonfire](crates/bonfire) | WebSocket events server |  |
|
||||||
|
| `services/january` | [crates/services/january](crates/services/january) | Proxy server |  |
|
||||||
|
| `services/autumn` | [crates/services/autumn](crates/services/autumn) | File server |  |
|
||||||
|
| `daemons/crond` | [crates/daemons/crond](crates/daemons/crond) | Timed data clean up daemon server |  |
|
||||||
|
| `daemons/pushd` | [crates/daemons/pushd](crates/daemons/pushd) | Push notification daemon server |  |
|
||||||
|
|
||||||
| Crate | Path | Description |
|
</div>
|
||||||
| ------------------ | -------------------------------------------------- | ----------------------------- |
|
<br/>
|
||||||
| `core/config` | [crates/core/config](crates/core/config) | Core: Configuration |
|
|
||||||
| `core/database` | [crates/core/database](crates/core/database) | Core: Database Implementation |
|
|
||||||
| `core/models` | [crates/core/models](crates/core/models) | Core: API Models |
|
|
||||||
| `core/permissions` | [crates/core/permissions](crates/core/permissions) | Core: Permission Logic |
|
|
||||||
| `core/presence` | [crates/core/presence](crates/core/presence) | Core: User Presence |
|
|
||||||
| `core/result` | [crates/core/result](crates/core/result) | Core: Result and Error types |
|
|
||||||
| `delta` | [crates/delta](crates/delta) | REST API server |
|
|
||||||
| `bonfire` | [crates/bonfire](crates/bonfire) | WebSocket events server |
|
|
||||||
|
|
||||||
Note: `january`, `autumn`, and `vortex` are yet to be moved into this monorepo.
|
|
||||||
|
|
||||||
## Minimum Supported Rust Version
|
## Minimum Supported Rust Version
|
||||||
|
|
||||||
Rust 1.70 or higher.
|
Rust 1.86.0 or higher.
|
||||||
|
|
||||||
|
> [!CAUTION]
|
||||||
|
> The events server has a significant performance regression between Rust 1.77.2 and 1.78.0 onwards, see [issue #341](https://github.com/revoltchat/backend/issues/341). This is currently solved by build time options but we are looking for a proper fix.
|
||||||
|
|
||||||
## Development Guide
|
## Development Guide
|
||||||
|
|
||||||
|
Before contributing, make yourself familiar with [our contribution guidelines](https://developers.revolt.chat/contrib.html) and the [technical documentation for this project](https://revoltchat.github.io/backend/).
|
||||||
|
|
||||||
Before getting started, you'll want to install:
|
Before getting started, you'll want to install:
|
||||||
|
|
||||||
- Rust toolchain (rustup recommended)
|
- Rust toolchain (rustup recommended)
|
||||||
@@ -31,6 +52,21 @@ Before getting started, you'll want to install:
|
|||||||
> A **default.nix** is available for Nix users!
|
> A **default.nix** is available for Nix users!
|
||||||
> Just run `nix-shell` and continue.
|
> Just run `nix-shell` and continue.
|
||||||
|
|
||||||
|
As a heads-up, the development environment uses the following ports:
|
||||||
|
|
||||||
|
| Service | Port |
|
||||||
|
| ------------------------- | :------------: |
|
||||||
|
| MongoDB | 27017 |
|
||||||
|
| Redis | 6379 |
|
||||||
|
| MinIO | 14009 |
|
||||||
|
| Maildev | 14025<br>14080 |
|
||||||
|
| Revolt Web App | 14701 |
|
||||||
|
| RabbitMQ | 5672<br>15672 |
|
||||||
|
| `crates/delta` | 14702 |
|
||||||
|
| `crates/bonfire` | 14703 |
|
||||||
|
| `crates/services/autumn` | 14704 |
|
||||||
|
| `crates/services/january` | 14705 |
|
||||||
|
|
||||||
Now you can clone and build the project:
|
Now you can clone and build the project:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -39,34 +75,54 @@ cd revolt-backend
|
|||||||
cargo build
|
cargo build
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to run the API and event servers:
|
A default configuration `Revolt.toml` is present in this project that is suited for development.
|
||||||
|
|
||||||
```bash
|
If you'd like to change anything, create a `Revolt.overrides.toml` file and specify relevant variables.
|
||||||
# create environment file (will be deprecated in future)
|
|
||||||
cp .env.example .env
|
|
||||||
|
|
||||||
# (optionally) copy the default configuration file
|
> [!TIP]
|
||||||
cp crates/core/config/Revolt.toml Revolt.toml
|
> Use Sentry to catch unexpected service errors:
|
||||||
# configure as necessary...
|
>
|
||||||
```
|
> ```toml
|
||||||
|
> # Revolt.overrides.toml
|
||||||
|
> [sentry]
|
||||||
|
> api = "https://abc@your.sentry/1"
|
||||||
|
> events = "https://abc@your.sentry/1"
|
||||||
|
> files = "https://abc@your.sentry/1"
|
||||||
|
> proxy = "https://abc@your.sentry/1"
|
||||||
|
> ```
|
||||||
|
|
||||||
You may want to copy the following configuration:
|
> [!TIP]
|
||||||
|
> If you have port conflicts on common services, you can try the following:
|
||||||
```toml
|
>
|
||||||
# Revolt.toml
|
> ```yaml
|
||||||
[database]
|
> # compose.override.yml
|
||||||
mongodb = "mongodb://localhost"
|
> services:
|
||||||
redis = "redis://localhost"
|
> redis:
|
||||||
|
> ports: !override
|
||||||
[hosts]
|
> - "14079:6379"
|
||||||
app = "http://local.revolt.chat"
|
>
|
||||||
api = "http://local.revolt.chat:8000"
|
> database:
|
||||||
events = "ws://local.revolt.chat:9000"
|
> ports: !override
|
||||||
autumn = "http://local.revolt.chat:3000"
|
> - "14017:27017"
|
||||||
january = "http://local.revolt.chat:7000"
|
>
|
||||||
voso_legacy = ""
|
> rabbit:
|
||||||
voso_legacy_ws = ""
|
> ports: !override
|
||||||
```
|
> - "14072:5672"
|
||||||
|
> - "14672:15672"
|
||||||
|
> ```
|
||||||
|
>
|
||||||
|
> And corresponding Revolt configuration:
|
||||||
|
>
|
||||||
|
> ```toml
|
||||||
|
> # Revolt.overrides.toml
|
||||||
|
> # and Revolt.test-overrides.toml
|
||||||
|
> [database]
|
||||||
|
> mongodb = "mongodb://127.0.0.1:14017"
|
||||||
|
> redis = "redis://127.0.0.1:14079/"
|
||||||
|
>
|
||||||
|
> [rabbit]
|
||||||
|
> port = 14072
|
||||||
|
> ```
|
||||||
|
|
||||||
Then continue:
|
Then continue:
|
||||||
|
|
||||||
@@ -74,13 +130,23 @@ Then continue:
|
|||||||
# start other necessary services
|
# start other necessary services
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
|
|
||||||
|
# run everything together
|
||||||
|
./scripts/start.sh
|
||||||
|
# .. or individually
|
||||||
# run the API server
|
# run the API server
|
||||||
cargo run --bin revolt-delta
|
cargo run --bin revolt-delta
|
||||||
# run the events server
|
# run the events server
|
||||||
cargo run --bin revolt-bonfire
|
cargo run --bin revolt-bonfire
|
||||||
|
# run the file server
|
||||||
|
cargo run --bin revolt-autumn
|
||||||
|
# run the proxy server
|
||||||
|
cargo run --bin revolt-january
|
||||||
|
# run the push daemon (not usually needed in regular development)
|
||||||
|
cargo run --bin revolt-pushd
|
||||||
|
|
||||||
# hint:
|
# hint:
|
||||||
# mold -run <cargo build, cargo run, etc...>
|
# mold -run <cargo build, cargo run, etc...>
|
||||||
|
# mold -run ./scripts/start.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
You can start a web client by doing the following:
|
You can start a web client by doing the following:
|
||||||
@@ -94,10 +160,13 @@ git clone --recursive https://github.com/revoltchat/revite
|
|||||||
cd revite
|
cd revite
|
||||||
yarn
|
yarn
|
||||||
yarn build:deps
|
yarn build:deps
|
||||||
yarn dev --port 3001
|
echo "VITE_API_URL=http://local.revolt.chat:14702" > .env.local
|
||||||
|
yarn dev --port 14701
|
||||||
```
|
```
|
||||||
|
|
||||||
Then go to https://local.revolt.chat:3001
|
Then go to http://local.revolt.chat:14701 to create an account/login.
|
||||||
|
|
||||||
|
When signing up, go to http://localhost:14080 to find confirmation/password reset emails.
|
||||||
|
|
||||||
## Deployment Guide
|
## Deployment Guide
|
||||||
|
|
||||||
@@ -141,7 +210,7 @@ Now run tests for whichever database:
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
TEST_DB=REFERENCE cargo nextest run
|
TEST_DB=REFERENCE cargo nextest run
|
||||||
TEST_DB=MONGOBD cargo nextest run
|
TEST_DB=MONGODB cargo nextest run
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
# ⚠️ This configuration is intended for development environment.
|
||||||
|
# If you'd like to override anything, create a Revolt.overrides.toml
|
||||||
|
|
||||||
|
[database]
|
||||||
|
# MongoDB connection URL
|
||||||
|
# Defaults to the container name specified in self-hosted
|
||||||
|
mongodb = "mongodb://127.0.0.1:27017"
|
||||||
|
# Redis connection URL
|
||||||
|
# Defaults to the container name specified in self-hosted
|
||||||
|
redis = "redis://127.0.0.1:6379/"
|
||||||
|
|
||||||
|
[rabbit]
|
||||||
|
host = "127.0.0.1"
|
||||||
|
|
||||||
|
[hosts]
|
||||||
|
# Web locations of various services
|
||||||
|
# Defaults assume all services are reverse-proxied
|
||||||
|
# See https://github.com/revoltchat/self-hosted/blob/master/Caddyfile
|
||||||
|
#
|
||||||
|
# Remember to change these to https/wss where appropriate in production!
|
||||||
|
app = "http://local.revolt.chat:14701"
|
||||||
|
api = "http://local.revolt.chat:14702"
|
||||||
|
events = "ws://local.revolt.chat:14703"
|
||||||
|
autumn = "http://local.revolt.chat:14704"
|
||||||
|
january = "http://local.revolt.chat:14705"
|
||||||
|
voso_legacy = ""
|
||||||
|
voso_legacy_ws = ""
|
||||||
|
|
||||||
|
[api]
|
||||||
|
|
||||||
|
[api.smtp]
|
||||||
|
# Email server configuration for verification
|
||||||
|
# Defaults to no email verification (host field is empty)
|
||||||
|
host = "localhost"
|
||||||
|
username = "smtp"
|
||||||
|
password = "smtp"
|
||||||
|
from_address = "development@revolt.chat"
|
||||||
|
reply_to = "support@revolt.chat"
|
||||||
|
port = 14025
|
||||||
|
use_tls = false
|
||||||
|
use_starttls = false
|
||||||
|
|
||||||
|
[files.s3]
|
||||||
|
# S3 protocol endpoint
|
||||||
|
endpoint = "http://127.0.0.1:14009"
|
||||||
|
# S3 region name
|
||||||
|
region = "minio"
|
||||||
|
# S3 protocol key ID
|
||||||
|
access_key_id = "minioautumn"
|
||||||
|
# S3 protocol access key
|
||||||
|
secret_access_key = "minioautumn"
|
||||||
|
# Bucket to upload to by default
|
||||||
|
default_bucket = "revolt-uploads"
|
||||||
+66
@@ -0,0 +1,66 @@
|
|||||||
|
services:
|
||||||
|
# Redis
|
||||||
|
redis:
|
||||||
|
image: eqalpha/keydb
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
|
|
||||||
|
# MongoDB
|
||||||
|
database:
|
||||||
|
image: mongo
|
||||||
|
ports:
|
||||||
|
- "27017:27017"
|
||||||
|
volumes:
|
||||||
|
- ./.data/db:/data/db
|
||||||
|
|
||||||
|
# MinIO
|
||||||
|
minio:
|
||||||
|
image: minio/minio
|
||||||
|
command: server /data
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: minioautumn
|
||||||
|
MINIO_ROOT_PASSWORD: minioautumn
|
||||||
|
volumes:
|
||||||
|
- ./.data/minio:/data
|
||||||
|
ports:
|
||||||
|
- "14009:9000"
|
||||||
|
- "14010:9001"
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
# Create buckets for minio.
|
||||||
|
createbuckets:
|
||||||
|
image: minio/mc
|
||||||
|
depends_on:
|
||||||
|
- minio
|
||||||
|
entrypoint: >
|
||||||
|
/bin/sh -c "while ! /usr/bin/mc ready minio; do
|
||||||
|
/usr/bin/mc alias set minio http://minio:9000 minioautumn minioautumn;
|
||||||
|
echo 'Waiting minio...' && sleep 1;
|
||||||
|
done; /usr/bin/mc mb minio/revolt-uploads; exit 0;"
|
||||||
|
|
||||||
|
# Rabbit
|
||||||
|
rabbit:
|
||||||
|
image: rabbitmq:3-management
|
||||||
|
environment:
|
||||||
|
RABBITMQ_DEFAULT_USER: rabbituser
|
||||||
|
RABBITMQ_DEFAULT_PASS: rabbitpass
|
||||||
|
volumes:
|
||||||
|
- ./.data/rabbit:/var/lib/rabbitmq
|
||||||
|
#- ./rabbit_plugins:/opt/rabbitmq/plugins/
|
||||||
|
#- ./rabbit_enabled_plugins:/etc/rabbitmq/enabled_plugins
|
||||||
|
# uncomment this if you need to enable other plugins
|
||||||
|
ports:
|
||||||
|
- "5672:5672"
|
||||||
|
- "15672:15672" # management UI, for development
|
||||||
|
|
||||||
|
# Mock SMTP server
|
||||||
|
maildev:
|
||||||
|
image: soulteary/maildev
|
||||||
|
ports:
|
||||||
|
- "14025:25"
|
||||||
|
- "14080:8080"
|
||||||
|
environment:
|
||||||
|
MAILDEV_SMTP_PORT: 25
|
||||||
|
MAILDEV_WEB_PORT: 8080
|
||||||
|
MAILDEV_INCOMING_USER: smtp
|
||||||
|
MAILDEV_INCOMING_PASS: smtp
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt-bonfire"
|
name = "revolt-bonfire"
|
||||||
version = "0.7.4"
|
version = "0.8.8"
|
||||||
license = "AGPL-3.0-or-later"
|
license = "AGPL-3.0-or-later"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
@@ -14,6 +14,8 @@ lru = "0.7.6"
|
|||||||
ulid = "0.5.0"
|
ulid = "0.5.0"
|
||||||
once_cell = "1.9.0"
|
once_cell = "1.9.0"
|
||||||
redis-kiss = "0.1.4"
|
redis-kiss = "0.1.4"
|
||||||
|
lru_time_cache = "0.11.11"
|
||||||
|
async-channel = "2.3.1"
|
||||||
|
|
||||||
# parsing
|
# parsing
|
||||||
querystring = "1.1.0"
|
querystring = "1.1.0"
|
||||||
@@ -34,12 +36,12 @@ async-std = { version = "1.8.0", features = [
|
|||||||
] }
|
] }
|
||||||
|
|
||||||
# core
|
# core
|
||||||
authifier = { version = "1.0.8" }
|
authifier = { version = "1.0.15" }
|
||||||
revolt-result = { path = "../core/result" }
|
revolt-result = { path = "../core/result" }
|
||||||
revolt-models = { path = "../core/models" }
|
revolt-models = { path = "../core/models" }
|
||||||
revolt-config = { path = "../core/config" }
|
revolt-config = { path = "../core/config" }
|
||||||
revolt-database = { path = "../core/database" }
|
revolt-database = { path = "../core/database" }
|
||||||
revolt-permissions = { version = "0.7.4", path = "../core/permissions" }
|
revolt-permissions = { version = "0.8.8", path = "../core/permissions" }
|
||||||
revolt-presence = { path = "../core/presence", features = ["redis-is-patched"] }
|
revolt-presence = { path = "../core/presence", features = ["redis-is-patched"] }
|
||||||
|
|
||||||
# redis
|
# redis
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
# Build Stage
|
# Build Stage
|
||||||
FROM ghcr.io/revoltchat/base:latest AS builder
|
FROM ghcr.io/revoltchat/base:latest AS builder
|
||||||
|
FROM debian:12 AS debian
|
||||||
|
|
||||||
# Bundle Stage
|
# Bundle Stage
|
||||||
FROM debian:bullseye-slim
|
FROM gcr.io/distroless/cc-debian12:nonroot
|
||||||
RUN apt-get update && \
|
|
||||||
apt-get install -y ca-certificates && \
|
|
||||||
apt-get clean
|
|
||||||
COPY --from=builder /home/rust/src/target/release/revolt-bonfire ./
|
COPY --from=builder /home/rust/src/target/release/revolt-bonfire ./
|
||||||
EXPOSE 9000
|
COPY --from=debian /usr/bin/uname /usr/bin/uname
|
||||||
|
|
||||||
|
EXPOSE 14703
|
||||||
|
USER nonroot
|
||||||
CMD ["./revolt-bonfire"]
|
CMD ["./revolt-bonfire"]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use async_tungstenite::tungstenite::{handshake, Message};
|
use async_tungstenite::tungstenite::{handshake, Message};
|
||||||
use futures::channel::oneshot::Sender;
|
use futures::channel::oneshot::Sender;
|
||||||
|
use revolt_database::events::client::ReadyPayloadFields;
|
||||||
use revolt_result::{create_error, Result};
|
use revolt_result::{create_error, Result};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -83,6 +84,17 @@ impl ProtocolConfiguration {
|
|||||||
pub fn get_protocol_format(&self) -> &ProtocolFormat {
|
pub fn get_protocol_format(&self) -> &ProtocolFormat {
|
||||||
&self.format
|
&self.format
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get ready payload fields
|
||||||
|
pub fn get_ready_payload_fields(&self) -> Vec<ReadyPayloadFields> {
|
||||||
|
vec![
|
||||||
|
ReadyPayloadFields::Users,
|
||||||
|
ReadyPayloadFields::Servers,
|
||||||
|
ReadyPayloadFields::Channels,
|
||||||
|
ReadyPayloadFields::Members,
|
||||||
|
ReadyPayloadFields::Emoji,
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Object holding one side of a channel for receiving the parsed information
|
/// Object holding one side of a channel for receiving the parsed information
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use futures::future::join_all;
|
||||||
use revolt_database::{
|
use revolt_database::{
|
||||||
events::client::EventV1, util::permissions::DatabasePermissionQuery, Channel, Database, Member,
|
events::client::{EventV1, ReadyPayloadFields},
|
||||||
MemberCompositeKey, Presence, RelationshipStatus,
|
util::permissions::DatabasePermissionQuery,
|
||||||
|
Channel, Database, Member, MemberCompositeKey, Presence, RelationshipStatus,
|
||||||
};
|
};
|
||||||
use revolt_models::v0;
|
use revolt_models::v0;
|
||||||
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
||||||
@@ -90,8 +92,25 @@ impl Cache {
|
|||||||
/// State Manager
|
/// State Manager
|
||||||
impl State {
|
impl State {
|
||||||
/// Generate a Ready packet for the current user
|
/// Generate a Ready packet for the current user
|
||||||
pub async fn generate_ready_payload(&mut self, db: &Database) -> Result<EventV1> {
|
pub async fn generate_ready_payload(
|
||||||
|
&mut self,
|
||||||
|
db: &Database,
|
||||||
|
fields: Vec<ReadyPayloadFields>,
|
||||||
|
) -> Result<EventV1> {
|
||||||
let user = self.clone_user();
|
let user = self.clone_user();
|
||||||
|
self.cache.is_bot = user.bot.is_some();
|
||||||
|
|
||||||
|
// Fetch pending policy changes.
|
||||||
|
let policy_changes = if user.bot.is_some() {
|
||||||
|
vec![]
|
||||||
|
} else {
|
||||||
|
db.fetch_policy_changes()
|
||||||
|
.await?
|
||||||
|
.into_iter()
|
||||||
|
.filter(|policy| policy.created_time > user.last_acknowledged_policy_change)
|
||||||
|
.map(Into::into)
|
||||||
|
.collect()
|
||||||
|
};
|
||||||
|
|
||||||
// Find all relationships to the user.
|
// Find all relationships to the user.
|
||||||
let mut user_ids: HashSet<String> = user
|
let mut user_ids: HashSet<String> = user
|
||||||
@@ -149,14 +168,36 @@ impl State {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Fetch customisations.
|
// Fetch customisations.
|
||||||
let emojis = db
|
let emojis = if fields.contains(&ReadyPayloadFields::Emoji) {
|
||||||
.fetch_emoji_by_parent_ids(
|
Some(
|
||||||
&servers
|
db.fetch_emoji_by_parent_ids(
|
||||||
.iter()
|
&servers
|
||||||
.map(|x| x.id.to_string())
|
.iter()
|
||||||
.collect::<Vec<String>>(),
|
.map(|x| x.id.to_string())
|
||||||
|
.collect::<Vec<String>>(),
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
)
|
)
|
||||||
.await?;
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fetch user settings
|
||||||
|
let user_settings = if let Some(ReadyPayloadFields::UserSettings(keys)) = fields
|
||||||
|
.iter()
|
||||||
|
.find(|e| matches!(e, ReadyPayloadFields::UserSettings(_)))
|
||||||
|
{
|
||||||
|
Some(db.fetch_user_settings(&user.id, keys).await?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fetch channel unreads
|
||||||
|
let channel_unreads = if fields.contains(&ReadyPayloadFields::ChannelUnreads) {
|
||||||
|
Some(db.fetch_unreads(&user.id).await?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
// Copy data into local state cache.
|
// Copy data into local state cache.
|
||||||
self.cache.users = users.iter().cloned().map(|x| (x.id.clone(), x)).collect();
|
self.cache.users = users.iter().cloned().map(|x| (x.id.clone(), x)).collect();
|
||||||
@@ -170,16 +211,14 @@ impl State {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Make all users appear from our perspective.
|
// Make all users appear from our perspective.
|
||||||
let mut users: Vec<v0::User> = users
|
let mut users: Vec<v0::User> = join_all(users.into_iter().map(|other_user| async {
|
||||||
.into_iter()
|
let is_online = online_ids.contains(&other_user.id);
|
||||||
.map(|other_user| {
|
other_user.into_known(&user, is_online).await
|
||||||
let is_online = online_ids.contains(&other_user.id);
|
}))
|
||||||
other_user.into_known(&user, is_online)
|
.await;
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Make sure we see our own user correctly.
|
// Make sure we see our own user correctly.
|
||||||
users.push(user.into_self().await);
|
users.push(user.into_self(true).await);
|
||||||
|
|
||||||
// Set subscription state internally.
|
// Set subscription state internally.
|
||||||
self.reset_state().await;
|
self.reset_state().await;
|
||||||
@@ -191,6 +230,10 @@ impl State {
|
|||||||
|
|
||||||
for server in &servers {
|
for server in &servers {
|
||||||
self.insert_subscription(server.id.clone()).await;
|
self.insert_subscription(server.id.clone()).await;
|
||||||
|
|
||||||
|
if self.cache.is_bot {
|
||||||
|
self.insert_subscription(format!("{}u", server.id)).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for channel in &channels {
|
for channel in &channels {
|
||||||
@@ -198,11 +241,32 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(EventV1::Ready {
|
Ok(EventV1::Ready {
|
||||||
users,
|
users: if fields.contains(&ReadyPayloadFields::Users) {
|
||||||
servers: servers.into_iter().map(Into::into).collect(),
|
Some(users)
|
||||||
channels: channels.into_iter().map(Into::into).collect(),
|
} else {
|
||||||
members: members.into_iter().map(Into::into).collect(),
|
None
|
||||||
emojis: emojis.into_iter().map(Into::into).collect(),
|
},
|
||||||
|
servers: if fields.contains(&ReadyPayloadFields::Servers) {
|
||||||
|
Some(servers.into_iter().map(Into::into).collect())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
channels: if fields.contains(&ReadyPayloadFields::Channels) {
|
||||||
|
Some(channels.into_iter().map(Into::into).collect())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
members: if fields.contains(&ReadyPayloadFields::Members) {
|
||||||
|
Some(members.into_iter().map(Into::into).collect())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
emojis: emojis.map(|vec| vec.into_iter().map(Into::into).collect()),
|
||||||
|
|
||||||
|
user_settings,
|
||||||
|
channel_unreads: channel_unreads.map(|vec| vec.into_iter().map(Into::into).collect()),
|
||||||
|
|
||||||
|
policy_changes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -397,6 +461,11 @@ impl State {
|
|||||||
emojis: _,
|
emojis: _,
|
||||||
} => {
|
} => {
|
||||||
self.insert_subscription(id.clone()).await;
|
self.insert_subscription(id.clone()).await;
|
||||||
|
|
||||||
|
if self.cache.is_bot {
|
||||||
|
self.insert_subscription(format!("{}u", id)).await;
|
||||||
|
}
|
||||||
|
|
||||||
self.cache.servers.insert(id.clone(), server.clone().into());
|
self.cache.servers.insert(id.clone(), server.clone().into());
|
||||||
let member = Member {
|
let member = Member {
|
||||||
id: MemberCompositeKey {
|
id: MemberCompositeKey {
|
||||||
@@ -433,7 +502,7 @@ impl State {
|
|||||||
EventV1::ServerMemberJoin { .. } => {
|
EventV1::ServerMemberJoin { .. } => {
|
||||||
// We will always receive ServerCreate when joining a new server.
|
// We will always receive ServerCreate when joining a new server.
|
||||||
}
|
}
|
||||||
EventV1::ServerMemberLeave { id, user } => {
|
EventV1::ServerMemberLeave { id, user, .. } => {
|
||||||
if user == &self.cache.user_id {
|
if user == &self.cache.user_id {
|
||||||
self.remove_subscription(id).await;
|
self.remove_subscription(id).await;
|
||||||
|
|
||||||
@@ -530,6 +599,20 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EventV1::Message(message) => {
|
||||||
|
// Since Message events are fanned out to many clients,
|
||||||
|
// we must reconstruct the relationship value at this end.
|
||||||
|
if let Some(user) = &mut message.user {
|
||||||
|
user.relationship = self
|
||||||
|
.cache
|
||||||
|
.users
|
||||||
|
.get(&self.cache.user_id)
|
||||||
|
.expect("missing self?")
|
||||||
|
.relationship_with(&message.author)
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use async_std::sync::RwLock;
|
use async_std::sync::{Mutex, RwLock};
|
||||||
use lru::LruCache;
|
use lru::LruCache;
|
||||||
|
use lru_time_cache::{LruCache as LruTimeCache, TimedEntry};
|
||||||
use revolt_database::{Channel, Member, Server, User};
|
use revolt_database::{Channel, Member, Server, User};
|
||||||
|
|
||||||
/// Enumeration representing some change in subscriptions
|
/// Enumeration representing some change in subscriptions
|
||||||
@@ -34,6 +36,7 @@ pub enum SubscriptionStateChange {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
pub user_id: String,
|
pub user_id: String,
|
||||||
|
pub is_bot: bool,
|
||||||
|
|
||||||
pub users: HashMap<String, User>,
|
pub users: HashMap<String, User>,
|
||||||
pub channels: HashMap<String, Channel>,
|
pub channels: HashMap<String, Channel>,
|
||||||
@@ -47,6 +50,7 @@ impl Default for Cache {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Cache {
|
Cache {
|
||||||
user_id: Default::default(),
|
user_id: Default::default(),
|
||||||
|
is_bot: false,
|
||||||
|
|
||||||
users: Default::default(),
|
users: Default::default(),
|
||||||
channels: Default::default(),
|
channels: Default::default(),
|
||||||
@@ -65,7 +69,9 @@ pub struct State {
|
|||||||
pub session_id: String,
|
pub session_id: String,
|
||||||
pub private_topic: String,
|
pub private_topic: String,
|
||||||
pub state: SubscriptionStateChange,
|
pub state: SubscriptionStateChange,
|
||||||
|
|
||||||
pub subscribed: Arc<RwLock<HashSet<String>>>,
|
pub subscribed: Arc<RwLock<HashSet<String>>>,
|
||||||
|
pub active_servers: Arc<Mutex<LruTimeCache<String, ()>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@@ -86,6 +92,10 @@ impl State {
|
|||||||
State {
|
State {
|
||||||
cache,
|
cache,
|
||||||
subscribed: Arc::new(RwLock::new(subscribed)),
|
subscribed: Arc::new(RwLock::new(subscribed)),
|
||||||
|
active_servers: Arc::new(Mutex::new(LruTimeCache::with_expiry_duration_and_capacity(
|
||||||
|
Duration::from_secs(900),
|
||||||
|
5,
|
||||||
|
))),
|
||||||
session_id,
|
session_id,
|
||||||
private_topic,
|
private_topic,
|
||||||
state: SubscriptionStateChange::Reset,
|
state: SubscriptionStateChange::Reset,
|
||||||
@@ -94,6 +104,39 @@ impl State {
|
|||||||
|
|
||||||
/// Apply currently queued state
|
/// Apply currently queued state
|
||||||
pub async fn apply_state(&mut self) -> SubscriptionStateChange {
|
pub async fn apply_state(&mut self) -> SubscriptionStateChange {
|
||||||
|
// Check if we need to change subscriptions to member event topics
|
||||||
|
if !self.cache.is_bot {
|
||||||
|
enum Server {
|
||||||
|
Subscribe(String),
|
||||||
|
Unsubscribe(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
let active_server_changes: Vec<Server> = {
|
||||||
|
let mut active_servers = self.active_servers.lock().await;
|
||||||
|
active_servers
|
||||||
|
.notify_iter()
|
||||||
|
.map(|e| match e {
|
||||||
|
TimedEntry::Valid(k, _) => Server::Subscribe(format!("{}u", k)),
|
||||||
|
TimedEntry::Expired(k, _) => Server::Unsubscribe(format!("{}u", k)),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
// It is bad practice to open more than one Mutex at once and could
|
||||||
|
// lead to a deadlock, so instead we choose to collect the changes.
|
||||||
|
};
|
||||||
|
|
||||||
|
for entry in active_server_changes {
|
||||||
|
match entry {
|
||||||
|
Server::Subscribe(k) => {
|
||||||
|
self.insert_subscription(k).await;
|
||||||
|
}
|
||||||
|
Server::Unsubscribe(k) => {
|
||||||
|
self.remove_subscription(&k).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush changes to subscriptions
|
||||||
let state = std::mem::replace(&mut self.state, SubscriptionStateChange::None);
|
let state = std::mem::replace(&mut self.state, SubscriptionStateChange::None);
|
||||||
let mut subscribed = self.subscribed.write().await;
|
let mut subscribed = self.subscribed.write().await;
|
||||||
if let SubscriptionStateChange::Change { add, remove } = &state {
|
if let SubscriptionStateChange::Change { add, remove } = &state {
|
||||||
|
|||||||
@@ -19,11 +19,14 @@ async fn main() {
|
|||||||
database::connect().await;
|
database::connect().await;
|
||||||
|
|
||||||
// Clean up the current region information.
|
// Clean up the current region information.
|
||||||
clear_region(None).await;
|
let no_clear_region = env::var("NO_CLEAR_PRESENCE").unwrap_or_else(|_| "0".into()) == "1";
|
||||||
|
if !no_clear_region {
|
||||||
|
clear_region(None).await;
|
||||||
|
}
|
||||||
|
|
||||||
// Setup a TCP listener to accept WebSocket connections on.
|
// Setup a TCP listener to accept WebSocket connections on.
|
||||||
// By default, we bind to port 9000 on all interfaces.
|
// By default, we bind to port 14703 on all interfaces.
|
||||||
let bind = env::var("HOST").unwrap_or_else(|_| "0.0.0.0:9000".into());
|
let bind = env::var("HOST").unwrap_or_else(|_| "0.0.0.0:14703".into());
|
||||||
info!("Listening on host {bind}");
|
info!("Listening on host {bind}");
|
||||||
let try_socket = TcpListener::bind(bind).await;
|
let try_socket = TcpListener::bind(bind).await;
|
||||||
let listener = try_socket.expect("Failed to bind");
|
let listener = try_socket.expect("Failed to bind");
|
||||||
|
|||||||
+318
-138
@@ -3,18 +3,21 @@ use std::{collections::HashSet, net::SocketAddr, sync::Arc};
|
|||||||
use async_tungstenite::WebSocketStream;
|
use async_tungstenite::WebSocketStream;
|
||||||
use authifier::AuthifierEvent;
|
use authifier::AuthifierEvent;
|
||||||
use fred::{
|
use fred::{
|
||||||
|
error::RedisErrorKind,
|
||||||
interfaces::{ClientLike, EventInterface, PubsubInterface},
|
interfaces::{ClientLike, EventInterface, PubsubInterface},
|
||||||
types::RedisConfig,
|
types::RedisConfig,
|
||||||
};
|
};
|
||||||
use futures::{
|
use futures::{
|
||||||
channel::oneshot,
|
channel::oneshot,
|
||||||
pin_mut, select,
|
join, pin_mut, select,
|
||||||
stream::{SplitSink, SplitStream},
|
stream::{SplitSink, SplitStream},
|
||||||
FutureExt, SinkExt, StreamExt, TryStreamExt,
|
FutureExt, SinkExt, StreamExt, TryStreamExt,
|
||||||
};
|
};
|
||||||
use redis_kiss::{PayloadType, REDIS_PAYLOAD_TYPE, REDIS_URI};
|
use redis_kiss::{PayloadType, REDIS_PAYLOAD_TYPE, REDIS_URI};
|
||||||
|
use revolt_config::report_internal_error;
|
||||||
use revolt_database::{
|
use revolt_database::{
|
||||||
events::{client::EventV1, server::ClientMessage},
|
events::{client::EventV1, server::ClientMessage},
|
||||||
|
iso8601_timestamp::Timestamp,
|
||||||
Database, User, UserHint,
|
Database, User, UserHint,
|
||||||
};
|
};
|
||||||
use revolt_presence::{create_session, delete_session};
|
use revolt_presence::{create_session, delete_session};
|
||||||
@@ -22,8 +25,10 @@ use revolt_presence::{create_session, delete_session};
|
|||||||
use async_std::{
|
use async_std::{
|
||||||
net::TcpStream,
|
net::TcpStream,
|
||||||
sync::{Mutex, RwLock},
|
sync::{Mutex, RwLock},
|
||||||
|
task::spawn,
|
||||||
};
|
};
|
||||||
use revolt_result::create_error;
|
use revolt_result::create_error;
|
||||||
|
use sentry::Level;
|
||||||
|
|
||||||
use crate::config::{ProtocolConfiguration, WebsocketHandshakeCallback};
|
use crate::config::{ProtocolConfiguration, WebsocketHandshakeCallback};
|
||||||
use crate::events::state::{State, SubscriptionStateChange};
|
use crate::events::state::{State, SubscriptionStateChange};
|
||||||
@@ -52,6 +57,7 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
|
|||||||
let Ok(mut config) = receiver.await else {
|
let Ok(mut config) = receiver.await else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"User {addr:?} provided protocol configuration (version = {}, format = {:?})",
|
"User {addr:?} provided protocol configuration (version = {}, format = {:?})",
|
||||||
config.get_protocol_version(),
|
config.get_protocol_version(),
|
||||||
@@ -63,10 +69,8 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
|
|||||||
|
|
||||||
// If the user has not provided authentication, request information.
|
// If the user has not provided authentication, request information.
|
||||||
if config.get_session_token().is_none() {
|
if config.get_session_token().is_none() {
|
||||||
while let Ok(message) = read.try_next().await {
|
while let Ok(Some(message)) = read.try_next().await {
|
||||||
if let Ok(ClientMessage::Authenticate { token }) =
|
if let Ok(ClientMessage::Authenticate { token }) = config.decode(&message) {
|
||||||
config.decode(message.as_ref().unwrap())
|
|
||||||
{
|
|
||||||
config.set_session_token(token);
|
config.set_session_token(token);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -75,39 +79,52 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
|
|||||||
|
|
||||||
// Try to authenticate the user.
|
// Try to authenticate the user.
|
||||||
let Some(token) = config.get_session_token().as_ref() else {
|
let Some(token) = config.get_session_token().as_ref() else {
|
||||||
write.send(config.encode(&create_error!(InvalidSession))).await.ok();
|
write
|
||||||
|
.send(config.encode(&EventV1::Error {
|
||||||
|
data: create_error!(InvalidSession),
|
||||||
|
}))
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let (user, session_id) = match User::from_token(db, token, UserHint::Any).await {
|
let (user, session_id) = match User::from_token(db, token, UserHint::Any).await {
|
||||||
Ok(user) => user,
|
Ok(user) => user,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
write.send(config.encode(&err)).await.ok();
|
write
|
||||||
|
.send(config.encode(&EventV1::Error { data: err }))
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("User {addr:?} authenticated as @{}", user.username);
|
info!("User {addr:?} authenticated as @{}", user.username);
|
||||||
|
|
||||||
|
db.update_session_last_seen(&session_id, Timestamp::now_utc())
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
|
||||||
// Create local state.
|
// Create local state.
|
||||||
let mut state = State::from(user, session_id);
|
let mut state = State::from(user, session_id);
|
||||||
let user_id = state.cache.user_id.clone();
|
let user_id = state.cache.user_id.clone();
|
||||||
|
|
||||||
// Notify socket we have authenticated.
|
// Notify socket we have authenticated.
|
||||||
if write
|
if report_internal_error!(write.send(config.encode(&EventV1::Authenticated)).await).is_err() {
|
||||||
.send(config.encode(&EventV1::Authenticated))
|
|
||||||
.await
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download required data to local cache and send Ready payload.
|
// Download required data to local cache and send Ready payload.
|
||||||
let Ok(ready_payload) = state.generate_ready_payload(db).await else {
|
let ready_payload = match report_internal_error!(
|
||||||
return;
|
state
|
||||||
|
.generate_ready_payload(db, config.get_ready_payload_fields())
|
||||||
|
.await
|
||||||
|
) {
|
||||||
|
Ok(ready_payload) => ready_payload,
|
||||||
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if write.send(config.encode(&ready_payload)).await.is_err() {
|
if report_internal_error!(write.send(config.encode(&ready_payload)).await).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,22 +137,44 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
// Setup channels and mutexes
|
||||||
let write = Mutex::new(write);
|
let write = Mutex::new(write);
|
||||||
let subscribed = state.subscribed.clone();
|
let subscribed = state.subscribed.clone();
|
||||||
|
let active_servers = state.active_servers.clone();
|
||||||
|
let (topic_signal_s, topic_signal_r) = async_channel::unbounded();
|
||||||
|
|
||||||
|
// TODO: this needs to be rewritten
|
||||||
|
// Create channels through which the tasks can signal to each other they need to clean up
|
||||||
|
let (kill_signal_1_s, kill_signal_1_r) = async_channel::bounded(1);
|
||||||
|
let (kill_signal_2_s, kill_signal_2_r) = async_channel::bounded(1);
|
||||||
|
|
||||||
// Create a PubSub connection to poll on.
|
// Create a PubSub connection to poll on.
|
||||||
let listener = listener(db, &mut state, addr, &config, &write).fuse();
|
let listener = listener_with_kill_signal(
|
||||||
// Read from WebSocket stream.
|
db,
|
||||||
let worker = worker(addr, subscribed, user_id.clone(), &config, read, &write).fuse();
|
&mut state,
|
||||||
|
addr,
|
||||||
// Pin both tasks.
|
&config,
|
||||||
pin_mut!(listener, worker);
|
topic_signal_r,
|
||||||
|
kill_signal_1_r,
|
||||||
// Wait for either disconnect or for listener to die.
|
&write,
|
||||||
select!(
|
kill_signal_2_s,
|
||||||
() = listener => {},
|
|
||||||
() = worker => {}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Read from WebSocket stream.
|
||||||
|
let worker = worker_with_kill_signal(
|
||||||
|
addr,
|
||||||
|
subscribed,
|
||||||
|
active_servers,
|
||||||
|
user_id.clone(),
|
||||||
|
&config,
|
||||||
|
topic_signal_s,
|
||||||
|
kill_signal_2_r,
|
||||||
|
read,
|
||||||
|
&write,
|
||||||
|
kill_signal_1_s,
|
||||||
|
);
|
||||||
|
|
||||||
|
join!(listener, worker);
|
||||||
}
|
}
|
||||||
// Clean up presence session.
|
// Clean up presence session.
|
||||||
let last_session = delete_session(&user_id, session_id).await;
|
let last_session = delete_session(&user_id, session_id).await;
|
||||||
@@ -146,31 +185,79 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
async fn listener_with_kill_signal(
|
||||||
|
db: &'static Database,
|
||||||
|
state: &mut State,
|
||||||
|
addr: SocketAddr,
|
||||||
|
config: &ProtocolConfiguration,
|
||||||
|
topic_signal_r: async_channel::Receiver<()>,
|
||||||
|
kill_signal_r: async_channel::Receiver<()>,
|
||||||
|
write: &Mutex<WsWriter>,
|
||||||
|
kill_signal_s: async_channel::Sender<()>,
|
||||||
|
) {
|
||||||
|
listener(
|
||||||
|
db,
|
||||||
|
state,
|
||||||
|
addr,
|
||||||
|
config,
|
||||||
|
topic_signal_r,
|
||||||
|
kill_signal_r,
|
||||||
|
write,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
kill_signal_s.send(()).await.ok();
|
||||||
|
}
|
||||||
|
|
||||||
async fn listener(
|
async fn listener(
|
||||||
db: &'static Database,
|
db: &'static Database,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
config: &ProtocolConfiguration,
|
config: &ProtocolConfiguration,
|
||||||
|
topic_signal_r: async_channel::Receiver<()>,
|
||||||
|
kill_signal_r: async_channel::Receiver<()>,
|
||||||
write: &Mutex<WsWriter>,
|
write: &Mutex<WsWriter>,
|
||||||
) {
|
) {
|
||||||
let redis_config = RedisConfig::from_url(&REDIS_URI).unwrap();
|
let redis_config = RedisConfig::from_url(&REDIS_URI).unwrap();
|
||||||
let Ok(subscriber) = fred::types::Builder::from_config(redis_config).build_subscriber_client()
|
let subscriber = match report_internal_error!(
|
||||||
else {
|
fred::types::Builder::from_config(redis_config).build_subscriber_client()
|
||||||
return;
|
) {
|
||||||
|
Ok(subscriber) => subscriber,
|
||||||
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
if subscriber.init().await.is_err() {
|
|
||||||
|
if report_internal_error!(subscriber.init().await).is_err() {
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// Handle Redis connection dropping
|
||||||
|
let (clean_up_s, clean_up_r) = async_channel::bounded(1);
|
||||||
|
let clean_up_s = Arc::new(Mutex::new(clean_up_s));
|
||||||
|
subscriber.on_error(move |err| {
|
||||||
|
if let RedisErrorKind::Canceled = err.kind() {
|
||||||
|
let clean_up_s = clean_up_s.clone();
|
||||||
|
spawn(async move {
|
||||||
|
clean_up_s.lock().await.send(()).await.ok();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
let mut message_rx = subscriber.message_rx();
|
let mut message_rx = subscriber.message_rx();
|
||||||
loop {
|
'out: loop {
|
||||||
// Check for state changes for subscriptions.
|
// Check for state changes for subscriptions.
|
||||||
match state.apply_state().await {
|
match state.apply_state().await {
|
||||||
SubscriptionStateChange::Reset => {
|
SubscriptionStateChange::Reset => {
|
||||||
subscriber.unsubscribe_all().await.unwrap();
|
if report_internal_error!(subscriber.unsubscribe_all().await).is_err() {
|
||||||
|
break 'out;
|
||||||
|
}
|
||||||
|
|
||||||
let subscribed = state.subscribed.read().await;
|
let subscribed = state.subscribed.read().await;
|
||||||
for id in subscribed.iter() {
|
for id in subscribed.iter() {
|
||||||
subscriber.subscribe(id).await.unwrap();
|
if report_internal_error!(subscriber.subscribe(id).await).is_err() {
|
||||||
|
break 'out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
@@ -181,149 +268,242 @@ async fn listener(
|
|||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
info!("{addr:?} unsubscribing from {id}");
|
info!("{addr:?} unsubscribing from {id}");
|
||||||
|
|
||||||
subscriber.unsubscribe(id).await.unwrap();
|
if report_internal_error!(subscriber.unsubscribe(id).await).is_err() {
|
||||||
|
break 'out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for id in add {
|
for id in add {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
info!("{addr:?} subscribing to {id}");
|
info!("{addr:?} subscribing to {id}");
|
||||||
|
|
||||||
subscriber.subscribe(id).await.unwrap();
|
if report_internal_error!(subscriber.subscribe(id).await).is_err() {
|
||||||
|
break 'out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SubscriptionStateChange::None => {}
|
SubscriptionStateChange::None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle incoming events.
|
let t1 = message_rx.recv().fuse();
|
||||||
let Ok(message) = message_rx.recv().await.map_err(|e| {
|
let t2 = topic_signal_r.recv().fuse();
|
||||||
warn!("Error while consuming pub/sub messages: {e:?}");
|
let t3 = kill_signal_r.recv().fuse();
|
||||||
sentry::capture_error(&e);
|
let t4 = clean_up_r.recv().fuse();
|
||||||
}) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let event = match *REDIS_PAYLOAD_TYPE {
|
pin_mut!(t1, t2, t3, t4);
|
||||||
PayloadType::Json => message
|
|
||||||
.value
|
|
||||||
.as_str()
|
|
||||||
.and_then(|s| serde_json::from_str::<EventV1>(s.as_ref()).ok()),
|
|
||||||
PayloadType::Msgpack => message
|
|
||||||
.value
|
|
||||||
.as_bytes()
|
|
||||||
.and_then(|b| rmp_serde::from_slice::<EventV1>(b).ok()),
|
|
||||||
PayloadType::Bincode => message
|
|
||||||
.value
|
|
||||||
.as_bytes()
|
|
||||||
.and_then(|b| bincode::deserialize::<EventV1>(b).ok()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(mut event) = event else {
|
select! {
|
||||||
warn!("Failed to deserialise an event for {}!", message.channel);
|
_ = t4 => {
|
||||||
return;
|
break 'out;
|
||||||
};
|
},
|
||||||
|
_ = t3 => {
|
||||||
|
break 'out;
|
||||||
|
},
|
||||||
|
_ = t2 => {},
|
||||||
|
message = t1 => {
|
||||||
|
// Handle incoming events.
|
||||||
|
let message = match report_internal_error!(message) {
|
||||||
|
Ok(message) => message,
|
||||||
|
Err(_) => break 'out
|
||||||
|
};
|
||||||
|
|
||||||
if let EventV1::Auth(auth) = &event {
|
let event = match *REDIS_PAYLOAD_TYPE {
|
||||||
if let AuthifierEvent::DeleteSession { session_id, .. } = auth {
|
PayloadType::Json => message
|
||||||
if &state.session_id == session_id {
|
.value
|
||||||
event = EventV1::Logout;
|
.as_str()
|
||||||
}
|
.and_then(|s| report_internal_error!(serde_json::from_str::<EventV1>(s.as_ref())).ok()),
|
||||||
} else if let AuthifierEvent::DeleteAllSessions {
|
PayloadType::Msgpack => message
|
||||||
exclude_session_id, ..
|
.value
|
||||||
} = auth
|
.as_bytes()
|
||||||
{
|
.and_then(|b| report_internal_error!(rmp_serde::from_slice::<EventV1>(b)).ok()),
|
||||||
if let Some(excluded) = exclude_session_id {
|
PayloadType::Bincode => message
|
||||||
if &state.session_id != excluded {
|
.value
|
||||||
event = EventV1::Logout;
|
.as_bytes()
|
||||||
|
.and_then(|b| report_internal_error!(bincode::deserialize::<EventV1>(b)).ok()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(mut event) = event else {
|
||||||
|
let err = format!(
|
||||||
|
"Failed to deserialise event for {}: `{:?}`",
|
||||||
|
message.channel,
|
||||||
|
message
|
||||||
|
.value
|
||||||
|
);
|
||||||
|
|
||||||
|
error!("{}", err);
|
||||||
|
sentry::capture_message(&err, Level::Error);
|
||||||
|
break 'out;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let EventV1::Auth(auth) = &event {
|
||||||
|
if let AuthifierEvent::DeleteSession { session_id, .. } = auth {
|
||||||
|
if &state.session_id == session_id {
|
||||||
|
event = EventV1::Logout;
|
||||||
|
}
|
||||||
|
} else if let AuthifierEvent::DeleteAllSessions {
|
||||||
|
exclude_session_id, ..
|
||||||
|
} = auth
|
||||||
|
{
|
||||||
|
if let Some(excluded) = exclude_session_id {
|
||||||
|
if &state.session_id != excluded {
|
||||||
|
event = EventV1::Logout;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
event = EventV1::Logout;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
event = EventV1::Logout;
|
let should_send = state.handle_incoming_event_v1(db, &mut event).await;
|
||||||
|
if !should_send {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = write.lock().await.send(config.encode(&event)).await;
|
||||||
|
if let Err(e) = result {
|
||||||
|
use async_tungstenite::tungstenite::Error;
|
||||||
|
if !matches!(e, Error::AlreadyClosed | Error::ConnectionClosed) {
|
||||||
|
let err = format!("Error while sending an event to {addr:?}: {e:?}");
|
||||||
|
warn!("{}", err);
|
||||||
|
sentry::capture_message(&err, Level::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
break 'out;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let EventV1::Logout = event {
|
||||||
|
info!("User {addr:?} received log out event!");
|
||||||
|
break 'out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let should_send = state.handle_incoming_event_v1(db, &mut event).await;
|
|
||||||
if !should_send {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = write.lock().await.send(config.encode(&event)).await;
|
|
||||||
if let Err(e) = result {
|
|
||||||
use async_tungstenite::tungstenite::Error;
|
|
||||||
if !matches!(e, Error::AlreadyClosed | Error::ConnectionClosed) {
|
|
||||||
warn!("Error while sending an event to {addr:?}: {e:?}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let EventV1::Logout = event {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
report_internal_error!(subscriber.quit().await).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
async fn worker_with_kill_signal(
|
||||||
|
addr: SocketAddr,
|
||||||
|
subscribed: Arc<RwLock<HashSet<String>>>,
|
||||||
|
active_servers: Arc<Mutex<lru_time_cache::LruCache<String, ()>>>,
|
||||||
|
user_id: String,
|
||||||
|
config: &ProtocolConfiguration,
|
||||||
|
topic_signal_s: async_channel::Sender<()>,
|
||||||
|
kill_signal_r: async_channel::Receiver<()>,
|
||||||
|
read: WsReader,
|
||||||
|
write: &Mutex<WsWriter>,
|
||||||
|
kill_signal_s: async_channel::Sender<()>,
|
||||||
|
) {
|
||||||
|
worker(
|
||||||
|
addr,
|
||||||
|
subscribed,
|
||||||
|
active_servers,
|
||||||
|
user_id,
|
||||||
|
config,
|
||||||
|
topic_signal_s,
|
||||||
|
kill_signal_r,
|
||||||
|
read,
|
||||||
|
write,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
kill_signal_s.send(()).await.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
async fn worker(
|
async fn worker(
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
subscribed: Arc<RwLock<HashSet<String>>>,
|
subscribed: Arc<RwLock<HashSet<String>>>,
|
||||||
|
active_servers: Arc<Mutex<lru_time_cache::LruCache<String, ()>>>,
|
||||||
user_id: String,
|
user_id: String,
|
||||||
config: &ProtocolConfiguration,
|
config: &ProtocolConfiguration,
|
||||||
|
topic_signal_s: async_channel::Sender<()>,
|
||||||
|
kill_signal_r: async_channel::Receiver<()>,
|
||||||
mut read: WsReader,
|
mut read: WsReader,
|
||||||
write: &Mutex<WsWriter>,
|
write: &Mutex<WsWriter>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
let result = read.try_next().await;
|
let t1 = read.try_next().fuse();
|
||||||
let msg = match result {
|
let t2 = kill_signal_r.recv().fuse();
|
||||||
Ok(Some(msg)) => msg,
|
|
||||||
Ok(None) => return,
|
pin_mut!(t1, t2);
|
||||||
Err(e) => {
|
|
||||||
use async_tungstenite::tungstenite::Error;
|
select! {
|
||||||
if !matches!(e, Error::AlreadyClosed | Error::ConnectionClosed) {
|
_ = t2 => {
|
||||||
warn!("Error while reading an event from {addr:?}: {e:?}");
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
},
|
||||||
};
|
result = t1 => {
|
||||||
|
let msg = match result {
|
||||||
|
Ok(Some(msg)) => msg,
|
||||||
|
Ok(None) => {
|
||||||
|
warn!("Received a None message!");
|
||||||
|
sentry::capture_message("Received a None message!", Level::Warning);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
use async_tungstenite::tungstenite::Error;
|
||||||
|
if !matches!(e, Error::AlreadyClosed | Error::ConnectionClosed) {
|
||||||
|
let err = format!("Error while reading an event from {addr:?}: {e:?}");
|
||||||
|
warn!("{}", err);
|
||||||
|
sentry::capture_message(&err, Level::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
let Ok(payload) = config.decode(&msg) else {
|
return;
|
||||||
continue;
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match payload {
|
let Ok(payload) = config.decode(&msg) else {
|
||||||
ClientMessage::BeginTyping { channel } => {
|
|
||||||
if !subscribed.read().await.contains(&channel) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
};
|
||||||
|
|
||||||
EventV1::ChannelStartTyping {
|
match payload {
|
||||||
id: channel.clone(),
|
ClientMessage::BeginTyping { channel } => {
|
||||||
user: user_id.clone(),
|
if !subscribed.read().await.contains(&channel) {
|
||||||
}
|
continue;
|
||||||
.p(channel.clone())
|
}
|
||||||
.await;
|
|
||||||
}
|
|
||||||
ClientMessage::EndTyping { channel } => {
|
|
||||||
if !subscribed.read().await.contains(&channel) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
EventV1::ChannelStopTyping {
|
EventV1::ChannelStartTyping {
|
||||||
id: channel.clone(),
|
id: channel.clone(),
|
||||||
user: user_id.clone(),
|
user: user_id.clone(),
|
||||||
}
|
}
|
||||||
.p(channel.clone())
|
.p(channel.clone())
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
ClientMessage::Ping { data, responded } => {
|
ClientMessage::EndTyping { channel } => {
|
||||||
if responded.is_none() {
|
if !subscribed.read().await.contains(&channel) {
|
||||||
write
|
continue;
|
||||||
.lock()
|
}
|
||||||
.await
|
|
||||||
.send(config.encode(&EventV1::Pong { data }))
|
EventV1::ChannelStopTyping {
|
||||||
.await
|
id: channel.clone(),
|
||||||
.ok();
|
user: user_id.clone(),
|
||||||
|
}
|
||||||
|
.p(channel.clone())
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
ClientMessage::Subscribe { server_id } => {
|
||||||
|
let mut servers = active_servers.lock().await;
|
||||||
|
let has_item = servers.contains_key(&server_id);
|
||||||
|
servers.insert(server_id, ());
|
||||||
|
|
||||||
|
if !has_item {
|
||||||
|
// Poke the listener to adjust subscriptions
|
||||||
|
topic_signal_s.send(()).await.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ClientMessage::Ping { data, responded } => {
|
||||||
|
if responded.is_none() {
|
||||||
|
write
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.send(config.encode(&EventV1::Pong { data }))
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt-config"
|
name = "revolt-config"
|
||||||
version = "0.7.4"
|
version = "0.8.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
authors = ["Paul Makles <me@insrt.uk>"]
|
authors = ["Paul Makles <me@insrt.uk>"]
|
||||||
@@ -9,12 +9,14 @@ description = "Revolt Backend: Configuration"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
anyhow = ["dep:sentry-anyhow"]
|
||||||
|
report-macros = ["revolt-result"]
|
||||||
|
sentry = ["dep:sentry"]
|
||||||
test = ["async-std"]
|
test = ["async-std"]
|
||||||
default = ["test"]
|
default = ["test", "sentry"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# Utility
|
# Utility
|
||||||
dotenv = "0.15.0"
|
|
||||||
config = "0.13.3"
|
config = "0.13.3"
|
||||||
cached = "0.44.0"
|
cached = "0.44.0"
|
||||||
once_cell = "1.18.0"
|
once_cell = "1.18.0"
|
||||||
@@ -31,4 +33,8 @@ log = "0.4.14"
|
|||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
|
|
||||||
# Sentry
|
# Sentry
|
||||||
sentry = "0.31.5"
|
sentry = { version = "0.31.5", optional = true }
|
||||||
|
sentry-anyhow = { version = "0.38.1", optional = true }
|
||||||
|
|
||||||
|
# Core
|
||||||
|
revolt-result = { version = "0.8.8", path = "../result", optional = true }
|
||||||
|
|||||||
@@ -1,3 +1,12 @@
|
|||||||
[database]
|
[database]
|
||||||
mongodb = "mongodb://localhost"
|
mongodb = "mongodb://localhost"
|
||||||
redis = "redis://localhost/"
|
redis = "redis://localhost/"
|
||||||
|
|
||||||
|
[rabbit]
|
||||||
|
host = "127.0.0.1"
|
||||||
|
port = 5672
|
||||||
|
username = "rabbituser"
|
||||||
|
password = "rabbitpass"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
webhooks_enabled = true
|
||||||
|
|||||||
+226
-18
@@ -1,8 +1,19 @@
|
|||||||
|
production = false
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
|
# MongoDB connection URL
|
||||||
|
# Defaults to the container name specified in self-hosted
|
||||||
mongodb = "mongodb://database"
|
mongodb = "mongodb://database"
|
||||||
|
# Redis connection URL
|
||||||
|
# Defaults to the container name specified in self-hosted
|
||||||
redis = "redis://redis/"
|
redis = "redis://redis/"
|
||||||
|
|
||||||
[hosts]
|
[hosts]
|
||||||
|
# Web locations of various services
|
||||||
|
# Defaults assume all services are reverse-proxied
|
||||||
|
# See https://github.com/revoltchat/self-hosted/blob/master/Caddyfile
|
||||||
|
#
|
||||||
|
# Remember to change these to https/wss where appropriate in production!
|
||||||
app = "http://local.revolt.chat"
|
app = "http://local.revolt.chat"
|
||||||
api = "http://local.revolt.chat/api"
|
api = "http://local.revolt.chat/api"
|
||||||
events = "ws://local.revolt.chat/ws"
|
events = "ws://local.revolt.chat/ws"
|
||||||
@@ -11,61 +22,258 @@ january = "http://local.revolt.chat/january"
|
|||||||
voso_legacy = ""
|
voso_legacy = ""
|
||||||
voso_legacy_ws = ""
|
voso_legacy_ws = ""
|
||||||
|
|
||||||
|
[rabbit]
|
||||||
|
host = "rabbit"
|
||||||
|
port = 5672
|
||||||
|
username = "rabbituser"
|
||||||
|
password = "rabbitpass"
|
||||||
|
|
||||||
[api]
|
[api]
|
||||||
|
|
||||||
[api.registration]
|
[api.registration]
|
||||||
|
# Whether an invite should be required for registration
|
||||||
|
# See https://github.com/revoltchat/self-hosted#making-your-instance-invite-only
|
||||||
invite_only = false
|
invite_only = false
|
||||||
|
|
||||||
[api.smtp]
|
[api.smtp]
|
||||||
|
# Email server configuration for verification
|
||||||
|
# Defaults to no email verification (host field is empty)
|
||||||
host = ""
|
host = ""
|
||||||
username = ""
|
username = ""
|
||||||
password = ""
|
password = ""
|
||||||
from_address = ""
|
from_address = "noreply@example.com"
|
||||||
|
# reply_to = "noreply@example.com"
|
||||||
|
# port = 587
|
||||||
|
# use_tls = true
|
||||||
|
|
||||||
[api.vapid]
|
|
||||||
private_key = "LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUJSUWpyTWxLRnBiVWhsUHpUbERvcEliYk1yeVNrNXpKYzVYVzIxSjJDS3hvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFWnkrQkg2TGJQZ2hEa3pEempXOG0rUXVPM3pCajRXT1phdkR6ZU00c0pqbmFwd1psTFE0WAp1ZDh2TzVodU94QWhMQlU3WWRldVovWHlBdFpWZmNyQi9BPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo"
|
|
||||||
public_key = "BGcvgR-i2z4IQ5Mw841vJvkLjt8wY-FjmWrw83jOLCY52qcGZS0OF7nfLzuYbjsQISwVO2HXrmf18gLWVX3Kwfw="
|
|
||||||
|
|
||||||
[api.fcm]
|
|
||||||
api_key = ""
|
|
||||||
|
|
||||||
[api.security]
|
[api.security]
|
||||||
|
# Authifier Shield API key
|
||||||
authifier_shield_key = ""
|
authifier_shield_key = ""
|
||||||
|
# Legacy voice server management token
|
||||||
voso_legacy_token = ""
|
voso_legacy_token = ""
|
||||||
|
# Whether services are behind the Cloudflare network
|
||||||
trust_cloudflare = false
|
trust_cloudflare = false
|
||||||
|
# easypwned endpoint
|
||||||
|
easypwned = ""
|
||||||
|
|
||||||
[api.security.captcha]
|
[api.security.captcha]
|
||||||
|
# hCaptcha configuration
|
||||||
hcaptcha_key = ""
|
hcaptcha_key = ""
|
||||||
hcaptcha_sitekey = ""
|
hcaptcha_sitekey = ""
|
||||||
|
|
||||||
[api.workers]
|
[api.workers]
|
||||||
|
# Maximum concurrent connections (to proxy server)
|
||||||
max_concurrent_connections = 50
|
max_concurrent_connections = 50
|
||||||
|
|
||||||
|
[api.users]
|
||||||
|
|
||||||
|
|
||||||
|
[pushd]
|
||||||
|
# this changes the names of the queues to not overlap
|
||||||
|
# prod/beta if they happen to be on the same exchange/instance.
|
||||||
|
# Usually they have to be, so that messages sent from one or the other get sent to everyone
|
||||||
|
production = true
|
||||||
|
|
||||||
|
# Changes how many users are processed in each chunk when resolving role/everyone mentions.
|
||||||
|
# Increasing this will resolve mentions faster, but will consume more memory while resolving.
|
||||||
|
mass_mention_chunk_size = 200
|
||||||
|
|
||||||
|
# none of these should need changing
|
||||||
|
exchange = "revolt.notifications"
|
||||||
|
message_queue = "notifications.origin.message"
|
||||||
|
mass_mention_queue = "notifications.origin.mass_mention" # handles messages that contain role or everyone mentions
|
||||||
|
fr_accepted_queue = "notifications.ingest.fr_accepted" # friend request accepted
|
||||||
|
fr_received_queue = "notifications.ingest.fr_received" # friend request received
|
||||||
|
generic_queue = "notifications.ingest.generic" # generic messages (title + body)
|
||||||
|
ack_queue = "notifications.process.ack" # updates badges for apple devices
|
||||||
|
|
||||||
|
|
||||||
|
[pushd.vapid]
|
||||||
|
queue = "notifications.outbound.vapid"
|
||||||
|
private_key = "LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUJSUWpyTWxLRnBiVWhsUHpUbERvcEliYk1yeVNrNXpKYzVYVzIxSjJDS3hvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFWnkrQkg2TGJQZ2hEa3pEempXOG0rUXVPM3pCajRXT1phdkR6ZU00c0pqbmFwd1psTFE0WAp1ZDh2TzVodU94QWhMQlU3WWRldVovWHlBdFpWZmNyQi9BPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo"
|
||||||
|
public_key = "BGcvgR-i2z4IQ5Mw841vJvkLjt8wY-FjmWrw83jOLCY52qcGZS0OF7nfLzuYbjsQISwVO2HXrmf18gLWVX3Kwfw="
|
||||||
|
|
||||||
|
[pushd.fcm]
|
||||||
|
queue = "notifications.outbound.fcm"
|
||||||
|
key_type = ""
|
||||||
|
project_id = ""
|
||||||
|
private_key_id = ""
|
||||||
|
private_key = ""
|
||||||
|
client_email = ""
|
||||||
|
client_id = ""
|
||||||
|
auth_uri = ""
|
||||||
|
token_uri = ""
|
||||||
|
auth_provider_x509_cert_url = ""
|
||||||
|
client_x509_cert_url = ""
|
||||||
|
|
||||||
|
[pushd.apn]
|
||||||
|
sandbox = false
|
||||||
|
queue = "notifications.outbound.apn"
|
||||||
|
pkcs8 = ""
|
||||||
|
key_id = ""
|
||||||
|
team_id = ""
|
||||||
|
|
||||||
|
|
||||||
|
[files]
|
||||||
|
# Encryption key for stored files
|
||||||
|
# Generate your own key using `openssl rand -base64 32`
|
||||||
|
encryption_key = "qcuMA+ssxhMyKaNAKBGFfryfFtUH8NDlamQyDwGW6fU="
|
||||||
|
# Quality used for lossy WebP previews (set to 100 for lossless)
|
||||||
|
webp_quality = 80.0
|
||||||
|
# Mime types that cannot be uploaded or served
|
||||||
|
#
|
||||||
|
# Example for Windows executables and Android installation files:
|
||||||
|
# ["application/vnd.microsoft.portable-executable", "application/vnd.android.package-archive"]
|
||||||
|
blocked_mime_types = []
|
||||||
|
# ClamAV service
|
||||||
|
# hostname:port
|
||||||
|
clamd_host = ""
|
||||||
|
# Mime types that should be virus scanned
|
||||||
|
#
|
||||||
|
# Leave empty to scan all file types
|
||||||
|
scan_mime_types = [
|
||||||
|
"application/vnd.microsoft.portable-executable",
|
||||||
|
"application/vnd.android.package-archive",
|
||||||
|
"application/zip",
|
||||||
|
]
|
||||||
|
|
||||||
|
[files.limit]
|
||||||
|
# Minimum file size (in bytes)
|
||||||
|
min_file_size = 1
|
||||||
|
# Minimum image resolution
|
||||||
|
min_resolution = [1, 1]
|
||||||
|
# Maximum MP of images
|
||||||
|
max_mega_pixels = 40
|
||||||
|
# Maximum pixel side of an image
|
||||||
|
max_pixel_side = 10_000
|
||||||
|
|
||||||
|
[files.preview]
|
||||||
|
# Maximum image resolution
|
||||||
|
attachments = [1280, 1280]
|
||||||
|
avatars = [128, 128]
|
||||||
|
backgrounds = [1280, 720]
|
||||||
|
icons = [128, 128]
|
||||||
|
banners = [480, 480]
|
||||||
|
emojis = [128, 128]
|
||||||
|
|
||||||
|
[files.s3]
|
||||||
|
# Configuration for S3
|
||||||
|
# Defaults included for MinIO + self-hosted setup
|
||||||
|
#
|
||||||
|
# Backblaze B2:
|
||||||
|
# - endpoint is listed on the "Buckets" page
|
||||||
|
# - path_style_buckets is set to true
|
||||||
|
# - region is `eu-central-003` string from endpoint URL
|
||||||
|
# - access_key_id is keyID generated on the "Application Keys" page
|
||||||
|
# - secret_access_key is token generated on the "Application Keys" page
|
||||||
|
# - default_bucket matches the name of the bucket you've created
|
||||||
|
|
||||||
|
# S3 protocol endpoint
|
||||||
|
endpoint = "http://minio:9000"
|
||||||
|
# Whether to use path-style buckets
|
||||||
|
# Generally true, except for MinIO
|
||||||
|
path_style_buckets = false
|
||||||
|
# S3 region name
|
||||||
|
region = "minio"
|
||||||
|
# S3 protocol key ID
|
||||||
|
access_key_id = "minioautumn"
|
||||||
|
# S3 protocol access key
|
||||||
|
secret_access_key = "minioautumn"
|
||||||
|
default_bucket = "revolt-uploads"
|
||||||
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
# Feature gate options
|
||||||
webhooks_enabled = false
|
webhooks_enabled = false
|
||||||
|
# Enable push notifications for mass pings (everyone, online, roles)
|
||||||
|
# When false this will still ping in-client but will not send notifications from pushd
|
||||||
|
mass_mentions_send_notifications = true
|
||||||
|
# Can role/everyone pings be used at all
|
||||||
|
mass_mentions_enabled = true
|
||||||
|
|
||||||
[features.limits]
|
[features.limits]
|
||||||
|
|
||||||
[features.limits.default]
|
[features.limits.global]
|
||||||
group_size = 100
|
group_size = 100
|
||||||
bots = 5
|
|
||||||
message_length = 2000
|
|
||||||
message_embeds = 5
|
message_embeds = 5
|
||||||
message_replies = 5
|
message_replies = 5
|
||||||
message_attachments = 5
|
|
||||||
message_reactions = 20
|
message_reactions = 20
|
||||||
servers = 100
|
|
||||||
server_emoji = 100
|
server_emoji = 100
|
||||||
server_roles = 200
|
server_roles = 200
|
||||||
server_channels = 200
|
server_channels = 200
|
||||||
|
|
||||||
attachment_size = 20000000
|
# How many hours since creation a user is considered new
|
||||||
avatar_size = 4000000
|
new_user_hours = 72
|
||||||
background_size = 6000000
|
|
||||||
icon_size = 2500000
|
# Maximum permissible body size in bytes for uploads
|
||||||
banner_size = 6000000
|
# (should be greater than any one file upload limit)
|
||||||
emoji_size = 500000
|
body_limit_size = 20_000_000
|
||||||
|
|
||||||
|
[features.limits.new_user]
|
||||||
|
# Limits imposed on new users
|
||||||
|
|
||||||
|
# Number of outgoing friend requests permitted at any time
|
||||||
|
outgoing_friend_requests = 5
|
||||||
|
|
||||||
|
# Maximum number of owned bots
|
||||||
|
bots = 2
|
||||||
|
|
||||||
|
# Message content length
|
||||||
|
message_length = 2000
|
||||||
|
|
||||||
|
# Number of attachments that can be included
|
||||||
|
message_attachments = 5
|
||||||
|
|
||||||
|
# Maximum number of servers the user can create/join
|
||||||
|
servers = 50
|
||||||
|
|
||||||
|
[features.limits.new_user.file_upload_size_limit]
|
||||||
|
# Maximum file size limits (in bytes)
|
||||||
|
attachments = 20_000_000
|
||||||
|
avatars = 4_000_000
|
||||||
|
backgrounds = 6_000_000
|
||||||
|
icons = 2_500_000
|
||||||
|
banners = 6_000_000
|
||||||
|
emojis = 500_000
|
||||||
|
|
||||||
|
[features.limits.default]
|
||||||
|
# Limits imposed on users by default
|
||||||
|
|
||||||
|
# Number of outgoing friend requests permitted at any time
|
||||||
|
outgoing_friend_requests = 10
|
||||||
|
|
||||||
|
# Maximum number of owned bots
|
||||||
|
bots = 5
|
||||||
|
|
||||||
|
# Message content length
|
||||||
|
message_length = 2000
|
||||||
|
|
||||||
|
# Number of attachments that can be included
|
||||||
|
message_attachments = 5
|
||||||
|
|
||||||
|
# Maximum number of servers the user can create/join
|
||||||
|
servers = 100
|
||||||
|
|
||||||
|
[features.limits.default.file_upload_size_limit]
|
||||||
|
# Maximum file size limits (in bytes)
|
||||||
|
attachments = 20_000_000
|
||||||
|
avatars = 4_000_000
|
||||||
|
backgrounds = 6_000_000
|
||||||
|
icons = 2_500_000
|
||||||
|
banners = 6_000_000
|
||||||
|
emojis = 500_000
|
||||||
|
|
||||||
|
[features.advanced]
|
||||||
|
# The max amount of messages the rabbitmq provider/db mention adder job will delay for before forcing handling of a channel.
|
||||||
|
# default: 5
|
||||||
|
process_message_delay_limit = 5
|
||||||
|
|
||||||
[sentry]
|
[sentry]
|
||||||
|
# Configuration for Sentry error reporting
|
||||||
api = ""
|
api = ""
|
||||||
events = ""
|
events = ""
|
||||||
|
files = ""
|
||||||
|
proxy = ""
|
||||||
|
pushd = ""
|
||||||
|
crond = ""
|
||||||
|
|||||||
+268
-43
@@ -6,9 +6,66 @@ use futures_locks::RwLock;
|
|||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(feature = "sentry")]
|
||||||
use std::env;
|
pub use sentry::{capture_error, capture_message, Level};
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
|
pub use sentry_anyhow::capture_anyhow;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "report-macros", feature = "sentry"))]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! report_error {
|
||||||
|
( $expr: expr, $error: ident $( $tt:tt )? ) => {
|
||||||
|
$expr
|
||||||
|
.inspect_err(|err| {
|
||||||
|
$crate::capture_message(
|
||||||
|
&format!("{err:?} ({}:{}:{})", file!(), line!(), column!()),
|
||||||
|
$crate::Level::Error,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.map_err(|_| ::revolt_result::create_error!($error))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "report-macros", feature = "sentry"))]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! capture_internal_error {
|
||||||
|
( $expr: expr ) => {
|
||||||
|
$crate::capture_message(
|
||||||
|
&format!("{:?} ({}:{}:{})", $expr, file!(), line!(), column!()),
|
||||||
|
$crate::Level::Error,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "report-macros", feature = "sentry"))]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! report_internal_error {
|
||||||
|
( $expr: expr ) => {
|
||||||
|
$expr
|
||||||
|
.inspect_err(|err| {
|
||||||
|
$crate::capture_message(
|
||||||
|
&format!("{err:?} ({}:{}:{})", file!(), line!(), column!()),
|
||||||
|
$crate::Level::Error,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.map_err(|_| ::revolt_result::create_error!(InternalError))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Paths to search for configuration
|
||||||
|
static CONFIG_SEARCH_PATHS: [&str; 3] = [
|
||||||
|
// current working directory
|
||||||
|
"Revolt.toml",
|
||||||
|
// current working directory - overrides file
|
||||||
|
"Revolt.overrides.toml",
|
||||||
|
// root directory, for Docker containers
|
||||||
|
"/Revolt.toml",
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Path to search for test overrides
|
||||||
|
static TEST_OVERRIDE_PATH: &str = "Revolt.test-overrides.toml";
|
||||||
|
|
||||||
|
/// Configuration builder
|
||||||
static CONFIG_BUILDER: Lazy<RwLock<Config>> = Lazy::new(|| {
|
static CONFIG_BUILDER: Lazy<RwLock<Config>> = Lazy::new(|| {
|
||||||
RwLock::new({
|
RwLock::new({
|
||||||
let mut builder = Config::builder().add_source(File::from_str(
|
let mut builder = Config::builder().add_source(File::from_str(
|
||||||
@@ -21,22 +78,46 @@ static CONFIG_BUILDER: Lazy<RwLock<Config>> = Lazy::new(|| {
|
|||||||
include_str!("../Revolt.test.toml"),
|
include_str!("../Revolt.test.toml"),
|
||||||
FileFormat::Toml,
|
FileFormat::Toml,
|
||||||
));
|
));
|
||||||
} else if std::path::Path::new("Revolt.toml").exists() {
|
|
||||||
builder = builder.add_source(File::new("Revolt.toml", FileFormat::Toml));
|
// recursively search upwards for an overrides file (if there is one)
|
||||||
|
if let Ok(cwd) = std::env::current_dir() {
|
||||||
|
let mut path = Some(cwd.as_path());
|
||||||
|
while let Some(current_path) = path {
|
||||||
|
let target_path = current_path.join(TEST_OVERRIDE_PATH);
|
||||||
|
if target_path.exists() {
|
||||||
|
builder = builder
|
||||||
|
.add_source(File::new(target_path.to_str().unwrap(), FileFormat::Toml));
|
||||||
|
}
|
||||||
|
|
||||||
|
path = current_path.parent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for path in CONFIG_SEARCH_PATHS {
|
||||||
|
if std::path::Path::new(path).exists() {
|
||||||
|
builder = builder.add_source(File::new(path, FileFormat::Toml));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.build().unwrap()
|
builder.build().unwrap()
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://gifbox.me/view/gT5mqxYKCZv-twilight-meow
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
pub mongodb: String,
|
pub mongodb: String,
|
||||||
pub redis: String,
|
pub redis: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct Rabbit {
|
||||||
|
pub host: String,
|
||||||
|
pub port: u16,
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Hosts {
|
pub struct Hosts {
|
||||||
pub app: String,
|
pub app: String,
|
||||||
@@ -62,17 +143,38 @@ pub struct ApiSmtp {
|
|||||||
pub reply_to: Option<String>,
|
pub reply_to: Option<String>,
|
||||||
pub port: Option<i32>,
|
pub port: Option<i32>,
|
||||||
pub use_tls: Option<bool>,
|
pub use_tls: Option<bool>,
|
||||||
|
pub use_starttls: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct ApiVapid {
|
pub struct PushVapid {
|
||||||
|
pub queue: String,
|
||||||
pub private_key: String,
|
pub private_key: String,
|
||||||
pub public_key: String,
|
pub public_key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct ApiFcm {
|
pub struct PushFcm {
|
||||||
pub api_key: String,
|
pub queue: String,
|
||||||
|
pub key_type: String,
|
||||||
|
pub project_id: String,
|
||||||
|
pub private_key_id: String,
|
||||||
|
pub private_key: String,
|
||||||
|
pub client_email: String,
|
||||||
|
pub client_id: String,
|
||||||
|
pub auth_uri: String,
|
||||||
|
pub token_uri: String,
|
||||||
|
pub auth_provider_x509_cert_url: String,
|
||||||
|
pub client_x509_cert_url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct PushApn {
|
||||||
|
pub queue: String,
|
||||||
|
pub sandbox: bool,
|
||||||
|
pub pkcs8: String,
|
||||||
|
pub key_id: String,
|
||||||
|
pub team_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
@@ -87,6 +189,7 @@ pub struct ApiSecurity {
|
|||||||
pub voso_legacy_token: String,
|
pub voso_legacy_token: String,
|
||||||
pub captcha: ApiSecurityCaptcha,
|
pub captcha: ApiSecurityCaptcha,
|
||||||
pub trust_cloudflare: bool,
|
pub trust_cloudflare: bool,
|
||||||
|
pub easypwned: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
@@ -95,85 +198,195 @@ pub struct ApiWorkers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Api {
|
pub struct ApiUsers {
|
||||||
pub registration: ApiRegistration,
|
pub early_adopter_cutoff: Option<u64>,
|
||||||
pub smtp: ApiSmtp,
|
|
||||||
pub vapid: ApiVapid,
|
|
||||||
pub fcm: ApiFcm,
|
|
||||||
pub security: ApiSecurity,
|
|
||||||
pub workers: ApiWorkers,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct FeaturesLimits {
|
pub struct Api {
|
||||||
|
pub registration: ApiRegistration,
|
||||||
|
pub smtp: ApiSmtp,
|
||||||
|
pub security: ApiSecurity,
|
||||||
|
pub workers: ApiWorkers,
|
||||||
|
pub users: ApiUsers,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct Pushd {
|
||||||
|
pub production: bool,
|
||||||
|
pub exchange: String,
|
||||||
|
pub mass_mention_chunk_size: usize,
|
||||||
|
|
||||||
|
// Queues
|
||||||
|
pub message_queue: String,
|
||||||
|
pub mass_mention_queue: String,
|
||||||
|
pub fr_accepted_queue: String,
|
||||||
|
pub fr_received_queue: String,
|
||||||
|
pub generic_queue: String,
|
||||||
|
pub ack_queue: String,
|
||||||
|
|
||||||
|
pub vapid: PushVapid,
|
||||||
|
pub fcm: PushFcm,
|
||||||
|
pub apn: PushApn,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pushd {
|
||||||
|
fn get_routing_key(&self, key: String) -> String {
|
||||||
|
match self.production {
|
||||||
|
true => key + "-prd",
|
||||||
|
false => key + "-tst",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_ack_routing_key(&self) -> String {
|
||||||
|
self.get_routing_key(self.ack_queue.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_message_routing_key(&self) -> String {
|
||||||
|
self.get_routing_key(self.message_queue.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mass_mention_routing_key(&self) -> String {
|
||||||
|
self.get_routing_key(self.mass_mention_queue.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_fr_accepted_routing_key(&self) -> String {
|
||||||
|
self.get_routing_key(self.fr_accepted_queue.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_fr_received_routing_key(&self) -> String {
|
||||||
|
self.get_routing_key(self.fr_received_queue.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_generic_routing_key(&self) -> String {
|
||||||
|
self.get_routing_key(self.generic_queue.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct FilesLimit {
|
||||||
|
pub min_file_size: usize,
|
||||||
|
pub min_resolution: [usize; 2],
|
||||||
|
pub max_mega_pixels: usize,
|
||||||
|
pub max_pixel_side: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct FilesS3 {
|
||||||
|
pub endpoint: String,
|
||||||
|
pub path_style_buckets: bool,
|
||||||
|
pub region: String,
|
||||||
|
pub access_key_id: String,
|
||||||
|
pub secret_access_key: String,
|
||||||
|
pub default_bucket: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct Files {
|
||||||
|
pub encryption_key: String,
|
||||||
|
pub webp_quality: f32,
|
||||||
|
pub blocked_mime_types: Vec<String>,
|
||||||
|
pub clamd_host: String,
|
||||||
|
pub scan_mime_types: Vec<String>,
|
||||||
|
|
||||||
|
pub limit: FilesLimit,
|
||||||
|
pub preview: HashMap<String, [usize; 2]>,
|
||||||
|
pub s3: FilesS3,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct GlobalLimits {
|
||||||
pub group_size: usize,
|
pub group_size: usize,
|
||||||
pub bots: usize,
|
|
||||||
pub message_length: usize,
|
|
||||||
pub message_replies: usize,
|
|
||||||
pub message_attachments: usize,
|
|
||||||
pub message_embeds: usize,
|
pub message_embeds: usize,
|
||||||
|
pub message_replies: usize,
|
||||||
pub message_reactions: usize,
|
pub message_reactions: usize,
|
||||||
pub servers: usize,
|
|
||||||
pub server_emoji: usize,
|
pub server_emoji: usize,
|
||||||
pub server_roles: usize,
|
pub server_roles: usize,
|
||||||
pub server_channels: usize,
|
pub server_channels: usize,
|
||||||
|
|
||||||
pub attachment_size: usize,
|
pub new_user_hours: usize,
|
||||||
pub avatar_size: usize,
|
|
||||||
pub background_size: usize,
|
pub body_limit_size: usize,
|
||||||
pub icon_size: usize,
|
}
|
||||||
pub banner_size: usize,
|
|
||||||
pub emoji_size: usize,
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct FeaturesLimits {
|
||||||
|
pub outgoing_friend_requests: usize,
|
||||||
|
|
||||||
|
pub bots: usize,
|
||||||
|
pub message_length: usize,
|
||||||
|
pub message_attachments: usize,
|
||||||
|
pub servers: usize,
|
||||||
|
|
||||||
|
pub file_upload_size_limit: HashMap<String, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct FeaturesLimitsCollection {
|
pub struct FeaturesLimitsCollection {
|
||||||
|
pub global: GlobalLimits,
|
||||||
|
|
||||||
|
pub new_user: FeaturesLimits,
|
||||||
pub default: FeaturesLimits,
|
pub default: FeaturesLimits,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub roles: HashMap<String, FeaturesLimits>,
|
pub roles: HashMap<String, FeaturesLimits>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct FeaturesAdvanced {
|
||||||
|
#[serde(default)]
|
||||||
|
pub process_message_delay_limit: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for FeaturesAdvanced {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
process_message_delay_limit: 5,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Features {
|
pub struct Features {
|
||||||
pub limits: FeaturesLimitsCollection,
|
pub limits: FeaturesLimitsCollection,
|
||||||
pub webhooks_enabled: bool,
|
pub webhooks_enabled: bool,
|
||||||
|
pub mass_mentions_send_notifications: bool,
|
||||||
|
pub mass_mentions_enabled: bool,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub advanced: FeaturesAdvanced,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Sentry {
|
pub struct Sentry {
|
||||||
pub api: String,
|
pub api: String,
|
||||||
pub events: String,
|
pub events: String,
|
||||||
|
pub files: String,
|
||||||
|
pub proxy: String,
|
||||||
|
pub pushd: String,
|
||||||
|
pub crond: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub database: Database,
|
pub database: Database,
|
||||||
|
pub rabbit: Rabbit,
|
||||||
pub hosts: Hosts,
|
pub hosts: Hosts,
|
||||||
pub api: Api,
|
pub api: Api,
|
||||||
|
pub pushd: Pushd,
|
||||||
|
pub files: Files,
|
||||||
pub features: Features,
|
pub features: Features,
|
||||||
pub sentry: Sentry,
|
pub sentry: Sentry,
|
||||||
|
pub production: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Settings {
|
||||||
pub fn preflight_checks(&self) {
|
pub fn preflight_checks(&self) {
|
||||||
if self.api.smtp.host.is_empty() {
|
if self.api.smtp.host.is_empty() {
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
if !env::var("REVOLT_UNSAFE_NO_EMAIL").map_or(false, |v| v == *"1") {
|
|
||||||
panic!("Running in production without email is not recommended, set REVOLT_UNSAFE_NO_EMAIL=1 to override.");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
log::warn!("No SMTP settings specified! Remember to configure email.");
|
log::warn!("No SMTP settings specified! Remember to configure email.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.api.security.captcha.hcaptcha_key.is_empty() {
|
if self.api.security.captcha.hcaptcha_key.is_empty() {
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
if !env::var("REVOLT_UNSAFE_NO_CAPTCHA").map_or(false, |v| v == *"1") {
|
|
||||||
panic!("Running in production without CAPTCHA is not recommended, set REVOLT_UNSAFE_NO_CAPTCHA=1 to override.");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
log::warn!("No Captcha key specified! Remember to add hCaptcha key.");
|
log::warn!("No Captcha key specified! Remember to add hCaptcha key.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,13 +405,24 @@ pub async fn read() -> Config {
|
|||||||
|
|
||||||
#[cached(time = 30)]
|
#[cached(time = 30)]
|
||||||
pub async fn config() -> Settings {
|
pub async fn config() -> Settings {
|
||||||
read().await.try_deserialize::<Settings>().unwrap()
|
let mut config = read().await.try_deserialize::<Settings>().unwrap();
|
||||||
|
|
||||||
|
// inject REDIS_URI for redis-kiss library
|
||||||
|
if std::env::var("REDIS_URL").is_err() {
|
||||||
|
std::env::set_var("REDIS_URI", config.database.redis.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// auto-detect production nodes
|
||||||
|
if config.hosts.api.contains("https") && config.hosts.api.contains("revolt.chat") {
|
||||||
|
config.production = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configure logging and common Rust variables
|
/// Configure logging and common Rust variables
|
||||||
|
#[cfg(feature = "sentry")]
|
||||||
pub async fn setup_logging(release: &'static str, dsn: String) -> Option<sentry::ClientInitGuard> {
|
pub async fn setup_logging(release: &'static str, dsn: String) -> Option<sentry::ClientInitGuard> {
|
||||||
dotenv::dotenv().ok();
|
|
||||||
|
|
||||||
if std::env::var("RUST_LOG").is_err() {
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
std::env::set_var("RUST_LOG", "info");
|
std::env::set_var("RUST_LOG", "info");
|
||||||
}
|
}
|
||||||
@@ -223,6 +447,7 @@ pub async fn setup_logging(release: &'static str, dsn: String) -> Option<sentry:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "sentry")]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! configure {
|
macro_rules! configure {
|
||||||
($application: ident) => {
|
($application: ident) => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt-database"
|
name = "revolt-database"
|
||||||
version = "0.7.4"
|
version = "0.8.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "AGPL-3.0-or-later"
|
license = "AGPL-3.0-or-later"
|
||||||
authors = ["Paul Makles <me@insrt.uk>"]
|
authors = ["Paul Makles <me@insrt.uk>"]
|
||||||
@@ -10,12 +10,13 @@ description = "Revolt Backend: Database Implementation"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
# Databases
|
# Databases
|
||||||
mongodb = ["dep:mongodb", "bson"]
|
mongodb = ["dep:mongodb", "bson", "authifier/database-mongodb"]
|
||||||
|
|
||||||
# ... Other
|
# ... Other
|
||||||
tasks = ["isahc", "linkify", "url-escape"]
|
tasks = ["isahc", "linkify", "url-escape"]
|
||||||
async-std-runtime = ["async-std"]
|
async-std-runtime = ["async-std", "authifier/async-std-runtime"]
|
||||||
rocket-impl = ["rocket", "schemars", "revolt_okapi", "revolt_rocket_okapi"]
|
rocket-impl = ["rocket", "schemars", "revolt_okapi", "revolt_rocket_okapi", "authifier/rocket_impl"]
|
||||||
|
axum-impl = ["axum"]
|
||||||
redis-is-patched = ["revolt-presence/redis-is-patched"]
|
redis-is-patched = ["revolt-presence/redis-is-patched"]
|
||||||
|
|
||||||
# Default Features
|
# Default Features
|
||||||
@@ -23,16 +24,19 @@ default = ["mongodb", "async-std-runtime", "tasks"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# Core
|
# Core
|
||||||
revolt-config = { version = "0.7.4", path = "../config" }
|
revolt-config = { version = "0.8.8", path = "../config", features = [
|
||||||
revolt-result = { version = "0.7.4", path = "../result" }
|
"report-macros",
|
||||||
revolt-models = { version = "0.7.4", path = "../models", features = [
|
] }
|
||||||
|
revolt-result = { version = "0.8.8", path = "../result" }
|
||||||
|
revolt-models = { version = "0.8.8", path = "../models", features = [
|
||||||
"validator",
|
"validator",
|
||||||
] }
|
] }
|
||||||
revolt-presence = { version = "0.7.4", path = "../presence" }
|
revolt-presence = { version = "0.8.8", path = "../presence" }
|
||||||
revolt-permissions = { version = "0.7.4", path = "../permissions", features = [
|
revolt-permissions = { version = "0.8.8", path = "../permissions", features = [
|
||||||
"serde",
|
"serde",
|
||||||
"bson",
|
"bson",
|
||||||
] }
|
] }
|
||||||
|
revolt-parser = { version = "0.8.8", path = "../parser" }
|
||||||
|
|
||||||
# Utility
|
# Utility
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
@@ -61,7 +65,7 @@ redis-kiss = { version = "0.1.4" }
|
|||||||
|
|
||||||
# Database
|
# Database
|
||||||
bson = { optional = true, version = "2.1.0" }
|
bson = { optional = true, version = "2.1.0" }
|
||||||
mongodb = { optional = true, version = "2.1.0", default-features = false }
|
mongodb = { optional = true, version = "3.1.0" }
|
||||||
|
|
||||||
# Database Migration
|
# Database Migration
|
||||||
unicode-segmentation = "1.10.1"
|
unicode-segmentation = "1.10.1"
|
||||||
@@ -76,17 +80,19 @@ async-recursion = "1.0.4"
|
|||||||
# Async
|
# Async
|
||||||
async-std = { version = "1.8.0", features = ["attributes"], optional = true }
|
async-std = { version = "1.8.0", features = ["attributes"], optional = true }
|
||||||
|
|
||||||
|
# Axum Impl
|
||||||
|
axum = { version = "0.7.5", optional = true }
|
||||||
|
|
||||||
# Rocket Impl
|
# Rocket Impl
|
||||||
schemars = { version = "0.8.8", optional = true }
|
schemars = { version = "0.8.8", optional = true }
|
||||||
rocket = { version = "0.5.0-rc.2", default-features = false, features = [
|
rocket = { version = "0.5.1", default-features = false, features = [
|
||||||
"json",
|
"json",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
revolt_okapi = { version = "0.9.1", optional = true }
|
revolt_okapi = { version = "0.9.1", optional = true }
|
||||||
revolt_rocket_okapi = { version = "0.9.1", optional = true }
|
revolt_rocket_okapi = { version = "0.10.0", optional = true }
|
||||||
|
|
||||||
# Notifications
|
|
||||||
fcm = "0.9.2"
|
|
||||||
web-push = "0.10.0"
|
|
||||||
|
|
||||||
# Authifier
|
# Authifier
|
||||||
authifier = { version = "1.0.8" }
|
authifier = { version = "1.0.15" }
|
||||||
|
|
||||||
|
# RabbitMQ
|
||||||
|
amqprs = { version = "1.7.0" }
|
||||||
|
|||||||
@@ -3,18 +3,21 @@
|
|||||||
"_object_type": "User",
|
"_object_type": "User",
|
||||||
"_id": "__ID:0__",
|
"_id": "__ID:0__",
|
||||||
"username": "Owner",
|
"username": "Owner",
|
||||||
|
"last_acknowledged_policy_change": "2025-06-07T04:04:48+0000",
|
||||||
"discriminator": "0001"
|
"discriminator": "0001"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_object_type": "User",
|
"_object_type": "User",
|
||||||
"_id": "__ID:1__",
|
"_id": "__ID:1__",
|
||||||
"username": "Member",
|
"username": "Member",
|
||||||
|
"last_acknowledged_policy_change": "2025-06-07T04:04:48+0000",
|
||||||
"discriminator": "0001"
|
"discriminator": "0001"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_object_type": "User",
|
"_object_type": "User",
|
||||||
"_id": "__ID:2__",
|
"_id": "__ID:2__",
|
||||||
"username": "Member",
|
"username": "Member",
|
||||||
|
"last_acknowledged_policy_change": "2025-06-07T04:04:48+0000",
|
||||||
"discriminator": "0002"
|
"discriminator": "0002"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -23,6 +26,9 @@
|
|||||||
"channel_type": "Group",
|
"channel_type": "Group",
|
||||||
"name": "My Group",
|
"name": "My Group",
|
||||||
"owner": "__ID:0__",
|
"owner": "__ID:0__",
|
||||||
"recipients": ["__ID:0__", "__ID:1__"]
|
"recipients": [
|
||||||
|
"__ID:0__",
|
||||||
|
"__ID:1__"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -3,18 +3,21 @@
|
|||||||
"_object_type": "User",
|
"_object_type": "User",
|
||||||
"_id": "__ID:0__",
|
"_id": "__ID:0__",
|
||||||
"username": "Owner",
|
"username": "Owner",
|
||||||
|
"last_acknowledged_policy_change": "2025-06-07T04:04:48+0000",
|
||||||
"discriminator": "0001"
|
"discriminator": "0001"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_object_type": "User",
|
"_object_type": "User",
|
||||||
"_id": "__ID:1__",
|
"_id": "__ID:1__",
|
||||||
"username": "Moderator",
|
"username": "Moderator",
|
||||||
|
"last_acknowledged_policy_change": "2025-06-07T04:04:48+0000",
|
||||||
"discriminator": "0001"
|
"discriminator": "0001"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_object_type": "User",
|
"_object_type": "User",
|
||||||
"_id": "__ID:2__",
|
"_id": "__ID:2__",
|
||||||
"username": "User",
|
"username": "User",
|
||||||
|
"last_acknowledged_policy_change": "2025-06-07T04:04:48+0000",
|
||||||
"discriminator": "0001"
|
"discriminator": "0001"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -39,7 +42,9 @@
|
|||||||
"_id": "__ID:4__",
|
"_id": "__ID:4__",
|
||||||
"owner": "__ID:0__",
|
"owner": "__ID:0__",
|
||||||
"name": "Server",
|
"name": "Server",
|
||||||
"channels": ["__ID:3__"],
|
"channels": [
|
||||||
|
"__ID:3__"
|
||||||
|
],
|
||||||
"roles": {
|
"roles": {
|
||||||
"__ID:5__": {
|
"__ID:5__": {
|
||||||
"name": "Moderator",
|
"name": "Moderator",
|
||||||
@@ -47,7 +52,7 @@
|
|||||||
"a": 545270208,
|
"a": 545270208,
|
||||||
"d": 0
|
"d": 0
|
||||||
},
|
},
|
||||||
"rank": 3
|
"rank": 1
|
||||||
},
|
},
|
||||||
"__ID:6__": {
|
"__ID:6__": {
|
||||||
"name": "Owner",
|
"name": "Owner",
|
||||||
@@ -66,7 +71,9 @@
|
|||||||
"user": "__ID:0__",
|
"user": "__ID:0__",
|
||||||
"server": "__ID:4__"
|
"server": "__ID:4__"
|
||||||
},
|
},
|
||||||
"roles": ["__ID:6__"],
|
"roles": [
|
||||||
|
"__ID:6__"
|
||||||
|
],
|
||||||
"joined_at": 1698318340195
|
"joined_at": 1698318340195
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -75,7 +82,9 @@
|
|||||||
"user": "__ID:1__",
|
"user": "__ID:1__",
|
||||||
"server": "__ID:4__"
|
"server": "__ID:4__"
|
||||||
},
|
},
|
||||||
"roles": ["__ID:5__"],
|
"roles": [
|
||||||
|
"__ID:5__"
|
||||||
|
],
|
||||||
"joined_at": 1698318340195
|
"joined_at": 1698318340195
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -86,4 +95,4 @@
|
|||||||
},
|
},
|
||||||
"joined_at": 1698318340195
|
"joined_at": 1698318340195
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -0,0 +1,243 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use crate::events::rabbit::*;
|
||||||
|
use crate::User;
|
||||||
|
use amqprs::channel::BasicPublishArguments;
|
||||||
|
use amqprs::{channel::Channel, connection::Connection, error::Error as AMQPError};
|
||||||
|
use amqprs::{BasicProperties, FieldTable};
|
||||||
|
use revolt_models::v0::PushNotification;
|
||||||
|
use revolt_presence::filter_online;
|
||||||
|
|
||||||
|
use serde_json::to_string;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct AMQP {
|
||||||
|
#[allow(unused)]
|
||||||
|
connection: Connection,
|
||||||
|
channel: Channel,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AMQP {
|
||||||
|
pub fn new(connection: Connection, channel: Channel) -> AMQP {
|
||||||
|
AMQP {
|
||||||
|
connection,
|
||||||
|
channel,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn friend_request_accepted(
|
||||||
|
&self,
|
||||||
|
accepted_request_user: &User,
|
||||||
|
sent_request_user: &User,
|
||||||
|
) -> Result<(), AMQPError> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
let payload = FRAcceptedPayload {
|
||||||
|
accepted_user: accepted_request_user.to_owned(),
|
||||||
|
user: sent_request_user.id.clone(),
|
||||||
|
};
|
||||||
|
let payload = to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Sending friend request accept payload on channel {}: {}",
|
||||||
|
config.pushd.get_fr_accepted_routing_key(),
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
self.channel
|
||||||
|
.basic_publish(
|
||||||
|
BasicProperties::default()
|
||||||
|
.with_content_type("application/json")
|
||||||
|
.with_persistence(true)
|
||||||
|
.finish(),
|
||||||
|
payload.into(),
|
||||||
|
BasicPublishArguments::new(
|
||||||
|
&config.pushd.exchange,
|
||||||
|
&config.pushd.get_fr_accepted_routing_key(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn friend_request_received(
|
||||||
|
&self,
|
||||||
|
received_request_user: &User,
|
||||||
|
sent_request_user: &User,
|
||||||
|
) -> Result<(), AMQPError> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
let payload = FRReceivedPayload {
|
||||||
|
from_user: sent_request_user.to_owned(),
|
||||||
|
user: received_request_user.id.clone(),
|
||||||
|
};
|
||||||
|
let payload = to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Sending friend request received payload on channel {}: {}",
|
||||||
|
config.pushd.get_fr_received_routing_key(),
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
|
||||||
|
self.channel
|
||||||
|
.basic_publish(
|
||||||
|
BasicProperties::default()
|
||||||
|
.with_content_type("application/json")
|
||||||
|
.with_persistence(true)
|
||||||
|
.finish(),
|
||||||
|
payload.into(),
|
||||||
|
BasicPublishArguments::new(
|
||||||
|
&config.pushd.exchange,
|
||||||
|
&config.pushd.get_fr_received_routing_key(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn generic_message(
|
||||||
|
&self,
|
||||||
|
user: &User,
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
icon: Option<String>,
|
||||||
|
) -> Result<(), AMQPError> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
let payload = GenericPayload {
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
icon,
|
||||||
|
user: user.to_owned(),
|
||||||
|
};
|
||||||
|
let payload = to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Sending generic payload on channel {}: {}",
|
||||||
|
config.pushd.get_generic_routing_key(),
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
|
||||||
|
self.channel
|
||||||
|
.basic_publish(
|
||||||
|
BasicProperties::default()
|
||||||
|
.with_content_type("application/json")
|
||||||
|
.with_persistence(true)
|
||||||
|
.finish(),
|
||||||
|
payload.into(),
|
||||||
|
BasicPublishArguments::new(
|
||||||
|
&config.pushd.exchange,
|
||||||
|
&config.pushd.get_generic_routing_key(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn message_sent(
|
||||||
|
&self,
|
||||||
|
recipients: Vec<String>,
|
||||||
|
payload: PushNotification,
|
||||||
|
) -> Result<(), AMQPError> {
|
||||||
|
if recipients.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
|
||||||
|
let online_ids = filter_online(&recipients).await;
|
||||||
|
let recipients = (&recipients.into_iter().collect::<HashSet<String>>() - &online_ids)
|
||||||
|
.into_iter()
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
let payload = MessageSentPayload {
|
||||||
|
notification: payload,
|
||||||
|
users: recipients,
|
||||||
|
};
|
||||||
|
let payload = to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Sending message payload on channel {}: {}",
|
||||||
|
config.pushd.get_message_routing_key(),
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
|
||||||
|
self.channel
|
||||||
|
.basic_publish(
|
||||||
|
BasicProperties::default()
|
||||||
|
.with_content_type("application/json")
|
||||||
|
.with_persistence(true)
|
||||||
|
.finish(),
|
||||||
|
payload.into(),
|
||||||
|
BasicPublishArguments::new(
|
||||||
|
&config.pushd.exchange,
|
||||||
|
&config.pushd.get_message_routing_key(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn mass_mention_message_sent(
|
||||||
|
&self,
|
||||||
|
server_id: String,
|
||||||
|
payload: Vec<PushNotification>,
|
||||||
|
) -> Result<(), AMQPError> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
|
||||||
|
let payload = MassMessageSentPayload {
|
||||||
|
notifications: payload,
|
||||||
|
server_id,
|
||||||
|
};
|
||||||
|
let payload = to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
let routing_key = config.pushd.get_mass_mention_routing_key();
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Sending mass mention payload on channel {}: {}",
|
||||||
|
routing_key, payload
|
||||||
|
);
|
||||||
|
|
||||||
|
self.channel
|
||||||
|
.basic_publish(
|
||||||
|
BasicProperties::default()
|
||||||
|
.with_content_type("application/json")
|
||||||
|
.with_persistence(true)
|
||||||
|
.finish(),
|
||||||
|
payload.into(),
|
||||||
|
BasicPublishArguments::new(&config.pushd.exchange, routing_key.as_str()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn ack_message(
|
||||||
|
&self,
|
||||||
|
user_id: String,
|
||||||
|
channel_id: String,
|
||||||
|
message_id: String,
|
||||||
|
) -> Result<(), AMQPError> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
|
||||||
|
let payload = AckPayload {
|
||||||
|
user_id: user_id.clone(),
|
||||||
|
channel_id: channel_id.clone(),
|
||||||
|
message_id,
|
||||||
|
};
|
||||||
|
let payload = to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Sending ack payload on channel {}: {}",
|
||||||
|
config.pushd.ack_queue, payload
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut headers = FieldTable::new();
|
||||||
|
headers.insert(
|
||||||
|
"x-deduplication-header".try_into().unwrap(),
|
||||||
|
format!("{}-{}", &user_id, &channel_id).into(),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.channel
|
||||||
|
.basic_publish(
|
||||||
|
BasicProperties::default()
|
||||||
|
.with_content_type("application/json")
|
||||||
|
.with_persistence(true)
|
||||||
|
//.with_headers(headers)
|
||||||
|
.finish(),
|
||||||
|
payload.into(),
|
||||||
|
BasicPublishArguments::new(&config.pushd.exchange, &config.pushd.ack_queue),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
#[allow(clippy::module_inception)]
|
||||||
|
pub mod amqp;
|
||||||
@@ -1,9 +1,20 @@
|
|||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
use authifier::config::Captcha;
|
||||||
|
use authifier::config::EmailVerificationConfig;
|
||||||
|
use authifier::config::PasswordScanning;
|
||||||
|
use authifier::config::ResolveIp;
|
||||||
|
use authifier::config::SMTPSettings;
|
||||||
|
use authifier::config::Shield;
|
||||||
|
use authifier::config::Template;
|
||||||
|
use authifier::config::Templates;
|
||||||
|
use authifier::Authifier;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use revolt_config::config;
|
use revolt_config::config;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
pub use self::mongodb::*;
|
pub use self::mongodb::*;
|
||||||
pub use self::reference::*;
|
pub use self::reference::*;
|
||||||
|
|
||||||
@@ -16,8 +27,10 @@ pub enum DatabaseInfo {
|
|||||||
/// Use the mock database
|
/// Use the mock database
|
||||||
Reference,
|
Reference,
|
||||||
/// Connect to MongoDB
|
/// Connect to MongoDB
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
MongoDb { uri: String, database_name: String },
|
MongoDb { uri: String, database_name: String },
|
||||||
/// Use existing MongoDB connection
|
/// Use existing MongoDB connection
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
MongoDbFromClient(::mongodb::Client, String),
|
MongoDbFromClient(::mongodb::Client, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +40,7 @@ pub enum Database {
|
|||||||
/// Mock database
|
/// Mock database
|
||||||
Reference(ReferenceDb),
|
Reference(ReferenceDb),
|
||||||
/// MongoDB database
|
/// MongoDB database
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
MongoDb(MongoDb),
|
MongoDb(MongoDb),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +50,7 @@ impl DatabaseInfo {
|
|||||||
pub async fn connect(self) -> Result<Database, String> {
|
pub async fn connect(self) -> Result<Database, String> {
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
|
|
||||||
Ok(match self {
|
match self {
|
||||||
DatabaseInfo::Auto => {
|
DatabaseInfo::Auto => {
|
||||||
if std::env::var("TEST_DB").is_ok() {
|
if std::env::var("TEST_DB").is_ok() {
|
||||||
DatabaseInfo::Test(format!(
|
DatabaseInfo::Test(format!(
|
||||||
@@ -44,16 +58,20 @@ impl DatabaseInfo {
|
|||||||
rand::thread_rng().gen_range(1_000_000..10_000_000)
|
rand::thread_rng().gen_range(1_000_000..10_000_000)
|
||||||
))
|
))
|
||||||
.connect()
|
.connect()
|
||||||
.await?
|
.await
|
||||||
} else if !config.database.mongodb.is_empty() {
|
} else if !config.database.mongodb.is_empty() {
|
||||||
DatabaseInfo::MongoDb {
|
#[cfg(feature = "mongodb")]
|
||||||
|
return DatabaseInfo::MongoDb {
|
||||||
uri: config.database.mongodb,
|
uri: config.database.mongodb,
|
||||||
database_name: "revolt".to_string(),
|
database_name: "revolt".to_string(),
|
||||||
}
|
}
|
||||||
.connect()
|
.connect()
|
||||||
.await?
|
.await;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "mongodb"))]
|
||||||
|
return Err("MongoDB not enabled.".to_string())
|
||||||
} else {
|
} else {
|
||||||
DatabaseInfo::Reference.connect().await?
|
DatabaseInfo::Reference.connect().await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DatabaseInfo::Test(database_name) => {
|
DatabaseInfo::Test(database_name) => {
|
||||||
@@ -61,29 +79,171 @@ impl DatabaseInfo {
|
|||||||
.expect("`TEST_DB` environment variable should be set to REFERENCE or MONGODB")
|
.expect("`TEST_DB` environment variable should be set to REFERENCE or MONGODB")
|
||||||
.as_str()
|
.as_str()
|
||||||
{
|
{
|
||||||
"REFERENCE" => DatabaseInfo::Reference.connect().await?,
|
"REFERENCE" => DatabaseInfo::Reference.connect().await,
|
||||||
"MONGODB" => {
|
"MONGODB" => {
|
||||||
DatabaseInfo::MongoDb {
|
#[cfg(feature = "mongodb")]
|
||||||
|
return DatabaseInfo::MongoDb {
|
||||||
uri: config.database.mongodb,
|
uri: config.database.mongodb,
|
||||||
database_name,
|
database_name,
|
||||||
}
|
}
|
||||||
.connect()
|
.connect()
|
||||||
.await?
|
.await;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "mongodb"))]
|
||||||
|
return Err("MongoDB not enabled.".to_string())
|
||||||
}
|
}
|
||||||
_ => unreachable!("must specify REFERENCE or MONGODB"),
|
_ => unreachable!("must specify REFERENCE or MONGODB"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DatabaseInfo::Reference => Database::Reference(Default::default()),
|
DatabaseInfo::Reference => Ok(Database::Reference(Default::default())),
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
DatabaseInfo::MongoDb { uri, database_name } => {
|
DatabaseInfo::MongoDb { uri, database_name } => {
|
||||||
let client = ::mongodb::Client::with_uri_str(uri)
|
let client = ::mongodb::Client::with_uri_str(uri)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| "Failed to init db connection.".to_string())?;
|
.map_err(|_| "Failed to init db connection.".to_string())?;
|
||||||
|
|
||||||
Database::MongoDb(MongoDb(client, database_name))
|
Ok(Database::MongoDb(MongoDb(client, database_name)))
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
DatabaseInfo::MongoDbFromClient(client, database_name) => {
|
DatabaseInfo::MongoDbFromClient(client, database_name) => {
|
||||||
Database::MongoDb(MongoDb(client, database_name))
|
Ok(Database::MongoDb(MongoDb(client, database_name)))
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Database {
|
||||||
|
/// Create an Authifier reference
|
||||||
|
pub async fn to_authifier(self) -> Authifier {
|
||||||
|
let config = config().await;
|
||||||
|
|
||||||
|
let mut auth_config = authifier::Config {
|
||||||
|
password_scanning: if config.api.security.easypwned.is_empty() {
|
||||||
|
Default::default()
|
||||||
|
} else {
|
||||||
|
PasswordScanning::EasyPwned {
|
||||||
|
endpoint: config.api.security.easypwned,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
email_verification: if !config.api.smtp.host.is_empty() {
|
||||||
|
EmailVerificationConfig::Enabled {
|
||||||
|
smtp: SMTPSettings {
|
||||||
|
from: config.api.smtp.from_address,
|
||||||
|
host: config.api.smtp.host,
|
||||||
|
username: config.api.smtp.username,
|
||||||
|
password: config.api.smtp.password,
|
||||||
|
reply_to: Some(
|
||||||
|
config
|
||||||
|
.api
|
||||||
|
.smtp
|
||||||
|
.reply_to
|
||||||
|
.unwrap_or("support@revolt.chat".into()),
|
||||||
|
),
|
||||||
|
port: config.api.smtp.port,
|
||||||
|
use_tls: config.api.smtp.use_tls,
|
||||||
|
use_starttls: config.api.smtp.use_starttls,
|
||||||
|
},
|
||||||
|
expiry: Default::default(),
|
||||||
|
templates: if config.production {
|
||||||
|
Templates {
|
||||||
|
verify: Template {
|
||||||
|
title: "Verify your Revolt account.".into(),
|
||||||
|
text: include_str!("../../templates/verify.txt").into(),
|
||||||
|
url: format!("{}/login/verify/", config.hosts.app),
|
||||||
|
html: Some(include_str!("../../templates/verify.html").into()),
|
||||||
|
},
|
||||||
|
reset: Template {
|
||||||
|
title: "Reset your Revolt password.".into(),
|
||||||
|
text: include_str!("../../templates/reset.txt").into(),
|
||||||
|
url: format!("{}/login/reset/", config.hosts.app),
|
||||||
|
html: Some(include_str!("../../templates/reset.html").into()),
|
||||||
|
},
|
||||||
|
reset_existing: Template {
|
||||||
|
title: "You already have a Revolt account, reset your password."
|
||||||
|
.into(),
|
||||||
|
text: include_str!("../../templates/reset-existing.txt").into(),
|
||||||
|
url: format!("{}/login/reset/", config.hosts.app),
|
||||||
|
html: Some(
|
||||||
|
include_str!("../../templates/reset-existing.html").into(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
deletion: Template {
|
||||||
|
title: "Confirm account deletion.".into(),
|
||||||
|
text: include_str!("../../templates/deletion.txt").into(),
|
||||||
|
url: format!("{}/delete/", config.hosts.app),
|
||||||
|
html: Some(include_str!("../../templates/deletion.html").into()),
|
||||||
|
},
|
||||||
|
welcome: None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Templates {
|
||||||
|
verify: Template {
|
||||||
|
title: "Verify your account.".into(),
|
||||||
|
text: include_str!("../../templates/verify.whitelabel.txt").into(),
|
||||||
|
url: format!("{}/login/verify/", config.hosts.app),
|
||||||
|
html: None,
|
||||||
|
},
|
||||||
|
reset: Template {
|
||||||
|
title: "Reset your password.".into(),
|
||||||
|
text: include_str!("../../templates/reset.whitelabel.txt").into(),
|
||||||
|
url: format!("{}/login/reset/", config.hosts.app),
|
||||||
|
html: None,
|
||||||
|
},
|
||||||
|
reset_existing: Template {
|
||||||
|
title: "Reset your password.".into(),
|
||||||
|
text: include_str!("../../templates/reset.whitelabel.txt").into(),
|
||||||
|
url: format!("{}/login/reset/", config.hosts.app),
|
||||||
|
html: None,
|
||||||
|
},
|
||||||
|
deletion: Template {
|
||||||
|
title: "Confirm account deletion.".into(),
|
||||||
|
text: include_str!("../../templates/deletion.whitelabel.txt")
|
||||||
|
.into(),
|
||||||
|
url: format!("{}/delete/", config.hosts.app),
|
||||||
|
html: None,
|
||||||
|
},
|
||||||
|
welcome: None,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
EmailVerificationConfig::Disabled
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
auth_config.invite_only = config.api.registration.invite_only;
|
||||||
|
|
||||||
|
if !config.api.security.captcha.hcaptcha_key.is_empty() {
|
||||||
|
auth_config.captcha = Captcha::HCaptcha {
|
||||||
|
secret: config.api.security.captcha.hcaptcha_key,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if !config.api.security.authifier_shield_key.is_empty() {
|
||||||
|
auth_config.shield = Shield::Enabled {
|
||||||
|
api_key: config.api.security.authifier_shield_key,
|
||||||
|
strict: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.api.security.trust_cloudflare {
|
||||||
|
auth_config.resolve_ip = ResolveIp::Cloudflare;
|
||||||
|
}
|
||||||
|
|
||||||
|
Authifier {
|
||||||
|
database: match self {
|
||||||
|
Database::Reference(_) => Default::default(),
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
Database::MongoDb(MongoDb(client, _)) => authifier::Database::MongoDb(
|
||||||
|
authifier::database::MongoDb(client.database("revolt")),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
config: auth_config,
|
||||||
|
#[cfg(feature = "tasks")]
|
||||||
|
event_channel: Some(crate::tasks::authifier_relay::sender()),
|
||||||
|
#[cfg(not(feature = "tasks"))]
|
||||||
|
event_channel: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ use serde::de::DeserializeOwned;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
database_derived!(
|
database_derived!(
|
||||||
#[cfg(feature = "mongodb")]
|
|
||||||
/// MongoDB implementation
|
/// MongoDB implementation
|
||||||
pub struct MongoDb(pub ::mongodb::Client, pub String);
|
pub struct MongoDb(pub ::mongodb::Client, pub String);
|
||||||
);
|
);
|
||||||
@@ -31,17 +30,17 @@ impl MongoDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get a collection by its name
|
/// Get a collection by its name
|
||||||
pub fn col<T>(&self, collection: &str) -> mongodb::Collection<T> {
|
pub fn col<T: Send + Sync>(&self, collection: &str) -> mongodb::Collection<T> {
|
||||||
self.db().collection(collection)
|
self.db().collection(collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert one document into a collection
|
/// Insert one document into a collection
|
||||||
pub async fn insert_one<T: Serialize>(
|
pub async fn insert_one<T: Serialize + Send + Sync>(
|
||||||
&self,
|
&self,
|
||||||
collection: &'static str,
|
collection: &'static str,
|
||||||
document: T,
|
document: T,
|
||||||
) -> Result<InsertOneResult> {
|
) -> Result<InsertOneResult> {
|
||||||
self.col::<T>(collection).insert_one(document, None).await
|
self.col::<T>(collection).insert_one(document).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Count documents by projection
|
/// Count documents by projection
|
||||||
@@ -51,7 +50,7 @@ impl MongoDb {
|
|||||||
projection: Document,
|
projection: Document,
|
||||||
) -> Result<u64> {
|
) -> Result<u64> {
|
||||||
self.col::<Document>(collection)
|
self.col::<Document>(collection)
|
||||||
.count_documents(projection, None)
|
.count_documents(projection)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +66,8 @@ impl MongoDb {
|
|||||||
{
|
{
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<T>(collection)
|
.col::<T>(collection)
|
||||||
.find(projection, options)
|
.find(projection)
|
||||||
|
.with_options(options)
|
||||||
.await?
|
.await?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
if cfg!(debug_assertions) {
|
if cfg!(debug_assertions) {
|
||||||
@@ -101,7 +101,8 @@ impl MongoDb {
|
|||||||
O: Into<Option<FindOneOptions>>,
|
O: Into<Option<FindOneOptions>>,
|
||||||
{
|
{
|
||||||
self.col::<T>(collection)
|
self.col::<T>(collection)
|
||||||
.find_one(projection, options)
|
.find_one(projection)
|
||||||
|
.with_options(options)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +166,7 @@ impl MongoDb {
|
|||||||
};
|
};
|
||||||
|
|
||||||
self.col::<Document>(collection)
|
self.col::<Document>(collection)
|
||||||
.update_one(projection, query, None)
|
.update_one(projection, query)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +201,7 @@ impl MongoDb {
|
|||||||
projection: Document,
|
projection: Document,
|
||||||
) -> Result<DeleteResult> {
|
) -> Result<DeleteResult> {
|
||||||
self.col::<Document>(collection)
|
self.col::<Document>(collection)
|
||||||
.delete_one(projection, None)
|
.delete_one(projection)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ use std::{collections::HashMap, sync::Arc};
|
|||||||
use futures::lock::Mutex;
|
use futures::lock::Mutex;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Bot, Channel, ChannelCompositeKey, ChannelUnread, Emoji, File, Invite, Member,
|
Bot, Channel, ChannelCompositeKey, ChannelUnread, Emoji, File, FileHash, Invite, Member,
|
||||||
MemberCompositeKey, Message, RatelimitEvent, Report, Server, ServerBan, Snapshot, User,
|
MemberCompositeKey, Message, PolicyChange, RatelimitEvent, Report, Server, ServerBan, Snapshot,
|
||||||
UserSettings, Webhook,
|
User, UserSettings, Webhook,
|
||||||
};
|
};
|
||||||
|
|
||||||
database_derived!(
|
database_derived!(
|
||||||
@@ -18,8 +18,10 @@ database_derived!(
|
|||||||
pub channel_unreads: Arc<Mutex<HashMap<ChannelCompositeKey, ChannelUnread>>>,
|
pub channel_unreads: Arc<Mutex<HashMap<ChannelCompositeKey, ChannelUnread>>>,
|
||||||
pub channel_webhooks: Arc<Mutex<HashMap<String, Webhook>>>,
|
pub channel_webhooks: Arc<Mutex<HashMap<String, Webhook>>>,
|
||||||
pub emojis: Arc<Mutex<HashMap<String, Emoji>>>,
|
pub emojis: Arc<Mutex<HashMap<String, Emoji>>>,
|
||||||
|
pub file_hashes: Arc<Mutex<HashMap<String, FileHash>>>,
|
||||||
pub files: Arc<Mutex<HashMap<String, File>>>,
|
pub files: Arc<Mutex<HashMap<String, File>>>,
|
||||||
pub messages: Arc<Mutex<HashMap<String, Message>>>,
|
pub messages: Arc<Mutex<HashMap<String, Message>>>,
|
||||||
|
pub policy_changes: Arc<Mutex<HashMap<String, PolicyChange>>>,
|
||||||
pub ratelimit_events: Arc<Mutex<HashMap<String, RatelimitEvent>>>,
|
pub ratelimit_events: Arc<Mutex<HashMap<String, RatelimitEvent>>>,
|
||||||
pub user_settings: Arc<Mutex<HashMap<String, UserSettings>>>,
|
pub user_settings: Arc<Mutex<HashMap<String, UserSettings>>>,
|
||||||
pub users: Arc<Mutex<HashMap<String, User>>>,
|
pub users: Arc<Mutex<HashMap<String, User>>>,
|
||||||
|
|||||||
@@ -1,28 +1,16 @@
|
|||||||
use authifier::AuthifierEvent;
|
use authifier::AuthifierEvent;
|
||||||
|
use revolt_result::Error;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use revolt_models::v0::{
|
use revolt_models::v0::{
|
||||||
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
|
AppendMessage, Channel, ChannelUnread, Emoji, FieldsChannel, FieldsMember, FieldsMessage,
|
||||||
FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message, PartialChannel, PartialMember,
|
FieldsRole, FieldsServer, FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message,
|
||||||
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Report, Server, User,
|
PartialChannel, PartialMember, PartialMessage, PartialRole, PartialServer, PartialUser,
|
||||||
UserSettings, Webhook,
|
PartialWebhook, PolicyChange, RemovalIntention, Report, Server, User, UserSettings, Webhook,
|
||||||
};
|
};
|
||||||
use revolt_result::Error;
|
|
||||||
|
|
||||||
use crate::Database;
|
use crate::Database;
|
||||||
|
|
||||||
/// WebSocket Client Errors
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
#[serde(tag = "error")]
|
|
||||||
pub enum WebSocketError {
|
|
||||||
LabelMe,
|
|
||||||
InternalError { at: String },
|
|
||||||
InvalidSession,
|
|
||||||
OnboardingNotFinished,
|
|
||||||
AlreadyAuthenticated,
|
|
||||||
MalformedData { msg: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ping Packet
|
/// Ping Packet
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
@@ -31,12 +19,17 @@ pub enum Ping {
|
|||||||
Number(usize),
|
Number(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Untagged Error
|
/// Fields provided in Ready payload
|
||||||
#[derive(Serialize)]
|
#[derive(PartialEq)]
|
||||||
#[serde(untagged)]
|
pub enum ReadyPayloadFields {
|
||||||
pub enum ErrorEvent {
|
Users,
|
||||||
Error(WebSocketError),
|
Servers,
|
||||||
APIError(Error),
|
Channels,
|
||||||
|
Members,
|
||||||
|
Emoji,
|
||||||
|
|
||||||
|
UserSettings(Vec<String>),
|
||||||
|
ChannelUnreads,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Protocol Events
|
/// Protocol Events
|
||||||
@@ -45,6 +38,8 @@ pub enum ErrorEvent {
|
|||||||
pub enum EventV1 {
|
pub enum EventV1 {
|
||||||
/// Multiple events
|
/// Multiple events
|
||||||
Bulk { v: Vec<EventV1> },
|
Bulk { v: Vec<EventV1> },
|
||||||
|
/// Error event
|
||||||
|
Error { data: Error },
|
||||||
|
|
||||||
/// Successfully authenticated
|
/// Successfully authenticated
|
||||||
Authenticated,
|
Authenticated,
|
||||||
@@ -52,11 +47,23 @@ pub enum EventV1 {
|
|||||||
Logout,
|
Logout,
|
||||||
/// Basic data to cache
|
/// Basic data to cache
|
||||||
Ready {
|
Ready {
|
||||||
users: Vec<User>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
servers: Vec<Server>,
|
users: Option<Vec<User>>,
|
||||||
channels: Vec<Channel>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
members: Vec<Member>,
|
servers: Option<Vec<Server>>,
|
||||||
emojis: Vec<Emoji>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
channels: Option<Vec<Channel>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
members: Option<Vec<Member>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
emojis: Option<Vec<Emoji>>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
user_settings: Option<UserSettings>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
channel_unreads: Option<Vec<ChannelUnread>>,
|
||||||
|
|
||||||
|
policy_changes: Vec<PolicyChange>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Ping response
|
/// Ping response
|
||||||
@@ -69,6 +76,8 @@ pub enum EventV1 {
|
|||||||
id: String,
|
id: String,
|
||||||
channel: String,
|
channel: String,
|
||||||
data: PartialMessage,
|
data: PartialMessage,
|
||||||
|
#[serde(default)]
|
||||||
|
clear: Vec<FieldsMessage>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Append information to existing message
|
/// Append information to existing message
|
||||||
@@ -119,6 +128,7 @@ pub enum EventV1 {
|
|||||||
ServerUpdate {
|
ServerUpdate {
|
||||||
id: String,
|
id: String,
|
||||||
data: PartialServer,
|
data: PartialServer,
|
||||||
|
#[serde(default)]
|
||||||
clear: Vec<FieldsServer>,
|
clear: Vec<FieldsServer>,
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -129,6 +139,7 @@ pub enum EventV1 {
|
|||||||
ServerMemberUpdate {
|
ServerMemberUpdate {
|
||||||
id: MemberCompositeKey,
|
id: MemberCompositeKey,
|
||||||
data: PartialMember,
|
data: PartialMember,
|
||||||
|
#[serde(default)]
|
||||||
clear: Vec<FieldsMember>,
|
clear: Vec<FieldsMember>,
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -136,23 +147,32 @@ pub enum EventV1 {
|
|||||||
ServerMemberJoin { id: String, user: String },
|
ServerMemberJoin { id: String, user: String },
|
||||||
|
|
||||||
/// User left server
|
/// User left server
|
||||||
ServerMemberLeave { id: String, user: String },
|
ServerMemberLeave {
|
||||||
|
id: String,
|
||||||
|
user: String,
|
||||||
|
reason: RemovalIntention,
|
||||||
|
},
|
||||||
|
|
||||||
/// Server role created or updated
|
/// Server role created or updated
|
||||||
ServerRoleUpdate {
|
ServerRoleUpdate {
|
||||||
id: String,
|
id: String,
|
||||||
role_id: String,
|
role_id: String,
|
||||||
data: PartialRole,
|
data: PartialRole,
|
||||||
|
#[serde(default)]
|
||||||
clear: Vec<FieldsRole>,
|
clear: Vec<FieldsRole>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Server role deleted
|
/// Server role deleted
|
||||||
ServerRoleDelete { id: String, role_id: String },
|
ServerRoleDelete { id: String, role_id: String },
|
||||||
|
|
||||||
|
/// Server roles ranks updated
|
||||||
|
ServerRoleRanksUpdate { id: String, ranks: Vec<String> },
|
||||||
|
|
||||||
/// Update existing user
|
/// Update existing user
|
||||||
UserUpdate {
|
UserUpdate {
|
||||||
id: String,
|
id: String,
|
||||||
data: PartialUser,
|
data: PartialUser,
|
||||||
|
#[serde(default)]
|
||||||
clear: Vec<FieldsUser>,
|
clear: Vec<FieldsUser>,
|
||||||
event_id: Option<String>,
|
event_id: Option<String>,
|
||||||
},
|
},
|
||||||
@@ -187,6 +207,7 @@ pub enum EventV1 {
|
|||||||
ChannelUpdate {
|
ChannelUpdate {
|
||||||
id: String,
|
id: String,
|
||||||
data: PartialChannel,
|
data: PartialChannel,
|
||||||
|
#[serde(default)]
|
||||||
clear: Vec<FieldsChannel>,
|
clear: Vec<FieldsChannel>,
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -249,7 +270,7 @@ impl EventV1 {
|
|||||||
// TODO: this should be captured by member list in the future and not immediately fanned out to users
|
// TODO: this should be captured by member list in the future and not immediately fanned out to users
|
||||||
if let Ok(members) = db.fetch_all_memberships(&id).await {
|
if let Ok(members) = db.fetch_all_memberships(&id).await {
|
||||||
for member in members {
|
for member in members {
|
||||||
self.clone().p(member.id.server).await;
|
self.clone().server(member.id.server).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -259,6 +280,11 @@ impl EventV1 {
|
|||||||
self.p(format!("{id}!")).await;
|
self.p(format!("{id}!")).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Publish server member event
|
||||||
|
pub async fn server(self, id: String) {
|
||||||
|
self.p(format!("{id}u")).await;
|
||||||
|
}
|
||||||
|
|
||||||
/// Publish internal global event
|
/// Publish internal global event
|
||||||
pub async fn global(self) {
|
pub async fn global(self) {
|
||||||
self.p("global".to_string()).await;
|
self.p("global".to_string()).await;
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod client;
|
pub mod client;
|
||||||
|
pub mod rabbit;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use revolt_models::v0::PushNotification;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::User;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct MessageSentPayload {
|
||||||
|
pub notification: PushNotification,
|
||||||
|
pub users: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct MassMessageSentPayload {
|
||||||
|
pub notifications: Vec<PushNotification>,
|
||||||
|
pub server_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub struct FRAcceptedPayload {
|
||||||
|
pub accepted_user: User,
|
||||||
|
pub user: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub struct FRReceivedPayload {
|
||||||
|
pub from_user: User,
|
||||||
|
pub user: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub struct GenericPayload {
|
||||||
|
pub title: String,
|
||||||
|
pub body: String,
|
||||||
|
pub icon: Option<String>,
|
||||||
|
pub user: User,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type", content = "data")]
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
|
pub enum PayloadKind {
|
||||||
|
MessageNotification(PushNotification),
|
||||||
|
FRAccepted(FRAcceptedPayload),
|
||||||
|
FRReceived(FRReceivedPayload),
|
||||||
|
BadgeUpdate(usize),
|
||||||
|
Generic(GenericPayload),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct PayloadToService {
|
||||||
|
pub notification: PayloadKind,
|
||||||
|
pub user_id: String,
|
||||||
|
pub session_id: String,
|
||||||
|
pub token: String,
|
||||||
|
pub extras: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct AckPayload {
|
||||||
|
pub user_id: String,
|
||||||
|
pub channel_id: String,
|
||||||
|
pub message_id: String,
|
||||||
|
}
|
||||||
@@ -8,5 +8,6 @@ pub enum ClientMessage {
|
|||||||
Authenticate { token: String },
|
Authenticate { token: String },
|
||||||
BeginTyping { channel: String },
|
BeginTyping { channel: String },
|
||||||
EndTyping { channel: String },
|
EndTyping { channel: String },
|
||||||
|
Subscribe { server_id: String },
|
||||||
Ping { data: Ping, responded: Option<()> },
|
Ping { data: Ping, responded: Option<()> },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ extern crate revolt_optional_struct;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate revolt_result;
|
extern crate revolt_result;
|
||||||
|
|
||||||
|
pub use iso8601_timestamp;
|
||||||
|
|
||||||
#[cfg(feature = "mongodb")]
|
#[cfg(feature = "mongodb")]
|
||||||
pub use mongodb;
|
pub use mongodb;
|
||||||
|
|
||||||
@@ -23,6 +25,29 @@ pub use mongodb;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate bson;
|
extern crate bson;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "async-std-runtime"))]
|
||||||
|
compile_error!("async-std-runtime feature must be enabled.");
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
macro_rules! query {
|
||||||
|
( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => {
|
||||||
|
Ok($self.$type($collection, $($rest),+).await.unwrap())
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
macro_rules! query {
|
||||||
|
( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => {
|
||||||
|
$self.$type($collection, $($rest),+).await
|
||||||
|
.map_err(|err| {
|
||||||
|
revolt_config::capture_internal_error!(err);
|
||||||
|
create_database_error!(stringify!($type), $collection)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! database_derived {
|
macro_rules! database_derived {
|
||||||
( $( $item:item )+ ) => {
|
( $( $item:item )+ ) => {
|
||||||
$(
|
$(
|
||||||
@@ -81,9 +106,18 @@ pub mod util;
|
|||||||
pub use models::*;
|
pub use models::*;
|
||||||
|
|
||||||
pub mod events;
|
pub mod events;
|
||||||
|
#[cfg(feature = "tasks")]
|
||||||
pub mod tasks;
|
pub mod tasks;
|
||||||
|
|
||||||
|
mod amqp;
|
||||||
|
pub use amqp::amqp::AMQP;
|
||||||
|
|
||||||
/// Utility function to check if a boolean value is false
|
/// Utility function to check if a boolean value is false
|
||||||
pub fn if_false(t: &bool) -> bool {
|
pub fn if_false(t: &bool) -> bool {
|
||||||
!t
|
!t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Utility function to check if an option doesnt contain true
|
||||||
|
pub fn if_option_false(t: &Option<bool>) -> bool {
|
||||||
|
t != &Some(true)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ impl AbstractMigrations for MongoDb {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
/// Drop the database
|
/// Drop the database
|
||||||
async fn drop_database(&self) {
|
async fn drop_database(&self) {
|
||||||
self.db().drop(None).await.ok();
|
self.db().drop().await.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Migrate the database
|
/// Migrate the database
|
||||||
@@ -18,7 +18,7 @@ impl AbstractMigrations for MongoDb {
|
|||||||
info!("Migrating the database.");
|
info!("Migrating the database.");
|
||||||
|
|
||||||
let list = self
|
let list = self
|
||||||
.list_database_names(None, None)
|
.list_database_names()
|
||||||
.await
|
.await
|
||||||
.expect("Failed to fetch database names.");
|
.expect("Failed to fetch database names.");
|
||||||
|
|
||||||
|
|||||||
@@ -8,227 +8,258 @@ pub async fn create_database(db: &MongoDb) {
|
|||||||
info!("Creating database.");
|
info!("Creating database.");
|
||||||
let db = db.db();
|
let db = db.db();
|
||||||
|
|
||||||
db.create_collection("accounts", None)
|
db.create_collection("accounts")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create accounts collection.");
|
.expect("Failed to create accounts collection.");
|
||||||
|
|
||||||
db.create_collection("users", None)
|
db.create_collection("users")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create users collection.");
|
.expect("Failed to create users collection.");
|
||||||
|
|
||||||
db.create_collection("channels", None)
|
db.create_collection("channels")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create channels collection.");
|
.expect("Failed to create channels collection.");
|
||||||
|
|
||||||
db.create_collection("messages", None)
|
db.create_collection("messages")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create messages collection.");
|
.expect("Failed to create messages collection.");
|
||||||
|
|
||||||
db.create_collection("servers", None)
|
db.create_collection("servers")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create servers collection.");
|
.expect("Failed to create servers collection.");
|
||||||
|
|
||||||
db.create_collection("server_members", None)
|
db.create_collection("server_members")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create server_members collection.");
|
.expect("Failed to create server_members collection.");
|
||||||
|
|
||||||
db.create_collection("server_bans", None)
|
db.create_collection("server_bans")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create server_bans collection.");
|
.expect("Failed to create server_bans collection.");
|
||||||
|
|
||||||
db.create_collection("channel_invites", None)
|
db.create_collection("channel_invites")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create channel_invites collection.");
|
.expect("Failed to create channel_invites collection.");
|
||||||
|
|
||||||
db.create_collection("channel_unreads", None)
|
db.create_collection("channel_unreads")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create channel_unreads collection.");
|
.expect("Failed to create channel_unreads collection.");
|
||||||
|
|
||||||
db.create_collection("channel_webhooks", None)
|
db.create_collection("channel_webhooks")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create channel_webhooks collection.");
|
.expect("Failed to create channel_webhooks collection.");
|
||||||
|
|
||||||
db.create_collection("migrations", None)
|
db.create_collection("migrations")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create migrations collection.");
|
.expect("Failed to create migrations collection.");
|
||||||
|
|
||||||
db.create_collection("attachments", None)
|
db.create_collection("attachments")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create attachments collection.");
|
.expect("Failed to create attachments collection.");
|
||||||
|
|
||||||
db.create_collection("user_settings", None)
|
db.create_collection("attachment_hashes")
|
||||||
|
.await
|
||||||
|
.expect("Failed to create attachment_hashes collection.");
|
||||||
|
|
||||||
|
db.create_collection("user_settings")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create user_settings collection.");
|
.expect("Failed to create user_settings collection.");
|
||||||
|
|
||||||
db.create_collection("safety_reports", None)
|
db.create_collection("policy_changes")
|
||||||
|
.await
|
||||||
|
.expect("Failed to create policy_changes collection.");
|
||||||
|
|
||||||
|
db.create_collection("safety_reports")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create safety_reports collection.");
|
.expect("Failed to create safety_reports collection.");
|
||||||
|
|
||||||
db.create_collection("safety_snapshots", None)
|
db.create_collection("safety_snapshots")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create safety_snapshots collection.");
|
.expect("Failed to create safety_snapshots collection.");
|
||||||
|
|
||||||
db.create_collection("safety_strikes", None)
|
db.create_collection("safety_strikes")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create safety_strikes collection.");
|
.expect("Failed to create safety_strikes collection.");
|
||||||
|
|
||||||
db.create_collection("bots", None)
|
db.create_collection("bots")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create bots collection.");
|
.expect("Failed to create bots collection.");
|
||||||
|
|
||||||
db.create_collection("ratelimit_events", None)
|
db.create_collection("ratelimit_events")
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create ratelimit_events collection.");
|
.expect("Failed to create ratelimit_events collection.");
|
||||||
|
|
||||||
db.create_collection(
|
db.create_collection("pubsub")
|
||||||
"pubsub",
|
.with_options(
|
||||||
CreateCollectionOptions::builder()
|
CreateCollectionOptions::builder()
|
||||||
.capped(true)
|
.capped(true)
|
||||||
.size(1_000_000)
|
.size(1_000_000)
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create pubsub collection.");
|
.expect("Failed to create pubsub collection.");
|
||||||
|
|
||||||
db.run_command(
|
db.run_command(doc! {
|
||||||
doc! {
|
"createIndexes": "users",
|
||||||
"createIndexes": "users",
|
"indexes": [
|
||||||
"indexes": [
|
{
|
||||||
{
|
"key": {
|
||||||
"key": {
|
"username": 1_i32
|
||||||
"username": 1_i32
|
|
||||||
},
|
|
||||||
"name": "username",
|
|
||||||
"unique": false,
|
|
||||||
"collation": {
|
|
||||||
"locale": "en",
|
|
||||||
"strength": 2_i32
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
"name": "username",
|
||||||
"key": {
|
"unique": false,
|
||||||
"username": 1_i32,
|
"collation": {
|
||||||
"discriminator": 1_i32
|
"locale": "en",
|
||||||
},
|
"strength": 2_i32
|
||||||
"name": "username_discriminator",
|
|
||||||
"unique": true,
|
|
||||||
"collation": {
|
|
||||||
"locale": "en",
|
|
||||||
"strength": 2_i32
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
},
|
||||||
},
|
{
|
||||||
None,
|
"key": {
|
||||||
)
|
"username": 1_i32,
|
||||||
|
"discriminator": 1_i32
|
||||||
|
},
|
||||||
|
"name": "username_discriminator",
|
||||||
|
"unique": true,
|
||||||
|
"collation": {
|
||||||
|
"locale": "en",
|
||||||
|
"strength": 2_i32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create username index.");
|
.expect("Failed to create username index.");
|
||||||
|
|
||||||
db.run_command(
|
db.run_command(doc! {
|
||||||
doc! {
|
"createIndexes": "messages",
|
||||||
"createIndexes": "messages",
|
"indexes": [
|
||||||
"indexes": [
|
{
|
||||||
{
|
"key": {
|
||||||
"key": {
|
"content": "text"
|
||||||
"content": "text"
|
|
||||||
},
|
|
||||||
"name": "content"
|
|
||||||
},
|
},
|
||||||
{
|
"name": "content"
|
||||||
"key": {
|
},
|
||||||
"channel": 1_i32,
|
{
|
||||||
"_id": 1_i32
|
"key": {
|
||||||
},
|
"channel": 1_i32,
|
||||||
"name": "channel_id_compound"
|
"_id": 1_i32
|
||||||
},
|
},
|
||||||
{
|
"name": "channel_id_compound"
|
||||||
"key": {
|
},
|
||||||
"author": 1_i32
|
{
|
||||||
},
|
"key": {
|
||||||
"name": "author"
|
"author": 1_i32
|
||||||
}
|
},
|
||||||
]
|
"name": "author"
|
||||||
},
|
},
|
||||||
None,
|
{
|
||||||
)
|
"key": {
|
||||||
|
"channel": 1_i32,
|
||||||
|
"pinned": 1_i32
|
||||||
|
},
|
||||||
|
"name": "channel_pinned_compound"
|
||||||
|
},
|
||||||
|
]
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create message index.");
|
.expect("Failed to create message index.");
|
||||||
|
|
||||||
db.run_command(
|
db.run_command(doc! {
|
||||||
doc! {
|
"createIndexes": "channel_unreads",
|
||||||
"createIndexes": "channel_unreads",
|
"indexes": [
|
||||||
"indexes": [
|
{
|
||||||
{
|
"key": {
|
||||||
"key": {
|
"_id.channel": 1_i32,
|
||||||
"_id.channel": 1_i32,
|
"_id.user": 1_i32,
|
||||||
"_id.user": 1_i32,
|
|
||||||
},
|
|
||||||
"name": "compound_id"
|
|
||||||
},
|
},
|
||||||
{
|
"name": "compound_id"
|
||||||
"key": {
|
},
|
||||||
"_id.user": 1_i32,
|
{
|
||||||
},
|
"key": {
|
||||||
"name": "user_id"
|
"_id.user": 1_i32,
|
||||||
}
|
},
|
||||||
]
|
"name": "user_id"
|
||||||
},
|
}
|
||||||
None,
|
]
|
||||||
)
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create channel_unreads index.");
|
.expect("Failed to create channel_unreads index.");
|
||||||
|
|
||||||
db.run_command(
|
db.run_command(doc! {
|
||||||
doc! {
|
"createIndexes": "server_members",
|
||||||
"createIndexes": "server_members",
|
"indexes": [
|
||||||
"indexes": [
|
{
|
||||||
{
|
"key": {
|
||||||
"key": {
|
"_id.server": 1_i32,
|
||||||
"_id.server": 1_i32,
|
"_id.user": 1_i32,
|
||||||
"_id.user": 1_i32,
|
|
||||||
},
|
|
||||||
"name": "compound_id"
|
|
||||||
},
|
},
|
||||||
{
|
"name": "compound_id"
|
||||||
"key": {
|
},
|
||||||
"_id.user": 1_i32,
|
{
|
||||||
},
|
"key": {
|
||||||
"name": "user_id"
|
"_id.user": 1_i32,
|
||||||
}
|
},
|
||||||
]
|
"name": "user_id"
|
||||||
},
|
}
|
||||||
None,
|
]
|
||||||
)
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create server_members index.");
|
.expect("Failed to create server_members index.");
|
||||||
|
|
||||||
db.collection("migrations")
|
db.run_command(doc! {
|
||||||
.insert_one(
|
"createIndexes": "attachments",
|
||||||
doc! {
|
"indexes": [
|
||||||
"_id": 0_i32,
|
{
|
||||||
"revision": LATEST_REVISION
|
"key": {
|
||||||
|
"hash": 1_i32
|
||||||
|
},
|
||||||
|
"name": "hash"
|
||||||
},
|
},
|
||||||
None,
|
{
|
||||||
)
|
"key": {
|
||||||
|
"used_for.id": 1_i32
|
||||||
|
},
|
||||||
|
"name": "used_for_id"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("Failed to create attachments index.");
|
||||||
|
|
||||||
|
db.run_command(doc! {
|
||||||
|
"createIndexes": "attachment_hashes",
|
||||||
|
"indexes": [
|
||||||
|
{
|
||||||
|
"key": {
|
||||||
|
"processed_hash": 1_i32
|
||||||
|
},
|
||||||
|
"name": "processed_hash"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("Failed to create attachment_hashes index.");
|
||||||
|
|
||||||
|
db.collection("migrations")
|
||||||
|
.insert_one(doc! {
|
||||||
|
"_id": 0_i32,
|
||||||
|
"revision": LATEST_REVISION
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Failed to save migration info.");
|
.expect("Failed to save migration info.");
|
||||||
|
|
||||||
db.run_command(
|
db.run_command(doc! {
|
||||||
doc! {
|
"createIndexes": "ratelimit_events",
|
||||||
"createIndexes": "ratelimit_events",
|
"indexes": [
|
||||||
"indexes": [
|
{
|
||||||
{
|
"key": {
|
||||||
"key": {
|
"_id": 1_i32,
|
||||||
"_id": 1_i32,
|
"target_id": 1_i32,
|
||||||
"target_id": 1_i32,
|
"event_type": 1_i32,
|
||||||
"event_type": 1_i32,
|
},
|
||||||
},
|
"name": "compound_key"
|
||||||
"name": "compound_key"
|
}
|
||||||
}
|
]
|
||||||
]
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create ratelimit_events index.");
|
.expect("Failed to create ratelimit_events index.");
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,3 @@
|
|||||||
use revolt_config::config;
|
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
@@ -72,7 +71,12 @@ impl Default for Bot {
|
|||||||
#[allow(clippy::disallowed_methods)]
|
#[allow(clippy::disallowed_methods)]
|
||||||
impl Bot {
|
impl Bot {
|
||||||
/// Create a new bot
|
/// Create a new bot
|
||||||
pub async fn create<D>(db: &Database, username: String, owner: &User, data: D) -> Result<Bot>
|
pub async fn create<D>(
|
||||||
|
db: &Database,
|
||||||
|
username: String,
|
||||||
|
owner: &User,
|
||||||
|
data: D,
|
||||||
|
) -> Result<(Bot, User)>
|
||||||
where
|
where
|
||||||
D: Into<Option<PartialBot>>,
|
D: Into<Option<PartialBot>>,
|
||||||
{
|
{
|
||||||
@@ -80,14 +84,13 @@ impl Bot {
|
|||||||
return Err(create_error!(IsBot));
|
return Err(create_error!(IsBot));
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = config().await;
|
if db.get_number_of_bots_by_user(&owner.id).await? >= owner.limits().await.bots {
|
||||||
if db.get_number_of_bots_by_user(&owner.id).await? >= config.features.limits.default.bots {
|
|
||||||
return Err(create_error!(ReachedMaximumBots));
|
return Err(create_error!(ReachedMaximumBots));
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
|
|
||||||
User::create(
|
let user = User::create(
|
||||||
db,
|
db,
|
||||||
username,
|
username,
|
||||||
Some(id.to_string()),
|
Some(id.to_string()),
|
||||||
@@ -112,7 +115,7 @@ impl Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
db.insert_bot(&bot).await?;
|
db.insert_bot(&bot).await?;
|
||||||
Ok(bot)
|
Ok((bot, user))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a field from this object
|
/// Remove a field from this object
|
||||||
@@ -168,7 +171,7 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let bot = Bot::create(
|
let (bot, _) = Bot::create(
|
||||||
&db,
|
&db,
|
||||||
"Bot Name".to_string(),
|
"Bot Name".to_string(),
|
||||||
&owner,
|
&owner,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::{Bot, FieldsBot, PartialBot};
|
use crate::{Bot, FieldsBot, PartialBot};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::Invite;
|
use crate::Invite;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -24,12 +24,9 @@ impl AbstractChannelInvites for MongoDb {
|
|||||||
async fn fetch_invites_for_server(&self, server_id: &str) -> Result<Vec<Invite>> {
|
async fn fetch_invites_for_server(&self, server_id: &str) -> Result<Vec<Invite>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Invite>(COL)
|
.col::<Invite>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"server": server_id,
|
||||||
"server": server_id,
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
|
|||||||
@@ -2,18 +2,19 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::ChannelUnread;
|
use crate::ChannelUnread;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait AbstractChannelUnreads: Sync + Send {
|
pub trait AbstractChannelUnreads: Sync + Send {
|
||||||
/// Acknowledge a message.
|
/// Acknowledge a message, and returns updated channel unread.
|
||||||
async fn acknowledge_message(
|
async fn acknowledge_message(
|
||||||
&self,
|
&self,
|
||||||
channel_id: &str,
|
channel_id: &str,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
message_id: &str,
|
message_id: &str,
|
||||||
) -> Result<()>;
|
) -> Result<Option<ChannelUnread>>;
|
||||||
|
|
||||||
/// Acknowledge many channels.
|
/// Acknowledge many channels.
|
||||||
async fn acknowledge_channels(&self, user_id: &str, channel_ids: &[String]) -> Result<()>;
|
async fn acknowledge_channels(&self, user_id: &str, channel_ids: &[String]) -> Result<()>;
|
||||||
@@ -26,6 +27,20 @@ pub trait AbstractChannelUnreads: Sync + Send {
|
|||||||
message_ids: &[String],
|
message_ids: &[String],
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Add a mention.
|
||||||
|
async fn add_mention_to_many_unreads<'a>(
|
||||||
|
&self,
|
||||||
|
channel_id: &str,
|
||||||
|
user_ids: &[String],
|
||||||
|
message_ids: &[String],
|
||||||
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Fetch all unreads with mentions for a user.
|
||||||
|
async fn fetch_unread_mentions(&self, user_id: &str) -> Result<Vec<ChannelUnread>>;
|
||||||
|
|
||||||
/// Fetch all channel unreads for a user.
|
/// Fetch all channel unreads for a user.
|
||||||
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>>;
|
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>>;
|
||||||
|
|
||||||
|
/// Fetch unread for a specific user in a channel.
|
||||||
|
async fn fetch_unread(&self, user_id: &str, channel_id: &str) -> Result<Option<ChannelUnread>>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
use bson::Document;
|
use bson::Document;
|
||||||
|
use mongodb::options::FindOneAndUpdateOptions;
|
||||||
|
use mongodb::options::ReturnDocument;
|
||||||
use mongodb::options::UpdateOptions;
|
use mongodb::options::UpdateOptions;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
@@ -12,31 +14,37 @@ static COL: &str = "channel_unreads";
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl AbstractChannelUnreads for MongoDb {
|
impl AbstractChannelUnreads for MongoDb {
|
||||||
/// Acknowledge a message.
|
/// Acknowledge a message, and returns updated channel unread.
|
||||||
async fn acknowledge_message(
|
async fn acknowledge_message(
|
||||||
&self,
|
&self,
|
||||||
channel_id: &str,
|
channel_id: &str,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
message_id: &str,
|
message_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<Option<ChannelUnread>> {
|
||||||
self.col::<Document>(COL)
|
self.col::<ChannelUnread>(COL)
|
||||||
.update_one(
|
.find_one_and_update(
|
||||||
doc! {
|
doc! {
|
||||||
"_id.channel": channel_id,
|
"_id.channel": channel_id,
|
||||||
"_id.user": user_id,
|
"_id.user": user_id,
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$unset": {
|
"$pull": {
|
||||||
"mentions": 1_i32
|
"mentions": {
|
||||||
|
"$lte": message_id
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"$set": {
|
"$set": {
|
||||||
"last_id": message_id
|
"last_id": message_id
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UpdateOptions::builder().upsert(true).build(),
|
)
|
||||||
|
.with_options(
|
||||||
|
FindOneAndUpdateOptions::builder()
|
||||||
|
.upsert(true)
|
||||||
|
.return_document(ReturnDocument::After)
|
||||||
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
|
||||||
.map_err(|_| create_database_error!("update_one", COL))
|
.map_err(|_| create_database_error!("update_one", COL))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,15 +53,12 @@ impl AbstractChannelUnreads for MongoDb {
|
|||||||
let current_time = Ulid::new().to_string();
|
let current_time = Ulid::new().to_string();
|
||||||
|
|
||||||
self.col::<Document>(COL)
|
self.col::<Document>(COL)
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"_id.channel": {
|
||||||
"_id.channel": {
|
"$in": channel_ids
|
||||||
"$in": channel_ids
|
|
||||||
},
|
|
||||||
"_id.user": user_id
|
|
||||||
},
|
},
|
||||||
None,
|
"_id.user": user_id
|
||||||
)
|
})
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", COL))?;
|
.map_err(|_| create_database_error!("delete_many", COL))?;
|
||||||
|
|
||||||
@@ -71,7 +76,6 @@ impl AbstractChannelUnreads for MongoDb {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<Document>>(),
|
.collect::<Vec<Document>>(),
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -98,13 +102,42 @@ impl AbstractChannelUnreads for MongoDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UpdateOptions::builder().upsert(true).build(),
|
|
||||||
)
|
)
|
||||||
|
.with_options(UpdateOptions::builder().upsert(true).build())
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| create_database_error!("update_one", COL))
|
.map_err(|_| create_database_error!("update_one", COL))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a mention to multiple users.
|
||||||
|
async fn add_mention_to_many_unreads<'a>(
|
||||||
|
&self,
|
||||||
|
channel_id: &str,
|
||||||
|
user_ids: &[String],
|
||||||
|
message_ids: &[String],
|
||||||
|
) -> Result<()> {
|
||||||
|
self.col::<Document>(COL)
|
||||||
|
.update_many(
|
||||||
|
doc! {
|
||||||
|
"_id.channel": channel_id,
|
||||||
|
"_id.user": {
|
||||||
|
"$in": user_ids
|
||||||
|
},
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$push": {
|
||||||
|
"mentions": {
|
||||||
|
"$each": message_ids
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.with_options(UpdateOptions::builder().upsert(true).build())
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|_| create_database_error!("update_many", COL))
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch all channel unreads for a user.
|
/// Fetch all channel unreads for a user.
|
||||||
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>> {
|
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>> {
|
||||||
query!(
|
query!(
|
||||||
@@ -116,4 +149,29 @@ impl AbstractChannelUnreads for MongoDb {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn fetch_unread_mentions(&self, user_id: &str) -> Result<Vec<ChannelUnread>> {
|
||||||
|
query! {
|
||||||
|
self,
|
||||||
|
find,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"_id.user": user_id,
|
||||||
|
"mentions": {"$ne": null}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch unread for a specific user in a channel.
|
||||||
|
async fn fetch_unread(&self, user_id: &str, channel_id: &str) -> Result<Option<ChannelUnread>> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
find_one,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"_id.user": user_id,
|
||||||
|
"_id.channel": channel_id
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ impl AbstractChannelUnreads for ReferenceDb {
|
|||||||
channel_id: &str,
|
channel_id: &str,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
message_id: &str,
|
message_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<Option<ChannelUnread>> {
|
||||||
let mut unreads = self.channel_unreads.lock().await;
|
let mut unreads = self.channel_unreads.lock().await;
|
||||||
let key = ChannelCompositeKey {
|
let key = ChannelCompositeKey {
|
||||||
channel: channel_id.to_string(),
|
channel: channel_id.to_string(),
|
||||||
@@ -27,14 +27,14 @@ impl AbstractChannelUnreads for ReferenceDb {
|
|||||||
unreads.insert(
|
unreads.insert(
|
||||||
key.clone(),
|
key.clone(),
|
||||||
ChannelUnread {
|
ChannelUnread {
|
||||||
id: key,
|
id: key.clone(),
|
||||||
last_id: Some(message_id.to_string()),
|
last_id: Some(message_id.to_string()),
|
||||||
mentions: None,
|
mentions: None,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(unreads.get(&key).cloned())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Acknowledge many channels.
|
/// Acknowledge many channels.
|
||||||
@@ -78,6 +78,47 @@ impl AbstractChannelUnreads for ReferenceDb {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a mention to multiple users.
|
||||||
|
async fn add_mention_to_many_unreads<'a>(
|
||||||
|
&self,
|
||||||
|
channel_id: &str,
|
||||||
|
user_ids: &[String],
|
||||||
|
message_ids: &[String],
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut unreads = self.channel_unreads.lock().await;
|
||||||
|
|
||||||
|
for user_id in user_ids {
|
||||||
|
let key = ChannelCompositeKey {
|
||||||
|
channel: channel_id.to_string(),
|
||||||
|
user: user_id.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(unread) = unreads.get_mut(&key) {
|
||||||
|
unread.mentions.replace(message_ids.to_vec());
|
||||||
|
} else {
|
||||||
|
unreads.insert(
|
||||||
|
key.clone(),
|
||||||
|
ChannelUnread {
|
||||||
|
id: key,
|
||||||
|
last_id: None,
|
||||||
|
mentions: Some(message_ids.to_vec()),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_unread_mentions(&self, user_id: &str) -> Result<Vec<ChannelUnread>> {
|
||||||
|
let unreads = self.channel_unreads.lock().await;
|
||||||
|
Ok(unreads
|
||||||
|
.values()
|
||||||
|
.filter(|unread| unread.id.user == user_id && unread.mentions.is_some())
|
||||||
|
.cloned()
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch all channel unreads for a user.
|
/// Fetch all channel unreads for a user.
|
||||||
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>> {
|
async fn fetch_unreads(&self, user_id: &str) -> Result<Vec<ChannelUnread>> {
|
||||||
let unreads = self.channel_unreads.lock().await;
|
let unreads = self.channel_unreads.lock().await;
|
||||||
@@ -87,4 +128,16 @@ impl AbstractChannelUnreads for ReferenceDb {
|
|||||||
.cloned()
|
.cloned()
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch unread for a specific user in a channel.
|
||||||
|
async fn fetch_unread(&self, user_id: &str, channel_id: &str) -> Result<Option<ChannelUnread>> {
|
||||||
|
let unreads = self.channel_unreads.lock().await;
|
||||||
|
|
||||||
|
Ok(unreads
|
||||||
|
.get(&ChannelCompositeKey {
|
||||||
|
channel: channel_id.to_string(),
|
||||||
|
user: user_id.to_string(),
|
||||||
|
})
|
||||||
|
.cloned())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ auto_derived_partial!(
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub avatar: Option<File>,
|
pub avatar: Option<File>,
|
||||||
|
|
||||||
|
/// User that created this webhook
|
||||||
|
pub creator_id: String,
|
||||||
|
|
||||||
/// The channel this webhook belongs to
|
/// The channel this webhook belongs to
|
||||||
pub channel_id: String,
|
pub channel_id: String,
|
||||||
|
|
||||||
@@ -43,6 +46,7 @@ impl Default for Webhook {
|
|||||||
id: Default::default(),
|
id: Default::default(),
|
||||||
name: Default::default(),
|
name: Default::default(),
|
||||||
avatar: None,
|
avatar: None,
|
||||||
|
creator_id: Default::default(),
|
||||||
channel_id: Default::default(),
|
channel_id: Default::default(),
|
||||||
permissions: Default::default(),
|
permissions: Default::default(),
|
||||||
token: Default::default(),
|
token: Default::default(),
|
||||||
@@ -70,7 +74,7 @@ impl Webhook {
|
|||||||
if self.token.as_deref() == Some(token) {
|
if self.token.as_deref() == Some(token) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(create_error!(InvalidCredentials))
|
Err(create_error!(NotAuthenticated))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +142,7 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
db.insert_webhook(&webhook).await.unwrap();
|
webhook.create(&db).await.unwrap();
|
||||||
|
|
||||||
let mut updated_webhook = webhook.clone();
|
let mut updated_webhook = webhook.clone();
|
||||||
updated_webhook
|
updated_webhook
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::{FieldsWebhook, PartialWebhook, Webhook};
|
use crate::{FieldsWebhook, PartialWebhook, Webhook};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -24,12 +24,9 @@ impl AbstractWebhooks for MongoDb {
|
|||||||
async fn fetch_webhooks_for_channel(&self, channel_id: &str) -> Result<Vec<Webhook>> {
|
async fn fetch_webhooks_for_channel(&self, channel_id: &str) -> Result<Vec<Webhook>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Webhook>(COL)
|
.col::<Webhook>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"channel_id": channel_id,
|
||||||
"channel_id": channel_id,
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
|
|||||||
@@ -8,10 +8,13 @@ use serde::{Deserialize, Serialize};
|
|||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
events::client::EventV1, tasks::ack::AckEvent, Database, File, IntoDocumentPath, PartialServer,
|
events::client::EventV1, Database, File, PartialServer,
|
||||||
Server, SystemMessage, User,
|
Server, SystemMessage, User, AMQP,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
use crate::IntoDocumentPath;
|
||||||
|
|
||||||
auto_derived!(
|
auto_derived!(
|
||||||
#[serde(tag = "channel_type")]
|
#[serde(tag = "channel_type")]
|
||||||
pub enum Channel {
|
pub enum Channel {
|
||||||
@@ -201,9 +204,9 @@ impl Channel {
|
|||||||
update_server: bool,
|
update_server: bool,
|
||||||
) -> Result<Channel> {
|
) -> Result<Channel> {
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
if server.channels.len() > config.features.limits.default.server_channels {
|
if server.channels.len() > config.features.limits.global.server_channels {
|
||||||
return Err(create_error!(TooManyChannels {
|
return Err(create_error!(TooManyChannels {
|
||||||
max: config.features.limits.default.server_channels,
|
max: config.features.limits.global.server_channels,
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -263,22 +266,30 @@ impl Channel {
|
|||||||
data.users.insert(owner_id.to_string());
|
data.users.insert(owner_id.to_string());
|
||||||
|
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
if data.users.len() > config.features.limits.default.group_size {
|
if data.users.len() > config.features.limits.global.group_size {
|
||||||
return Err(create_error!(GroupTooLarge {
|
return Err(create_error!(GroupTooLarge {
|
||||||
max: config.features.limits.default.group_size,
|
max: config.features.limits.global.group_size,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let id = ulid::Ulid::new().to_string();
|
||||||
|
|
||||||
|
let icon = if let Some(icon_id) = data.icon {
|
||||||
|
Some(File::use_channel_icon(db, &icon_id, &id, &owner_id).await?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let recipients = data.users.into_iter().collect::<Vec<String>>();
|
let recipients = data.users.into_iter().collect::<Vec<String>>();
|
||||||
let channel = Channel::Group {
|
let channel = Channel::Group {
|
||||||
id: ulid::Ulid::new().to_string(),
|
id,
|
||||||
|
|
||||||
name: data.name,
|
name: data.name,
|
||||||
owner: owner_id,
|
owner: owner_id,
|
||||||
description: data.description,
|
description: data.description,
|
||||||
recipients: recipients.clone(),
|
recipients: recipients.clone(),
|
||||||
|
|
||||||
icon: None,
|
icon,
|
||||||
last_message_id: None,
|
last_message_id: None,
|
||||||
|
|
||||||
permissions: None,
|
permissions: None,
|
||||||
@@ -320,13 +331,10 @@ impl Channel {
|
|||||||
|
|
||||||
db.insert_channel(&channel).await?;
|
db.insert_channel(&channel).await?;
|
||||||
|
|
||||||
match &channel {
|
if let Channel::DirectMessage { .. } = &channel {
|
||||||
Channel::DirectMessage { .. } => {
|
let event = EventV1::ChannelCreate(channel.clone().into());
|
||||||
let event = EventV1::ChannelCreate(channel.clone().into());
|
event.clone().private(user_a.id.clone()).await;
|
||||||
event.clone().private(user_a.id.clone()).await;
|
event.private(user_b.id.clone()).await;
|
||||||
event.private(user_b.id.clone()).await;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(channel)
|
Ok(channel)
|
||||||
@@ -337,6 +345,7 @@ impl Channel {
|
|||||||
pub async fn add_user_to_group(
|
pub async fn add_user_to_group(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
|
amqp: &AMQP,
|
||||||
user: &User,
|
user: &User,
|
||||||
by_id: &str,
|
by_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
@@ -346,9 +355,9 @@ impl Channel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
if recipients.len() >= config.features.limits.default.group_size {
|
if recipients.len() >= config.features.limits.global.group_size {
|
||||||
return Err(create_error!(GroupTooLarge {
|
return Err(create_error!(GroupTooLarge {
|
||||||
max: config.features.limits.default.group_size
|
max: config.features.limits.global.group_size
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,10 +382,13 @@ impl Channel {
|
|||||||
.into_message(id.to_string())
|
.into_message(id.to_string())
|
||||||
.send(
|
.send(
|
||||||
db,
|
db,
|
||||||
|
Some(amqp),
|
||||||
MessageAuthor::System {
|
MessageAuthor::System {
|
||||||
username: &user.username,
|
username: &user.username,
|
||||||
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
|
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
|
||||||
},
|
},
|
||||||
|
None,
|
||||||
|
None,
|
||||||
self,
|
self,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
@@ -415,13 +427,13 @@ impl Channel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Clone this channel's id
|
/// Clone this channel's id
|
||||||
pub fn id(&self) -> String {
|
pub fn id(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
Channel::DirectMessage { id, .. }
|
Channel::DirectMessage { id, .. }
|
||||||
| Channel::Group { id, .. }
|
| Channel::Group { id, .. }
|
||||||
| Channel::SavedMessages { id, .. }
|
| Channel::SavedMessages { id, .. }
|
||||||
| Channel::TextChannel { id, .. }
|
| Channel::TextChannel { id, .. }
|
||||||
| Channel::VoiceChannel { id, .. } => id.clone(),
|
| Channel::VoiceChannel { id, .. } => id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -637,10 +649,11 @@ impl Channel {
|
|||||||
.private(user.to_string())
|
.private(user.to_string())
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
crate::tasks::ack::queue(
|
#[cfg(feature = "tasks")]
|
||||||
|
crate::tasks::ack::queue_ack(
|
||||||
self.id().to_string(),
|
self.id().to_string(),
|
||||||
user.to_string(),
|
user.to_string(),
|
||||||
AckEvent::AckMessage {
|
crate::tasks::ack::AckEvent::AckMessage {
|
||||||
id: message.to_string(),
|
id: message.to_string(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -653,6 +666,7 @@ impl Channel {
|
|||||||
pub async fn remove_user_from_group(
|
pub async fn remove_user_from_group(
|
||||||
&self,
|
&self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
|
amqp: &AMQP,
|
||||||
user: &User,
|
user: &User,
|
||||||
by_id: Option<&str>,
|
by_id: Option<&str>,
|
||||||
silent: bool,
|
silent: bool,
|
||||||
@@ -684,10 +698,13 @@ impl Channel {
|
|||||||
.into_message(id.to_string())
|
.into_message(id.to_string())
|
||||||
.send(
|
.send(
|
||||||
db,
|
db,
|
||||||
|
Some(amqp),
|
||||||
MessageAuthor::System {
|
MessageAuthor::System {
|
||||||
username: name,
|
username: name,
|
||||||
avatar: None,
|
avatar: None,
|
||||||
},
|
},
|
||||||
|
None,
|
||||||
|
None,
|
||||||
self,
|
self,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
@@ -698,6 +715,8 @@ impl Channel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.remove_user_from_group(id, &user.id).await?;
|
||||||
|
|
||||||
EventV1::ChannelGroupLeave {
|
EventV1::ChannelGroupLeave {
|
||||||
id: id.to_string(),
|
id: id.to_string(),
|
||||||
user: user.id.to_string(),
|
user: user.id.to_string(),
|
||||||
@@ -719,10 +738,13 @@ impl Channel {
|
|||||||
.into_message(id.to_string())
|
.into_message(id.to_string())
|
||||||
.send(
|
.send(
|
||||||
db,
|
db,
|
||||||
|
Some(amqp),
|
||||||
MessageAuthor::System {
|
MessageAuthor::System {
|
||||||
username: &user.username,
|
username: &user.username,
|
||||||
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
|
avatar: user.avatar.as_ref().map(|file| file.id.as_ref()),
|
||||||
},
|
},
|
||||||
|
None,
|
||||||
|
None,
|
||||||
self,
|
self,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
@@ -748,6 +770,7 @@ impl Channel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
impl IntoDocumentPath for FieldsChannel {
|
impl IntoDocumentPath for FieldsChannel {
|
||||||
fn as_path(&self) -> Option<&'static str> {
|
fn as_path(&self) -> Option<&'static str> {
|
||||||
Some(match self {
|
Some(match self {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use crate::{revolt_result::Result, Channel, FieldsChannel, PartialChannel};
|
use crate::{revolt_result::Result, Channel, FieldsChannel, PartialChannel};
|
||||||
use revolt_permissions::OverrideField;
|
use revolt_permissions::OverrideField;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -23,14 +23,11 @@ impl AbstractChannels for MongoDb {
|
|||||||
async fn fetch_channels<'a>(&self, ids: &'a [String]) -> Result<Vec<Channel>> {
|
async fn fetch_channels<'a>(&self, ids: &'a [String]) -> Result<Vec<Channel>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Channel>(COL)
|
.col::<Channel>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"_id": {
|
||||||
"_id": {
|
"$in": ids
|
||||||
"$in": ids
|
}
|
||||||
}
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("fetch", "channels"))?
|
.map_err(|_| create_database_error!("fetch", "channels"))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -119,7 +116,6 @@ impl AbstractChannels for MongoDb {
|
|||||||
"recipients": user
|
"recipients": user
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -141,7 +137,6 @@ impl AbstractChannels for MongoDb {
|
|||||||
"role_permissions.".to_owned() + role: permissions
|
"role_permissions.".to_owned() + role: permissions
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -179,7 +174,6 @@ impl AbstractChannels for MongoDb {
|
|||||||
"recipients": user
|
"recipients": user
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -253,7 +247,6 @@ impl AbstractChannels for MongoDb {
|
|||||||
"_id": server.id
|
"_id": server.id
|
||||||
},
|
},
|
||||||
update,
|
update,
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("update_one", "servers"))?;
|
.map_err(|_| create_database_error!("update_one", "servers"))?;
|
||||||
@@ -261,12 +254,12 @@ impl AbstractChannels for MongoDb {
|
|||||||
|
|
||||||
// Delete associated attachments
|
// Delete associated attachments
|
||||||
self.delete_many_attachments(doc! {
|
self.delete_many_attachments(doc! {
|
||||||
"object_id": &id
|
"used_for.id": &id
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Delete the channel itself
|
// Delete the channel itself
|
||||||
query!(self, delete_one_by_id, COL, &channel.id()).map(|_| ())
|
query!(self, delete_one_by_id, COL, channel.id()).map(|_| ())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,23 +267,17 @@ impl MongoDb {
|
|||||||
pub async fn delete_associated_channel_objects(&self, id: Bson) -> Result<()> {
|
pub async fn delete_associated_channel_objects(&self, id: Bson) -> Result<()> {
|
||||||
// Delete all invites to these channels.
|
// Delete all invites to these channels.
|
||||||
self.col::<Document>("channel_invites")
|
self.col::<Document>("channel_invites")
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"channel": &id
|
||||||
"channel": &id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", "channel_invites"))?;
|
.map_err(|_| create_database_error!("delete_many", "channel_invites"))?;
|
||||||
|
|
||||||
// Delete unread message objects on channels.
|
// Delete unread message objects on channels.
|
||||||
self.col::<Document>("channel_unreads")
|
self.col::<Document>("channel_unreads")
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"_id.channel": &id
|
||||||
"_id.channel": &id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", "channel_unreads"))
|
.map_err(|_| create_database_error!("delete_many", "channel_unreads"))
|
||||||
.map(|_| ())?;
|
.map(|_| ())?;
|
||||||
@@ -299,12 +286,9 @@ impl MongoDb {
|
|||||||
|
|
||||||
// Delete all webhooks on this channel.
|
// Delete all webhooks on this channel.
|
||||||
self.col::<Document>("webhooks")
|
self.col::<Document>("webhooks")
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"channel": &id
|
||||||
"channel": &id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", "webhooks"))
|
.map_err(|_| create_database_error!("delete_many", "webhooks"))
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ impl AbstractChannels for ReferenceDb {
|
|||||||
/// Insert a new channel in the database
|
/// Insert a new channel in the database
|
||||||
async fn insert_channel(&self, channel: &Channel) -> Result<()> {
|
async fn insert_channel(&self, channel: &Channel) -> Result<()> {
|
||||||
let mut channels = self.channels.lock().await;
|
let mut channels = self.channels.lock().await;
|
||||||
if let Entry::Vacant(entry) = channels.entry(channel.id()) {
|
if let Entry::Vacant(entry) = channels.entry(channel.id().to_string()) {
|
||||||
entry.insert(channel.clone());
|
entry.insert(channel.clone());
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@@ -148,7 +148,7 @@ impl AbstractChannels for ReferenceDb {
|
|||||||
// Delete a channel
|
// Delete a channel
|
||||||
async fn delete_channel(&self, channel: &Channel) -> Result<()> {
|
async fn delete_channel(&self, channel: &Channel) -> Result<()> {
|
||||||
let mut channels = self.channels.lock().await;
|
let mut channels = self.channels.lock().await;
|
||||||
if channels.remove(&channel.id()).is_some() {
|
if channels.remove(channel.id()).is_some() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(create_error!(NotFound))
|
Err(create_error!(NotFound))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::Emoji;
|
use crate::Emoji;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ impl AbstractEmojis for MongoDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
mod model;
|
||||||
|
mod ops;
|
||||||
|
|
||||||
|
pub use model::*;
|
||||||
|
pub use ops::*;
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
|
|
||||||
|
use crate::File;
|
||||||
|
|
||||||
|
auto_derived_partial!(
|
||||||
|
/// File hash
|
||||||
|
pub struct FileHash {
|
||||||
|
/// Sha256 hash of the file
|
||||||
|
#[serde(rename = "_id")]
|
||||||
|
pub id: String,
|
||||||
|
/// Sha256 hash of file after it has been processed
|
||||||
|
pub processed_hash: String,
|
||||||
|
|
||||||
|
/// When this file was created in system
|
||||||
|
pub created_at: Timestamp,
|
||||||
|
|
||||||
|
/// The bucket this file is stored in
|
||||||
|
pub bucket_id: String,
|
||||||
|
/// The path at which this file exists in
|
||||||
|
pub path: String,
|
||||||
|
/// Cryptographic nonce used to encrypt this file
|
||||||
|
pub iv: String,
|
||||||
|
|
||||||
|
/// Parsed metadata of this file
|
||||||
|
pub metadata: Metadata,
|
||||||
|
/// Raw content type of this file
|
||||||
|
pub content_type: String,
|
||||||
|
/// Size of this file (in bytes)
|
||||||
|
pub size: isize,
|
||||||
|
},
|
||||||
|
"PartialFileHash"
|
||||||
|
);
|
||||||
|
|
||||||
|
auto_derived!(
|
||||||
|
/// Metadata associated with a file
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
#[derive(Default)]
|
||||||
|
pub enum Metadata {
|
||||||
|
/// File is just a generic uncategorised file
|
||||||
|
#[default]
|
||||||
|
File,
|
||||||
|
/// File contains textual data and should be displayed as such
|
||||||
|
Text,
|
||||||
|
/// File is an image with specific dimensions
|
||||||
|
Image {
|
||||||
|
width: isize,
|
||||||
|
height: isize,
|
||||||
|
// animated: bool // TODO: https://docs.rs/image/latest/image/trait.AnimationDecoder.html for APNG support
|
||||||
|
},
|
||||||
|
/// File is a video with specific dimensions
|
||||||
|
Video { width: isize, height: isize },
|
||||||
|
/// File is audio
|
||||||
|
Audio,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
impl FileHash {
|
||||||
|
/// Create a file from a file hash
|
||||||
|
pub fn into_file(
|
||||||
|
&self,
|
||||||
|
id: String,
|
||||||
|
tag: String,
|
||||||
|
filename: String,
|
||||||
|
uploader_id: String,
|
||||||
|
) -> File {
|
||||||
|
File {
|
||||||
|
id,
|
||||||
|
tag,
|
||||||
|
filename,
|
||||||
|
hash: Some(self.id.clone()),
|
||||||
|
|
||||||
|
uploaded_at: Some(Timestamp::now_utc()),
|
||||||
|
uploader_id: Some(uploader_id),
|
||||||
|
|
||||||
|
used_for: None,
|
||||||
|
|
||||||
|
deleted: None,
|
||||||
|
reported: None,
|
||||||
|
|
||||||
|
// TODO: remove this data
|
||||||
|
metadata: self.metadata.clone(),
|
||||||
|
content_type: self.content_type.clone(),
|
||||||
|
size: self.size,
|
||||||
|
|
||||||
|
// TODO: superseded by "used_for"
|
||||||
|
message_id: None,
|
||||||
|
object_id: None,
|
||||||
|
server_id: None,
|
||||||
|
user_id: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use crate::FileHash;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
mod mongodb;
|
||||||
|
mod reference;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait AbstractAttachmentHashes: Sync + Send {
|
||||||
|
/// Insert a new attachment hash into the database.
|
||||||
|
async fn insert_attachment_hash(&self, hash: &FileHash) -> Result<()>;
|
||||||
|
|
||||||
|
/// Fetch an attachment hash entry by sha256 hash.
|
||||||
|
async fn fetch_attachment_hash(&self, hash: &str) -> Result<FileHash>;
|
||||||
|
|
||||||
|
/// Update an attachment hash nonce value.
|
||||||
|
async fn set_attachment_hash_nonce(&self, hash: &str, nonce: &str) -> Result<()>;
|
||||||
|
|
||||||
|
/// Delete attachment hash by id.
|
||||||
|
async fn delete_attachment_hash(&self, id: &str) -> Result<()>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use crate::FileHash;
|
||||||
|
use crate::MongoDb;
|
||||||
|
|
||||||
|
use super::AbstractAttachmentHashes;
|
||||||
|
|
||||||
|
static COL: &str = "attachment_hashes";
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl AbstractAttachmentHashes for MongoDb {
|
||||||
|
/// Insert a new attachment hash into the database.
|
||||||
|
async fn insert_attachment_hash(&self, hash: &FileHash) -> Result<()> {
|
||||||
|
query!(self, insert_one, COL, &hash).map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch an attachment hash entry by sha256 hash.
|
||||||
|
async fn fetch_attachment_hash(&self, hash: &str) -> Result<FileHash> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
find_one,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"$or": [
|
||||||
|
{"_id": hash},
|
||||||
|
{"processed_hash": hash}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)?
|
||||||
|
.ok_or_else(|| create_error!(NotFound))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update an attachment hash nonce value.
|
||||||
|
async fn set_attachment_hash_nonce(&self, hash: &str, nonce: &str) -> Result<()> {
|
||||||
|
self.col::<FileHash>(COL)
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": hash
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$set": {
|
||||||
|
"iv": nonce
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|_| create_database_error!("update_one", COL))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete attachment hash by id.
|
||||||
|
async fn delete_attachment_hash(&self, id: &str) -> Result<()> {
|
||||||
|
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use crate::FileHash;
|
||||||
|
use crate::ReferenceDb;
|
||||||
|
|
||||||
|
use super::AbstractAttachmentHashes;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl AbstractAttachmentHashes for ReferenceDb {
|
||||||
|
/// Insert a new attachment hash into the database.
|
||||||
|
async fn insert_attachment_hash(&self, hash: &FileHash) -> Result<()> {
|
||||||
|
let mut hashes = self.file_hashes.lock().await;
|
||||||
|
if hashes.contains_key(&hash.id) {
|
||||||
|
Err(create_database_error!("insert", "attachment"))
|
||||||
|
} else {
|
||||||
|
hashes.insert(hash.id.to_string(), hash.clone());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch an attachment hash entry by sha256 hash.
|
||||||
|
async fn fetch_attachment_hash(&self, hash_value: &str) -> Result<FileHash> {
|
||||||
|
let hashes = self.file_hashes.lock().await;
|
||||||
|
hashes
|
||||||
|
.values()
|
||||||
|
.find(|&hash| hash.id == hash_value || hash.processed_hash == hash_value)
|
||||||
|
.cloned()
|
||||||
|
.ok_or(create_error!(NotFound))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update an attachment hash nonce value.
|
||||||
|
async fn set_attachment_hash_nonce(&self, hash: &str, nonce: &str) -> Result<()> {
|
||||||
|
let mut hashes = self.file_hashes.lock().await;
|
||||||
|
if let Some(file) = hashes.get_mut(hash) {
|
||||||
|
file.iv = nonce.to_owned();
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(create_error!(NotFound))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete attachment hash by id.
|
||||||
|
async fn delete_attachment_hash(&self, id: &str) -> Result<()> {
|
||||||
|
let mut file_hashes = self.file_hashes.lock().await;
|
||||||
|
if file_hashes.remove(id).is_some() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(create_error!(NotFound))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use crate::Database;
|
use crate::{Database, FileHash, Metadata};
|
||||||
|
|
||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
auto_derived_partial!(
|
auto_derived_partial!(
|
||||||
@@ -12,12 +13,18 @@ auto_derived_partial!(
|
|||||||
pub tag: String,
|
pub tag: String,
|
||||||
/// Original filename
|
/// Original filename
|
||||||
pub filename: String,
|
pub filename: String,
|
||||||
/// Parsed metadata of this file
|
/// Hash of this file
|
||||||
pub metadata: Metadata,
|
pub hash: Option<String>, // these are Option<>s to not break file uploads on legacy Autumn
|
||||||
/// Raw content type of this file
|
|
||||||
pub content_type: String,
|
/// When this file was uploaded
|
||||||
/// Size of this file (in bytes)
|
pub uploaded_at: Option<Timestamp>, // these are Option<>s to not break file uploads on legacy Autumn
|
||||||
pub size: isize,
|
/// ID of user who uploaded this file
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub uploader_id: Option<String>, // these are Option<>s to not break file uploads on legacy Autumn
|
||||||
|
|
||||||
|
/// What the file was used for
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub used_for: Option<FileUsedFor>,
|
||||||
|
|
||||||
/// Whether this file was deleted
|
/// Whether this file was deleted
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
@@ -26,6 +33,14 @@ auto_derived_partial!(
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub reported: Option<bool>,
|
pub reported: Option<bool>,
|
||||||
|
|
||||||
|
// !!! DEPRECATED:
|
||||||
|
/// Parsed metadata of this file
|
||||||
|
pub metadata: Metadata,
|
||||||
|
/// Raw content type of this file
|
||||||
|
pub content_type: String,
|
||||||
|
/// Size of this file (in bytes)
|
||||||
|
pub size: isize,
|
||||||
|
|
||||||
// TODO: migrate this mess to having:
|
// TODO: migrate this mess to having:
|
||||||
// - author_id
|
// - author_id
|
||||||
// - parent: Parent { Message(id), User(id), etc }
|
// - parent: Parent { Message(id), User(id), etc }
|
||||||
@@ -44,64 +59,184 @@ auto_derived_partial!(
|
|||||||
);
|
);
|
||||||
|
|
||||||
auto_derived!(
|
auto_derived!(
|
||||||
/// Metadata associated with a file
|
/// Type of object file was used for
|
||||||
#[serde(tag = "type")]
|
pub enum FileUsedForType {
|
||||||
#[derive(Default)]
|
Message,
|
||||||
pub enum Metadata {
|
ServerBanner,
|
||||||
/// File is just a generic uncategorised file
|
Emoji,
|
||||||
#[default]
|
UserAvatar,
|
||||||
File,
|
WebhookAvatar,
|
||||||
/// File contains textual data and should be displayed as such
|
UserProfileBackground,
|
||||||
Text,
|
LegacyGroupIcon,
|
||||||
/// File is an image with specific dimensions
|
ChannelIcon,
|
||||||
Image { width: isize, height: isize },
|
ServerIcon,
|
||||||
/// File is a video with specific dimensions
|
}
|
||||||
Video { width: isize, height: isize },
|
|
||||||
/// File is audio
|
/// Information about what the file was used for
|
||||||
Audio,
|
pub struct FileUsedFor {
|
||||||
|
/// Type of the object
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub object_type: FileUsedForType,
|
||||||
|
/// ID of the object
|
||||||
|
pub id: String,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
|
/// Get the hash entry for this file
|
||||||
|
pub async fn as_hash(&self, db: &Database) -> Result<FileHash> {
|
||||||
|
db.fetch_attachment_hash(self.hash.as_ref().unwrap()).await
|
||||||
|
}
|
||||||
|
|
||||||
/// Use a file for a message attachment
|
/// Use a file for a message attachment
|
||||||
pub async fn use_attachment(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_attachment(
|
||||||
db.find_and_use_attachment(id, "attachments", "message", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"attachments",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::Message,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use a file for a user profile background
|
/// Use a file for a user profile background
|
||||||
pub async fn use_background(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_background(
|
||||||
db.find_and_use_attachment(id, "backgrounds", "user", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"backgrounds",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::UserProfileBackground,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use a file for a user avatar
|
/// Use a file for a user avatar
|
||||||
pub async fn use_avatar(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_user_avatar(
|
||||||
db.find_and_use_attachment(id, "avatars", "user", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"avatars",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::UserAvatar,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use a file for an icon
|
/// Use a file for a webhook avatar
|
||||||
pub async fn use_icon(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_webhook_avatar(
|
||||||
db.find_and_use_attachment(id, "icons", "object", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"avatars",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::WebhookAvatar,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use a file for a server icon
|
/// Use a file for a server icon
|
||||||
pub async fn use_server_icon(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_server_icon(
|
||||||
db.find_and_use_attachment(id, "icons", "object", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"icons",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::ServerIcon,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Use a file for a channel icon
|
||||||
|
pub async fn use_channel_icon(
|
||||||
|
db: &Database,
|
||||||
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"icons",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::ChannelIcon,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use a file for a server banner
|
/// Use a file for a server banner
|
||||||
pub async fn use_banner(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_server_banner(
|
||||||
db.find_and_use_attachment(id, "banners", "server", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"banners",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::ServerBanner,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Use a file for an emoji
|
/// Use a file for an emoji
|
||||||
pub async fn use_emoji(db: &Database, id: &str, parent: &str) -> Result<File> {
|
pub async fn use_emoji(
|
||||||
db.find_and_use_attachment(id, "emojis", "object", parent)
|
db: &Database,
|
||||||
.await
|
id: &str,
|
||||||
|
parent: &str,
|
||||||
|
uploader_id: &str,
|
||||||
|
) -> Result<File> {
|
||||||
|
db.find_and_use_attachment(
|
||||||
|
id,
|
||||||
|
"emojis",
|
||||||
|
FileUsedFor {
|
||||||
|
id: parent.to_owned(),
|
||||||
|
object_type: FileUsedForType::Emoji,
|
||||||
|
},
|
||||||
|
uploader_id.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::File;
|
use crate::File;
|
||||||
|
|
||||||
|
use super::FileUsedFor;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
@@ -10,13 +13,25 @@ pub trait AbstractAttachments: Sync + Send {
|
|||||||
/// Insert attachment into database.
|
/// Insert attachment into database.
|
||||||
async fn insert_attachment(&self, attachment: &File) -> Result<()>;
|
async fn insert_attachment(&self, attachment: &File) -> Result<()>;
|
||||||
|
|
||||||
|
/// Fetch an attachment by its id.
|
||||||
|
async fn fetch_attachment(&self, tag: &str, file_id: &str) -> Result<File>;
|
||||||
|
|
||||||
|
/// Fetch all deleted attachments.
|
||||||
|
async fn fetch_deleted_attachments(&self) -> Result<Vec<File>>;
|
||||||
|
|
||||||
|
/// Fetch all dangling attachments.
|
||||||
|
async fn fetch_dangling_files(&self) -> Result<Vec<File>>;
|
||||||
|
|
||||||
|
/// Count references to a given hash.
|
||||||
|
async fn count_file_hash_references(&self, hash: &str) -> Result<usize>;
|
||||||
|
|
||||||
/// Find an attachment by its details and mark it as used by a given parent.
|
/// Find an attachment by its details and mark it as used by a given parent.
|
||||||
async fn find_and_use_attachment(
|
async fn find_and_use_attachment(
|
||||||
&self,
|
&self,
|
||||||
id: &str,
|
id: &str,
|
||||||
tag: &str,
|
tag: &str,
|
||||||
parent_type: &str,
|
used_for: FileUsedFor,
|
||||||
parent_id: &str,
|
uploader_id: String,
|
||||||
) -> Result<File>;
|
) -> Result<File>;
|
||||||
|
|
||||||
/// Mark an attachment as having been reported.
|
/// Mark an attachment as having been reported.
|
||||||
@@ -27,4 +42,7 @@ pub trait AbstractAttachments: Sync + Send {
|
|||||||
|
|
||||||
/// Mark multiple attachments as having been deleted.
|
/// Mark multiple attachments as having been deleted.
|
||||||
async fn mark_attachments_as_deleted(&self, ids: &[String]) -> Result<()>;
|
async fn mark_attachments_as_deleted(&self, ids: &[String]) -> Result<()>;
|
||||||
|
|
||||||
|
/// Delete the attachment entry.
|
||||||
|
async fn delete_attachment(&self, id: &str) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
use bson::to_document;
|
||||||
use bson::Document;
|
use bson::Document;
|
||||||
|
use revolt_config::report_internal_error;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::File;
|
use crate::File;
|
||||||
|
use crate::FileUsedFor;
|
||||||
use crate::MongoDb;
|
use crate::MongoDb;
|
||||||
|
|
||||||
use super::AbstractAttachments;
|
use super::AbstractAttachments;
|
||||||
@@ -15,15 +18,73 @@ impl AbstractAttachments for MongoDb {
|
|||||||
query!(self, insert_one, COL, &attachment).map(|_| ())
|
query!(self, insert_one, COL, &attachment).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch an attachment by its id.
|
||||||
|
async fn fetch_attachment(&self, tag: &str, file_id: &str) -> Result<File> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
find_one,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"_id": file_id,
|
||||||
|
"tag": tag
|
||||||
|
}
|
||||||
|
)?
|
||||||
|
.ok_or_else(|| create_error!(NotFound))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch all deleted attachments.
|
||||||
|
async fn fetch_deleted_attachments(&self) -> Result<Vec<File>> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
find,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"deleted": true,
|
||||||
|
"reported": {
|
||||||
|
"$ne": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch all dangling attachments.
|
||||||
|
async fn fetch_dangling_files(&self) -> Result<Vec<File>> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
find,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"used_for.type": {
|
||||||
|
"$exists": 0
|
||||||
|
},
|
||||||
|
"deleted": {
|
||||||
|
"$ne": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Count references to a given hash.
|
||||||
|
async fn count_file_hash_references(&self, hash: &str) -> Result<usize> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
count_documents,
|
||||||
|
COL,
|
||||||
|
doc! {
|
||||||
|
"hash": hash
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.map(|count| count as usize)
|
||||||
|
}
|
||||||
|
|
||||||
/// Find an attachment by its details and mark it as used by a given parent.
|
/// Find an attachment by its details and mark it as used by a given parent.
|
||||||
async fn find_and_use_attachment(
|
async fn find_and_use_attachment(
|
||||||
&self,
|
&self,
|
||||||
id: &str,
|
id: &str,
|
||||||
tag: &str,
|
tag: &str,
|
||||||
parent_type: &str,
|
used_for: FileUsedFor,
|
||||||
parent_id: &str,
|
uploader_id: String,
|
||||||
) -> Result<File> {
|
) -> Result<File> {
|
||||||
let key = format!("{parent_type}_id");
|
|
||||||
let file = query!(
|
let file = query!(
|
||||||
self,
|
self,
|
||||||
find_one,
|
find_one,
|
||||||
@@ -31,7 +92,7 @@ impl AbstractAttachments for MongoDb {
|
|||||||
doc! {
|
doc! {
|
||||||
"_id": id,
|
"_id": id,
|
||||||
"tag": tag,
|
"tag": tag,
|
||||||
&key: {
|
"used_for": {
|
||||||
"$exists": false
|
"$exists": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,10 +106,10 @@ impl AbstractAttachments for MongoDb {
|
|||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"$set": {
|
||||||
key: parent_id
|
"used_for": report_internal_error!(to_document(&used_for))?,
|
||||||
|
"uploader_id": uploader_id
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("update_one", COL))?;
|
.map_err(|_| create_database_error!("update_one", COL))?;
|
||||||
@@ -68,7 +129,6 @@ impl AbstractAttachments for MongoDb {
|
|||||||
"reported": true
|
"reported": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -87,7 +147,6 @@ impl AbstractAttachments for MongoDb {
|
|||||||
"deleted": true
|
"deleted": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -97,7 +156,7 @@ impl AbstractAttachments for MongoDb {
|
|||||||
/// Mark multiple attachments as having been deleted.
|
/// Mark multiple attachments as having been deleted.
|
||||||
async fn mark_attachments_as_deleted(&self, ids: &[String]) -> Result<()> {
|
async fn mark_attachments_as_deleted(&self, ids: &[String]) -> Result<()> {
|
||||||
self.col::<Document>(COL)
|
self.col::<Document>(COL)
|
||||||
.update_one(
|
.update_many(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": {
|
"_id": {
|
||||||
"$in": ids
|
"$in": ids
|
||||||
@@ -108,11 +167,15 @@ impl AbstractAttachments for MongoDb {
|
|||||||
"deleted": true
|
"deleted": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| create_database_error!("update_one", COL))
|
.map_err(|_| create_database_error!("update_many", COL))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete the attachment entry.
|
||||||
|
async fn delete_attachment(&self, id: &str) -> Result<()> {
|
||||||
|
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +189,6 @@ impl MongoDb {
|
|||||||
"deleted": true
|
"deleted": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::File;
|
use crate::File;
|
||||||
|
use crate::FileUsedFor;
|
||||||
use crate::ReferenceDb;
|
use crate::ReferenceDb;
|
||||||
|
|
||||||
use super::AbstractAttachments;
|
use super::AbstractAttachments;
|
||||||
@@ -18,24 +19,68 @@ impl AbstractAttachments for ReferenceDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch an attachment by its id.
|
||||||
|
async fn fetch_attachment(&self, tag: &str, file_id: &str) -> Result<File> {
|
||||||
|
let files = self.files.lock().await;
|
||||||
|
if let Some(file) = files.get(file_id) {
|
||||||
|
if file.tag == tag {
|
||||||
|
Ok(file.clone())
|
||||||
|
} else {
|
||||||
|
Err(create_error!(NotFound))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(create_error!(NotFound))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch all deleted attachments.
|
||||||
|
async fn fetch_deleted_attachments(&self) -> Result<Vec<File>> {
|
||||||
|
let files = self.files.lock().await;
|
||||||
|
Ok(files
|
||||||
|
.values()
|
||||||
|
.filter(|file| {
|
||||||
|
// file has been marked as deleted
|
||||||
|
file.deleted.is_some_and(|v| v)
|
||||||
|
// and it has not been reported
|
||||||
|
&& !file.reported.is_some_and(|v| v)
|
||||||
|
})
|
||||||
|
.cloned()
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch all dangling attachments.
|
||||||
|
async fn fetch_dangling_files(&self) -> Result<Vec<File>> {
|
||||||
|
let files = self.files.lock().await;
|
||||||
|
Ok(files
|
||||||
|
.values()
|
||||||
|
.filter(|file| file.used_for.is_none() && !file.deleted.is_some_and(|v| v))
|
||||||
|
.cloned()
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Count references to a given hash.
|
||||||
|
async fn count_file_hash_references(&self, hash: &str) -> Result<usize> {
|
||||||
|
let files = self.files.lock().await;
|
||||||
|
Ok(files
|
||||||
|
.values()
|
||||||
|
.filter(|file| file.hash.as_ref().is_some_and(|h| h == hash))
|
||||||
|
.cloned()
|
||||||
|
.count())
|
||||||
|
}
|
||||||
|
|
||||||
/// Find an attachment by its details and mark it as used by a given parent.
|
/// Find an attachment by its details and mark it as used by a given parent.
|
||||||
async fn find_and_use_attachment(
|
async fn find_and_use_attachment(
|
||||||
&self,
|
&self,
|
||||||
id: &str,
|
id: &str,
|
||||||
tag: &str,
|
tag: &str,
|
||||||
parent_type: &str,
|
used_for: FileUsedFor,
|
||||||
parent_id: &str,
|
uploader_id: String,
|
||||||
) -> Result<File> {
|
) -> Result<File> {
|
||||||
let mut files = self.files.lock().await;
|
let mut files = self.files.lock().await;
|
||||||
if let Some(file) = files.get_mut(id) {
|
if let Some(file) = files.get_mut(id) {
|
||||||
if file.tag == tag {
|
if file.tag == tag {
|
||||||
match parent_type {
|
file.uploader_id = Some(uploader_id);
|
||||||
"message" => file.message_id = Some(parent_id.to_owned()),
|
file.used_for = Some(used_for);
|
||||||
"user" => file.user_id = Some(parent_id.to_owned()),
|
|
||||||
"object" => file.object_id = Some(parent_id.to_owned()),
|
|
||||||
"server" => file.server_id = Some(parent_id.to_owned()),
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(file.clone())
|
Ok(file.clone())
|
||||||
} else {
|
} else {
|
||||||
@@ -86,4 +131,14 @@ impl AbstractAttachments for ReferenceDb {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete the attachment entry.
|
||||||
|
async fn delete_attachment(&self, id: &str) -> Result<()> {
|
||||||
|
let mut files = self.files.lock().await;
|
||||||
|
if files.remove(id).is_some() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(create_error!(NotFound))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,29 @@
|
|||||||
use std::collections::HashSet;
|
use std::{collections::HashSet, hash::RandomState};
|
||||||
|
|
||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
use iso8601_timestamp::Timestamp;
|
use iso8601_timestamp::Timestamp;
|
||||||
use revolt_config::config;
|
use revolt_config::{config, FeaturesLimits};
|
||||||
use revolt_models::v0::{
|
use revolt_models::v0::{
|
||||||
self, BulkMessageResponse, DataMessageSend, Embed, MessageAuthor, MessageSort, MessageWebhook,
|
self, BulkMessageResponse, DataMessageSend, Embed, MessageAuthor, MessageFlags, MessageSort,
|
||||||
PushNotification, ReplyIntent, SendableEmbed, Text, RE_MENTION,
|
MessageWebhook, PushNotification, ReplyIntent, SendableEmbed, Text,
|
||||||
};
|
};
|
||||||
use revolt_permissions::{ChannelPermission, PermissionValue};
|
use revolt_permissions::{calculate_channel_permissions, ChannelPermission, PermissionValue};
|
||||||
use revolt_result::Result;
|
use revolt_result::{ErrorType, Result};
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
events::client::EventV1,
|
events::client::EventV1,
|
||||||
tasks::{self, ack::AckEvent},
|
util::{
|
||||||
util::idempotency::IdempotencyKey,
|
bulk_permissions::BulkDatabasePermissionQuery, idempotency::IdempotencyKey,
|
||||||
Channel, Database, Emoji, File, User,
|
permissions::DatabasePermissionQuery,
|
||||||
|
},
|
||||||
|
Channel, Database, Emoji, File, User, AMQP,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "tasks")]
|
||||||
|
use crate::tasks::{self, ack::AckEvent};
|
||||||
|
|
||||||
auto_derived_partial!(
|
auto_derived_partial!(
|
||||||
/// Message
|
/// Message
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
@@ -53,6 +58,9 @@ auto_derived_partial!(
|
|||||||
/// Array of user ids mentioned in this message
|
/// Array of user ids mentioned in this message
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub mentions: Option<Vec<String>>,
|
pub mentions: Option<Vec<String>>,
|
||||||
|
/// Array of role ids mentioned in this message
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub role_mentions: Option<Vec<String>>,
|
||||||
/// Array of message ids this message is replying to
|
/// Array of message ids this message is replying to
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub replies: Option<Vec<String>>,
|
pub replies: Option<Vec<String>>,
|
||||||
@@ -65,6 +73,13 @@ auto_derived_partial!(
|
|||||||
/// Name and / or avatar overrides for this message
|
/// Name and / or avatar overrides for this message
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub masquerade: Option<Masquerade>,
|
pub masquerade: Option<Masquerade>,
|
||||||
|
/// Whether or not the message in pinned
|
||||||
|
#[serde(skip_serializing_if = "crate::if_option_false")]
|
||||||
|
pub pinned: Option<bool>,
|
||||||
|
|
||||||
|
/// Bitfield of message flags
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub flags: Option<u32>,
|
||||||
},
|
},
|
||||||
"PartialMessage"
|
"PartialMessage"
|
||||||
);
|
);
|
||||||
@@ -95,6 +110,10 @@ auto_derived!(
|
|||||||
ChannelIconChanged { by: String },
|
ChannelIconChanged { by: String },
|
||||||
#[serde(rename = "channel_ownership_changed")]
|
#[serde(rename = "channel_ownership_changed")]
|
||||||
ChannelOwnershipChanged { from: String, to: String },
|
ChannelOwnershipChanged { from: String, to: String },
|
||||||
|
#[serde(rename = "message_pinned")]
|
||||||
|
MessagePinned { id: String, by: String },
|
||||||
|
#[serde(rename = "message_unpinned")]
|
||||||
|
MessageUnpinned { id: String, by: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Name and / or avatar override information
|
/// Name and / or avatar override information
|
||||||
@@ -164,6 +183,8 @@ auto_derived!(
|
|||||||
pub author: Option<String>,
|
pub author: Option<String>,
|
||||||
/// Search query
|
/// Search query
|
||||||
pub query: Option<String>,
|
pub query: Option<String>,
|
||||||
|
/// Search for pinned
|
||||||
|
pub pinned: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Message Query
|
/// Message Query
|
||||||
@@ -179,8 +200,37 @@ auto_derived!(
|
|||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub time_period: MessageTimePeriod,
|
pub time_period: MessageTimePeriod,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Optional fields on message
|
||||||
|
pub enum FieldsMessage {
|
||||||
|
Pinned,
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
pub struct MessageFlagsValue(pub u32);
|
||||||
|
|
||||||
|
impl MessageFlagsValue {
|
||||||
|
pub fn has(&self, flag: MessageFlags) -> bool {
|
||||||
|
self.has_value(flag as u32)
|
||||||
|
}
|
||||||
|
pub fn has_value(&self, bit: u32) -> bool {
|
||||||
|
let mask = 1 << bit;
|
||||||
|
self.0 & mask == mask
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, flag: MessageFlags, toggle: bool) -> &mut Self {
|
||||||
|
self.set_value(flag as u32, toggle)
|
||||||
|
}
|
||||||
|
pub fn set_value(&mut self, bit: u32, toggle: bool) -> &mut Self {
|
||||||
|
if toggle {
|
||||||
|
self.0 |= 1 << bit;
|
||||||
|
} else {
|
||||||
|
self.0 &= !(1 << bit);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::derivable_impls)]
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for Message {
|
impl Default for Message {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
@@ -196,10 +246,13 @@ impl Default for Message {
|
|||||||
edited: None,
|
edited: None,
|
||||||
embeds: None,
|
embeds: None,
|
||||||
mentions: None,
|
mentions: None,
|
||||||
|
role_mentions: None,
|
||||||
replies: None,
|
replies: None,
|
||||||
reactions: Default::default(),
|
reactions: Default::default(),
|
||||||
interactions: Default::default(),
|
interactions: Default::default(),
|
||||||
masquerade: None,
|
masquerade: None,
|
||||||
|
flags: None,
|
||||||
|
pinned: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,11 +260,16 @@ impl Default for Message {
|
|||||||
#[allow(clippy::disallowed_methods)]
|
#[allow(clippy::disallowed_methods)]
|
||||||
impl Message {
|
impl Message {
|
||||||
/// Create message from API data
|
/// Create message from API data
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub async fn create_from_api(
|
pub async fn create_from_api(
|
||||||
db: &Database,
|
db: &Database,
|
||||||
|
amqp: Option<&AMQP>,
|
||||||
channel: Channel,
|
channel: Channel,
|
||||||
data: DataMessageSend,
|
data: DataMessageSend,
|
||||||
author: MessageAuthor<'_>,
|
author: MessageAuthor<'_>,
|
||||||
|
user: Option<v0::User>,
|
||||||
|
member: Option<v0::Member>,
|
||||||
|
limits: FeaturesLimits,
|
||||||
mut idempotency: IdempotencyKey,
|
mut idempotency: IdempotencyKey,
|
||||||
generate_embeds: bool,
|
generate_embeds: bool,
|
||||||
allow_mentions: bool,
|
allow_mentions: bool,
|
||||||
@@ -221,7 +279,7 @@ impl Message {
|
|||||||
Message::validate_sum(
|
Message::validate_sum(
|
||||||
&data.content,
|
&data.content,
|
||||||
data.embeds.as_deref().unwrap_or_default(),
|
data.embeds.as_deref().unwrap_or_default(),
|
||||||
config.features.limits.default.message_length,
|
limits.message_length,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
idempotency
|
idempotency
|
||||||
@@ -230,13 +288,50 @@ impl Message {
|
|||||||
.map_err(|_| create_error!(InvalidOperation))?;
|
.map_err(|_| create_error!(InvalidOperation))?;
|
||||||
|
|
||||||
// Check the message is not empty
|
// Check the message is not empty
|
||||||
if (data.content.as_ref().map_or(true, |v| v.is_empty()))
|
if (data.content.as_ref().is_none_or(|v| v.is_empty()))
|
||||||
&& (data.attachments.as_ref().map_or(true, |v| v.is_empty()))
|
&& (data.attachments.as_ref().is_none_or(|v| v.is_empty()))
|
||||||
&& (data.embeds.as_ref().map_or(true, |v| v.is_empty()))
|
&& (data.embeds.as_ref().is_none_or(|v| v.is_empty()))
|
||||||
{
|
{
|
||||||
return Err(create_error!(EmptyMessage));
|
return Err(create_error!(EmptyMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let allow_mass_mentions = allow_mentions && config.features.mass_mentions_enabled;
|
||||||
|
|
||||||
|
let mut mentions_everyone = false;
|
||||||
|
let mut mentions_online = false;
|
||||||
|
let mut suppress_notifications = false;
|
||||||
|
|
||||||
|
if let Some(raw_flags) = &data.flags {
|
||||||
|
if raw_flags > &7 {
|
||||||
|
// quick path to failure: bigger than all the bits combined
|
||||||
|
return Err(create_error!(InvalidProperty));
|
||||||
|
}
|
||||||
|
|
||||||
|
// First step of mass mention resolution
|
||||||
|
let flags = MessageFlagsValue(*raw_flags);
|
||||||
|
suppress_notifications = flags.has(MessageFlags::SuppressNotifications);
|
||||||
|
mentions_everyone = allow_mentions && flags.has(MessageFlags::MentionsEveryone);
|
||||||
|
mentions_online = allow_mentions && flags.has(MessageFlags::MentionsOnline);
|
||||||
|
|
||||||
|
// Not a bot, and attempting to set mention flags
|
||||||
|
if user.as_ref().is_some_and(|u| u.bot.as_ref().is_none())
|
||||||
|
&& (mentions_everyone || mentions_online)
|
||||||
|
{
|
||||||
|
return Err(create_error!(IsNotBot));
|
||||||
|
}
|
||||||
|
|
||||||
|
if mentions_everyone && mentions_online {
|
||||||
|
return Err(create_error!(InvalidFlagValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let server_id = match channel {
|
||||||
|
Channel::TextChannel { ref server, .. } | Channel::VoiceChannel { ref server, .. } => {
|
||||||
|
Some(server.clone())
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
// Ensure restrict_reactions is not specified without reactions list
|
// Ensure restrict_reactions is not specified without reactions list
|
||||||
if let Some(interactions) = &data.interactions {
|
if let Some(interactions) = &data.interactions {
|
||||||
if interactions.restrict_reactions {
|
if interactions.restrict_reactions {
|
||||||
@@ -262,7 +357,7 @@ impl Message {
|
|||||||
let message_id = Ulid::new().to_string();
|
let message_id = Ulid::new().to_string();
|
||||||
let mut message = Message {
|
let mut message = Message {
|
||||||
id: message_id.clone(),
|
id: message_id.clone(),
|
||||||
channel: channel.id(),
|
channel: channel.id().to_string(),
|
||||||
masquerade: data.masquerade.map(|masquerade| masquerade.into()),
|
masquerade: data.masquerade.map(|masquerade| masquerade.into()),
|
||||||
interactions: data
|
interactions: data
|
||||||
.interactions
|
.interactions
|
||||||
@@ -270,17 +365,76 @@ impl Message {
|
|||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
author: author_id,
|
author: author_id,
|
||||||
webhook: webhook.map(|w| w.into()),
|
webhook: webhook.map(|w| w.into()),
|
||||||
|
flags: data.flags,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Parse mentions in message.
|
// Parse mentions in message.
|
||||||
let mut mentions = HashSet::new();
|
|
||||||
if allow_mentions {
|
let mut message_mentions = if let Some(raw_content) = &data.content {
|
||||||
if let Some(content) = &data.content {
|
revolt_parser::parse_message(raw_content)
|
||||||
for capture in RE_MENTION.captures_iter(content) {
|
} else {
|
||||||
if let Some(mention) = capture.get(1) {
|
revolt_parser::MessageResults::default()
|
||||||
mentions.insert(mention.as_str().to_string());
|
};
|
||||||
}
|
|
||||||
|
message_mentions.mentions_everyone |= mentions_everyone;
|
||||||
|
message_mentions.mentions_online |= mentions_online;
|
||||||
|
|
||||||
|
let revolt_parser::MessageResults {
|
||||||
|
mut user_mentions,
|
||||||
|
mut role_mentions,
|
||||||
|
mut mentions_everyone,
|
||||||
|
mut mentions_online,
|
||||||
|
} = message_mentions;
|
||||||
|
|
||||||
|
if allow_mass_mentions && server_id.is_some() && !role_mentions.is_empty() {
|
||||||
|
let server_data = db
|
||||||
|
.fetch_server(server_id.unwrap().as_str())
|
||||||
|
.await
|
||||||
|
.expect("Failed to fetch server");
|
||||||
|
|
||||||
|
role_mentions.retain(|role_id| server_data.roles.contains_key(role_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the user can perform a mass mention
|
||||||
|
if !config.features.mass_mentions_enabled
|
||||||
|
&& (mentions_everyone || mentions_online || !role_mentions.is_empty())
|
||||||
|
{
|
||||||
|
mentions_everyone = false;
|
||||||
|
mentions_online = false;
|
||||||
|
role_mentions.clear();
|
||||||
|
} else if mentions_everyone || mentions_online || !role_mentions.is_empty() {
|
||||||
|
debug!(
|
||||||
|
"Mentioned everyone: {}, mentioned online: {}, mentioned roles: {:?}",
|
||||||
|
mentions_everyone, mentions_online, &role_mentions
|
||||||
|
);
|
||||||
|
if let Some(user) = match author {
|
||||||
|
MessageAuthor::User(user) => Some(Ok(user)),
|
||||||
|
MessageAuthor::System { .. } => Some(Err(())), // DISALLOWED
|
||||||
|
MessageAuthor::Webhook(..) => None, // Bypass check
|
||||||
|
} {
|
||||||
|
if user.is_err() {
|
||||||
|
return Err(create_error!(InvalidProperty));
|
||||||
|
}
|
||||||
|
let owned_user: User = user.unwrap().to_owned().into();
|
||||||
|
|
||||||
|
let mut query = DatabasePermissionQuery::new(db, &owned_user).channel(&channel);
|
||||||
|
let perms = calculate_channel_permissions(&mut query).await;
|
||||||
|
|
||||||
|
if (mentions_everyone || mentions_online)
|
||||||
|
&& !perms.has_channel_permission(ChannelPermission::MentionEveryone)
|
||||||
|
{
|
||||||
|
return Err(create_error!(MissingPermission {
|
||||||
|
permission: ChannelPermission::MentionEveryone.to_string()
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !role_mentions.is_empty()
|
||||||
|
&& !perms.has_channel_permission(ChannelPermission::MentionRoles)
|
||||||
|
{
|
||||||
|
return Err(create_error!(MissingPermission {
|
||||||
|
permission: ChannelPermission::MentionRoles.to_string()
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -288,25 +442,95 @@ impl Message {
|
|||||||
// Verify replies are valid.
|
// 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() > config.features.limits.default.message_replies {
|
if entries.len() > config.features.limits.global.message_replies {
|
||||||
return Err(create_error!(TooManyReplies {
|
return Err(create_error!(TooManyReplies {
|
||||||
max: config.features.limits.default.message_replies,
|
max: config.features.limits.global.message_replies,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
for ReplyIntent { id, mention } in entries {
|
for ReplyIntent {
|
||||||
let message = db.fetch_message(&id).await?;
|
id,
|
||||||
|
mention,
|
||||||
|
fail_if_not_exists,
|
||||||
|
} in entries
|
||||||
|
{
|
||||||
|
match db.fetch_message(&id).await {
|
||||||
|
// Referenced message exists
|
||||||
|
Ok(message) => {
|
||||||
|
if mention && allow_mentions {
|
||||||
|
user_mentions.insert(message.author.to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
if mention && allow_mentions {
|
replies.insert(message.id);
|
||||||
mentions.insert(message.author.to_owned());
|
}
|
||||||
|
// If the referenced message doesn't exist and fail_if_not_exists
|
||||||
|
// is set to false, send the message without the reply.
|
||||||
|
Err(e) => {
|
||||||
|
if !matches!(e.error_type, ErrorType::NotFound)
|
||||||
|
|| fail_if_not_exists.unwrap_or(true)
|
||||||
|
{
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
replies.insert(message.id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !mentions.is_empty() {
|
// Validate the mentions go to users in the channel/server
|
||||||
message.mentions.replace(mentions.into_iter().collect());
|
if !user_mentions.is_empty() {
|
||||||
|
match channel {
|
||||||
|
Channel::DirectMessage { ref recipients, .. }
|
||||||
|
| Channel::Group { ref recipients, .. } => {
|
||||||
|
let recipients_hash = HashSet::<&String, RandomState>::from_iter(recipients);
|
||||||
|
user_mentions.retain(|m| recipients_hash.contains(m));
|
||||||
|
role_mentions.clear();
|
||||||
|
}
|
||||||
|
Channel::TextChannel { ref server, .. }
|
||||||
|
| Channel::VoiceChannel { ref server, .. } => {
|
||||||
|
let mentions_vec = Vec::from_iter(user_mentions.iter().cloned());
|
||||||
|
|
||||||
|
let valid_members = db.fetch_members(server.as_str(), &mentions_vec[..]).await;
|
||||||
|
if let Ok(valid_members) = valid_members {
|
||||||
|
let valid_mentions = HashSet::<&String, RandomState>::from_iter(
|
||||||
|
valid_members.iter().map(|m| &m.id.user),
|
||||||
|
);
|
||||||
|
|
||||||
|
user_mentions.retain(|m| valid_mentions.contains(m)); // quick pass, validate mentions are in the server
|
||||||
|
|
||||||
|
if !user_mentions.is_empty() {
|
||||||
|
// if there are still mentions, drill down to a channel-level
|
||||||
|
let member_channel_view_perms =
|
||||||
|
BulkDatabasePermissionQuery::from_server_id(db, server)
|
||||||
|
.await
|
||||||
|
.channel(&channel)
|
||||||
|
.members(&valid_members)
|
||||||
|
.members_can_see_channel()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
user_mentions
|
||||||
|
.retain(|m| *member_channel_view_perms.get(m).unwrap_or(&false));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
revolt_config::capture_error(&valid_members.unwrap_err());
|
||||||
|
return Err(create_error!(InternalError));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Channel::SavedMessages { .. } => {
|
||||||
|
user_mentions.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !user_mentions.is_empty() {
|
||||||
|
message
|
||||||
|
.mentions
|
||||||
|
.replace(user_mentions.into_iter().collect());
|
||||||
|
}
|
||||||
|
|
||||||
|
if !role_mentions.is_empty() {
|
||||||
|
message
|
||||||
|
.role_mentions
|
||||||
|
.replace(role_mentions.into_iter().collect());
|
||||||
}
|
}
|
||||||
|
|
||||||
if !replies.is_empty() {
|
if !replies.is_empty() {
|
||||||
@@ -315,33 +539,40 @@ impl Message {
|
|||||||
.replace(replies.into_iter().collect::<Vec<String>>());
|
.replace(replies.into_iter().collect::<Vec<String>>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate final message flags
|
||||||
|
let mut flag_value = MessageFlagsValue(0);
|
||||||
|
flag_value
|
||||||
|
.set(MessageFlags::SuppressNotifications, suppress_notifications)
|
||||||
|
.set(MessageFlags::MentionsEveryone, mentions_everyone)
|
||||||
|
.set(MessageFlags::MentionsOnline, mentions_online);
|
||||||
|
|
||||||
|
message.flags = Some(flag_value.0);
|
||||||
|
|
||||||
// Add attachments to message.
|
// Add attachments to message.
|
||||||
let mut attachments = vec![];
|
let mut attachments = vec![];
|
||||||
if data
|
if data
|
||||||
.attachments
|
.attachments
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|v| v.len() > config.features.limits.default.message_attachments)
|
.is_some_and(|v| v.len() > limits.message_attachments)
|
||||||
{
|
{
|
||||||
return Err(create_error!(TooManyAttachments {
|
return Err(create_error!(TooManyAttachments {
|
||||||
max: config.features.limits.default.message_attachments,
|
max: limits.message_attachments,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
if data
|
if data
|
||||||
.embeds
|
.embeds
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|v| v.len() > config.features.limits.default.message_embeds)
|
.is_some_and(|v| v.len() > config.features.limits.global.message_embeds)
|
||||||
{
|
{
|
||||||
return Err(create_error!(TooManyEmbeds {
|
return Err(create_error!(TooManyEmbeds {
|
||||||
max: config.features.limits.default.message_embeds,
|
max: config.features.limits.global.message_embeds,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
for attachment_id in data.attachments.as_deref().unwrap_or_default() {
|
for attachment_id in data.attachments.as_deref().unwrap_or_default() {
|
||||||
attachments.push(
|
attachments
|
||||||
db.find_and_use_attachment(attachment_id, "attachments", "message", &message_id)
|
.push(File::use_attachment(db, attachment_id, &message_id, author.id()).await?);
|
||||||
.await?,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !attachments.is_empty() {
|
if !attachments.is_empty() {
|
||||||
@@ -360,7 +591,9 @@ impl Message {
|
|||||||
message.nonce = Some(idempotency.into_key());
|
message.nonce = Some(idempotency.into_key());
|
||||||
|
|
||||||
// Send the message
|
// Send the message
|
||||||
message.send(db, author, &channel, generate_embeds).await?;
|
message
|
||||||
|
.send(db, amqp, author, user, member, &channel, generate_embeds)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(message)
|
Ok(message)
|
||||||
}
|
}
|
||||||
@@ -369,27 +602,38 @@ impl Message {
|
|||||||
pub async fn send_without_notifications(
|
pub async fn send_without_notifications(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
|
user: Option<v0::User>,
|
||||||
|
member: Option<v0::Member>,
|
||||||
is_dm: bool,
|
is_dm: bool,
|
||||||
generate_embeds: bool,
|
generate_embeds: bool,
|
||||||
|
// This determines if this function should queue the mentions task or if somewhere else will.
|
||||||
|
// If this is true, you MUST call tasks::ack::queue yourself.
|
||||||
|
mentions_elsewhere: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
db.insert_message(self).await?;
|
db.insert_message(self).await?;
|
||||||
|
|
||||||
// Fan out events
|
// Fan out events
|
||||||
EventV1::Message(self.clone().into())
|
EventV1::Message(self.clone().into_model(user, member))
|
||||||
.p(self.channel.to_string())
|
.p(self.channel.to_string())
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// Update last_message_id
|
// Update last_message_id
|
||||||
|
#[cfg(feature = "tasks")]
|
||||||
tasks::last_message_id::queue(self.channel.to_string(), self.id.to_string(), is_dm).await;
|
tasks::last_message_id::queue(self.channel.to_string(), self.id.to_string(), is_dm).await;
|
||||||
|
|
||||||
// Add mentions for affected users
|
// Add mentions for affected users
|
||||||
if let Some(mentions) = &self.mentions {
|
#[cfg(feature = "tasks")]
|
||||||
for user in mentions {
|
if !mentions_elsewhere {
|
||||||
tasks::ack::queue(
|
if let Some(mentions) = &self.mentions {
|
||||||
|
tasks::ack::queue_message(
|
||||||
self.channel.to_string(),
|
self.channel.to_string(),
|
||||||
user.to_string(),
|
AckEvent::ProcessMessage {
|
||||||
AckEvent::AddMention {
|
messages: vec![(
|
||||||
ids: vec![self.id.to_string()],
|
None,
|
||||||
|
self.clone(),
|
||||||
|
mentions.clone(),
|
||||||
|
self.has_suppressed_notifications(),
|
||||||
|
)],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@@ -397,6 +641,7 @@ impl Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate embeds
|
// Generate embeds
|
||||||
|
#[cfg(feature = "tasks")]
|
||||||
if generate_embeds {
|
if generate_embeds {
|
||||||
if let Some(content) = &self.content {
|
if let Some(content) = &self.content {
|
||||||
tasks::process_embeds::queue(
|
tasks::process_embeds::queue(
|
||||||
@@ -412,33 +657,60 @@ impl Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Send a message
|
/// Send a message
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub async fn send(
|
pub async fn send(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
|
_amqp: Option<&AMQP>, // this is optional mostly for tests.
|
||||||
author: MessageAuthor<'_>,
|
author: MessageAuthor<'_>,
|
||||||
|
user: Option<v0::User>,
|
||||||
|
member: Option<v0::Member>,
|
||||||
channel: &Channel,
|
channel: &Channel,
|
||||||
generate_embeds: bool,
|
generate_embeds: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.send_without_notifications(
|
self.send_without_notifications(
|
||||||
db,
|
db,
|
||||||
|
user.clone(),
|
||||||
|
member.clone(),
|
||||||
matches!(channel, Channel::DirectMessage { .. }),
|
matches!(channel, Channel::DirectMessage { .. }),
|
||||||
generate_embeds,
|
generate_embeds,
|
||||||
|
true,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Push out Web Push notifications
|
|
||||||
crate::tasks::web_push::queue(
|
if !self.has_suppressed_notifications()
|
||||||
{
|
&& (self.mentions.is_some() || self.contains_mass_push_mention())
|
||||||
match channel {
|
{
|
||||||
Channel::DirectMessage { recipients, .. }
|
// send Push notifications
|
||||||
| Channel::Group { recipients, .. } => recipients.clone(),
|
#[cfg(feature = "tasks")]
|
||||||
Channel::TextChannel { .. } => self.mentions.clone().unwrap_or_default(),
|
tasks::ack::queue_message(
|
||||||
_ => vec![],
|
self.channel.to_string(),
|
||||||
}
|
AckEvent::ProcessMessage {
|
||||||
},
|
messages: vec![(
|
||||||
PushNotification::from(self.clone().into(), Some(author), &channel.id()).await,
|
Some(
|
||||||
)
|
PushNotification::from(
|
||||||
.await;
|
self.clone().into_model(user, member),
|
||||||
|
Some(author),
|
||||||
|
channel.to_owned().into(),
|
||||||
|
)
|
||||||
|
.await,
|
||||||
|
),
|
||||||
|
self.clone(),
|
||||||
|
match channel {
|
||||||
|
Channel::DirectMessage { recipients, .. }
|
||||||
|
| Channel::Group { recipients, .. } => recipients.clone(),
|
||||||
|
Channel::TextChannel { .. } => {
|
||||||
|
self.mentions.clone().unwrap_or_default()
|
||||||
|
}
|
||||||
|
_ => vec![],
|
||||||
|
},
|
||||||
|
false, // branch already dictates this
|
||||||
|
)],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -452,10 +724,7 @@ impl Message {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
let media = if let Some(id) = embed.media {
|
let media = if let Some(id) = embed.media {
|
||||||
Some(
|
Some(File::use_attachment(db, &id, &self.id, &self.author).await?)
|
||||||
db.find_and_use_attachment(&id, "attachments", "message", &self.id)
|
|
||||||
.await?,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -470,15 +739,48 @@ impl Message {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether this message has suppressed notifications
|
||||||
|
pub fn has_suppressed_notifications(&self) -> bool {
|
||||||
|
if let Some(flags) = self.flags {
|
||||||
|
flags & MessageFlags::SuppressNotifications as u32
|
||||||
|
== MessageFlags::SuppressNotifications as u32
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contains_mass_push_mention(&self) -> bool {
|
||||||
|
let ping = if let Some(flags) = self.flags {
|
||||||
|
let flags = MessageFlagsValue(flags);
|
||||||
|
flags.has(MessageFlags::MentionsEveryone)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
|
ping || self.role_mentions.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
/// Update message data
|
/// Update message data
|
||||||
pub async fn update(&mut self, db: &Database, partial: PartialMessage) -> Result<()> {
|
pub async fn update(
|
||||||
|
&mut self,
|
||||||
|
db: &Database,
|
||||||
|
partial: PartialMessage,
|
||||||
|
remove: Vec<FieldsMessage>,
|
||||||
|
) -> Result<()> {
|
||||||
self.apply_options(partial.clone());
|
self.apply_options(partial.clone());
|
||||||
db.update_message(&self.id, &partial).await?;
|
|
||||||
|
for field in &remove {
|
||||||
|
self.remove_field(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.update_message(&self.id, &partial, remove.clone())
|
||||||
|
.await?;
|
||||||
|
|
||||||
EventV1::MessageUpdate {
|
EventV1::MessageUpdate {
|
||||||
id: self.id.clone(),
|
id: self.id.clone(),
|
||||||
channel: self.channel.clone(),
|
channel: self.channel.clone(),
|
||||||
data: partial.into(),
|
data: partial.into(),
|
||||||
|
clear: remove.into_iter().map(|field| field.into()).collect(),
|
||||||
}
|
}
|
||||||
.p(self.channel.clone())
|
.p(self.channel.clone())
|
||||||
.await;
|
.await;
|
||||||
@@ -498,13 +800,47 @@ impl Message {
|
|||||||
.fetch_messages(query)
|
.fetch_messages(query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(Into::into)
|
.map(|msg| msg.into_model(None, None))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if let Some(true) = include_users {
|
if let Some(true) = include_users {
|
||||||
let user_ids = messages
|
let user_ids = messages
|
||||||
.iter()
|
.iter()
|
||||||
.map(|m| m.author.clone())
|
.flat_map(|m| {
|
||||||
|
let mut users = vec![m.author.clone()];
|
||||||
|
if let Some(system) = &m.system {
|
||||||
|
match system {
|
||||||
|
v0::SystemMessage::ChannelDescriptionChanged { by } => {
|
||||||
|
users.push(by.clone())
|
||||||
|
}
|
||||||
|
v0::SystemMessage::ChannelIconChanged { by } => users.push(by.clone()),
|
||||||
|
v0::SystemMessage::ChannelOwnershipChanged { from, to, .. } => {
|
||||||
|
users.push(from.clone());
|
||||||
|
users.push(to.clone())
|
||||||
|
}
|
||||||
|
v0::SystemMessage::ChannelRenamed { by, .. } => users.push(by.clone()),
|
||||||
|
v0::SystemMessage::UserAdded { by, id, .. }
|
||||||
|
| v0::SystemMessage::UserRemove { by, id, .. } => {
|
||||||
|
users.push(by.clone());
|
||||||
|
users.push(id.clone());
|
||||||
|
}
|
||||||
|
v0::SystemMessage::UserBanned { id, .. }
|
||||||
|
| v0::SystemMessage::UserKicked { id, .. }
|
||||||
|
| v0::SystemMessage::UserJoined { id, .. }
|
||||||
|
| v0::SystemMessage::UserLeft { id, .. } => {
|
||||||
|
users.push(id.clone());
|
||||||
|
}
|
||||||
|
v0::SystemMessage::Text { .. } => {}
|
||||||
|
v0::SystemMessage::MessagePinned { by, .. } => {
|
||||||
|
users.push(by.clone());
|
||||||
|
}
|
||||||
|
v0::SystemMessage::MessageUnpinned { by, .. } => {
|
||||||
|
users.push(by.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
users
|
||||||
|
})
|
||||||
.collect::<HashSet<String>>()
|
.collect::<HashSet<String>>()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
@@ -558,7 +894,7 @@ impl Message {
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let media: Option<v0::File> = if let Some(id) = embed.media {
|
let media: Option<v0::File> = if let Some(id) = embed.media {
|
||||||
Some(
|
Some(
|
||||||
db.find_and_use_attachment(&id, "attachments", "message", &self.id)
|
File::use_attachment(db, &id, &self.id, &self.author)
|
||||||
.await?
|
.await?
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
@@ -588,7 +924,7 @@ impl Message {
|
|||||||
pub async fn add_reaction(&self, db: &Database, user: &User, emoji: &str) -> Result<()> {
|
pub async fn add_reaction(&self, db: &Database, user: &User, emoji: &str) -> Result<()> {
|
||||||
// Check how many reactions are already on the message
|
// Check how many reactions are already on the message
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
if self.reactions.len() >= config.features.limits.default.message_reactions
|
if self.reactions.len() >= config.features.limits.global.message_reactions
|
||||||
&& !self.reactions.contains_key(emoji)
|
&& !self.reactions.contains_key(emoji)
|
||||||
{
|
{
|
||||||
return Err(create_error!(InvalidOperation));
|
return Err(create_error!(InvalidOperation));
|
||||||
@@ -730,6 +1066,12 @@ impl Message {
|
|||||||
// Write to database
|
// Write to database
|
||||||
db.clear_reaction(&self.id, emoji).await
|
db.clear_reaction(&self.id, emoji).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_field(&mut self, field: &FieldsMessage) {
|
||||||
|
match field {
|
||||||
|
FieldsMessage::Pinned => self.pinned = None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SystemMessage {
|
impl SystemMessage {
|
||||||
@@ -753,7 +1095,7 @@ impl Interactions {
|
|||||||
if let Some(reactions) = &self.reactions {
|
if let Some(reactions) = &self.reactions {
|
||||||
permissions.throw_if_lacking_channel_permission(ChannelPermission::React)?;
|
permissions.throw_if_lacking_channel_permission(ChannelPermission::React)?;
|
||||||
|
|
||||||
if reactions.len() > config.features.limits.default.message_reactions {
|
if reactions.len() > config.features.limits.global.message_reactions {
|
||||||
return Err(create_error!(InvalidOperation));
|
return Err(create_error!(InvalidOperation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{AppendMessage, Message, MessageQuery, PartialMessage};
|
use crate::{AppendMessage, FieldsMessage, Message, MessageQuery, PartialMessage};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ pub trait AbstractMessages: Sync + Send {
|
|||||||
async fn fetch_messages_by_id(&self, ids: &[String]) -> Result<Vec<Message>>;
|
async fn fetch_messages_by_id(&self, ids: &[String]) -> Result<Vec<Message>>;
|
||||||
|
|
||||||
/// Update a given message with new information
|
/// Update a given message with new information
|
||||||
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()>;
|
async fn update_message(&self, id: &str, message: &PartialMessage, remove: Vec<FieldsMessage>) -> Result<()>;
|
||||||
|
|
||||||
/// Append information to a given message
|
/// Append information to a given message
|
||||||
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()>;
|
async fn append_message(&self, id: &str, append: &AppendMessage) -> Result<()>;
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ use revolt_models::v0::MessageSort;
|
|||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AppendMessage, DocumentId, Message, MessageQuery, MessageTimePeriod, MongoDb, PartialMessage,
|
AppendMessage, DocumentId, FieldsMessage, IntoDocumentPath, Message, MessageQuery,
|
||||||
|
MessageTimePeriod, MongoDb, PartialMessage,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::AbstractMessages;
|
use super::AbstractMessages;
|
||||||
@@ -50,6 +51,10 @@ impl AbstractMessages for MongoDb {
|
|||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Some(pinned) = query.filter.pinned {
|
||||||
|
filter.insert("pinned", pinned);
|
||||||
|
};
|
||||||
|
|
||||||
// 2. Find query limit
|
// 2. Find query limit
|
||||||
let limit = query.limit.unwrap_or(50);
|
let limit = query.limit.unwrap_or(50);
|
||||||
|
|
||||||
@@ -90,7 +95,7 @@ impl AbstractMessages for MongoDb {
|
|||||||
COL,
|
COL,
|
||||||
older_message_filter,
|
older_message_filter,
|
||||||
FindOptions::builder()
|
FindOptions::builder()
|
||||||
.limit(limit / 2)
|
.limit(limit / 2 + 1)
|
||||||
.sort(doc! {
|
.sort(doc! {
|
||||||
"_id": -1_i32
|
"_id": -1_i32
|
||||||
})
|
})
|
||||||
@@ -166,7 +171,7 @@ impl AbstractMessages for MongoDb {
|
|||||||
self.find_with_options(
|
self.find_with_options(
|
||||||
COL,
|
COL,
|
||||||
doc! {
|
doc! {
|
||||||
"ids": {
|
"_id": {
|
||||||
"$in": ids
|
"$in": ids
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -177,8 +182,22 @@ impl AbstractMessages for MongoDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update a given message with new information
|
/// Update a given message with new information
|
||||||
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
|
async fn update_message(
|
||||||
query!(self, update_one_by_id, COL, id, message, vec![], None).map(|_| ())
|
&self,
|
||||||
|
id: &str,
|
||||||
|
message: &PartialMessage,
|
||||||
|
remove: Vec<FieldsMessage>,
|
||||||
|
) -> Result<()> {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
update_one_by_id,
|
||||||
|
COL,
|
||||||
|
id,
|
||||||
|
message,
|
||||||
|
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
||||||
|
None
|
||||||
|
)
|
||||||
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Append information to a given message
|
/// Append information to a given message
|
||||||
@@ -209,7 +228,6 @@ impl AbstractMessages for MongoDb {
|
|||||||
"_id": id
|
"_id": id
|
||||||
},
|
},
|
||||||
query,
|
query,
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -228,7 +246,6 @@ impl AbstractMessages for MongoDb {
|
|||||||
format!("reactions.{emoji}"): user
|
format!("reactions.{emoji}"): user
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -247,7 +264,6 @@ impl AbstractMessages for MongoDb {
|
|||||||
format!("reactions.{emoji}"): user
|
format!("reactions.{emoji}"): user
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -266,7 +282,6 @@ impl AbstractMessages for MongoDb {
|
|||||||
format!("reactions.{emoji}"): 1
|
format!("reactions.{emoji}"): 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -281,21 +296,26 @@ impl AbstractMessages for MongoDb {
|
|||||||
/// 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: &[String]) -> Result<()> {
|
async fn delete_messages(&self, channel: &str, ids: &[String]) -> Result<()> {
|
||||||
self.col::<Document>(COL)
|
self.col::<Document>(COL)
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"channel": channel,
|
||||||
"channel": channel,
|
"_id": {
|
||||||
"_id": {
|
"$in": ids
|
||||||
"$in": ids
|
}
|
||||||
}
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| create_database_error!("delete_many", COL))
|
.map_err(|_| create_database_error!("delete_many", COL))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IntoDocumentPath for FieldsMessage {
|
||||||
|
fn as_path(&self) -> Option<&'static str> {
|
||||||
|
Some(match self {
|
||||||
|
FieldsMessage::Pinned => "pinned",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MongoDb {
|
impl MongoDb {
|
||||||
pub async fn delete_bulk_messages(&self, projection: Document) -> Result<()> {
|
pub async fn delete_bulk_messages(&self, projection: Document) -> Result<()> {
|
||||||
let mut for_attachments = projection.clone();
|
let mut for_attachments = projection.clone();
|
||||||
@@ -335,7 +355,6 @@ impl MongoDb {
|
|||||||
"deleted": true
|
"deleted": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("update_many", "attachments"))?;
|
.map_err(|_| create_database_error!("update_many", "attachments"))?;
|
||||||
@@ -343,7 +362,7 @@ impl MongoDb {
|
|||||||
|
|
||||||
// And then delete said messages.
|
// And then delete said messages.
|
||||||
self.col::<Document>(COL)
|
self.col::<Document>(COL)
|
||||||
.delete_many(projection, None)
|
.delete_many(projection)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| create_database_error!("delete_many", COL))
|
.map_err(|_| create_database_error!("delete_many", COL))
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use futures::future::try_join_all;
|
|||||||
use indexmap::IndexSet;
|
use indexmap::IndexSet;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{AppendMessage, Message, MessageQuery, PartialMessage, ReferenceDb};
|
use crate::{AppendMessage, FieldsMessage, Message, MessageQuery, PartialMessage, ReferenceDb};
|
||||||
|
|
||||||
use super::AbstractMessages;
|
use super::AbstractMessages;
|
||||||
|
|
||||||
@@ -56,6 +56,12 @@ impl AbstractMessages for ReferenceDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(pinned) = query.filter.pinned {
|
||||||
|
if message.pinned.unwrap_or_default() == pinned {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
@@ -183,10 +189,15 @@ impl AbstractMessages for ReferenceDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update a given message with new information
|
/// Update a given message with new information
|
||||||
async fn update_message(&self, id: &str, message: &PartialMessage) -> Result<()> {
|
async fn update_message(&self, id: &str, message: &PartialMessage, remove: Vec<FieldsMessage>) -> Result<()> {
|
||||||
let mut messages = self.messages.lock().await;
|
let mut messages = self.messages.lock().await;
|
||||||
if let Some(message_data) = messages.get_mut(id) {
|
if let Some(message_data) = messages.get_mut(id) {
|
||||||
message_data.apply_options(message.to_owned());
|
message_data.apply_options(message.to_owned());
|
||||||
|
|
||||||
|
for field in remove {
|
||||||
|
#[allow(clippy::disallowed_methods)]
|
||||||
|
message_data.remove_field(&field);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(create_error!(NotFound))
|
Err(create_error!(NotFound))
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ mod channel_unreads;
|
|||||||
mod channel_webhooks;
|
mod channel_webhooks;
|
||||||
mod channels;
|
mod channels;
|
||||||
mod emojis;
|
mod emojis;
|
||||||
|
mod file_hashes;
|
||||||
mod files;
|
mod files;
|
||||||
mod messages;
|
mod messages;
|
||||||
|
mod policy_changes;
|
||||||
mod ratelimit_events;
|
mod ratelimit_events;
|
||||||
mod safety_reports;
|
mod safety_reports;
|
||||||
mod safety_snapshots;
|
mod safety_snapshots;
|
||||||
@@ -23,8 +25,10 @@ pub use channel_unreads::*;
|
|||||||
pub use channel_webhooks::*;
|
pub use channel_webhooks::*;
|
||||||
pub use channels::*;
|
pub use channels::*;
|
||||||
pub use emojis::*;
|
pub use emojis::*;
|
||||||
|
pub use file_hashes::*;
|
||||||
pub use files::*;
|
pub use files::*;
|
||||||
pub use messages::*;
|
pub use messages::*;
|
||||||
|
pub use policy_changes::*;
|
||||||
pub use ratelimit_events::*;
|
pub use ratelimit_events::*;
|
||||||
pub use safety_reports::*;
|
pub use safety_reports::*;
|
||||||
pub use safety_snapshots::*;
|
pub use safety_snapshots::*;
|
||||||
@@ -34,7 +38,10 @@ pub use servers::*;
|
|||||||
pub use user_settings::*;
|
pub use user_settings::*;
|
||||||
pub use users::*;
|
pub use users::*;
|
||||||
|
|
||||||
use crate::{Database, MongoDb, ReferenceDb};
|
use crate::{Database, ReferenceDb};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
use crate::MongoDb;
|
||||||
|
|
||||||
pub trait AbstractDatabase:
|
pub trait AbstractDatabase:
|
||||||
Sync
|
Sync
|
||||||
@@ -46,8 +53,10 @@ pub trait AbstractDatabase:
|
|||||||
+ channel_unreads::AbstractChannelUnreads
|
+ channel_unreads::AbstractChannelUnreads
|
||||||
+ channel_webhooks::AbstractWebhooks
|
+ channel_webhooks::AbstractWebhooks
|
||||||
+ emojis::AbstractEmojis
|
+ emojis::AbstractEmojis
|
||||||
|
+ file_hashes::AbstractAttachmentHashes
|
||||||
+ files::AbstractAttachments
|
+ files::AbstractAttachments
|
||||||
+ messages::AbstractMessages
|
+ messages::AbstractMessages
|
||||||
|
+ policy_changes::AbstractPolicyChange
|
||||||
+ ratelimit_events::AbstractRatelimitEvents
|
+ ratelimit_events::AbstractRatelimitEvents
|
||||||
+ safety_reports::AbstractReport
|
+ safety_reports::AbstractReport
|
||||||
+ safety_snapshots::AbstractSnapshot
|
+ safety_snapshots::AbstractSnapshot
|
||||||
@@ -60,6 +69,8 @@ pub trait AbstractDatabase:
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractDatabase for ReferenceDb {}
|
impl AbstractDatabase for ReferenceDb {}
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
impl AbstractDatabase for MongoDb {}
|
impl AbstractDatabase for MongoDb {}
|
||||||
|
|
||||||
impl std::ops::Deref for Database {
|
impl std::ops::Deref for Database {
|
||||||
@@ -68,6 +79,7 @@ impl std::ops::Deref for Database {
|
|||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
match &self {
|
match &self {
|
||||||
Database::Reference(dummy) => dummy,
|
Database::Reference(dummy) => dummy,
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
Database::MongoDb(mongo) => mongo,
|
Database::MongoDb(mongo) => mongo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
mod model;
|
||||||
|
mod ops;
|
||||||
|
|
||||||
|
pub use model::*;
|
||||||
|
pub use ops::*;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
|
|
||||||
|
auto_derived!(
|
||||||
|
/// Platform policy change
|
||||||
|
pub struct PolicyChange {
|
||||||
|
/// Unique Id
|
||||||
|
#[serde(rename = "_id")]
|
||||||
|
pub id: String,
|
||||||
|
|
||||||
|
/// Time at which this policy was created
|
||||||
|
pub created_time: Timestamp,
|
||||||
|
/// Time at which this policy is effective
|
||||||
|
pub effective_time: Timestamp,
|
||||||
|
|
||||||
|
/// Message shown to users
|
||||||
|
pub description: String,
|
||||||
|
/// URL with details about changes
|
||||||
|
pub url: String,
|
||||||
|
}
|
||||||
|
);
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use crate::PolicyChange;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
mod mongodb;
|
||||||
|
mod reference;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait AbstractPolicyChange: Sync + Send {
|
||||||
|
/// Fetch all policy changes
|
||||||
|
async fn fetch_policy_changes(&self) -> Result<Vec<PolicyChange>>;
|
||||||
|
|
||||||
|
/// Acknowledge policy changes
|
||||||
|
async fn acknowledge_policy_changes(&self, user_id: &str) -> Result<()>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
use bson::to_bson;
|
||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use crate::MongoDb;
|
||||||
|
use crate::PolicyChange;
|
||||||
|
use crate::User;
|
||||||
|
|
||||||
|
use super::AbstractPolicyChange;
|
||||||
|
|
||||||
|
static COL: &str = "policy_changes";
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl AbstractPolicyChange for MongoDb {
|
||||||
|
/// Fetch all policy changes
|
||||||
|
async fn fetch_policy_changes(&self) -> Result<Vec<PolicyChange>> {
|
||||||
|
query!(self, find, COL, doc! {})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Acknowledge policy changes
|
||||||
|
async fn acknowledge_policy_changes(&self, user_id: &str) -> Result<()> {
|
||||||
|
let latest_policy = self
|
||||||
|
.fetch_policy_changes()
|
||||||
|
.await?
|
||||||
|
.into_iter()
|
||||||
|
.map(|policy| policy.created_time)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(Timestamp::UNIX_EPOCH);
|
||||||
|
|
||||||
|
self.col::<User>("users")
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": user_id
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$set": {
|
||||||
|
"last_acknowledged_policy_change": to_bson(&latest_policy)
|
||||||
|
.map_err(|_| create_database_error!("to_bson", "timestamp"))?
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|_| create_database_error!("update_one", COL))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use crate::PolicyChange;
|
||||||
|
use crate::ReferenceDb;
|
||||||
|
|
||||||
|
use super::AbstractPolicyChange;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl AbstractPolicyChange for ReferenceDb {
|
||||||
|
/// Fetch all policy changes
|
||||||
|
async fn fetch_policy_changes(&self) -> Result<Vec<PolicyChange>> {
|
||||||
|
let policy_changes = self.policy_changes.lock().await;
|
||||||
|
Ok(policy_changes.values().cloned().collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Acknowledge policy changes
|
||||||
|
async fn acknowledge_policy_changes(&self, user_id: &str) -> Result<()> {
|
||||||
|
let mut users = self.users.lock().await;
|
||||||
|
let user = users.get_mut(user_id).expect("user doesn't exist");
|
||||||
|
user.last_acknowledged_policy_change = self
|
||||||
|
.fetch_policy_changes()
|
||||||
|
.await?
|
||||||
|
.into_iter()
|
||||||
|
.map(|policy| policy.created_time)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(Timestamp::UNIX_EPOCH);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::{revolt_result::Result, RatelimitEvent, RatelimitEventType};
|
use crate::{revolt_result::Result, RatelimitEvent, RatelimitEventType};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -23,16 +23,13 @@ impl AbstractRatelimitEvents for MongoDb {
|
|||||||
count: usize,
|
count: usize,
|
||||||
) -> Result<bool> {
|
) -> Result<bool> {
|
||||||
self.col::<RatelimitEvent>(COL)
|
self.col::<RatelimitEvent>(COL)
|
||||||
.count_documents(
|
.count_documents(doc! {
|
||||||
doc! {
|
"_id": {
|
||||||
"_id": {
|
"$gte": Ulid::from_datetime(SystemTime::now() - period).to_string()
|
||||||
"$gte": Ulid::from_datetime(SystemTime::now() - period).to_string()
|
|
||||||
},
|
|
||||||
"target_id": target_id,
|
|
||||||
"event_type": event_type.to_string()
|
|
||||||
},
|
},
|
||||||
None,
|
"target_id": target_id,
|
||||||
)
|
"event_type": event_type.to_string()
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
.map(|c| c as usize >= count)
|
.map(|c| c as usize >= count)
|
||||||
.map_err(|_| create_database_error!("count_documents", COL))
|
.map_err(|_| create_database_error!("count_documents", COL))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::Report;
|
use crate::Report;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -85,8 +85,8 @@ impl SnapshotContent {
|
|||||||
Ok((
|
Ok((
|
||||||
SnapshotContent::Message {
|
SnapshotContent::Message {
|
||||||
message,
|
message,
|
||||||
prior_context: prior_context.into_iter().map(Into::into).collect(),
|
prior_context: prior_context.into_iter().collect(),
|
||||||
leading_context: leading_context.into_iter().map(Into::into).collect(),
|
leading_context: leading_context.into_iter().collect(),
|
||||||
},
|
},
|
||||||
files,
|
files,
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::Snapshot;
|
use crate::Snapshot;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ auto_derived!(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[allow(clippy::disallowed_methods)]
|
||||||
impl ServerBan {
|
impl ServerBan {
|
||||||
/// Create ban
|
/// Create ban
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::{MemberCompositeKey, ServerBan};
|
use crate::{MemberCompositeKey, ServerBan};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ impl Member {
|
|||||||
server: &Server,
|
server: &Server,
|
||||||
user: &User,
|
user: &User,
|
||||||
channels: Option<Vec<Channel>>,
|
channels: Option<Vec<Channel>>,
|
||||||
) -> Result<Vec<Channel>> {
|
) -> Result<(Member, Vec<Channel>)> {
|
||||||
if db.fetch_ban(&server.id, &user.id).await.is_ok() {
|
if db.fetch_ban(&server.id, &user.id).await.is_ok() {
|
||||||
return Err(create_error!(Banned));
|
return Err(create_error!(Banned));
|
||||||
}
|
}
|
||||||
@@ -150,16 +150,16 @@ impl Member {
|
|||||||
id: user.id.clone(),
|
id: user.id.clone(),
|
||||||
}
|
}
|
||||||
.into_message(id.to_string())
|
.into_message(id.to_string())
|
||||||
.send_without_notifications(db, false, false)
|
.send_without_notifications(db, None, None, false, false, false)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(channels)
|
Ok((member, channels))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update member data
|
/// Update member data
|
||||||
pub async fn update<'a>(
|
pub async fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
partial: PartialMember,
|
partial: PartialMember,
|
||||||
@@ -229,6 +229,7 @@ impl Member {
|
|||||||
EventV1::ServerMemberLeave {
|
EventV1::ServerMemberLeave {
|
||||||
id: self.id.server.to_string(),
|
id: self.id.server.to_string(),
|
||||||
user: self.id.user.to_string(),
|
user: self.id.user.to_string(),
|
||||||
|
reason: intention.clone().into(),
|
||||||
}
|
}
|
||||||
.p(self.id.server.to_string())
|
.p(self.id.server.to_string())
|
||||||
.await;
|
.await;
|
||||||
@@ -250,7 +251,7 @@ impl Member {
|
|||||||
}
|
}
|
||||||
.into_message(id.to_string())
|
.into_message(id.to_string())
|
||||||
// TODO: support notifications here in the future?
|
// TODO: support notifications here in the future?
|
||||||
.send_without_notifications(db, false, false)
|
.send_without_notifications(db, None, None, false, false, false)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,75 @@
|
|||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
use ::mongodb::{ClientSession, SessionCursor};
|
||||||
|
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{FieldsMember, Member, MemberCompositeKey, PartialMember};
|
use crate::{FieldsMember, Member, MemberCompositeKey, PartialMember};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
|
pub enum ChunkedServerMembersGenerator {
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
MongoDb {
|
||||||
|
session: ClientSession,
|
||||||
|
cursor: Option<SessionCursor<Member>>,
|
||||||
|
},
|
||||||
|
|
||||||
|
Reference {
|
||||||
|
offset: i32,
|
||||||
|
data: Option<Vec<Member>>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChunkedServerMembersGenerator {
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
pub fn new_mongo(session: ClientSession, cursor: SessionCursor<Member>) -> Self {
|
||||||
|
ChunkedServerMembersGenerator::MongoDb {
|
||||||
|
session,
|
||||||
|
cursor: Some(cursor),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_reference(data: Vec<Member>) -> Self {
|
||||||
|
ChunkedServerMembersGenerator::Reference {
|
||||||
|
offset: 0,
|
||||||
|
data: Some(data),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn next(&mut self) -> Option<Member> {
|
||||||
|
match self {
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
|
ChunkedServerMembersGenerator::MongoDb { session, cursor } => {
|
||||||
|
if let Some(cursor) = cursor {
|
||||||
|
let value = cursor.next(session).await;
|
||||||
|
value.map(|val| val.expect("Failed to fetch the next member"))
|
||||||
|
} else {
|
||||||
|
warn!("Attempted to access a (MongoDb) server member generator without first setting a cursor");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ChunkedServerMembersGenerator::Reference { offset, data } => {
|
||||||
|
if let Some(data) = data {
|
||||||
|
if data.len() as i32 >= *offset {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let resp = &data[*offset as usize];
|
||||||
|
*offset += 1;
|
||||||
|
Some(resp.clone())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
warn!("Attempted to access a (Reference) server member generator without first providing data");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait AbstractServerMembers: Sync + Send {
|
pub trait AbstractServerMembers: Sync + Send {
|
||||||
/// Insert a new server member into the database
|
/// Insert a new server member into the database
|
||||||
@@ -16,6 +81,24 @@ pub trait AbstractServerMembers: Sync + Send {
|
|||||||
/// Fetch all members in a server
|
/// Fetch all members in a server
|
||||||
async fn fetch_all_members<'a>(&self, server_id: &str) -> Result<Vec<Member>>;
|
async fn fetch_all_members<'a>(&self, server_id: &str) -> Result<Vec<Member>>;
|
||||||
|
|
||||||
|
/// Fetch all members in a server as an iterator
|
||||||
|
async fn fetch_all_members_chunked(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
) -> Result<ChunkedServerMembersGenerator>;
|
||||||
|
|
||||||
|
async fn fetch_all_members_with_roles(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<Vec<Member>>;
|
||||||
|
|
||||||
|
async fn fetch_all_members_with_roles_chunked(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<ChunkedServerMembersGenerator>;
|
||||||
|
|
||||||
/// Fetch all memberships for a user
|
/// Fetch all memberships for a user
|
||||||
async fn fetch_all_memberships<'a>(&self, user_id: &str) -> Result<Vec<Member>>;
|
async fn fetch_all_memberships<'a>(&self, user_id: &str) -> Result<Vec<Member>>;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
use mongodb::options::ReadConcern;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{FieldsMember, Member, MemberCompositeKey, PartialMember};
|
use crate::{FieldsMember, Member, MemberCompositeKey, PartialMember};
|
||||||
use crate::{IntoDocumentPath, MongoDb};
|
use crate::{IntoDocumentPath, MongoDb};
|
||||||
|
|
||||||
use super::AbstractServerMembers;
|
use super::{AbstractServerMembers, ChunkedServerMembersGenerator};
|
||||||
|
|
||||||
static COL: &str = "server_members";
|
static COL: &str = "server_members";
|
||||||
|
|
||||||
@@ -33,12 +34,9 @@ impl AbstractServerMembers for MongoDb {
|
|||||||
async fn fetch_all_members<'a>(&self, server_id: &str) -> Result<Vec<Member>> {
|
async fn fetch_all_members<'a>(&self, server_id: &str) -> Result<Vec<Member>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Member>(COL)
|
.col::<Member>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"_id.server": server_id
|
||||||
"_id.server": server_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -52,16 +50,101 @@ impl AbstractServerMembers for MongoDb {
|
|||||||
.await)
|
.await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch all members in a server as a generator.
|
||||||
|
/// Uses config key pushd.mass_mention_chunk_size as the batch size.
|
||||||
|
async fn fetch_all_members_chunked(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
) -> Result<ChunkedServerMembersGenerator> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
|
||||||
|
let mut session = self
|
||||||
|
.start_session()
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("start_session", COL))?;
|
||||||
|
|
||||||
|
session
|
||||||
|
.start_transaction()
|
||||||
|
.read_concern(ReadConcern::snapshot())
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("start_transaction", COL))?;
|
||||||
|
|
||||||
|
let cursor = self
|
||||||
|
.col::<Member>(COL)
|
||||||
|
.find(doc! {
|
||||||
|
"_id.server": server_id
|
||||||
|
})
|
||||||
|
.session(&mut session)
|
||||||
|
.batch_size(config.pushd.mass_mention_chunk_size as u32)
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("find", COL))?;
|
||||||
|
|
||||||
|
Ok(ChunkedServerMembersGenerator::new_mongo(session, cursor))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_all_members_with_roles(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<Vec<Member>> {
|
||||||
|
Ok(self
|
||||||
|
.col::<Member>(COL)
|
||||||
|
.find(doc! {
|
||||||
|
"_id.server": server_id,
|
||||||
|
"roles": {"$in": roles}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
|
.filter_map(|s| async {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
Some(s.unwrap())
|
||||||
|
} else {
|
||||||
|
s.ok()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
.await)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_all_members_with_roles_chunked(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<ChunkedServerMembersGenerator> {
|
||||||
|
let config = revolt_config::config().await;
|
||||||
|
|
||||||
|
let mut session = self
|
||||||
|
.start_session()
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("start_session", COL))?;
|
||||||
|
|
||||||
|
session
|
||||||
|
.start_transaction()
|
||||||
|
.read_concern(ReadConcern::snapshot())
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("start_transaction", COL))?;
|
||||||
|
|
||||||
|
let cursor = self
|
||||||
|
.col::<Member>(COL)
|
||||||
|
.find(doc! {
|
||||||
|
"_id.server": server_id,
|
||||||
|
"roles": {"$in": roles}
|
||||||
|
})
|
||||||
|
.session(&mut session)
|
||||||
|
.batch_size(config.pushd.mass_mention_chunk_size as u32)
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_database_error!("find", COL))?;
|
||||||
|
|
||||||
|
return Ok(ChunkedServerMembersGenerator::new_mongo(session, cursor));
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch all memberships for a user
|
/// Fetch all memberships for a user
|
||||||
async fn fetch_all_memberships<'a>(&self, user_id: &str) -> Result<Vec<Member>> {
|
async fn fetch_all_memberships<'a>(&self, user_id: &str) -> Result<Vec<Member>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Member>(COL)
|
.col::<Member>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"_id.user": user_id
|
||||||
"_id.user": user_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -79,15 +162,12 @@ impl AbstractServerMembers for MongoDb {
|
|||||||
async fn fetch_members<'a>(&self, server_id: &str, ids: &'a [String]) -> Result<Vec<Member>> {
|
async fn fetch_members<'a>(&self, server_id: &str, ids: &'a [String]) -> Result<Vec<Member>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Member>(COL)
|
.col::<Member>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"_id.server": server_id,
|
||||||
"_id.server": server_id,
|
"_id.user": {
|
||||||
"_id.user": {
|
"$in": ids
|
||||||
"$in": ids
|
}
|
||||||
}
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -104,12 +184,9 @@ impl AbstractServerMembers for MongoDb {
|
|||||||
/// Fetch member count of a server
|
/// Fetch member count of a server
|
||||||
async fn fetch_member_count(&self, server_id: &str) -> Result<usize> {
|
async fn fetch_member_count(&self, server_id: &str) -> Result<usize> {
|
||||||
self.col::<Member>(COL)
|
self.col::<Member>(COL)
|
||||||
.count_documents(
|
.count_documents(doc! {
|
||||||
doc! {
|
"_id.server": server_id
|
||||||
"_id.server": server_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map(|c| c as usize)
|
.map(|c| c as usize)
|
||||||
.map_err(|_| create_database_error!("count_documents", COL))
|
.map_err(|_| create_database_error!("count_documents", COL))
|
||||||
@@ -118,12 +195,9 @@ impl AbstractServerMembers for MongoDb {
|
|||||||
/// Fetch server count of a user
|
/// Fetch server count of a user
|
||||||
async fn fetch_server_count(&self, user_id: &str) -> Result<usize> {
|
async fn fetch_server_count(&self, user_id: &str) -> Result<usize> {
|
||||||
self.col::<Member>(COL)
|
self.col::<Member>(COL)
|
||||||
.count_documents(
|
.count_documents(doc! {
|
||||||
doc! {
|
"_id.user": user_id
|
||||||
"_id.user": user_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map(|c| c as usize)
|
.map(|c| c as usize)
|
||||||
.map_err(|_| create_database_error!("count_documents", COL))
|
.map_err(|_| create_database_error!("count_documents", COL))
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use revolt_result::Result;
|
|||||||
use crate::ReferenceDb;
|
use crate::ReferenceDb;
|
||||||
use crate::{FieldsMember, Member, MemberCompositeKey, PartialMember};
|
use crate::{FieldsMember, Member, MemberCompositeKey, PartialMember};
|
||||||
|
|
||||||
use super::AbstractServerMembers;
|
use super::{AbstractServerMembers, ChunkedServerMembersGenerator};
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl AbstractServerMembers for ReferenceDb {
|
impl AbstractServerMembers for ReferenceDb {
|
||||||
@@ -40,6 +40,70 @@ impl AbstractServerMembers for ReferenceDb {
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch all members in a server as an iterator
|
||||||
|
async fn fetch_all_members_chunked(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
) -> Result<ChunkedServerMembersGenerator> {
|
||||||
|
let server_members = self.server_members.lock().await;
|
||||||
|
|
||||||
|
let members = server_members
|
||||||
|
.clone()
|
||||||
|
.into_values()
|
||||||
|
.filter(move |member| member.id.server == server_id)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// this is inefficient as shit but its the reference db so its fine
|
||||||
|
Ok(ChunkedServerMembersGenerator::new_reference(members))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch all members that have any of the roles given
|
||||||
|
async fn fetch_all_members_with_roles(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<Vec<Member>> {
|
||||||
|
let server_members = self.server_members.lock().await;
|
||||||
|
|
||||||
|
Ok(server_members
|
||||||
|
.clone()
|
||||||
|
.into_values()
|
||||||
|
.filter(|member| {
|
||||||
|
member.id.server == server_id
|
||||||
|
&& !member
|
||||||
|
.roles
|
||||||
|
.iter()
|
||||||
|
.filter(|p| roles.contains(*p))
|
||||||
|
.collect::<Vec<&String>>()
|
||||||
|
.is_empty()
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_all_members_with_roles_chunked(
|
||||||
|
&self,
|
||||||
|
server_id: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<ChunkedServerMembersGenerator> {
|
||||||
|
let server_members = self.server_members.lock().await;
|
||||||
|
|
||||||
|
let resp = server_members
|
||||||
|
.clone()
|
||||||
|
.into_values()
|
||||||
|
.filter(|member| {
|
||||||
|
member.id.server == server_id
|
||||||
|
&& !member
|
||||||
|
.roles
|
||||||
|
.iter()
|
||||||
|
.filter(|p| roles.contains(*p))
|
||||||
|
.collect::<Vec<&String>>()
|
||||||
|
.is_empty()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
return Ok(ChunkedServerMembersGenerator::new_reference(resp));
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch all memberships for a user
|
/// Fetch all memberships for a user
|
||||||
async fn fetch_all_memberships<'a>(&self, user_id: &str) -> Result<Vec<Member>> {
|
async fn fetch_all_memberships<'a>(&self, user_id: &str) -> Result<Vec<Member>> {
|
||||||
let server_members = self.server_members.lock().await;
|
let server_members = self.server_members.lock().await;
|
||||||
@@ -53,17 +117,17 @@ impl AbstractServerMembers for ReferenceDb {
|
|||||||
/// Fetch multiple members by their ids
|
/// Fetch multiple members by their ids
|
||||||
async fn fetch_members<'a>(&self, server_id: &str, ids: &'a [String]) -> Result<Vec<Member>> {
|
async fn fetch_members<'a>(&self, server_id: &str, ids: &'a [String]) -> Result<Vec<Member>> {
|
||||||
let server_members = self.server_members.lock().await;
|
let server_members = self.server_members.lock().await;
|
||||||
ids.iter()
|
Ok(ids
|
||||||
.map(|id| {
|
.iter()
|
||||||
|
.filter_map(|id| {
|
||||||
server_members
|
server_members
|
||||||
.get(&MemberCompositeKey {
|
.get(&MemberCompositeKey {
|
||||||
server: server_id.to_string(),
|
server: server_id.to_string(),
|
||||||
user: id.to_string(),
|
user: id.to_string(),
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
.ok_or_else(|| create_error!(NotFound))
|
|
||||||
})
|
})
|
||||||
.collect()
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch member count of a server
|
/// Fetch member count of a server
|
||||||
|
|||||||
@@ -175,13 +175,13 @@ impl Server {
|
|||||||
vec![]
|
vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
server.channels = channels.iter().map(|c| c.id()).collect();
|
server.channels = channels.iter().map(|c| c.id().to_string()).collect();
|
||||||
db.insert_server(&server).await?;
|
db.insert_server(&server).await?;
|
||||||
Ok((server, channels))
|
Ok((server, channels))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update server data
|
/// Update server data
|
||||||
pub async fn update<'a>(
|
pub async fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
partial: PartialServer,
|
partial: PartialServer,
|
||||||
@@ -228,6 +228,13 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ordered roles list
|
||||||
|
pub fn ordered_roles(&self) -> Vec<(String, Role)> {
|
||||||
|
let mut ordered_roles = self.roles.clone().into_iter().collect::<Vec<_>>();
|
||||||
|
ordered_roles.sort_by(|(_, role_a), (_, role_b)| role_a.rank.cmp(&role_b.rank));
|
||||||
|
ordered_roles
|
||||||
|
}
|
||||||
|
|
||||||
/// Set role permission on a server
|
/// Set role permission on a server
|
||||||
pub async fn set_role_permission(
|
pub async fn set_role_permission(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -253,6 +260,37 @@ impl Server {
|
|||||||
Err(create_error!(NotFound))
|
Err(create_error!(NotFound))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reorders the server's roles rankings
|
||||||
|
pub async fn set_role_ordering(&mut self, db: &Database, new_order: Vec<String>) -> Result<()> {
|
||||||
|
// New order must always contain every role
|
||||||
|
debug_assert_eq!(self.roles.len(), new_order.len());
|
||||||
|
|
||||||
|
// Set the role's ranks to the positions in the vec
|
||||||
|
for (rank, id) in new_order.iter().enumerate() {
|
||||||
|
self.roles.get_mut(id).unwrap().rank = rank as i64;
|
||||||
|
}
|
||||||
|
|
||||||
|
db.update_server(
|
||||||
|
&self.id,
|
||||||
|
&PartialServer {
|
||||||
|
roles: Some(self.roles.clone()),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Vec::new(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Publish bulk update event
|
||||||
|
EventV1::ServerRoleRanksUpdate {
|
||||||
|
id: self.id.clone(),
|
||||||
|
ranks: new_order,
|
||||||
|
}
|
||||||
|
.p(self.id.clone())
|
||||||
|
.await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Role {
|
impl Role {
|
||||||
@@ -285,7 +323,7 @@ impl Role {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update server data
|
/// Update server data
|
||||||
pub async fn update<'a>(
|
pub async fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
server_id: &str,
|
server_id: &str,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::{FieldsRole, FieldsServer, PartialRole, PartialServer, Role, Server};
|
use crate::{FieldsRole, FieldsServer, PartialRole, PartialServer, Role, Server};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -25,14 +25,11 @@ impl AbstractServers for MongoDb {
|
|||||||
async fn fetch_servers<'a>(&self, ids: &'a [String]) -> Result<Vec<Server>> {
|
async fn fetch_servers<'a>(&self, ids: &'a [String]) -> Result<Vec<Server>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<Server>(COL)
|
.col::<Server>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"_id": {
|
||||||
"_id": {
|
"$in": ids
|
||||||
"$in": ids
|
}
|
||||||
}
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", "servers"))?
|
.map_err(|_| create_database_error!("find", "servers"))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -84,7 +81,6 @@ impl AbstractServers for MongoDb {
|
|||||||
.map_err(|_| create_database_error!("to_document", "role"))?
|
.map_err(|_| create_database_error!("to_document", "role"))?
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -125,7 +121,6 @@ impl AbstractServers for MongoDb {
|
|||||||
"roles": &role_id
|
"roles": &role_id
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("update_many", "server_members"))?;
|
.map_err(|_| create_database_error!("update_many", "server_members"))?;
|
||||||
@@ -140,7 +135,6 @@ impl AbstractServers for MongoDb {
|
|||||||
"role_permissions.".to_owned() + role_id: 1_i32
|
"role_permissions.".to_owned() + role_id: 1_i32
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("update_one", "channels"))?;
|
.map_err(|_| create_database_error!("update_one", "channels"))?;
|
||||||
@@ -155,7 +149,6 @@ impl AbstractServers for MongoDb {
|
|||||||
"roles.".to_owned() + role_id: 1_i32
|
"roles.".to_owned() + role_id: 1_i32
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -188,12 +181,9 @@ impl MongoDb {
|
|||||||
// Find all channels
|
// Find all channels
|
||||||
let channels: Vec<String> = self
|
let channels: Vec<String> = self
|
||||||
.col::<Document>("channels")
|
.col::<Document>("channels")
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"server": server_id
|
||||||
"server": server_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", "channels"))?
|
.map_err(|_| create_database_error!("find", "channels"))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -214,23 +204,26 @@ impl MongoDb {
|
|||||||
|
|
||||||
// Delete all emoji.
|
// Delete all emoji.
|
||||||
self.col::<Document>("emojis")
|
self.col::<Document>("emojis")
|
||||||
.delete_many(
|
.update_many(
|
||||||
doc! {
|
doc! {
|
||||||
"parent.id": &server_id
|
"parent.id": &server_id
|
||||||
},
|
},
|
||||||
None,
|
doc! {
|
||||||
|
"$set": {
|
||||||
|
"parent": {
|
||||||
|
"type": "Detached"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", "emojis"))?;
|
.map_err(|_| create_database_error!("update_many", "emojis"))?;
|
||||||
|
|
||||||
// Delete all channels.
|
// Delete all channels.
|
||||||
self.col::<Document>("channels")
|
self.col::<Document>("channels")
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"server": &server_id
|
||||||
"server": &server_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", "channels"))?;
|
.map_err(|_| create_database_error!("delete_many", "channels"))?;
|
||||||
|
|
||||||
@@ -241,19 +234,16 @@ impl MongoDb {
|
|||||||
// Delete members and bans.
|
// Delete members and bans.
|
||||||
for with in &["server_members", "server_bans"] {
|
for with in &["server_members", "server_bans"] {
|
||||||
self.col::<Document>(with)
|
self.col::<Document>(with)
|
||||||
.delete_many(
|
.delete_many(doc! {
|
||||||
doc! {
|
"_id.server": &server_id
|
||||||
"_id.server": &server_id
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("delete_many", with))?;
|
.map_err(|_| create_database_error!("delete_many", with))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update many attachments with parent id.
|
// Update many attachments with parent id.
|
||||||
self.delete_many_attachments(doc! {
|
self.delete_many_attachments(doc! {
|
||||||
"object_id": &server_id
|
"used_for.id": &server_id
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use revolt_result::Result;
|
|||||||
|
|
||||||
use crate::UserSettings;
|
use crate::UserSettings;
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ impl AbstractUserSettings for MongoDb {
|
|||||||
doc! {
|
doc! {
|
||||||
"$set": set
|
"$set": set
|
||||||
},
|
},
|
||||||
UpdateOptions::builder().upsert(true).build(),
|
|
||||||
)
|
)
|
||||||
|
.with_options(UpdateOptions::builder().upsert(true).build())
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| create_database_error!("update_one", "user_settings"))
|
.map_err(|_| create_database_error!("update_one", "user_settings"))
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
use axum::{extract::FromRequestParts, http::request::Parts};
|
||||||
|
|
||||||
|
use revolt_result::{create_error, Error, Result};
|
||||||
|
|
||||||
|
use crate::{Database, User};
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl FromRequestParts<Database> for User {
|
||||||
|
type Rejection = Error;
|
||||||
|
|
||||||
|
async fn from_request_parts(parts: &mut Parts, db: &Database) -> Result<User> {
|
||||||
|
if let Some(Ok(bot_token)) = parts.headers.get("x-bot-token").map(|v| v.to_str()) {
|
||||||
|
let bot = db.fetch_bot_by_token(bot_token).await?;
|
||||||
|
db.fetch_user(&bot.id).await
|
||||||
|
} else if let Some(Ok(session_token)) =
|
||||||
|
parts.headers.get("x-session-token").map(|v| v.to_str())
|
||||||
|
{
|
||||||
|
let session = db.fetch_session_by_token(session_token).await?;
|
||||||
|
db.fetch_user(&session.user_id).await
|
||||||
|
} else {
|
||||||
|
Err(create_error!(NotAuthenticated))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#[cfg(feature = "axum-impl")]
|
||||||
|
mod axum;
|
||||||
mod model;
|
mod model;
|
||||||
mod ops;
|
mod ops;
|
||||||
#[cfg(feature = "rocket-impl")]
|
#[cfg(feature = "rocket-impl")]
|
||||||
@@ -5,9 +7,5 @@ mod rocket;
|
|||||||
#[cfg(feature = "rocket-impl")]
|
#[cfg(feature = "rocket-impl")]
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
#[cfg(feature = "rocket-impl")]
|
|
||||||
pub use self::rocket::*;
|
|
||||||
#[cfg(feature = "rocket-impl")]
|
|
||||||
pub use self::schema::*;
|
|
||||||
pub use model::*;
|
pub use model::*;
|
||||||
pub use ops::*;
|
pub use ops::*;
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
use std::{collections::HashSet, time::Duration};
|
use std::{collections::HashSet, str::FromStr, time::Duration};
|
||||||
|
|
||||||
use crate::{events::client::EventV1, Database, File, RatelimitEvent};
|
use crate::{events::client::EventV1, Database, File, RatelimitEvent, AMQP};
|
||||||
|
|
||||||
|
use authifier::config::{EmailVerificationConfig, Template};
|
||||||
|
use futures::future::join_all;
|
||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use revolt_config::config;
|
use revolt_config::{config, FeaturesLimits};
|
||||||
use revolt_models::v0;
|
use revolt_models::v0::{self, UserBadges, UserFlags};
|
||||||
use revolt_presence::filter_online;
|
use revolt_presence::filter_online;
|
||||||
use revolt_result::{create_error, Result};
|
use revolt_result::{create_error, Result};
|
||||||
|
use serde_json::json;
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
auto_derived_partial!(
|
auto_derived_partial!(
|
||||||
@@ -49,6 +53,12 @@ auto_derived_partial!(
|
|||||||
/// Bot information
|
/// Bot information
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub bot: Option<BotInformation>,
|
pub bot: Option<BotInformation>,
|
||||||
|
|
||||||
|
/// Time until user is unsuspended
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub suspended_until: Option<Timestamp>,
|
||||||
|
/// Last acknowledged policy change
|
||||||
|
pub last_acknowledged_policy_change: Timestamp,
|
||||||
},
|
},
|
||||||
"PartialUser"
|
"PartialUser"
|
||||||
);
|
);
|
||||||
@@ -61,6 +71,11 @@ auto_derived!(
|
|||||||
StatusPresence,
|
StatusPresence,
|
||||||
ProfileContent,
|
ProfileContent,
|
||||||
ProfileBackground,
|
ProfileBackground,
|
||||||
|
DisplayName,
|
||||||
|
|
||||||
|
// internal fields
|
||||||
|
Suspension,
|
||||||
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// User's relationship with another user (or themselves)
|
/// User's relationship with another user (or themselves)
|
||||||
@@ -140,7 +155,7 @@ pub static DISCRIMINATOR_SEARCH_SPACE: Lazy<HashSet<String>> = Lazy::new(|| {
|
|||||||
.collect::<HashSet<String>>();
|
.collect::<HashSet<String>>();
|
||||||
|
|
||||||
for discrim in [
|
for discrim in [
|
||||||
123, 1234, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999,
|
123, 1234, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 1488,
|
||||||
] {
|
] {
|
||||||
set.remove(&format!("{:0>4}", discrim));
|
set.remove(&format!("{:0>4}", discrim));
|
||||||
}
|
}
|
||||||
@@ -164,6 +179,8 @@ impl Default for User {
|
|||||||
flags: Default::default(),
|
flags: Default::default(),
|
||||||
privileged: Default::default(),
|
privileged: Default::default(),
|
||||||
bot: Default::default(),
|
bot: Default::default(),
|
||||||
|
suspended_until: Default::default(),
|
||||||
|
last_acknowledged_policy_change: Timestamp::UNIX_EPOCH,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,6 +203,7 @@ impl User {
|
|||||||
id: account_id.into().unwrap_or_else(|| Ulid::new().to_string()),
|
id: account_id.into().unwrap_or_else(|| Ulid::new().to_string()),
|
||||||
discriminator: User::find_discriminator(db, &username, None).await?,
|
discriminator: User::find_discriminator(db, &username, None).await?,
|
||||||
username,
|
username,
|
||||||
|
last_acknowledged_policy_change: Timestamp::now_utc(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -197,6 +215,22 @@ impl User {
|
|||||||
Ok(user)
|
Ok(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get limits for this user
|
||||||
|
pub async fn limits(&self) -> FeaturesLimits {
|
||||||
|
let config = config().await;
|
||||||
|
if ulid::Ulid::from_str(&self.id)
|
||||||
|
.expect("`ulid`")
|
||||||
|
.datetime()
|
||||||
|
.elapsed()
|
||||||
|
.expect("time went backwards")
|
||||||
|
<= Duration::from_secs(3600u64 * config.features.limits.global.new_user_hours as u64)
|
||||||
|
{
|
||||||
|
config.features.limits.new_user
|
||||||
|
} else {
|
||||||
|
config.features.limits.default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the relationship with another user
|
/// Get the relationship with another user
|
||||||
pub fn relationship_with(&self, user_b: &str) -> RelationshipStatus {
|
pub fn relationship_with(&self, user_b: &str) -> RelationshipStatus {
|
||||||
if self.id == user_b {
|
if self.id == user_b {
|
||||||
@@ -235,12 +269,11 @@ impl User {
|
|||||||
|
|
||||||
/// Check if this user can acquire another server
|
/// Check if this user can acquire another server
|
||||||
pub async fn can_acquire_server(&self, db: &Database) -> Result<()> {
|
pub async fn can_acquire_server(&self, db: &Database) -> Result<()> {
|
||||||
let config = config().await;
|
if db.fetch_server_count(&self.id).await? <= self.limits().await.servers {
|
||||||
if db.fetch_server_count(&self.id).await? <= config.features.limits.default.servers {
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(create_error!(TooManyServers {
|
Err(create_error!(TooManyServers {
|
||||||
max: config.features.limits.default.servers
|
max: self.limits().await.servers
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -265,7 +298,14 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure none of the following substrings show up in the username
|
// Ensure none of the following substrings show up in the username
|
||||||
const BLOCKED_SUBSTRINGS: &[&str] = &["```"];
|
const BLOCKED_SUBSTRINGS: &[&str] = &[
|
||||||
|
"```",
|
||||||
|
"discord.gg",
|
||||||
|
"rvlt.gg",
|
||||||
|
"guilded.gg",
|
||||||
|
"https://",
|
||||||
|
"http://",
|
||||||
|
];
|
||||||
|
|
||||||
for substr in BLOCKED_SUBSTRINGS {
|
for substr in BLOCKED_SUBSTRINGS {
|
||||||
if username_lowercase.contains(substr) {
|
if username_lowercase.contains(substr) {
|
||||||
@@ -313,15 +353,13 @@ impl User {
|
|||||||
) -> Result<Vec<v0::User>> {
|
) -> Result<Vec<v0::User>> {
|
||||||
let online_ids = filter_online(ids).await;
|
let online_ids = filter_online(ids).await;
|
||||||
|
|
||||||
Ok(db
|
Ok(
|
||||||
.fetch_users(ids)
|
join_all(db.fetch_users(ids).await?.into_iter().map(|user| async {
|
||||||
.await?
|
|
||||||
.into_iter()
|
|
||||||
.map(|user| {
|
|
||||||
let is_online = online_ids.contains(&user.id);
|
let is_online = online_ids.contains(&user.id);
|
||||||
user.into_known(perspective, is_online)
|
user.into_known(perspective, is_online).await
|
||||||
})
|
}))
|
||||||
.collect())
|
.await,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find a free discriminator for a given username
|
/// Find a free discriminator for a given username
|
||||||
@@ -469,7 +507,12 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add another user as a friend
|
/// Add another user as a friend
|
||||||
pub async fn add_friend(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
pub async fn add_friend(
|
||||||
|
&mut self,
|
||||||
|
db: &Database,
|
||||||
|
amqp: &AMQP,
|
||||||
|
target: &mut User,
|
||||||
|
) -> Result<()> {
|
||||||
match self.relationship_with(&target.id) {
|
match self.relationship_with(&target.id) {
|
||||||
RelationshipStatus::User => Err(create_error!(NoEffect)),
|
RelationshipStatus::User => Err(create_error!(NoEffect)),
|
||||||
RelationshipStatus::Friend => Err(create_error!(AlreadyFriends)),
|
RelationshipStatus::Friend => Err(create_error!(AlreadyFriends)),
|
||||||
@@ -477,6 +520,9 @@ impl User {
|
|||||||
RelationshipStatus::Blocked => Err(create_error!(Blocked)),
|
RelationshipStatus::Blocked => Err(create_error!(Blocked)),
|
||||||
RelationshipStatus::BlockedOther => Err(create_error!(BlockedByOther)),
|
RelationshipStatus::BlockedOther => Err(create_error!(BlockedByOther)),
|
||||||
RelationshipStatus::Incoming => {
|
RelationshipStatus::Incoming => {
|
||||||
|
// Accept incoming friend request
|
||||||
|
_ = amqp.friend_request_accepted(self, target).await;
|
||||||
|
|
||||||
self.apply_relationship(
|
self.apply_relationship(
|
||||||
db,
|
db,
|
||||||
target,
|
target,
|
||||||
@@ -486,6 +532,28 @@ impl User {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
RelationshipStatus::None => {
|
RelationshipStatus::None => {
|
||||||
|
// Get this user's current count of outgoing friend requests
|
||||||
|
let count = self
|
||||||
|
.relations
|
||||||
|
.as_ref()
|
||||||
|
.map(|relations| {
|
||||||
|
relations
|
||||||
|
.iter()
|
||||||
|
.filter(|r| matches!(r.status, RelationshipStatus::Outgoing))
|
||||||
|
.count()
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
// If we're over the limit, don't allow creating more requests
|
||||||
|
if count >= self.limits().await.outgoing_friend_requests {
|
||||||
|
return Err(create_error!(TooManyPendingFriendRequests {
|
||||||
|
max: self.limits().await.outgoing_friend_requests
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = amqp.friend_request_received(target, self).await;
|
||||||
|
|
||||||
|
// Send the friend request
|
||||||
self.apply_relationship(
|
self.apply_relationship(
|
||||||
db,
|
db,
|
||||||
target,
|
target,
|
||||||
@@ -572,7 +640,7 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update user data
|
/// Update user data
|
||||||
pub async fn update<'a>(
|
pub async fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &Database,
|
db: &Database,
|
||||||
partial: PartialUser,
|
partial: PartialUser,
|
||||||
@@ -621,9 +689,107 @@ impl User {
|
|||||||
x.background = None;
|
x.background = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
FieldsUser::DisplayName => self.display_name = None,
|
||||||
|
FieldsUser::Suspension => self.suspended_until = None,
|
||||||
|
FieldsUser::None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Suspend the user
|
||||||
|
///
|
||||||
|
/// - If a duration is specified, the user will be automatically unsuspended after the given time.
|
||||||
|
/// - If a reason is specified, an email will be sent.
|
||||||
|
pub async fn suspend(
|
||||||
|
&mut self,
|
||||||
|
db: &Database,
|
||||||
|
duration_days: Option<usize>,
|
||||||
|
reason: Option<Vec<String>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let authifier = db.clone().to_authifier().await;
|
||||||
|
let mut account = authifier
|
||||||
|
.database
|
||||||
|
.find_account(&self.id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_error!(InternalError))?;
|
||||||
|
|
||||||
|
account
|
||||||
|
.disable(&authifier)
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_error!(InternalError))?;
|
||||||
|
|
||||||
|
account
|
||||||
|
.delete_all_sessions(&authifier, None)
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_error!(InternalError))?;
|
||||||
|
|
||||||
|
self.update(
|
||||||
|
db,
|
||||||
|
PartialUser {
|
||||||
|
flags: Some(UserFlags::SuspendedUntil as i32),
|
||||||
|
suspended_until: duration_days.and_then(|dur| {
|
||||||
|
Timestamp::now_utc().checked_add(iso8601_timestamp::Duration::days(dur as i64))
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
vec![],
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(reason) = reason {
|
||||||
|
if let EmailVerificationConfig::Enabled { smtp, .. } =
|
||||||
|
authifier.config.email_verification
|
||||||
|
{
|
||||||
|
smtp.send_email(
|
||||||
|
account.email.clone(),
|
||||||
|
// maybe move this to common area?
|
||||||
|
&Template {
|
||||||
|
title: "Account Suspension".to_string(),
|
||||||
|
html: Some(include_str!("../../../templates/suspension.html").to_owned()),
|
||||||
|
text: include_str!("../../../templates/suspension.txt").to_owned(),
|
||||||
|
url: Default::default(),
|
||||||
|
},
|
||||||
|
json!({
|
||||||
|
"email": account.email,
|
||||||
|
"list": reason.join(", "),
|
||||||
|
"duration": duration_days,
|
||||||
|
"duration_display": if duration_days.is_some() {
|
||||||
|
"block"
|
||||||
|
} else {
|
||||||
|
"none"
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.map_err(|_| create_error!(InternalError))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unsuspend the user
|
||||||
|
pub async fn unsuspend(&mut self, db: &Database) -> Result<()> {
|
||||||
|
self.update(
|
||||||
|
db,
|
||||||
|
PartialUser {
|
||||||
|
flags: Some(0),
|
||||||
|
suspended_until: None,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
vec![],
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Permanently ban the user
|
||||||
|
///
|
||||||
|
/// - If a reason is specified, an email will be sent.
|
||||||
|
pub async fn ban(&mut self, _db: &Database, _reason: Option<String>) -> Result<()> {
|
||||||
|
// Send ban email (if reason provided)
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
/// Mark as deleted
|
/// Mark as deleted
|
||||||
pub async fn mark_deleted(&mut self, db: &Database) -> Result<()> {
|
pub async fn mark_deleted(&mut self, db: &Database) -> Result<()> {
|
||||||
self.update(
|
self.update(
|
||||||
@@ -639,8 +805,23 @@ impl User {
|
|||||||
FieldsUser::StatusPresence,
|
FieldsUser::StatusPresence,
|
||||||
FieldsUser::ProfileContent,
|
FieldsUser::ProfileContent,
|
||||||
FieldsUser::ProfileBackground,
|
FieldsUser::ProfileBackground,
|
||||||
|
FieldsUser::Suspension,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the user's badges along with calculating any dynamic badges
|
||||||
|
pub async fn get_badges(&self) -> u32 {
|
||||||
|
let config = config().await;
|
||||||
|
let badges = self.badges.unwrap_or_default() as u32;
|
||||||
|
|
||||||
|
if let Some(cutoff) = config.api.users.early_adopter_cutoff {
|
||||||
|
if Ulid::from_string(&self.id).unwrap().timestamp_ms() < cutoff {
|
||||||
|
return badges + UserBadges::EarlyAdopter as u32;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
badges
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use authifier::models::Session;
|
use authifier::models::Session;
|
||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{FieldsUser, PartialUser, RelationshipStatus, User};
|
use crate::{FieldsUser, PartialUser, RelationshipStatus, User};
|
||||||
|
|
||||||
|
#[cfg(feature = "mongodb")]
|
||||||
mod mongodb;
|
mod mongodb;
|
||||||
mod reference;
|
mod reference;
|
||||||
|
|
||||||
@@ -58,4 +60,9 @@ pub trait AbstractUsers: Sync + Send {
|
|||||||
|
|
||||||
/// Delete a user by their id
|
/// Delete a user by their id
|
||||||
async fn delete_user(&self, id: &str) -> Result<()>;
|
async fn delete_user(&self, id: &str) -> Result<()>;
|
||||||
|
|
||||||
|
/// Remove push subscription for a session by session id (TODO: remove)
|
||||||
|
async fn remove_push_subscription_by_session_id(&self, session_id: &str) -> Result<()>;
|
||||||
|
|
||||||
|
async fn update_session_last_seen(&self, session_id: &str, when: Timestamp) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use ::mongodb::options::{Collation, CollationStrength, FindOneOptions, FindOptions};
|
use ::mongodb::options::{Collation, CollationStrength, FindOneOptions, FindOptions};
|
||||||
use authifier::models::Session;
|
use authifier::models::Session;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::DocumentId;
|
use crate::DocumentId;
|
||||||
@@ -49,12 +50,9 @@ impl AbstractUsers for MongoDb {
|
|||||||
/// Fetch a session from the database by token
|
/// Fetch a session from the database by token
|
||||||
async fn fetch_session_by_token(&self, token: &str) -> Result<Session> {
|
async fn fetch_session_by_token(&self, token: &str) -> Result<Session> {
|
||||||
self.col::<Session>("sessions")
|
self.col::<Session>("sessions")
|
||||||
.find_one(
|
.find_one(doc! {
|
||||||
doc! {
|
"token": token
|
||||||
"token": token
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find_one", "sessions"))?
|
.map_err(|_| create_database_error!("find_one", "sessions"))?
|
||||||
.ok_or_else(|| create_error!(InvalidSession))
|
.ok_or_else(|| create_error!(InvalidSession))
|
||||||
@@ -64,14 +62,11 @@ impl AbstractUsers for MongoDb {
|
|||||||
async fn fetch_users<'a>(&self, ids: &'a [String]) -> Result<Vec<User>> {
|
async fn fetch_users<'a>(&self, ids: &'a [String]) -> Result<Vec<User>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<User>(COL)
|
.col::<User>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"_id": {
|
||||||
"_id": {
|
"$in": ids
|
||||||
"$in": ids
|
}
|
||||||
}
|
})
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async {
|
.filter_map(|s| async {
|
||||||
@@ -94,10 +89,10 @@ impl AbstractUsers for MongoDb {
|
|||||||
|
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<UserDocument>(COL)
|
.col::<UserDocument>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"username": username
|
||||||
"username": username
|
})
|
||||||
},
|
.with_options(
|
||||||
FindOptions::builder()
|
FindOptions::builder()
|
||||||
.collation(
|
.collation(
|
||||||
Collation::builder()
|
Collation::builder()
|
||||||
@@ -122,15 +117,13 @@ impl AbstractUsers for MongoDb {
|
|||||||
async fn fetch_mutual_user_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
async fn fetch_mutual_user_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<DocumentId>(COL)
|
.col::<DocumentId>(COL)
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"$and": [
|
||||||
"$and": [
|
{ "relations": { "$elemMatch": { "_id": &user_a, "status": "Friend" } } },
|
||||||
{ "relations": { "$elemMatch": { "_id": &user_a, "status": "Friend" } } },
|
{ "relations": { "$elemMatch": { "_id": &user_b, "status": "Friend" } } }
|
||||||
{ "relations": { "$elemMatch": { "_id": &user_b, "status": "Friend" } } }
|
]
|
||||||
]
|
})
|
||||||
},
|
.with_options(FindOptions::builder().projection(doc! { "_id": 1 }).build())
|
||||||
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", COL))?
|
.map_err(|_| create_database_error!("find", COL))?
|
||||||
.filter_map(|s| async { s.ok() })
|
.filter_map(|s| async { s.ok() })
|
||||||
@@ -143,17 +136,15 @@ impl AbstractUsers for MongoDb {
|
|||||||
async fn fetch_mutual_channel_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
async fn fetch_mutual_channel_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<DocumentId>("channels")
|
.col::<DocumentId>("channels")
|
||||||
.find(
|
.find(doc! {
|
||||||
doc! {
|
"channel_type": {
|
||||||
"channel_type": {
|
"$in": ["Group", "DirectMessage"]
|
||||||
"$in": ["Group", "DirectMessage"]
|
|
||||||
},
|
|
||||||
"recipients": {
|
|
||||||
"$all": [ user_a, user_b ]
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
|
"recipients": {
|
||||||
)
|
"$all": [ user_a, user_b ]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_options(FindOptions::builder().projection(doc! { "_id": 1 }).build())
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("find", "channels"))?
|
.map_err(|_| create_database_error!("find", "channels"))?
|
||||||
.filter_map(|s| async { s.ok() })
|
.filter_map(|s| async { s.ok() })
|
||||||
@@ -166,49 +157,46 @@ impl AbstractUsers for MongoDb {
|
|||||||
async fn fetch_mutual_server_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
async fn fetch_mutual_server_ids(&self, user_a: &str, user_b: &str) -> Result<Vec<String>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.col::<DocumentId>("server_members")
|
.col::<DocumentId>("server_members")
|
||||||
.aggregate(
|
.aggregate(vec![
|
||||||
vec![
|
doc! {
|
||||||
doc! {
|
"$match": {
|
||||||
"$match": {
|
"_id.user": user_a
|
||||||
"_id.user": user_a
|
}
|
||||||
}
|
},
|
||||||
},
|
doc! {
|
||||||
doc! {
|
"$lookup": {
|
||||||
"$lookup": {
|
"from": "server_members",
|
||||||
"from": "server_members",
|
"as": "members",
|
||||||
"as": "members",
|
"let": {
|
||||||
"let": {
|
"server": "$_id.server"
|
||||||
"server": "$_id.server"
|
},
|
||||||
},
|
"pipeline": [
|
||||||
"pipeline": [
|
{
|
||||||
{
|
"$match": {
|
||||||
"$match": {
|
"$expr": {
|
||||||
"$expr": {
|
"$and": [
|
||||||
"$and": [
|
{ "$eq": [ "$_id.user", user_b ] },
|
||||||
{ "$eq": [ "$_id.user", user_b ] },
|
{ "$eq": [ "$_id.server", "$$server" ] }
|
||||||
{ "$eq": [ "$_id.server", "$$server" ] }
|
]
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
doc! {
|
|
||||||
"$match": {
|
|
||||||
"members": {
|
|
||||||
"$size": 1_i32
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$match": {
|
||||||
|
"members": {
|
||||||
|
"$size": 1_i32
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
doc! {
|
},
|
||||||
"$project": {
|
doc! {
|
||||||
"_id": "$_id.server"
|
"$project": {
|
||||||
}
|
"_id": "$_id.server"
|
||||||
},
|
}
|
||||||
],
|
},
|
||||||
None,
|
])
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| create_database_error!("aggregate", "server_members"))?
|
.map_err(|_| create_database_error!("aggregate", "server_members"))?
|
||||||
.filter_map(|s| async { s.ok() })
|
.filter_map(|s| async { s.ok() })
|
||||||
@@ -224,16 +212,34 @@ impl AbstractUsers for MongoDb {
|
|||||||
partial: &PartialUser,
|
partial: &PartialUser,
|
||||||
remove: Vec<FieldsUser>,
|
remove: Vec<FieldsUser>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
query!(
|
if remove.contains(&FieldsUser::StatusText) && partial.status.is_some() {
|
||||||
self,
|
// stupid-ass workaround to fix mongo conflicting the same item
|
||||||
update_one_by_id,
|
let _: Result<()> = query!(
|
||||||
COL,
|
self,
|
||||||
id,
|
update_one_by_id,
|
||||||
partial,
|
COL,
|
||||||
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
id,
|
||||||
None
|
PartialUser {
|
||||||
)
|
..Default::default()
|
||||||
.map(|_| ())
|
},
|
||||||
|
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
||||||
|
None
|
||||||
|
)
|
||||||
|
.map(|_| ());
|
||||||
|
|
||||||
|
query!(self, update_one_by_id, COL, id, partial, vec![], None).map(|_| ())
|
||||||
|
} else {
|
||||||
|
query!(
|
||||||
|
self,
|
||||||
|
update_one_by_id,
|
||||||
|
COL,
|
||||||
|
id,
|
||||||
|
partial,
|
||||||
|
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
||||||
|
None
|
||||||
|
)
|
||||||
|
.map(|_| ())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set relationship with another user
|
/// Set relationship with another user
|
||||||
@@ -284,7 +290,6 @@ impl AbstractUsers for MongoDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}],
|
}],
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -305,7 +310,6 @@ impl AbstractUsers for MongoDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -316,6 +320,43 @@ impl AbstractUsers for MongoDb {
|
|||||||
async fn delete_user(&self, id: &str) -> Result<()> {
|
async fn delete_user(&self, id: &str) -> Result<()> {
|
||||||
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove push subscription for a session by session id (TODO: remove)
|
||||||
|
async fn remove_push_subscription_by_session_id(&self, session_id: &str) -> Result<()> {
|
||||||
|
self.col::<User>("sessions")
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": session_id
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$unset": {
|
||||||
|
"subscription": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|_| create_database_error!("update_one", "sessions"))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_session_last_seen(&self, session_id: &str, when: Timestamp) -> Result<()> {
|
||||||
|
let formatted: &str = &when.format();
|
||||||
|
|
||||||
|
self.col::<Session>("sessions")
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": session_id
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$set": {
|
||||||
|
"last_seen": formatted
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|_| create_database_error!("update_one", "sessions"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoDocumentPath for FieldsUser {
|
impl IntoDocumentPath for FieldsUser {
|
||||||
@@ -326,6 +367,9 @@ impl IntoDocumentPath for FieldsUser {
|
|||||||
FieldsUser::ProfileContent => "profile.content",
|
FieldsUser::ProfileContent => "profile.content",
|
||||||
FieldsUser::StatusPresence => "status.presence",
|
FieldsUser::StatusPresence => "status.presence",
|
||||||
FieldsUser::StatusText => "status.text",
|
FieldsUser::StatusText => "status.text",
|
||||||
|
FieldsUser::DisplayName => "display_name",
|
||||||
|
FieldsUser::Suspension => "suspended_until",
|
||||||
|
FieldsUser::None => "none",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use authifier::models::Session;
|
use authifier::models::Session;
|
||||||
|
use iso8601_timestamp::Timestamp;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
use crate::{FieldsUser, PartialUser, RelationshipStatus, User};
|
use crate::{FieldsUser, PartialUser, RelationshipStatus, User};
|
||||||
@@ -163,4 +164,13 @@ impl AbstractUsers for ReferenceDb {
|
|||||||
Err(create_error!(NotFound))
|
Err(create_error!(NotFound))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove push subscription for a session by session id (TODO: remove)
|
||||||
|
async fn remove_push_subscription_by_session_id(&self, _session_id: &str) -> Result<()> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_session_last_seen(&self, _session_id: &str, _when: Timestamp) -> Result<()> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ impl<'r> FromRequest<'r> for User {
|
|||||||
if let Some(user) = user {
|
if let Some(user) = user {
|
||||||
Outcome::Success(user.clone())
|
Outcome::Success(user.clone())
|
||||||
} else {
|
} else {
|
||||||
Outcome::Failure((Status::Unauthorized, authifier::Error::InvalidSession))
|
Outcome::Error((Status::Unauthorized, authifier::Error::InvalidSession))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use revolt_rocket_okapi::{
|
|||||||
|
|
||||||
use crate::User;
|
use crate::User;
|
||||||
|
|
||||||
impl<'r> OpenApiFromRequest<'r> for User {
|
impl OpenApiFromRequest<'_> for User {
|
||||||
fn from_request_input(
|
fn from_request_input(
|
||||||
_gen: &mut OpenApiGenerator,
|
_gen: &mut OpenApiGenerator,
|
||||||
_name: String,
|
_name: String,
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user