Compare commits

...

191 Commits

Author SHA1 Message Date
Paul Makles
ab4c3f913e chore: bump and publish 2022-01-15 11:09:05 +00:00
Zomatree
8be51dc4f0 feat(messaging): add sendable embeds (#119) 2022-01-15 10:43:47 +00:00
Paul Makles
96fd18f679 chore: remove limitations on private channels 2022-01-04 20:08:04 +00:00
Paul Makles
2db54cf8de chore: merge local branch 'feat/discoverability' 2022-01-03 15:27:32 +00:00
Paul Makles
c3f092aaef feat(invite): allow direct invitation by id for discoverable servers 2022-01-03 15:26:29 +00:00
Paul Makles
ce68f7eeed fix: don't return all members on message query / search 2022-01-03 15:21:12 +00:00
Paul Makles
139e704746 feat: add discoverability and analytics flags (#130) 2022-01-03 13:12:13 +00:00
Paul Makles
345c4c627c feat: add discoverability and analytics flags 2022-01-03 12:46:10 +00:00
insertish
03ca611553 ci: Synced local '.github/workflows/triage_pr.yml' with remote 'workflows/triage_pr.yml' 2021-12-31 17:32:26 +00:00
Paul
0fe63e3955 chore: bump rauth revision and version to 0.5.3-alpha.11 2021-11-28 17:58:24 +00:00
Paul
4474061103 chore: bump to 0.5.3-alpha.10 2021-11-23 08:57:34 +00:00
Paul
788580a64f fix: broken index migration
feat: add new index
2021-11-23 08:57:17 +00:00
Paul
1303dbc844 chore: bump version to 0.5.3-alpha.9 2021-11-20 13:20:21 +00:00
Paul
7981748b7b fix: include associated user ids (system messages)
fixes #3
2021-11-20 13:18:13 +00:00
Paul
53277cb235 feat(push): include URL for each notification 2021-11-20 12:49:57 +00:00
Paul
92c7f023b1 fix(push): use correct query to fetch sessions 2021-11-20 12:49:28 +00:00
Paul
839d7d0d9a feat(db): add indexes to various collections 2021-11-20 12:46:42 +00:00
Paul
ea8784215d feat: add new badges [skip ci] 2021-11-13 19:43:08 +00:00
insertish
084c635e9c ci: Synced local '.github/workflows/triage_pr.yml' with remote 'workflows/triage_pr.yml' 2021-11-11 20:08:22 +00:00
insertish
924398df1d ci: Created local '.github/workflows/triage_pr.yml' from remote 'workflows/triage_pr.yml' 2021-11-11 19:08:47 +00:00
insertish
c204288b76 ci: Synced local '.github/workflows/triage_issue.yml' with remote 'workflows/triage_issue.yml' 2021-11-11 19:01:42 +00:00
Paul Makles
dcee48c69f Merge pull request #118 from revoltchat/bot/update-global-workflow-651726897879937c474b292841037fc969ff31b9 2021-11-11 18:34:39 +00:00
web-flow
c6efe3b190 ci: update global workflows 2021-11-11 18:33:27 +00:00
Paul Makles
13ed849657 feat: get rid of arm64 because problems
todo: bring it back later
2021-11-09 23:59:24 +00:00
Paul
08c1aa6f5d feat(messaging): add masquerade to messages 2021-11-04 20:50:30 +00:00
Paul
84ba038350 chore: bump version to 0.5.3-alpha.7 2021-11-02 13:41:52 +00:00
Paul
8eb6f5a8af chore(task_queue): add logging 2021-11-02 13:41:08 +00:00
Paul
012a30ce61 feat(task_queue): ack task
closes #111 and closes #86
2021-11-02 13:30:36 +00:00
Paul
0025bea23d feat(task_queue): web push task
closes #113
2021-11-02 12:50:35 +00:00
Paul
997d1fffc0 feat(task_queue): process embeds task
closes #112
2021-11-01 22:04:11 +00:00
Paul
6366e0ac24 feat(task_queue): last_message_id task
closes #110
2021-11-01 20:54:48 +00:00
Paul
8d25dd1d65 feat: implement idempotency tokens
closes #108 and closes #106
2021-11-01 18:32:45 +00:00
Paul
49c7bc0ffe fix: prevent server owner being kicked
Fixes #96.
2021-11-01 13:51:38 +00:00
Paul Makles
fe5ba39e21 chore(docker): split up amd64 / arm64 builds
attempts to help with #103
2021-11-01 13:21:34 +00:00
Paul
2a4e290489 chore: bump version to 0.5.3-alpha.6 2021-10-31 18:28:31 +00:00
Paul Makles
193846e289 chore(ci): update Docker workflow
Adjusts current workflow to build images when any tag is created.
2021-10-31 18:25:28 +00:00
Paul Makles
7421ffc8a2 Merge pull request #90 from Mar0xy/patch-1 2021-09-23 18:00:24 +01:00
Paul
2ef6b28029 Switch to Redis from MongoDB for event broker.
Closes #1.
2021-09-19 22:13:25 +01:00
Marie
39a759396f Merge branch 'revoltchat:master' into patch-1 2021-09-18 21:15:49 +02:00
Paul
e5e0031cad Fix rate limiter for bots.
Use local-request state to cache User object.
2021-09-18 20:06:00 +01:00
Marie
211d74bb47 Merge branch 'revoltchat:master' into patch-1 2021-09-18 20:42:22 +02:00
Paul
0dcf8cb281 Bump rauth release and publish changes. 2021-09-18 19:06:59 +01:00
Marie
24a20a8719 Add NSFW single editing 2021-09-17 23:15:43 +02:00
Paul
37d45889ec Add reported flag and bump version. 2021-09-17 13:45:05 +01:00
Paul Makles
8e9991a2de Merge pull request #84 from rmeritz/change-username 2021-09-17 13:26:40 +01:00
Paul Makles
a6d49d388f Update src/routes/users/change_username.rs
Co-authored-by: Splatterxl <splatterxl@outlook.ie>
2021-09-17 13:26:32 +01:00
Paul Makles
6eee1bc669 Merge pull request #79 from Zomatree/ratelimits 2021-09-17 13:26:03 +01:00
Paul
f8e9924f18 Drop support for armv7 Docker builds. 2021-09-16 10:30:59 +01:00
Paul
6e1f3a79cf Update Docker image to use buster-slim + glibc. 2021-09-15 22:43:23 +01:00
Paul Makles
b1f5ba32ff Merge pull request #73 from Zomatree/msgpack_fix
fix msgpack encoding
2021-09-15 22:42:16 +01:00
Zomatree
6e70a39fce fix rauth 2021-09-15 21:37:42 +01:00
Paul Makles
53aa2818b4 Merge pull request #88 from meguminloli/re_username 2021-09-15 21:30:58 +01:00
Zomatree
00a7bcb958 Merge branch 'msgpack_fix' of github.com:Zomatree/delta into msgpack_fix 2021-09-15 21:25:41 +01:00
Zomatree
edcb7c50ce remove clone 2021-09-15 21:25:37 +01:00
Paul Makles
2e40927457 Merge branch 'master' into msgpack_fix 2021-09-15 21:12:27 +01:00
Zomatree
e636879065 Merge remote-tracking branch 'upstream/master' into ratelimits 2021-09-15 21:06:08 +01:00
Zomatree
46c4de8c68 cleanup 2021-09-15 20:55:02 +01:00
Megumin
6e46362d13 add a newline at the end of regex.rs 2021-09-15 22:22:55 +03:00
meguminloli
a2bfaa5791 moved all RE_USERNAME occurrences to regex.rs
switch to normal Cargo.toml

switch to normal Cargo.toml

switch to normal Cargo.toml

solved bug
2021-09-15 21:07:01 +03:00
Paul Makles
e2da00e6b0 Merge pull request #83 from Jan0660/master
Add route for fetching self for bots and users
2021-09-14 21:22:34 +01:00
Paul
839b66109c Only run Docker test if Dockerfile changes in PR. 2021-09-14 21:19:57 +01:00
Paul Makles
7f3141eec4 Merge pull request #81 from goaaats/feat/embed_improvements 2021-09-14 21:15:06 +01:00
Paul Makles
536e335b25 Merge branch 'master' into feat/embed_improvements 2021-09-14 20:54:21 +01:00
Paul Makles
4b8205b75b Merge pull request #76 from goaaats/feature/server_limit
Add basic server limit
2021-09-14 20:52:16 +01:00
Paul
3cbc548dd8 Don't run Docker test on PR. 2021-09-14 20:50:39 +01:00
Paul Makles
6a44a00eeb Merge pull request #77 from yannickfunk/unit-tests 2021-09-14 20:38:08 +01:00
meguminloli
2660b96250 push dependencies 2021-09-14 18:26:02 +03:00
Paul
b4db321d0e Fix missing definition of flags on creation. 2021-09-14 16:16:18 +01:00
Paul Makles
0eb52d32ef Merge pull request #68 from ryanalexander/server-flags
Add server flags value
2021-09-14 16:05:08 +01:00
Paul
9b6893ca48 Write database migration for last_message_id. #67 2021-09-14 15:07:41 +01:00
Paul Makles
8aa92564fc Merge pull request #67 from yannickfunk/unify-last-message 2021-09-14 13:39:27 +01:00
Yannick Funk
ddfc0a5f86 Merge branch 'master' into unify-last-message 2021-09-13 20:19:54 +02:00
Paul
ad364320c8 Fix route definitions for change pass / email. 2021-09-12 21:37:42 +01:00
Rebecca Meritz
4f1d841312 Allow users to change case of own username
Usernames are case-insensitive so now one have the name `foo` and
someone else have the name `FOO`, but user `foo` should be able to
change their own name to `FOO` if they want to for display purposes.
2021-09-11 18:59:23 -07:00
Jan0660
405f45d0f7 Add GET /users/@me 2021-09-12 00:01:30 +00:00
Paul
0b0249f544 Fix migration again. 2021-09-11 21:11:15 +01:00
Paul
d0cb73106d Fix session migration. 2021-09-11 18:41:06 +01:00
Paul Makles
5d3fef3bfa Merge pull request #82 from revoltchat/rauth-v1 2021-09-11 17:53:21 +01:00
Paul
8bb5fb25dd Fix bot logon, should do nested match.
Add comment to migrations.
Fix attachment deletes on messages.
2021-09-11 17:46:20 +01:00
Paul
fa8eb874d0 Handle both numerical and binary pings. 2021-09-11 12:43:26 +01:00
Paul
06f968022d Update to rAuth v1, closes #63.
Add Pong packet for normal frames.
2021-09-11 11:23:15 +01:00
Paul Makles
65165b1f8e Merge pull request #80 from goaaats/fix/youtube_timestamp 2021-09-09 20:50:31 +01:00
goat
6a29c7cf03 feat(january): add MAX_EMBED_COUNT variable 2021-09-08 18:16:56 +02:00
goat
8443cfab00 feat(january): fetch embeds for multiple links 2021-09-08 17:59:29 +02:00
goat
99a6e19cfc feat(january): ignore links between angle brackets 2021-09-08 16:56:17 +02:00
goat
77422f023e fix(january): add youtube timestamp field 2021-09-07 17:10:44 +02:00
Zomatree
b99807cf2a use unwrap 2021-09-07 15:57:38 +01:00
Zomatree
fc5d14d4aa remove line 2021-09-07 15:52:33 +01:00
Zomatree
fcb59b2493 remove comments 2021-09-07 15:50:56 +01:00
Zomatree
2f452b15b1 add ratelimits 2021-09-07 15:48:06 +01:00
Yannick Funk
602a65f809 Change branch to master 2021-09-07 14:48:46 +02:00
goat
88a1d46bcc feat: add basic server limit via env var 2021-09-07 05:16:27 +02:00
Yannick Funk
d8d0d93a21 Remove release build 2021-09-06 17:13:07 +02:00
Yannick Funk
fe085ef100 Change to nightly 2021-09-06 16:40:30 +02:00
Yannick Funk
8813515786 Change branch 2021-09-06 16:30:40 +02:00
Yannick Funk
6813fdd513 Create rust.yaml 2021-09-06 16:28:33 +02:00
Zomatree
175d257e34 fix msgpack encoding 2021-09-05 22:01:58 +01:00
Paul
5328c66cbb Fix image name. 2021-09-03 16:27:53 +01:00
Paul
862f57235c Fallback to unknown user.
Add GitHub CI.
2021-09-03 16:26:34 +01:00
Paul
835a32932f Merge branch 'master' of https://github.com/revoltchat/delta 2021-09-03 16:15:27 +01:00
Paul Makles
03cb48ed00 Merge pull request #72 from yannickfunk/push-notifications 2021-09-03 16:13:24 +01:00
Paul
dbfed52f17 Prevent access to bot routes as bot. 2021-09-03 14:37:40 +01:00
Yannick Funk
43b75a2310 Add author and tag to push notification 2021-09-02 23:22:23 +02:00
Yannick Funk
5a17038012 Remove whitespace 2021-09-02 20:34:26 +02:00
Yannick Funk
f6b55574b1 Only build notification when there are subscriptions 2021-09-02 20:32:36 +02:00
Yannick Funk
b5aa0ee971 Remove print 2021-09-02 18:24:22 +02:00
Yannick Funk
697132082a First draft for push notifications 2021-09-01 21:52:24 +02:00
Ryan Alexander
1a847d0084 Add server flags value 2021-08-29 09:14:10 +10:00
Paul Makles
17a378494c Merge pull request #65 from yannickfunk/mention-regex 2021-08-26 17:57:30 +01:00
Yannick Funk
6c6a1508dc Unify last_message_id 2021-08-26 16:56:13 +02:00
Yannick Funk
b15e07c707 Initial commit 2021-08-25 21:48:43 +02:00
Yannick Funk
ffa721117e Shorten regex further 2021-08-25 16:34:42 +02:00
Yannick Funk
c29f6d830c Ignore I L O and u 2021-08-25 16:32:11 +02:00
Yannick Funk
effe3763da Shorten regex and adapt capture evaluation 2021-08-25 00:10:20 +02:00
Paul Makles
b0d2e99588 Merge pull request #64 from yannickfunk/impl-ping-pong 2021-08-24 15:18:34 +01:00
Yannick Funk
1ecd74eb6c Remove whitespace 2021-08-23 14:25:09 +02:00
Yannick Funk
ceb35e58ce Parse ws spec ping messages 2021-08-23 14:22:23 +02:00
Zomatree
31ebf19cf9 color -> colour 2021-08-23 12:44:59 +01:00
Paul Makles
40a4cf244d Merge pull request #37 from infi/master
Update README to consistent format
2021-08-22 21:59:36 +01:00
Infi
6d2d3003bd Implement requested changes 2021-08-22 22:58:39 +02:00
Paul Makles
302c3e0d4b Merge pull request #42 from Zomatree/msgpack 2021-08-17 19:07:23 +01:00
Zomatree
867962cc3c reword comment 2021-08-17 17:35:05 +01:00
Zomatree
bcfde79745 add msgpack support 2021-08-17 17:23:36 +01:00
Paul
326e539ad3 Fix issues with incoming PRs. 2021-08-16 14:07:42 +01:00
Paul Makles
c632341eb7 Merge pull request #36 from Zomatree/master 2021-08-16 13:57:09 +01:00
Paul Makles
6de1d1f98b Merge pull request #38 from heikkari/13-24 2021-08-16 13:52:42 +01:00
heikkari
721ca3a4f5 Enhancement: Routes with no body should return 204 No Content. 2021-08-16 15:34:44 +03:00
heikkari
48d2fbe5ae Add a responder for empty responses 2021-08-16 14:05:59 +03:00
heikkari
c1d03960ed Respond to malformed WS packets 2021-08-16 13:40:52 +03:00
Infi
7c7573d3cf Fix Naming for Revolt, replace mention of Revolt with Delta 2021-08-16 10:48:09 +02:00
Infi
8ca3e50d91 Remove Unused Banner 2021-08-16 10:45:40 +02:00
Infi
906dd3fb04 Update README to consistent format, closes #28 2021-08-16 10:41:28 +02:00
Zomatree
08fe01a7f2 actually remove old func 2021-08-16 01:06:13 +01:00
Zomatree
d972395e43 use existing if_false function 2021-08-16 00:35:53 +01:00
Zomatree
5179504ad9 remove whitespace 2021-08-16 00:24:55 +01:00
Zomatree
d4f7c4a77d add nsfw to servers and channels 2021-08-16 00:18:14 +01:00
Paul Makles
28c4312880 Merge pull request #35 from heikkari/34 2021-08-15 23:45:12 +01:00
heikkari
7e84d2671a Move code to database/entities/user.rs 2021-08-16 01:26:05 +03:00
Paul
bffac8c4c0 Update CORS options.
Disallow bots to be added as friends.
2021-08-15 23:11:33 +01:00
heikkari
3b5bebab78 Fix: User's invisibility status is exposed 2021-08-16 01:05:54 +03:00
heikkari
e35a6b12e1 Fix: User's invisibility status is exposed 2021-08-16 00:40:33 +03:00
Paul
f04137d50f Include servers in mutual route. 2021-08-12 16:16:13 +01:00
Paul
b3bdf403cf Add delete route for bots. Closes #8. 2021-08-12 14:28:46 +01:00
Paul
bd3606176a Add bot edit route for #8. 2021-08-12 14:05:48 +01:00
Paul
a71bfa908c Add fetch owned, public and specific bots for #8. 2021-08-12 13:40:03 +01:00
Paul
9f749cfdf8 Invite bot to server / group for #8. 2021-08-12 09:24:06 +01:00
Paul
93610ebbc3 Restrict bots from creating servers, groups, acking messages, and making friends. 2021-08-11 20:16:11 +01:00
Paul
084d71f050 Add role hoisting / ranking.
Add bot creation for #8.
2021-08-11 20:04:22 +01:00
Paul
e70f848f21 Patch password length limit on change username. 2021-08-09 14:53:26 +01:00
Paul
e7065cbf1d Bump reqwest version.
Add mark server as read route.
Mark servers as read on join.
2021-08-07 12:26:25 +01:00
Paul
fdba2d9e15 Bump musl-builder. 2021-08-06 17:56:44 +01:00
Paul
8890e3bab0 Fixes #2: Include own beta badge in payload object 2021-08-06 17:34:48 +01:00
Paul
f09fde616a Add rate limiting to message sending. 2021-08-06 17:24:16 +01:00
Paul
6fe0725799 Organise Cargo.toml 2021-08-06 17:24:16 +01:00
Paul
382972ea22 Migrate the rest of the code. 2021-08-06 17:24:16 +01:00
Paul
9fec4e48c0 Start moving to Rocket 0.5.0 2021-08-06 17:24:16 +01:00
Paul
33262cc703 Add cache control header for default avatars. 2021-07-31 14:48:08 +01:00
Paul
e655f53641 Fix embeds not re-generating properly on edit.
Include own memberships in Ready payload.
2021-07-31 12:09:03 +01:00
Paul
74b4c6bf2a Re-write emails, remove HTML body.
SpamAssassin is a bitch.
2021-07-28 14:59:16 +01:00
Paul
3c0d736f1e Use plain reset / verify emails. 2021-07-28 14:08:55 +01:00
Paul
8f6e5be1d4 Events: Distribute user updates to servers.
Fix: Deleting a server now correctly deletes all associated objects.
2021-07-28 13:40:03 +01:00
Paul
7d3ce0c96a Remove broken HTML attribute. 2021-07-27 17:24:47 +01:00
Paul
c6de871ad3 Add extra snippet. 2021-07-27 17:14:36 +01:00
insert
f32eaa98d4 Revert "Strip all <a /> tags, fuck SpamAssassin."
This reverts commit d4743b1453
2021-07-27 16:11:59 +00:00
Paul
d4743b1453 Strip all <a /> tags, fuck SpamAssassin. 2021-07-27 16:32:55 +01:00
Paul
dd65b9f358 Try make content match href. 2021-07-27 16:25:32 +01:00
Paul
ec59d0e9a9 Try to make email not go into spam. 2021-07-27 16:16:17 +01:00
Paul
2407d4da3a Deprecate UserPresence event. 2021-07-27 14:22:41 +01:00
Paul
a5f7314f97 Fix description not being updated on servers. 2021-07-27 12:29:55 +01:00
Paul
deb2344428 Merge Jan's changes and bump version. 2021-07-25 21:53:35 +01:00
insert
37bb0f397f Merge branch 'master' into 'master'
Add member_count to /invites/:id

See merge request revolt/delta!3
2021-07-25 20:46:55 +00:00
jan0660
ca04f4d510 Add member_count to /invites/:id 2021-07-25 16:49:27 +02:00
Paul
682b6cbd9f Return banned user objects.
Fix category edit.
2021-07-24 19:42:57 +01:00
Paul
1a8006b3e4 Add before, after and sort support to search. 2021-07-21 18:26:35 +01:00
Paul
27a9fc3e21 Fix username block list. 2021-07-14 11:21:53 +01:00
Paul
1872432725 0.5.1-alpha.8
Add Edit Role route.
2021-07-13 13:43:42 +01:00
Paul
6f8802ab5f Feature: W.I.P. search route 2021-07-09 14:34:01 +01:00
Paul
fe821b8283 Temporary fix for compile.
Add badge ID.
2021-07-08 22:54:46 +01:00
Paul
a4c227ce41 Add "nearby" message query. 2021-07-07 16:21:21 +01:00
Paul
edd8ef8a1d Patch max password length on rauth. 2021-07-06 12:21:40 +01:00
Paul
96c9190fa8 Fix: Allow users with server overlap to access each other. 2021-07-05 17:49:30 +01:00
Paul
e452d6a362 Fix: Permission PUT route priority. 2021-07-01 17:49:48 +01:00
Paul
57f81e018c Servers: Create or delete roles.
Members: Assign / un-assign multiple roles.
Servers: Routes to set permissions.
2021-06-30 20:09:42 +01:00
Paul Makles
31c93b9954 Channels: Add role / default permission set routes.
Server: Add role create / delete route.
2021-06-30 12:56:18 +01:00
Paul
51dc25c94c Refactor: Use category_route.rs naming convention for channels. 2021-06-27 09:58:08 +01:00
Paul
a017872c99 Servers: Add categories to server model. 2021-06-27 00:13:35 +01:00
Paul
f35a1049da Fix: Use UploadFiles instead of EmbedLinks perm. 2021-06-26 22:35:21 +01:00
Paul
07e74e1074 Servers: New permissions implementation.
Feature: Roles and group permissions. (server-side)
2021-06-26 22:16:37 +01:00
Paul
35b0c69cdb Permissions: Add EmbedLinks / UploadFiles to channels
Web Push: Send events if mentioned
2021-06-25 13:33:05 +01:00
Paul
b45ea266d8 Fix various issues relating to voice channels. 2021-06-24 12:33:16 +01:00
Paul
9a417aa6ad Messaging: Add message replies.
Servers: Add VoiceChannel.
2021-06-23 13:50:18 +01:00
129 changed files with 6193 additions and 2180 deletions

2
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,2 @@
ko_fi: insertish
custom: https://insrt.uk/donate

103
.github/workflows/docker.yml vendored Normal file
View File

@@ -0,0 +1,103 @@
name: Docker
on:
push:
branches:
- "master"
tags:
- "*"
paths-ignore:
- ".github/**"
- "!.github/workflows/docker.yml"
- ".vscode/**"
- ".gitignore"
- ".gitlab-ci.yml"
- "LICENSE"
- "README"
pull_request:
branches:
- "master"
paths:
- "Dockerfile"
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
architecture: [linux/amd64]
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: "recursive"
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache/${{ matrix.architecture }}
key: ${{ runner.os }}-buildx-${{ matrix.architecture }}-${{ github.sha }}
- name: Build
uses: docker/build-push-action@v2
with:
context: .
platforms: ${{ matrix.architecture }}
cache-from: type=local,src=/tmp/.buildx-cache/${{ matrix.architecture }}
cache-to: type=local,dest=/tmp/.buildx-cache-new/${{ matrix.architecture }},mode=max
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache/${{ matrix.architecture }}
mv /tmp/.buildx-cache-new/${{ matrix.architecture }} /tmp/.buildx-cache/${{ matrix.architecture }}
publish_amd64:
needs: [test]
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: "recursive"
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache amd64 Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache/linux/amd64
key: ${{ runner.os }}-buildx-linux/amd64-${{ github.sha }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: revoltchat/server, ghcr.io/revoltchat/server
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to Github Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and publish
uses: docker/build-push-action@v2
with:
context: .
push: true
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache/linux/amd64
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

33
.github/workflows/rust.yaml vendored Normal file
View File

@@ -0,0 +1,33 @@
name: Rust build and test
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
CARGO_TERM_COLOR: always
jobs:
check:
name: Rust project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Run cargo build
uses: actions-rs/cargo@v1
with:
command: build
- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test

49
.github/workflows/triage_issue.yml vendored Normal file
View File

@@ -0,0 +1,49 @@
name: Add Issue to Board
on:
issues:
types: [opened]
jobs:
track_issue:
runs-on: ubuntu-latest
steps:
- name: Get project data
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
run: |
gh api graphql -f query='
query {
organization(login: "revoltchat"){
projectNext(number: 3) {
id
fields(first:20) {
nodes {
id
name
settings
}
}
}
}
}' > project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'TODO_OPTION_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
- name: Add issue to project
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
ISSUE_ID: ${{ github.event.issue.node_id }}
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $issue:ID!) {
addProjectNextItem(input: {projectId: $project, contentId: $issue}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f issue=$ISSUE_ID --jq '.data.addProjectNextItem.projectNextItem.id')"
echo 'ITEM_ID='$item_id >> $GITHUB_ENV

72
.github/workflows/triage_pr.yml vendored Normal file
View File

@@ -0,0 +1,72 @@
name: Add PR to Board
on:
pull_request_target:
types: [opened, synchronize, ready_for_review, review_requested]
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
- name: Get project data
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
run: |
gh api graphql -f query='
query {
organization(login: "revoltchat"){
projectNext(number: 3) {
id
fields(first:20) {
nodes {
id
name
settings
}
}
}
}
}' > project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'INCOMING_OPTION_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="Incoming PRs") |.id' project_data.json) >> $GITHUB_ENV
- name: Add PR to project
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
PR_ID: ${{ github.event.pull_request.node_id }}
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $pr:ID!) {
addProjectNextItem(input: {projectId: $project, contentId: $pr}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectNextItem.projectNextItem.id')"
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
- name: Set fields
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
run: |
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$status_field: ID!
$status_value: String!
) {
set_status: updateProjectNextItemField(input: {
projectId: $project
itemId: $item
fieldId: $status_field
value: $status_value
}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f status_field=$STATUS_FIELD_ID -f status_value=${{ env.INCOMING_OPTION_ID }} --silent

1998
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,39 +10,56 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1"
md5 = "0.7.0"
# Utility
lru = "0.7.0"
url = "2.2.2"
log = "0.4.11"
ulid = "0.4.1"
rand = "0.7.3"
time = "0.2.16"
nanoid = "0.4.0"
base64 = "0.13.0"
linkify = "0.6.0"
dotenv = "0.15.0"
futures = "0.3.8"
chrono = "0.4.15"
linkify = "0.6.0"
once_cell = "1.4.1"
env_logger = "0.7.1"
lazy_static = "1.4.0"
ctrlc = { version = "3.0", features = ["termination"] }
# Lang. Utilities
regex = "1"
num_enum = "0.5.1"
impl_ops = "0.1.1"
web-push = "0.7.2"
once_cell = "1.4.1"
bitfield = "0.13.2"
env_logger = "0.7.1"
phf = { version = "0.9.0", features = ["macros"] }
# ID / key generation
ulid = "0.4.1"
nanoid = "0.4.0"
base64 = "0.13.0"
# serde
serde_json = "1.0.57"
lazy_static = "1.4.0"
urlencoding = "1.1.1"
many-to-many = "0.1.2"
lettre = "0.10.0-alpha.1"
reqwest = { version = "0.10.8", features = ["json"] }
serde = { version = "1.0.115", features = ["derive"] }
validator = { version = "0.11", features = ["derive"] }
ctrlc = { version = "3.0", features = ["termination"] }
hive_pubsub = { version = "0.4.4", features = ["mongo"] }
async-std = { version = "1.8.0", features = ["tokio02", "attributes"] }
rmp-serde = { git = "https://github.com/insertish/msgpack-rust", rev = "5bf2c24203ad422233cf35b7b7bfad9f7e811814" }
# async
futures = "0.3.8"
chrono = "0.4.15"
async-channel = "1.6.1"
reqwest = { version = "0.11.4", features = ["json"] }
async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"] }
# internal util
web-push = "0.7.2"
many-to-many = "0.1.2"
lettre = "0.10.0-alpha.4"
rauth = { git = "https://github.com/insertish/rauth", rev = "157263ffcbd6cb1073e288b215db227634c4c29a" }
hive_pubsub = { git = "https://gitlab.insrt.uk/insert/hive", rev = "b0f3db9d33990530d7640d4bbb309c8d9eb5c0cf", features = ["redis-backend"] }
# redis
redis = { version = "0.21.2", features = ["async-std-comp"] }
mobc = { version = "0.7.3" }
mobc-redis = { version = "0.7.0", default-features = false, features = ["async-std-comp"] }
# web
async-tungstenite = { version = "0.10.0", features = ["async-std-runtime"] }
rocket_cors = { git = "https://github.com/insertish/rocket_cors", branch = "master" }
mongodb = { version = "1.1.1", features = ["tokio-runtime"], default-features = false }
rauth = { git = "https://gitlab.insrt.uk/insert/rauth", rev = "1db825a0059639144ea8802f6d51b5d27c5b986e" }
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", rev = "031948c1daaa146128d8a435be116476f2adde00" }
rocket_prometheus = { git = "https://github.com/insertish/rocket_prometheus", rev = "3d825aedb42793246c306a81fe67c5b187948983" }
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "031948c1daaa146128d8a435be116476f2adde00", default-features = false }
rocket = { version = "0.5.0-rc.1", default-features = false, features = ["json"] }
mongodb = { version = "1.2.2", features = ["tokio-runtime"], default-features = false }
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", rev = "5843861a88958c16bfaa0b40f0d8910772bcd2f6" }

View File

@@ -1,5 +1,5 @@
# Build Stage
FROM ekidd/rust-musl-builder:nightly-2021-01-01 AS builder
FROM rustlang/rust:nightly-slim AS builder
USER 0:0
WORKDIR /home/rust/src
@@ -7,16 +7,16 @@ RUN USER=root cargo new --bin revolt
WORKDIR /home/rust/src/revolt
COPY Cargo.toml Cargo.lock ./
COPY src/bin/dummy.rs ./src/bin/dummy.rs
RUN cargo build --release --bin dummy
RUN apt-get update && apt-get install -y libssl-dev pkg-config && cargo build --release --bin dummy
COPY assets/templates ./assets/templates
COPY src ./src
RUN cargo build --release
RUN cargo install --locked --path .
# Bundle Stage
FROM alpine:latest
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
COPY --from=builder /home/rust/src/revolt/target/x86_64-unknown-linux-musl/release/revolt ./
FROM debian:buster-slim
RUN apt-get update && apt-get install -y ca-certificates
COPY --from=builder /usr/local/cargo/bin/revolt ./
COPY assets ./assets
EXPOSE 8000
EXPOSE 9000

View File

@@ -1,20 +1,45 @@
![](assets/banner.png)
# Delta
Delta is a **blazing fast API server** built with Rust for the REVOLT platform.
## Description
Delta is a blazing fast API server built with Rust for Revolt.
**Features:**
Features:
- Robust and efficient API routes for running a chat platform.
- Distributed notification system, allowing any node to be seamlessly connected.
- Simple deployment, based mostly on pure Rust code and libraries.
- Hooks up to a MongoDB deployment, provide URI and no extra work needed.
## Docker Helper Scripts
## Stack
If you have Docker installed, you can use the helper scripts to deploy Revolt in your development environment.
- [Rocket](https://rocket.rs/) (REST)
- [Async Tungstenite](https://github.com/sdroege/async-tungstenite) (WebSockets)
- [MongoDB](https://mongodb.com/)
```bash
./build.sh # build Docker image
./run.sh # run Docker container
./monitor.sh # view container logs
./remove.sh # kill and remove container
```
## Resources
### Revolt
- [Revolt Project Board](https://github.com/revoltchat/revolt/discussions) (Submit feature requests here)
- [Revolt Testers Server](https://app.revolt.chat/invite/Testers)
- [Contribution Guide](https://developers.revolt.chat/contributing)
## CLI Commands
| Command | Description |
| ------------------ | ----------------------------------------------------------------------------------------- |
| `./publish.sh` | Publish a Docker Image. |
| `./set_version.sh` | Update the version. **Not intended for PR use.** |
| `cargo build` | Build/compile Delta. |
| `cargo run` | Run Delta. |
| `cargo fmt` | Format Delta. Not intended for PR use to avoid accidentally formatting unformatted files. |
## Contributing
The contribution guide is located at [developers.revolt.chat/contributing](https://developers.revolt.chat/contributing).
Please note that a pull request should only take care of one issue so that we can review it quickly.
## License
Delta is licensed under the [GNU Affero General Public License v3.0](https://github.com/revoltchat/delta/blob/master/LICENSE).

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 KiB

View File

@@ -4,57 +4,58 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Reset your password.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css">
a[x-apple-data-detectors] {color: inherit !important;}
</style>
</head>
<body style="margin: 0; padding: 0;">
<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="padding: 20px 0 30px 0;">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse; border: 1px solid #cccccc;">
<tr>
<td align="center" bgcolor="#ff4654">
<img src="https://revolt.chat/header.png" alt="REVOLT logo" width="600" height="168" style="display: block;" />
</td>
</tr>
<tr>
<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #153643; font-family: Arial, sans-serif;">
<h1 style="font-size: 24px; margin: 0;">Reset your password!</h1>
</td>
</tr>
<tr>
<td style="color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 24px; padding: 20px 0 0 0;">
<p style="margin: 0;">
Reset your password by <a href="{{url}}">clicking here</a>.
</p>
<p>
Or by manually navigating to the URL: {{url}}
</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#ff4654" style="padding: 30px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #ffffff; font-family: Arial, sans-serif; font-size: 14px;">
<p style="margin: 0;">Sent by REVOLT. &middot; <a style="color: white;" href="https://revolt.chat">Website</a> &middot; <a style="color: white;" href="https://gitlab.insrt.uk/revolt">Source Code</a></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse; border: 1px solid #cccccc;">
<tr>
<td align="center" bgcolor="#ff4654">
<img src="https://revolt.chat/header.png" alt="Revolt logo" width="600" height="168" style="display: block;" />
</td>
</tr>
<tr>
<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #153643; font-family: Arial, sans-serif;">
<h1 style="font-size: 24px; margin: 0;">Reset your password!</h1>
</td>
</tr>
<tr>
<td style="color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 24px; padding: 20px 0 0 0;">
<p>
You requested a password reset, if you didn't perform this action you can safely ignore this email.
</p>
<p style="margin: 0;">
Reset your password by navigating to <a href="{{url}}">{{url}}</a>.
</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#ff4654" style="padding: 30px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #ffffff; font-family: Arial, sans-serif; font-size: 14px;">
<p style="margin: 0;">Sent by Revolt. &middot; Website: <a style="color: white;" href="https://revolt.chat">https://revolt.chat</a></p>
<p>Revolt is a user-first chat platform built with modern web technologies.</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

View File

@@ -0,0 +1,5 @@
You requested a password reset, if you did not perform this action you can safely ignore this email.
Reset your password here: {{url}}
Sent by Revolt.

View File

@@ -0,0 +1,14 @@
<h2>Reset your password.</h2>
<p>
You requested a password reset, if you did not perform this action you can safely ignore this email.
</p>
<p>
Reset your password here: <a href="{{url}}">{{url}}</a>
</p>
<br/>
<p>
Sent by Revolt. · Website: <a href="https://revolt.chat">https://revolt.chat</a>
</p>
<p>
Revolt is a user-first chat platform built with modern web technologies.
</p>

View File

@@ -4,57 +4,58 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Verify your account.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css">
a[x-apple-data-detectors] {color: inherit !important;}
</style>
</head>
<body style="margin: 0; padding: 0;">
<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="padding: 20px 0 30px 0;">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse; border: 1px solid #cccccc;">
<tr>
<td align="center" bgcolor="#ff4654">
<img src="https://revolt.chat/header.png" alt="REVOLT logo" width="600" height="168" style="display: block;" />
</td>
</tr>
<tr>
<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #153643; font-family: Arial, sans-serif;">
<h1 style="font-size: 24px; margin: 0;">Verify your account!</h1>
</td>
</tr>
<tr>
<td style="color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 24px; padding: 20px 0 0 0;">
<p style="margin: 0;">
Please verify your account by <a href="{{url}}">clicking here</a>.
</p>
<p>
Or by manually navigating to the URL: {{url}}
</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#ff4654" style="padding: 30px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #ffffff; font-family: Arial, sans-serif; font-size: 14px;">
<p style="margin: 0;">Sent by REVOLT. &middot; <a style="color: white;" href="https://revolt.chat">Website</a> &middot; <a style="color: white;" href="https://gitlab.insrt.uk/revolt">Source Code</a></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse; border: 1px solid #cccccc;">
<tr>
<td align="center" bgcolor="#ff4654">
<img src="https://revolt.chat/header.png" alt="Revolt logo" width="600" height="168" style="display: block;" />
</td>
</tr>
<tr>
<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #153643; font-family: Arial, sans-serif;">
<h1 style="font-size: 24px; margin: 0;">You're almost there!</h1>
</td>
</tr>
<tr>
<td style="color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 24px; padding: 20px 0 0 0;">
<p>
Verify your account to be able to log into the platform. If you didn't perform this action you can safely ignore this email.
</p>
<p style="margin: 0;">
Please verify your account by navigating to <a href="{{url}}">{{url}}</a>.
</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#ff4654" style="padding: 30px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="color: #ffffff; font-family: Arial, sans-serif; font-size: 14px;">
<p style="margin: 0;">Sent by Revolt. &middot; Website: <a style="color: white;" href="https://revolt.chat">https://revolt.chat</a></p>
<p>Revolt is a user-first chat platform built with modern web technologies.</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

View File

@@ -0,0 +1,6 @@
You're almost there!
If you did not perform this action you can safely ignore this email.
Please verify your account here: {{url}}
Sent by Revolt.

View File

@@ -0,0 +1,15 @@
<h2>You're almost there!</h2>
<p>
Verify your account to be able to log into the platform.<br/>
If you did not perform this action you can safely ignore this email.
</p>
<p>
Please verify your account here: <a href="{{url}}">{{url}}</a>
</p>
<br/>
<p>
Sent by Revolt. · Website: <a href="https://revolt.chat">https://revolt.chat</a>
</p>
<p>
Revolt is a user-first chat platform built with modern web technologies.
</p>

View File

@@ -1,3 +1,3 @@
#!/bin/bash
export version=0.5.0-alpha.5
export version=0.5.3-alpha.16
echo "pub const VERSION: &str = \"${version}\";" > src/version.rs

View File

@@ -0,0 +1,20 @@
use serde::{Serialize, Deserialize};
pub fn if_false(t: &bool) -> bool {
!t
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Bot {
#[serde(rename = "_id")]
pub id: String,
pub owner: String,
pub token: String,
pub public: bool,
#[serde(skip_serializing_if = "if_false", default)]
pub analytics: bool,
#[serde(skip_serializing_if = "if_false", default)]
pub discoverable: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub interactions_url: Option<String>,
}

View File

@@ -1,22 +1,18 @@
use std::collections::HashMap;
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_GROUP_SIZE;
use futures::StreamExt;
use mongodb::bson::Bson;
use mongodb::{
bson::{doc, to_document, Document},
options::FindOptions,
};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
#[serde(rename = "_id")]
id: String,
author: String,
short: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "channel_type")]
pub enum Channel {
@@ -32,13 +28,11 @@ pub enum Channel {
active: bool,
recipients: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message: Option<LastMessage>,
last_message_id: Option<String>,
},
Group {
#[serde(rename = "_id")]
id: String,
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<String>,
name: String,
owner: String,
@@ -49,22 +43,54 @@ pub enum Channel {
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message: Option<LastMessage>,
},
last_message_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
permissions: Option<i32>,
#[serde(skip_serializing_if = "entities::server::if_false", default)]
nsfw: bool
},
TextChannel {
#[serde(rename = "_id")]
id: String,
server: String,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<String>,
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
default_permissions: Option<i32>,
#[serde(default = "HashMap::new", skip_serializing_if = "HashMap::is_empty")]
role_permissions: HashMap<String, i32>,
#[serde(skip_serializing_if = "entities::server::if_false", default)]
nsfw: bool
},
VoiceChannel {
#[serde(rename = "_id")]
id: String,
server: String,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message: Option<String>,
default_permissions: Option<i32>,
#[serde(default = "HashMap::new", skip_serializing_if = "HashMap::is_empty")]
role_permissions: HashMap<String, i32>,
#[serde(skip_serializing_if = "entities::server::if_false", default)]
nsfw: bool
},
}
@@ -74,7 +100,17 @@ impl Channel {
Channel::SavedMessages { id, .. }
| Channel::DirectMessage { id, .. }
| Channel::Group { id, .. }
| Channel::TextChannel { id, .. } => id,
| Channel::TextChannel { id, .. }
| Channel::VoiceChannel { id, .. } => id,
}
}
pub fn has_messaging(&self) -> Result<()> {
match self {
Channel::SavedMessages { .. }
| Channel::DirectMessage { .. }
| Channel::Group { .. }
| Channel::TextChannel { .. } => Ok(()),
Channel::VoiceChannel { .. } => Err(Error::InvalidOperation)
}
}
@@ -99,7 +135,7 @@ impl Channel {
Ok(())
}
pub async fn publish_update(&self, data: JsonValue) -> Result<()> {
pub async fn publish_update(&self, data: Value) -> Result<()> {
let id = self.id().to_string();
ClientboundNotification::ChannelUpdate {
id: id.clone(),
@@ -111,11 +147,7 @@ impl Channel {
Ok(())
}
pub async fn delete(&self) -> Result<()> {
let id = self.id();
let messages = get_collection("messages");
// Delete any invites.
pub async fn delete_associated_objects(id: Bson) -> Result<()> {
get_collection("channel_invites")
.delete_many(
doc! {
@@ -124,16 +156,21 @@ impl Channel {
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "channel_invites",
})?;
})
}
pub async fn delete_messages(id: Bson) -> Result<()> {
let messages = get_collection("messages");
// Delete any unreads.
get_collection("channel_unreads")
.delete_many(
doc! {
"_id.channel": id
"_id.channel": &id
},
None,
)
@@ -147,7 +184,7 @@ impl Channel {
let message_ids = messages
.find(
doc! {
"channel": id,
"channel": &id,
"attachment": {
"$exists": 1
}
@@ -198,65 +235,85 @@ impl Channel {
None,
)
.await
.map(|_| ())
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "messages",
})?;
})
}
pub async fn delete(&self) -> Result<()> {
let id = self.id();
// Delete any invites.
Channel::delete_associated_objects(Bson::String(id.to_string())).await?;
// Delete messages.
match &self {
Channel::VoiceChannel { .. } => {},
_ => {
Channel::delete_messages(Bson::String(id.to_string())).await?;
}
}
// Remove from server object.
if let Channel::TextChannel { server, .. } = &self {
let server = Ref::from_unchecked(server.clone()).fetch_server().await?;
let mut update = doc! {
"$pull": {
"channels": id
}
};
match &self {
Channel::TextChannel { server, .. }
| Channel::VoiceChannel { server, .. } => {
let server = Ref::from_unchecked(server.clone()).fetch_server().await?;
let mut update = doc! {
"$pull": {
"channels": id
}
};
if let Some(sys) = &server.system_messages {
let mut unset = doc! {};
if let Some(sys) = &server.system_messages {
let mut unset = doc! {};
if let Some(cid) = &sys.user_joined {
if id == cid {
unset.insert("system_messages.user_joined", 1);
if let Some(cid) = &sys.user_joined {
if id == cid {
unset.insert("system_messages.user_joined", 1);
}
}
if let Some(cid) = &sys.user_left {
if id == cid {
unset.insert("system_messages.user_left", 1);
}
}
if let Some(cid) = &sys.user_kicked {
if id == cid {
unset.insert("system_messages.user_kicked", 1);
}
}
if let Some(cid) = &sys.user_banned {
if id == cid {
unset.insert("system_messages.user_banned", 1);
}
}
if unset.len() > 0 {
update.insert("$unset", unset);
}
}
if let Some(cid) = &sys.user_left {
if id == cid {
unset.insert("system_messages.user_left", 1);
}
}
if let Some(cid) = &sys.user_kicked {
if id == cid {
unset.insert("system_messages.user_kicked", 1);
}
}
if let Some(cid) = &sys.user_banned {
if id == cid {
unset.insert("system_messages.user_banned", 1);
}
}
if unset.len() > 0 {
update.insert("$unset", unset);
}
}
get_collection("servers")
.update_one(
doc! {
"_id": server.id
},
update,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "servers",
})?;
get_collection("servers")
.update_one(
doc! {
"_id": server.id
},
update,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "servers",
})?;
},
_ => {}
}
// Finally, delete the channel object.
@@ -283,4 +340,53 @@ impl Channel {
Ok(())
}
pub async fn add_to_group(&self, member: String, by_user: String) -> Result<()> {
if let Channel::Group { id, recipients, .. } = &self {
if recipients.len() >= *MAX_GROUP_SIZE {
Err(Error::GroupTooLarge {
max: *MAX_GROUP_SIZE,
})?
}
if recipients.iter().find(|x| *x == &member).is_some() {
Err(Error::AlreadyInGroup)?
}
get_collection("channels")
.update_one(
doc! {
"_id": &id
},
doc! {
"$push": {
"recipients": &member
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;
ClientboundNotification::ChannelGroupJoin {
id: id.clone(),
user: member.clone(),
}
.publish(id.clone());
Content::SystemMessage(SystemMessage::UserAdded {
id: member,
by: by_user,
})
.send_as_system(&self)
.await
.ok();
Ok(())
} else {
Err(Error::InvalidOperation)
}
}
}

View File

@@ -1,22 +1,75 @@
use crate::util::variables::{USE_JANUARY, VAPID_PRIVATE_KEY};
use crate::util::variables::{USE_JANUARY, PUBLIC_URL, APP_URL};
use crate::{
database::*,
notifications::{events::ClientboundNotification, websocket::is_online},
util::result::{Error, Result},
};
use futures::StreamExt;
use mongodb::options::UpdateOptions;
use mongodb::{
bson::{doc, to_bson, DateTime},
options::FindOptions,
};
use rocket_contrib::json::JsonValue;
use mongodb::bson::{doc, to_bson, DateTime};
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use web_push::{
ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder,
};
use validator::Validate;
use std::collections::HashSet;
use std::time::SystemTime;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PushNotification {
pub author: String,
pub icon: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub image: Option<String>,
pub body: String,
pub tag: String,
pub timestamp: u64,
pub url: String,
}
impl PushNotification {
pub async fn new(msg: Message, channel: &Channel) -> Self {
let author = Ref::from_unchecked(msg.author.clone())
.fetch_user()
.await;
let (author, avatar) = if let Ok(author) = author {
(Some(author.username), author.avatar)
} else {
(None, None)
};
let icon = if let Some(avatar) = avatar {
avatar.get_autumn_url()
} else {
format!("{}/users/{}/default_avatar", PUBLIC_URL.as_str(), msg.author)
};
let image = msg.attachments.map_or(None, |attachments| {
attachments
.first()
.map_or(None, |v| Some(v.get_autumn_url()))
});
let body = match msg.content {
Content::Text(body) => body,
Content::SystemMessage(sys_msg) => sys_msg.into()
};
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("system time should be valid")
.as_secs();
Self {
author: author.unwrap_or_else(|| "Unknown".into()),
icon,
image,
body,
tag: channel.id().to_string(),
timestamp,
url: format!("{}/channel/{}/{}", *APP_URL, channel.id(), msg.id),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
@@ -43,6 +96,23 @@ pub enum SystemMessage {
ChannelIconChanged { by: String },
}
impl Into<String> for SystemMessage {
fn into(self) -> String {
match self {
SystemMessage::Text { content } => content,
SystemMessage::UserAdded { .. } => "User added to the channel.".to_string(),
SystemMessage::UserRemove { .. } => "User removed from the channel.".to_string(),
SystemMessage::UserJoined { .. } => "User joined the channel.".to_string(),
SystemMessage::UserLeft { .. } => "User left the channel.".to_string(),
SystemMessage::UserKicked { .. } => "User kicked from the channel.".to_string(),
SystemMessage::UserBanned { .. } => "User banned from the channel.".to_string(),
SystemMessage::ChannelRenamed { .. } => "Channel renamed.".to_string(),
SystemMessage::ChannelDescriptionChanged { .. } => "Channel description changed.".to_string(),
SystemMessage::ChannelIconChanged { .. } => "Channel icon changed.".to_string()
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Content {
@@ -57,12 +127,24 @@ impl Content {
target.id().to_string(),
self,
None,
None,
None
)
.publish(&target)
.publish(&target, false)
.await
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Validate)]
pub struct Masquerade {
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(length(min = 1, max = 32))]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(length(min = 1, max = 128))]
avatar: Option<String>
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Message {
#[serde(rename = "_id")]
@@ -81,6 +163,10 @@ pub struct Message {
pub embeds: Option<Vec<Embed>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mentions: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub replies: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub masquerade: Option<Masquerade>
}
impl Message {
@@ -89,22 +175,30 @@ impl Message {
channel: String,
content: Content,
mentions: Option<Vec<String>>,
replies: Option<Vec<String>>,
masquerade: Option<Masquerade>,
) -> Message {
Message {
id: Ulid::new().to_string(),
nonce: None,
channel,
author,
content,
attachments: None,
edited: None,
embeds: None,
mentions,
replies,
masquerade
}
}
pub async fn publish(self, channel: &Channel) -> Result<()> {
pub async fn publish(self, channel: &Channel, process_embeds: bool) -> Result<()> {
// Publish message event
ClientboundNotification::Message(self.clone())
.publish(channel.id().to_string());
// Commit message to database
get_collection("messages")
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
.await
@@ -113,204 +207,65 @@ impl Message {
with: "message",
})?;
// ! FIXME: all this code is legitimately crap
// ! rewrite when can be asked
let ss = self.clone();
let c_clone = channel.clone();
async_std::task::spawn(async move {
let mut set = if let Content::Text(text) = &ss.content {
doc! {
"last_message": {
"_id": ss.id.clone(),
"author": ss.author.clone(),
"short": text.chars().take(128).collect::<String>()
}
}
} else {
doc! {}
};
// ! MARK AS ACTIVE
// ! FIXME: temp code
let channels = get_collection("channels");
match &c_clone {
Channel::DirectMessage { id, .. } => {
set.insert("active", true);
channels
.update_one(
doc! { "_id": id },
doc! {
"$set": set
},
None,
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;*/
.unwrap();
}
Channel::Group { id, .. } => {
if let Content::Text(_) = &ss.content {
channels
.update_one(
doc! { "_id": id },
doc! {
"$set": set
},
None,
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;*/
.unwrap();
}
}
Channel::TextChannel { id, .. } => {
if let Content::Text(_) = &ss.content {
channels
.update_one(
doc! { "_id": id },
doc! {
"$set": {
"last_message": &ss.id
}
},
None,
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;*/
.unwrap();
}
}
_ => {}
}
});
// ! FIXME: also temp code
// ! THIS ADDS ANY MENTIONS
if let Some(mentions) = &self.mentions {
let message = self.id.clone();
let channel = self.channel.clone();
let mentions = mentions.clone();
async_std::task::spawn(async move {
get_collection("channel_unreads")
.update_many(
doc! {
"_id.channel": channel,
"_id.user": {
"$in": mentions
}
},
doc! {
"$push": {
"mentions": message
}
},
UpdateOptions::builder().upsert(true).build(),
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "channel_unreads",
})?;*/
.unwrap();
});
// spawn task_queue ( process embeds )
if process_embeds {
self.process_embed().await;
}
self.process_embed();
// spawn task_queue ( update last_message_id )
match channel {
Channel::DirectMessage { id, .. } =>
crate::task_queue::task_last_message_id::queue(id.clone(), self.id.clone(), true).await,
Channel::Group { id, .. } | Channel::TextChannel { id, .. } =>
crate::task_queue::task_last_message_id::queue(id.clone(), self.id.clone(), false).await,
_ => {}
}
let enc = serde_json::to_string(&self).unwrap();
ClientboundNotification::Message(self).publish(channel.id().to_string());
/*
Web Push Test Code
*/
let c_clone = channel.clone();
async_std::task::spawn(async move {
// Find all offline users.
let mut target_ids = vec![];
match &c_clone {
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
for recipient in recipients {
if !is_online(recipient) {
target_ids.push(recipient.clone());
}
// if mentions {
// spawn task_queue ( update channel_unreads )
// }
if let Some(mentions) = &self.mentions {
for user in mentions {
crate::task_queue::task_ack::queue(
channel.id().into(),
user.clone(),
crate::task_queue::task_ack::AckEvent::AddMention {
ids: vec![ self.id.clone() ]
}
}
_ => {}
).await;
}
}
// Fetch their corresponding sessions.
if let Ok(mut cursor) = get_collection("accounts")
.find(
doc! {
"_id": {
"$in": target_ids
},
"sessions.subscription": {
"$exists": true
}
},
FindOptions::builder()
.projection(doc! { "sessions": 1 })
.build(),
)
.await
{
let mut subscriptions = vec![];
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
if let Ok(sessions) = doc.get_array("sessions") {
for session in sessions {
if let Some(doc) = session.as_document() {
if let Ok(sub) = doc.get_document("subscription") {
let endpoint = sub.get_str("endpoint").unwrap().to_string();
let p256dh = sub.get_str("p256dh").unwrap().to_string();
let auth = sub.get_str("auth").unwrap().to_string();
subscriptions
.push(SubscriptionInfo::new(endpoint, p256dh, auth));
}
}
}
}
}
}
if subscriptions.len() > 0 {
let client = WebPushClient::new();
let key =
base64::decode_config(VAPID_PRIVATE_KEY.clone(), base64::URL_SAFE).unwrap();
for subscription in subscriptions {
let mut builder = WebPushMessageBuilder::new(&subscription).unwrap();
let sig_builder = VapidSignatureBuilder::from_pem(
std::io::Cursor::new(&key),
&subscription,
)
.unwrap();
let signature = sig_builder.build().unwrap();
builder.set_vapid_signature(signature);
builder.set_payload(ContentEncoding::AesGcm, enc.as_bytes());
let m = builder.build().unwrap();
client.send(m).await.ok();
// if (channel => DM | Group) | mentions {
// spawn task_queue ( web push )
// }
let mut target_ids = vec![];
match &channel {
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
for recipient in recipients {
if !is_online(recipient) {
target_ids.push(recipient.clone());
}
}
}
});
Channel::TextChannel { .. } => {
if let Some(mentions) = &self.mentions {
target_ids.append(&mut mentions.clone());
}
}
_ => {}
}
if target_ids.len() > 0 {
if let Ok(payload) = serde_json::to_string(&PushNotification::new(self, &channel).await) {
crate::task_queue::task_web_push::queue(target_ids, payload).await;
}
}
Ok(())
}
pub async fn publish_update(self, data: JsonValue) -> Result<()> {
pub async fn publish_update(self, data: Value) -> Result<()> {
let channel = self.channel.clone();
ClientboundNotification::MessageUpdate {
id: self.id.clone(),
@@ -318,49 +273,18 @@ impl Message {
data,
}
.publish(channel);
self.process_embed();
self.process_embed().await;
Ok(())
}
pub fn process_embed(&self) {
pub async fn process_embed(&self) {
if !*USE_JANUARY {
return;
}
if let Content::Text(text) = &self.content {
// ! FIXME: re-write this at some point,
// ! or just before we allow user generated embeds
let id = self.id.clone();
let content = text.clone();
let channel = self.channel.clone();
async_std::task::spawn(async move {
if let Ok(embeds) = Embed::generate(content).await {
if let Ok(bson) = to_bson(&embeds) {
if let Ok(_) = get_collection("messages")
.update_one(
doc! {
"_id": &id
},
doc! {
"$set": {
"embeds": bson
}
},
None,
)
.await
{
ClientboundNotification::MessageUpdate {
id,
channel: channel.clone(),
data: json!({ "embeds": embeds }),
}
.publish(channel);
}
}
}
});
crate::task_queue::task_process_embeds::queue(self.channel.clone(), self.id.clone(), text.clone()).await;
}
}
@@ -395,7 +319,7 @@ impl Message {
let attachment_ids: Vec<String> =
attachments.iter().map(|f| f.id.to_string()).collect();
get_collection("attachments")
.update_one(
.update_many(
doc! {
"_id": {
"$in": attachment_ids
@@ -410,11 +334,30 @@ impl Message {
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
operation: "update_many",
with: "attachment",
})?;
}
Ok(())
}
pub fn add_associated_user_ids(&self, ids: &mut HashSet<String>) {
ids.insert(self.author.clone());
if let Content::SystemMessage(sys) = &self.content {
match sys {
SystemMessage::Text { .. } => {},
SystemMessage::UserAdded { id, by } => { ids.insert(id.clone()); ids.insert(by.clone()); },
SystemMessage::UserRemove { id, by } => { ids.insert(id.clone()); ids.insert(by.clone()); },
SystemMessage::UserJoined { id } => { ids.insert(id.clone()); },
SystemMessage::UserLeft { id } => { ids.insert(id.clone()); },
SystemMessage::UserKicked { id } => { ids.insert(id.clone()); },
SystemMessage::UserBanned { id } => { ids.insert(id.clone()); },
SystemMessage::ChannelRenamed { by, .. } => { ids.insert(by.clone()); },
SystemMessage::ChannelDescriptionChanged { by } => { ids.insert(by.clone()); },
SystemMessage::ChannelIconChanged { by } => { ids.insert(by.clone()); }
}
}
}
}

View File

@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::variables::AUTUMN_URL;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
@@ -26,6 +27,8 @@ pub struct File {
#[serde(skip_serializing_if = "Option::is_none")]
deleted: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
reported: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
message_id: Option<String>,
@@ -112,4 +115,8 @@ impl File {
with: "attachment",
})
}
pub fn get_autumn_url(&self) -> String {
format!("{}/{}/{}", AUTUMN_URL.as_str(), self.tag, self.id)
}
}

View File

@@ -1,7 +1,9 @@
use crate::util::{
result::{Error, Result},
variables::JANUARY_URL,
variables::MAX_EMBED_COUNT,
};
use crate::database::entities::microservice::autumn::File;
use linkify::{LinkFinder, LinkKind};
use regex::Regex;
use serde::{Deserialize, Serialize};
@@ -46,6 +48,9 @@ pub enum Special {
None,
YouTube {
id: String,
#[serde(skip_serializing_if = "Option::is_none")]
timestamp: Option<String>,
},
Twitch {
content_type: TwitchType,
@@ -85,7 +90,23 @@ pub struct Metadata {
#[serde(skip_serializing_if = "Option::is_none")]
icon_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
color: Option<String>,
colour: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Text {
#[serde(skip_serializing_if = "Option::is_none")]
pub icon_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub media: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
pub colour: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -93,6 +114,7 @@ pub struct Metadata {
pub enum Embed {
Website(Metadata),
Image(Image),
Text(Text),
None,
}
@@ -100,11 +122,15 @@ impl Embed {
pub async fn generate(content: String) -> Result<Vec<Embed>> {
lazy_static! {
static ref RE_CODE: Regex = Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap();
static ref RE_IGNORED: Regex = Regex::new("(<http.+>)").unwrap();
}
// Ignore code blocks.
let content = RE_CODE.replace_all(&content, "");
// Ignore all content between angle brackets starting with http.
let content = RE_IGNORED.replace_all(&content, "");
let content = content
// Ignore quoted lines.
.split("\n")
@@ -120,35 +146,55 @@ impl Embed {
.collect::<Vec<&str>>()
.join("\n");
// ! FIXME: allow multiple links
// ! FIXME: prevent generation if link is surrounded with < >
let mut finder = LinkFinder::new();
finder.kinds(&[LinkKind::Url]);
let links: Vec<_> = finder.links(&content).collect();
let links: Vec<_> = finder.links(&content).take(*MAX_EMBED_COUNT).collect();
if links.len() == 0 {
return Err(Error::LabelMe);
}
let link = &links[0];
let mut embeds: Vec<Embed> = Vec::new();
let client = reqwest::Client::new();
let result = client
.get(&format!("{}/embed", *JANUARY_URL))
.query(&[("url", link.as_str())])
.send()
.await;
let mut link_index = 0;
match result {
Err(_) => return Err(Error::LabelMe),
Ok(result) => match result.status() {
reqwest::StatusCode::OK => {
let res: Embed = result.json().await.map_err(|_| Error::InvalidOperation)?;
// ! FIXME: batch request to january?
while link_index < links.len() {
let link = &links[link_index];
Ok(vec![res])
}
_ => return Err(Error::LabelMe),
},
// Check if we already processed this link.
if link_index != 0 && links.iter().take(link_index).any(|x| x.as_str() == link.as_str()) {
link_index = link_index + 1;
continue;
}
let client = reqwest::Client::new();
let result = client
.get(&format!("{}/embed", *JANUARY_URL))
.query(&[("url", link.as_str())])
.send()
.await;
if result.is_err() {
link_index = link_index + 1;
continue;
}
let response = result.unwrap();
if response.status().is_success() {
let res: Embed = response.json().await.map_err(|_| Error::InvalidOperation)?;
embeds.push(res);
}
link_index = link_index + 1;
}
// Prevent database update when no embeds are found.
if embeds.len() > 0 {
Ok(embeds)
} else {
Err(Error::LabelMe)
}
}
}

View File

@@ -5,6 +5,7 @@ mod microservice;
mod server;
mod sync;
mod user;
mod bots;
use microservice::*;
@@ -16,3 +17,4 @@ pub use message::*;
pub use server::*;
pub use sync::*;
pub use user::*;
pub use bots::*;

View File

@@ -1,14 +1,16 @@
use std::collections::HashMap;
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::bson::doc;
use mongodb::bson::{Bson, doc};
use mongodb::bson::from_document;
use mongodb::bson::to_document;
use mongodb::bson::Document;
use mongodb::options::FindOptions;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
use ulid::Ulid;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemberCompositeKey {
@@ -25,6 +27,37 @@ pub struct Member {
pub nickname: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
pub roles: Option<Vec<String>>
}
pub type PermissionTuple = (
i32, // server permission
i32 // channel permission
);
pub fn if_false(t: &bool) -> bool {
!t
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Role {
pub name: String,
pub permissions: PermissionTuple,
#[serde(skip_serializing_if = "Option::is_none")]
pub colour: Option<String>,
#[serde(skip_serializing_if = "if_false", default)]
pub hoist: bool,
#[serde(default)]
pub rank: i64,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Category {
pub id: String,
pub title: String,
pub channels: Vec<String>
}
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -52,21 +85,36 @@ pub enum RemoveMember {
pub struct Server {
#[serde(rename = "_id")]
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub nonce: Option<String>,
pub owner: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub channels: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub categories: Option<Vec<Category>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system_messages: Option<SystemMessageChannels>,
#[serde(default = "HashMap::new", skip_serializing_if = "HashMap::is_empty")]
pub roles: HashMap<String, Role>,
pub default_permissions: PermissionTuple,
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
pub banner: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
#[serde(skip_serializing_if = "if_false", default)]
pub nsfw: bool,
#[serde(skip_serializing_if = "if_false", default)]
pub analytics: bool,
#[serde(skip_serializing_if = "if_false", default)]
pub discoverable: bool
}
impl Server {
@@ -88,7 +136,7 @@ impl Server {
Ok(())
}
pub async fn publish_update(&self, data: JsonValue) -> Result<()> {
pub async fn publish_update(&self, data: Value) -> Result<()> {
ClientboundNotification::ServerUpdate {
id: self.id.clone(),
data,
@@ -100,58 +148,11 @@ impl Server {
}
pub async fn delete(&self) -> Result<()> {
let messages = get_collection("messages");
// Check if there are any attachments we need to delete.
// ! FIXME: make this generic and merge with channel delete
// ! e.g. delete_channel(filter: doc!)
let message_ids = messages
.find(
doc! {
"server": &self.id,
"attachment": {
"$exists": 1
}
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "fetch_many",
with: "messages",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string()))
.collect::<Vec<String>>();
Channel::delete_messages(Bson::Document(doc! { "$in": &self.channels })).await?;
// If we found any, mark them as deleted.
if message_ids.len() > 0 {
get_collection("attachments")
.update_many(
doc! {
"message_id": {
"$in": message_ids
}
},
doc! {
"$set": {
"deleted": true
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "attachments",
})?;
}
// And then delete said messages.
messages
// Delete all channels.
get_collection("channels")
.delete_many(
doc! {
"server": &self.id
@@ -161,27 +162,13 @@ impl Server {
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "messages",
with: "channels",
})?;
// Delete all channels, members, bans and invites.
for with in &["channels", "channel_invites"] {
get_collection(with)
.delete_many(
doc! {
"server": &self.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with,
})?;
}
// ! FIXME: delete any unreads
// Delete any associated objects, e.g. unreads and invites.
Channel::delete_associated_objects(Bson::Document(doc! { "$in": &self.channels })).await?;
// Delete members and bans.
for with in &["server_members", "server_bans"] {
get_collection(with)
.delete_many(
@@ -197,6 +184,16 @@ impl Server {
})?;
}
// Delete server icon / banner.
if let Some(attachment) = &self.icon {
attachment.delete().await?;
}
if let Some(attachment) = &self.banner {
attachment.delete().await?;
}
// Delete the server
get_collection("servers")
.delete_one(
doc! {
@@ -215,14 +212,6 @@ impl Server {
}
.publish(self.id.clone());
if let Some(attachment) = &self.icon {
attachment.delete().await?;
}
if let Some(attachment) = &self.banner {
attachment.delete().await?;
}
Ok(())
}
@@ -247,6 +236,30 @@ impl Server {
.collect::<Vec<Member>>())
}
pub async fn fetch_members_with_ids(id: &str, ids: &Vec<String>) -> Result<Vec<Member>> {
Ok(get_collection("server_members")
.find(
doc! {
"_id.server": id,
"_id.user": {
"$in": ids
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "server_members",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| from_document(x).ok())
.collect::<Vec<Member>>())
}
pub async fn fetch_member_ids(id: &str) -> Result<Vec<String>> {
Ok(get_collection("server_members")
.find(
@@ -273,7 +286,48 @@ impl Server {
.collect::<Vec<String>>())
}
pub async fn mark_as_read(&self, id: &str) -> Result<()> {
let current_time = Ulid::new().to_string();
let unreads = get_collection("channel_unreads");
unreads.delete_many(
doc! {
"_id.channel": {
"$in": &self.channels
},
"_id.user": &id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_many",
with: "channel_unreads",
})?;
unreads.insert_many(
self.channels
.iter()
.map(|channel| doc! {
"_id": {
"channel": channel,
"user": &id
},
"last_id": &current_time
})
.collect::<Vec<Document>>(),
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "channel_unreads",
})
.map(|_| ())
}
pub async fn join_member(&self, id: &str) -> Result<()> {
// Check if user is banned.
if get_collection("server_bans")
.find_one(
doc! {
@@ -292,6 +346,7 @@ impl Server {
return Err(Error::Banned);
}
// Add user to server.
get_collection("server_members")
.insert_one(
doc! {
@@ -308,12 +363,14 @@ impl Server {
with: "server_members",
})?;
// Announce that user joined server.
ClientboundNotification::ServerMemberJoin {
id: self.id.clone(),
user: id.to_string(),
}
.publish(self.id.clone());
// Broadcast join message.
if let Some(channels) = &self.system_messages {
if let Some(cid) = &channels.user_joined {
let channel = Ref::from_unchecked(cid.clone()).fetch_channel().await?;
@@ -323,6 +380,9 @@ impl Server {
}
}
// Mark entire server as read.
self.mark_as_read(&id).await?;
Ok(())
}
@@ -392,4 +452,19 @@ impl Server {
Ok(())
}
pub async fn get_member_count(id: &str) -> Result<i64> {
Ok(get_collection("server_members")
.count_documents(
doc! {
"_id.server": id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "count_documents",
with: "server_members",
})?)
}
}

View File

@@ -16,6 +16,7 @@ use crate::database::*;
use crate::notifications::websocket::is_online;
use crate::util::result::{Error, Result};
use crate::util::variables::EARLY_ADOPTER_BADGE;
use crate::util::variables::MAX_SERVER_COUNT;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum RelationshipStatus {
@@ -35,7 +36,7 @@ pub struct Relationship {
pub status: RelationshipStatus,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Presence {
Online,
Idle,
@@ -67,12 +68,22 @@ pub enum Badges {
Translator = 2,
Supporter = 4,
ResponsibleDisclosure = 8,
Founder = 16,
PlatformModeration = 32,
ActiveSupporter = 64,
Paw = 128,
EarlyAdopter = 256,
ReservedRelevantJokeBadge1 = 512
}
impl_op_ex_commutative!(+ |a: &i32, b: &Badges| -> i32 { *a | *b as i32 });
// When changing this struct, update notifications/payload.rs#80
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BotInformation {
owner: String
}
// When changing this struct, update notifications/payload.rs#113
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
#[serde(rename = "_id")]
@@ -90,6 +101,11 @@ pub struct User {
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<UserProfile>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bot: Option<BotInformation>,
// ? This should never be pushed to the collection.
#[serde(skip_serializing_if = "Option::is_none")]
pub relationship: Option<RelationshipStatus>,
@@ -118,8 +134,8 @@ impl User {
self
}
/// Mutate the user object to appear as seen by user.
pub fn with(mut self, permissions: UserPermissions<[u32; 1]>) -> User {
/// Apply any relevant badges.
pub fn apply_badges(mut self) -> User {
let mut badges = self.badges.unwrap_or_else(|| 0);
if let Ok(id) = Ulid::from_string(&self.id) {
if id.datetime().timestamp_millis() < *EARLY_ADOPTER_BADGE {
@@ -128,15 +144,31 @@ impl User {
}
self.badges = Some(badges);
self
}
/// Mutate the user object to appear as seen by user.
pub fn with(self, permissions: UserPermissions<[u32; 1]>) -> User {
let mut user = self.apply_badges();
if permissions.get_view_profile() {
self.online = Some(is_online(&self.id));
user.online = Some(is_online(&user.id));
} else {
self.status = None;
user.status = None;
}
self.profile = None;
self
// If the user's status is `Presence::Invisible`, return it as `Presence::Offline`
if let Some(status) = &user.status {
if let Some(presence) = &status.presence {
if presence == &Presence::Invisible {
user.status = None;
user.online = Some(false);
}
}
}
user.profile = None;
user
}
/// Mutate the user object to appear as seen by user.
@@ -158,7 +190,7 @@ impl User {
/// Utility function for checking claimed usernames.
pub async fn is_username_taken(username: &str) -> Result<bool> {
if username.to_lowercase() == "revolt" && username.to_lowercase() == "admin" {
if username.to_lowercase() == "revolt" || username.to_lowercase() == "admin" || username.to_lowercase() == "system" {
return Ok(true);
}
@@ -186,7 +218,7 @@ impl User {
/// Utility function for fetching multiple users from the perspective of one.
/// Assumes user has a mutual connection with others.
pub async fn fetch_multiple_users(&self, user_ids: Vec<String>) -> Result<Vec<User>> {
pub async fn fetch_multiple_users(&self, user_ids: &Vec<String>) -> Result<Vec<User>> {
let mut users = vec![];
let mut cursor = get_collection("users")
.find(
@@ -197,7 +229,7 @@ impl User {
},
FindOptions::builder()
.projection(
doc! { "_id": 1, "username": 1, "avatar": 1, "badges": 1, "status": 1 },
doc! { "_id": 1, "username": 1, "avatar": 1, "badges": 1, "status": 1, "flags": 1, "bot": 1 },
)
.build(),
)
@@ -227,12 +259,36 @@ impl User {
Ok(users)
}
/// Utility function to get all the server IDs the user is in.
pub async fn fetch_server_ids(&self) -> Result<Vec<String>> {
/// Utility function to get all of a user's memberships.
pub async fn fetch_memberships(id: &str) -> Result<Vec<Member>> {
Ok(get_collection("server_members")
.find(
doc! {
"_id.user": &self.id
"_id.user": id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "server_members",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| {
from_document(x).ok()
})
.collect::<Vec<Member>>())
}
/// Utility function to get all the server IDs the user is in.
pub async fn fetch_server_ids(id: &str) -> Result<Vec<String>> {
Ok(get_collection("server_members")
.find(
doc! {
"_id.user": id
},
None,
)
@@ -255,11 +311,11 @@ impl User {
}
/// Utility function to fetch unread objects for user.
pub async fn fetch_unreads(&self) -> Result<Vec<Document>> {
pub async fn fetch_unreads(id: &str) -> Result<Vec<Document>> {
Ok(get_collection("channel_unreads")
.find(
doc! {
"_id.user": &self.id
"_id.user": id
},
None,
)
@@ -272,4 +328,10 @@ impl User {
.collect::<Vec<Document>>()
.await)
}
/// Check if this user can acquire another server.
pub async fn can_acquire_server(id: &str) -> Result<bool> {
let server_ids = User::fetch_server_ids(&id).await?;
Ok(server_ids.len() < *MAX_SERVER_COUNT)
}
}

View File

@@ -2,7 +2,6 @@ use crate::database::*;
use crate::util::result::{Error, Result};
use mongodb::bson::{doc, from_document};
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use validator::Validate;
@@ -59,7 +58,29 @@ impl Ref {
}
pub async fn fetch_invite(&self) -> Result<Invite> {
self.fetch("channel_invites").await
match self.fetch("channel_invites").await {
Ok(invite) => Ok(invite),
Err(err) => {
if let Ok(server) = self.fetch::<Server>("servers").await {
if server.discoverable {
return Ok(
Invite::Server {
code: server.id.clone(),
server: server.id,
creator: server.owner,
channel: server.channels[0].clone(),
}
)
}
}
Err(err)
}
}
}
pub async fn fetch_bot(&self) -> Result<Bot> {
self.fetch("bots").await
}
pub async fn fetch_member(&self, server: &str) -> Result<Member> {
@@ -127,9 +148,9 @@ impl User {
}
impl<'r> FromParam<'r> for Ref {
type Error = &'r RawStr;
type Error = &'r str;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
if let Ok(result) = Ref::from(param.to_string()) {
if result.validate().is_ok() {
return Ok(result);

View File

@@ -1,38 +1,80 @@
use crate::database::*;
use mongodb::bson::{doc, from_document};
use rauth::auth::Session;
use rauth::entities::Session;
use rocket::http::Status;
use rocket::request::{self, FromRequest, Outcome, Request};
#[rocket::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for User {
impl<'r> FromRequest<'r> for User {
type Error = rauth::util::Error;
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let session: Session = try_outcome!(request.guard::<Session>().await);
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let user: &Option<User> = request.local_cache_async(async {
let header_bot_token = request
.headers()
.get("x-bot-token")
.next()
.map(|x| x.to_string());
if let Ok(result) = get_collection("users")
.find_one(
doc! {
"_id": &session.user_id
},
None,
)
.await
{
if let Some(doc) = result {
Outcome::Success(from_document(doc).unwrap())
if let Some(bot_token) = header_bot_token {
if let Ok(result) = get_collection("bots")
.find_one(
doc! {
"token": bot_token
},
None,
)
.await
{
if let Some(doc) = result {
let id = doc.get_str("_id").unwrap();
if let Ok(result) = get_collection("users")
.find_one(
doc! {
"_id": &id
},
None,
)
.await
{
if let Some(doc) = result {
if let Ok(user) = from_document(doc) {
return Some(user)
}
}
}
}
}
} else {
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
if let Outcome::Success(session) = request.guard::<Session>().await {
if let Ok(result) = get_collection("users")
.find_one(
doc! {
"_id": &session.user_id
},
None,
)
.await
{
if let Some(doc) = result {
if let Ok(user) = from_document(doc) {
return Some(user)
}
}
}
}
}
None
}).await;
if let Some(user) = user {
Outcome::Success(user.clone())
} else {
Outcome::Failure((
Status::InternalServerError,
rauth::util::Error::DatabaseError {
operation: "find_one",
with: "user",
},
Status::Forbidden,
rauth::util::Error::InvalidSession,
))
}
}

View File

@@ -57,6 +57,10 @@ pub async fn create_database() {
.await
.expect("Failed to create user_settings collection.");
db.create_collection("bots", None)
.await
.expect("Failed to create bots collection.");
db.create_collection(
"pubsub",
CreateCollectionOptions::builder()
@@ -67,39 +71,6 @@ pub async fn create_database() {
.await
.expect("Failed to create pubsub collection.");
db.run_command(
doc! {
"createIndexes": "accounts",
"indexes": [
{
"key": {
"email": 1
},
"name": "email",
"unique": true,
"collation": {
"locale": "en",
"strength": 2
}
},
{
"key": {
"email_normalised": 1
},
"name": "email_normalised",
"unique": true,
"collation": {
"locale": "en",
"strength": 2
}
}
]
},
None,
)
.await
.expect("Failed to create account index.");
db.run_command(
doc! {
"createIndexes": "users",
@@ -122,6 +93,86 @@ pub async fn create_database() {
.await
.expect("Failed to create username index.");
db.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"content": "text"
},
"name": "content"
},
{
"key": {
"channel": 1
},
"name": "channel"
},
{
"key": {
"channel": 1,
"_id": 1
},
"name": "channel_id_compound"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
get_db()
.run_command(
doc! {
"createIndexes": "channel_unreads",
"indexes": [
{
"key": {
"_id.channel": 1,
"_id.user": 1,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create channel_unreads index.");
get_db()
.run_command(
doc! {
"createIndexes": "server_members",
"indexes": [
{
"key": {
"_id.server": 1,
"_id.user": 1,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create server_members index.");
db.collection("migrations")
.insert_one(
doc! {

View File

@@ -10,10 +10,13 @@ pub async fn run_migrations() {
.list_database_names(None, None)
.await
.expect("Failed to fetch database names.");
if list.iter().position(|x| x == "revolt").is_none() {
init::create_database().await;
} else {
scripts::migrate_database().await;
}
// panic!("https://pbs.twimg.com/media/EDTpB5JWwAUvyxd.jpg");
rauth::entities::sync_models(&super::get_db()).await;
}

View File

@@ -1,11 +1,8 @@
use crate::database::{get_collection, get_db};
use crate::database::{permissions, get_collection, get_db, PermissionTuple};
use futures::StreamExt;
use log::info;
use mongodb::{
bson::{doc, from_document},
options::FindOptions,
};
use mongodb::{bson::{Document, doc, from_bson, from_document, to_document}, options::FindOptions};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
@@ -14,7 +11,7 @@ struct MigrationInfo {
revision: i32,
}
pub const LATEST_REVISION: i32 = 5;
pub const LATEST_REVISION: i32 = 13;
pub async fn migrate_database() {
let migrations = get_collection("migrations");
@@ -157,6 +154,321 @@ pub async fn run_migrations(revision: i32) -> i32 {
.expect("Failed to create channel_invites collection.");
}
if revision <= 5 {
info!("Running migration [revision 5 / 2021-06-26]: Add permissions.");
#[derive(Serialize)]
struct Server {
pub default_permissions: PermissionTuple,
}
let server = Server {
default_permissions: (
*permissions::server::DEFAULT_PERMISSION as i32,
*permissions::channel::DEFAULT_PERMISSION_SERVER as i32
)
};
get_collection("servers")
.update_many(
doc! { },
doc! {
"$set": to_document(&server).unwrap()
},
None
)
.await
.expect("Failed to migrate servers.");
}
if revision <= 6 {
info!("Running migration [revision 6 / 2021-07-09]: Add message text index.");
get_db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"content": "text"
},
"name": "content"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
}
if revision <= 7 {
info!("Running migration [revision 7 / 2021-08-11]: Add message text index.");
get_db()
.create_collection("bots", None)
.await
.expect("Failed to create bots collection.");
}
if revision <= 8 {
info!("Running migration [revision 8 / 2021-09-10]: Update to rAuth version 1.");
get_db()
.run_command(
doc! {
"dropIndexes": "accounts",
"index": ["email", "email_normalised"]
},
None,
)
.await
.expect("Failed to delete legacy account indexes.");
let col = get_collection("sessions");
let mut cursor = get_collection("accounts")
.find(doc! { }, None)
.await
.unwrap();
while let Some(doc) = cursor.next().await {
if let Ok(account) = doc {
let id = account.get_str("_id").unwrap();
if let Some(sessions) = account.get("sessions") {
#[derive(Deserialize)]
struct Session {
id: String,
token: String,
friendly_name: String,
subscription: Option<Document>,
}
let sessions = from_bson::<Vec<Session>>(sessions.clone()).unwrap();
for session in sessions {
info!("Converting session {} to new format.", &session.id);
let mut doc = doc! {
"_id": session.id,
"token": session.token,
"user_id": id.clone(),
"name": session.friendly_name,
};
if let Some(sub) = session.subscription {
doc.insert("subscription", sub);
}
col.insert_one(doc, None).await.ok();
}
} else {
info!("Account doesn't have any sessions!");
}
}
}
get_collection("accounts")
.update_many(
doc! { },
doc! {
"$unset": {
"sessions": 1,
},
"$set": {
"mfa": {
"recovery_codes": []
}
}
},
None
)
.await
.unwrap();
}
if revision <= 9 {
info!("Running migration [revision 9 / 2021-09-14]: Switch from last_message to last_message_id.");
let mut cursor = get_collection("channels")
.find(doc! { }, None)
.await
.unwrap();
while let Some(doc) = cursor.next().await {
if let Ok(channel) = doc {
let channel_id = channel.get_str("_id").unwrap();
if let Some(last_message) = channel.get("last_message") {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Obj {
#[serde(rename = "_id")]
id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum LastMessage {
Obj(Obj),
Id(String)
}
let lm = from_bson::<LastMessage>(last_message.clone()).unwrap();
let id = match lm {
LastMessage::Obj(Obj { id }) => id,
LastMessage::Id(id) => id
};
info!("Converting session {} to new format.", &channel_id);
get_collection("channels")
.update_one(
doc! {
"_id": channel_id
},
doc! {
"$set": {
"last_message_id": id
},
"$unset": {
"last_message": 1,
}
},
None
)
.await
.unwrap();
} else {
info!("{} has no last_message.", &channel_id);
}
}
}
}
if revision <= 10 {
info!("Running migration [revision 10 / 2021-11-01]: Remove nonce values on channels and servers.");
get_collection("servers")
.update_many(
doc! {},
doc! {
"$unset": {
"nonce": 1,
}
},
None
)
.await
.unwrap();
get_collection("channels")
.update_many(
doc! {},
doc! {
"$unset": {
"nonce": 1,
}
},
None
)
.await
.unwrap();
}
if revision <= 11 {
info!("Running migration [revision 11 / 2021-11-14]: Add indexes to database.");
get_db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"channel": 1
},
"name": "channel"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
get_db()
.run_command(
doc! {
"createIndexes": "channel_unreads",
"indexes": [
{
"key": {
"_id.channel": 1,
"_id.user": 1,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create channel_unreads index.");
get_db()
.run_command(
doc! {
"createIndexes": "server_members",
"indexes": [
{
"key": {
"_id.server": 1,
"_id.user": 1,
},
"name": "compound_id"
},
{
"key": {
"_id.user": 1,
},
"name": "user_id"
}
]
},
None,
)
.await
.expect("Failed to create server_members index.");
}
if revision <= 12 {
info!("Running migration [revision 12 / 2021-11-21]: Add indexes to database.");
get_db()
.run_command(
doc! {
"createIndexes": "messages",
"indexes": [
{
"key": {
"channel": 1,
"_id": 1
},
"name": "channel_id_compound"
}
]
},
None,
)
.await
.expect("Failed to create message index.");
}
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
// Reminder to update LATEST_REVISION when adding new migrations.
LATEST_REVISION
}

View File

@@ -1,5 +1,5 @@
use crate::database::*;
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use super::PermissionCalculator;
@@ -15,6 +15,29 @@ pub enum ChannelPermission {
ManageChannel = 0b00000000000000000000000000001000, // 8
VoiceCall = 0b00000000000000000000000000010000, // 16
InviteOthers = 0b00000000000000000000000000100000, // 32
EmbedLinks = 0b00000000000000000000000001000000, // 64
UploadFiles = 0b00000000000000000000000010000000, // 128
Masquerade = 0b00000000000000000000000100000000, // 256
}
lazy_static! {
pub static ref DEFAULT_PERMISSION_DM: u32 =
ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::ManageChannel
+ ChannelPermission::VoiceCall
+ ChannelPermission::InviteOthers
+ ChannelPermission::EmbedLinks
+ ChannelPermission::UploadFiles
+ ChannelPermission::Masquerade;
pub static ref DEFAULT_PERMISSION_SERVER: u32 =
ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::VoiceCall
+ ChannelPermission::InviteOthers
+ ChannelPermission::EmbedLinks
+ ChannelPermission::UploadFiles;
}
impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 });
@@ -29,6 +52,9 @@ bitfield! {
pub get_manage_channel, _: 28;
pub get_voice_call, _: 27;
pub get_invite_others, _: 26;
pub get_embed_links, _: 25;
pub get_upload_files, _: 24;
pub get_masquerade, _: 23;
}
impl<'a> PermissionCalculator<'a> {
@@ -58,9 +84,7 @@ impl<'a> PermissionCalculator<'a> {
let perms = self.for_user(recipient).await?;
if perms.get_send_message() {
return Ok(ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::VoiceCall);
return Ok(*DEFAULT_PERMISSION_DM);
}
return Ok(ChannelPermission::View as u32);
@@ -69,30 +93,61 @@ impl<'a> PermissionCalculator<'a> {
Ok(0)
}
Channel::Group { recipients, .. } => {
Channel::Group { recipients, permissions, owner, .. } => {
if &self.perspective.id == owner {
return Ok(*DEFAULT_PERMISSION_DM)
}
if recipients
.iter()
.find(|x| *x == &self.perspective.id)
.is_some()
{
Ok(ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::ManageChannel
+ ChannelPermission::VoiceCall
+ ChannelPermission::InviteOthers)
if let Some(permissions) = permissions {
Ok(permissions.clone() as u32)
} else {
Ok(*DEFAULT_PERMISSION_DM)
}
} else {
Ok(0)
}
}
Channel::TextChannel { server, .. } => {
Channel::TextChannel { server, default_permissions, role_permissions, .. }
| Channel::VoiceChannel { server, default_permissions, role_permissions, .. } => {
let server = Ref::from_unchecked(server.clone()).fetch_server().await?;
if self.perspective.id == server.owner {
Ok(u32::MAX)
} else {
Ok(ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::InviteOthers)
match Ref::from_unchecked(self.perspective.id.clone()).fetch_member(&server.id).await {
Ok(member) => {
let mut perm = if let Some(permission) = default_permissions {
*permission as u32
} else {
server.default_permissions.1 as u32
};
if let Some(roles) = member.roles {
for role in roles {
if let Some(permission) = role_permissions.get(&role) {
perm |= *permission as u32;
}
if let Some(server_role) = server.roles.get(&role) {
perm |= server_role.permissions.1 as u32;
}
}
}
Ok(perm)
}
Err(error) => {
match &error {
Error::NotFound => Ok(0),
_ => Err(error)
}
}
}
}
}
}

View File

@@ -13,6 +13,7 @@ pub struct PermissionCalculator<'a> {
relationship: Option<&'a RelationshipStatus>,
channel: Option<&'a Channel>,
server: Option<&'a Server>,
// member: Option<&'a Member>,
has_mutual_connection: bool,
}
@@ -26,6 +27,7 @@ impl<'a> PermissionCalculator<'a> {
relationship: None,
channel: None,
server: None,
// member: None,
has_mutual_connection: false,
}
@@ -59,6 +61,13 @@ impl<'a> PermissionCalculator<'a> {
}
}
/* pub fn with_member(self, member: &'a Member) -> PermissionCalculator {
PermissionCalculator {
member: Some(&member),
..self
}
} */
pub fn with_mutual_connection(self) -> PermissionCalculator<'a> {
PermissionCalculator {
has_mutual_connection: true,

View File

@@ -1,6 +1,7 @@
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use super::PermissionCalculator;
use super::Ref;
use num_enum::TryFromPrimitive;
use std::ops;
@@ -17,11 +18,18 @@ pub enum ServerPermission {
// 6 bits of space
ChangeNickname = 0b00000000000000000001000000000000, // 4096
ManageNicknames = 0b00000000000000000010000000000000, // 8192
ChangeAvatar = 0b00000000000000000100000000000000, // 16392
RemoveAvatars = 0b00000000000000001000000000000000, // 32784
ChangeAvatar = 0b00000000000000000100000000000000, // 16382
RemoveAvatars = 0b00000000000000001000000000000000, // 32768
// 16 bits of space
}
lazy_static! {
pub static ref DEFAULT_PERMISSION: u32 =
ServerPermission::View
+ ServerPermission::ChangeNickname
+ ServerPermission::ChangeAvatar;
}
impl_op_ex!(+ |a: &ServerPermission, b: &ServerPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &ServerPermission| -> u32 { *a | *b as u32 });
@@ -29,7 +37,7 @@ bitfield! {
pub struct ServerPermissions(MSB0 [u32]);
u32;
pub get_view, _: 31;
pub get_manage_members, _: 30;
pub get_manage_roles, _: 30;
pub get_manage_channels, _: 29;
pub get_manage_server, _: 28;
pub get_kick_members, _: 27;
@@ -52,9 +60,26 @@ impl<'a> PermissionCalculator<'a> {
if self.perspective.id == server.owner {
Ok(u32::MAX)
} else {
Ok(ServerPermission::View
+ ServerPermission::ChangeNickname
+ ServerPermission::ChangeAvatar)
match Ref::from_unchecked(self.perspective.id.clone()).fetch_member(&server.id).await {
Ok(member) => {
let mut perm = server.default_permissions.0 as u32;
if let Some(roles) = member.roles {
for role in roles {
if let Some(server_role) = server.roles.get(&role) {
perm |= server_role.permissions.0 as u32;
}
}
}
Ok(perm)
}
Err(error) => {
match &error {
Error::NotFound => Ok(0),
_ => Err(error)
}
}
}
}
}

View File

@@ -69,7 +69,31 @@ impl<'a> PermissionCalculator<'a> {
_ => {}
}
let check_server_overlap = async || {
let server_ids = User::fetch_server_ids(&self.perspective.id).await?;
Ok(
get_collection("server_members")
.find_one(
doc! {
"_id.user": &target,
"_id.server": {
"$in": server_ids
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "server_members",
})?
.is_some()
)
};
if self.has_mutual_connection
|| check_server_overlap().await?
|| get_collection("channels")
.find_one(
doc! {
@@ -84,7 +108,7 @@ impl<'a> PermissionCalculator<'a> {
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
operation: "find_one",
with: "channels",
})?
.is_some()

View File

@@ -1,10 +1,11 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(async_closure)]
#![feature(const_option)]
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
extern crate serde_json;
#[macro_use]
extern crate lazy_static;
#[macro_use]
@@ -16,24 +17,25 @@ extern crate ctrlc;
pub mod database;
pub mod notifications;
pub mod routes;
pub mod redis;
pub mod util;
pub mod version;
pub mod task_queue;
use async_std::task;
use chrono::Duration;
use futures::join;
use log::info;
use rauth::options::{EmailVerification, Options, SMTP};
use rauth::{
auth::Auth,
options::{Template, Templates},
config::{Captcha, Config, EmailVerification, SMTPSettings, Template, Templates},
logic::Auth,
};
use std::str::FromStr;
use rocket_cors::AllowedOrigins;
use rocket_prometheus::PrometheusMetrics;
use util::variables::{
APP_URL, HCAPTCHA_KEY, INVITE_ONLY, PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD,
SMTP_USERNAME, USE_EMAIL, USE_HCAPTCHA, USE_PROMETHEUS,
APP_URL, HCAPTCHA_KEY, INVITE_ONLY, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_USERNAME,
USE_EMAIL, USE_HCAPTCHA,
};
use crate::util::ratelimit::RatelimitState;
#[async_std::main]
async fn main() {
@@ -47,7 +49,9 @@ async fn main() {
util::variables::preflight_checks();
database::connect().await;
redis::connect().await;
notifications::hive::init_hive().await;
task_queue::start_queues().await;
ctrlc::set_handler(move || {
// Force ungraceful exit to avoid hang.
@@ -56,7 +60,7 @@ async fn main() {
.expect("Error setting Ctrl-C handler");
let web_task = task::spawn(launch_web());
let hive_task = task::spawn(notifications::hive::listen());
let hive_task = task::spawn_local(notifications::hive::listen());
join!(
web_task,
@@ -68,72 +72,71 @@ async fn main() {
async fn launch_web() {
let cors = rocket_cors::CorsOptions {
allowed_origins: AllowedOrigins::All,
allowed_methods: [
"Get", "Put", "Post", "Delete", "Options", "Head", "Trace", "Connect", "Patch",
]
.iter()
.map(|s| FromStr::from_str(s).unwrap())
.collect(),
..Default::default()
}
.to_cors()
.expect("Failed to create CORS.");
let mut options = Options::new()
.base_url(format!("{}/auth", *PUBLIC_URL))
.email_verification(if *USE_EMAIL {
let mut config = Config {
email_verification: if *USE_EMAIL {
EmailVerification::Enabled {
success_redirect_uri: format!("{}/login", *APP_URL),
welcome_redirect_uri: format!("{}/welcome", *APP_URL),
password_reset_url: Some(format!("{}/login/reset", *APP_URL)),
verification_expiry: Duration::days(1),
password_reset_expiry: Duration::hours(1),
templates: Templates {
verify_email: Template {
title: "Verify your REVOLT account.",
text: "Verify your email here: {{url}}",
html: include_str!("../assets/templates/verify.html"),
},
reset_password: Template {
title: "Reset your REVOLT password.",
text: "Reset your password here: {{url}}",
html: include_str!("../assets/templates/reset.html"),
},
welcome: None,
},
smtp: SMTP {
smtp: SMTPSettings {
from: (*SMTP_FROM).to_string(),
host: (*SMTP_HOST).to_string(),
username: (*SMTP_USERNAME).to_string(),
password: (*SMTP_PASSWORD).to_string(),
reply_to: Some("support@revolt.chat".into()),
port: None,
use_tls: None,
},
expiry: Default::default(),
templates: Templates {
verify: Template {
title: "Verify your Revolt account.".into(),
text: include_str!("../assets/templates/verify.txt").into(),
url: format!("{}/login/verify/", *APP_URL),
html: None,
},
reset: Template {
title: "Reset your Revolt password.".into(),
text: include_str!("../assets/templates/reset.txt").into(),
url: format!("{}/login/reset/", *APP_URL),
html: None,
},
welcome: None,
},
}
} else {
EmailVerification::Disabled
});
},
..Default::default()
};
if *INVITE_ONLY {
options = options.invite_only_collection(database::get_collection("invites"))
config.invite_only = true;
}
if *USE_HCAPTCHA {
options = options.hcaptcha_secret(HCAPTCHA_KEY.clone());
}
let auth = Auth::new(database::get_collection("accounts"), options);
let mut rocket = rocket::ignite();
if *USE_PROMETHEUS {
info!("Enabled Prometheus metrics!");
let prometheus = PrometheusMetrics::new();
rocket = rocket
.attach(prometheus.clone())
.mount("/metrics", prometheus);
config.captcha = Captcha::HCaptcha {
secret: HCAPTCHA_KEY.clone(),
};
}
let auth = Auth::new(database::get_db(), config);
let rocket = rocket::build();
routes::mount(rocket)
.mount("/", rocket_cors::catch_all_options_routes())
.mount("/auth", rauth::routes::routes())
.mount("/auth/account", rauth::web::account::routes())
.mount("/auth/session", rauth::web::session::routes())
.manage(auth)
.manage(cors.clone())
.manage(RatelimitState::new())
.attach(cors)
.launch()
.await

View File

@@ -1,7 +1,6 @@
use hive_pubsub::PubSub;
use mongodb::bson::doc;
use rauth::auth::Session;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
use super::hive::{get_hive, subscribe_if_exists};
@@ -15,14 +14,28 @@ pub enum WebSocketError {
InvalidSession,
OnboardingNotFinished,
AlreadyAuthenticated,
MalformedData { msg: String },
}
#[derive(Deserialize, Debug)]
pub struct Auth {
pub token: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Ping {
Binary(Vec<u8>),
Number(usize)
}
#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
pub enum ServerboundNotification {
Authenticate(Session),
Authenticate(Auth),
BeginTyping { channel: String },
EndTyping { channel: String },
Ping { data: Ping, responded: Option<()> },
}
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -46,12 +59,22 @@ pub enum RemoveServerField {
Description,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RemoveRoleField {
Colour,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RemoveMemberField {
Nickname,
Avatar,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RemoveBotField {
InteractionsURL,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum ClientboundNotification {
@@ -61,13 +84,15 @@ pub enum ClientboundNotification {
users: Vec<User>,
servers: Vec<Server>,
channels: Vec<Channel>,
members: Vec<Member>,
},
Pong { data: Ping },
Message(Message),
MessageUpdate {
id: String,
channel: String,
data: JsonValue,
data: Value,
},
MessageDelete {
id: String,
@@ -77,7 +102,7 @@ pub enum ClientboundNotification {
ChannelCreate(Channel),
ChannelUpdate {
id: String,
data: JsonValue,
data: Value,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveChannelField>,
},
@@ -108,7 +133,7 @@ pub enum ClientboundNotification {
ServerUpdate {
id: String,
data: JsonValue,
data: Value,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveServerField>,
},
@@ -117,7 +142,7 @@ pub enum ClientboundNotification {
},
ServerMemberUpdate {
id: MemberCompositeKey,
data: JsonValue,
data: Value,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveMemberField>,
},
@@ -129,10 +154,21 @@ pub enum ClientboundNotification {
id: String,
user: String,
},
ServerRoleUpdate {
id: String,
role_id: String,
data: Value,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveRoleField>,
},
ServerRoleDelete {
id: String,
role_id: String,
},
UserUpdate {
id: String,
data: JsonValue,
data: Value,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveUserField>,
},
@@ -141,13 +177,9 @@ pub enum ClientboundNotification {
user: User,
status: RelationshipStatus,
},
UserPresence {
id: String,
online: bool,
},
UserSettingsUpdate {
id: String,
update: JsonValue,
update: Value,
},
}
@@ -155,11 +187,23 @@ impl ClientboundNotification {
pub fn publish(self, topic: String) {
async_std::task::spawn(async move {
prehandle_hook(&self).await.ok(); // ! FIXME: this should be moved to pubsub
hive_pubsub::backend::mongo::publish(get_hive(), &topic, self)
hive_pubsub::backend::redis::publish(get_hive(), topic, self)
.await
.ok();
});
}
pub fn publish_as_user(self, user: String) {
self.clone().publish(user.clone());
async_std::task::spawn(async move {
if let Ok(server_ids) = User::fetch_server_ids(&user).await {
for server in server_ids {
self.clone().publish(server.clone());
}
}
});
}
}
pub async fn prehandle_hook(notification: &ClientboundNotification) -> Result<()> {
@@ -178,7 +222,7 @@ pub async fn prehandle_hook(notification: &ClientboundNotification) -> Result<()
subscribe_if_exists(recipient.clone(), channel_id.to_string()).ok();
}
}
Channel::TextChannel { server, .. } => {
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
// ! FIXME: write a better algorithm?
let members = Server::fetch_member_ids(server).await?;
for member in members {

View File

@@ -1,18 +1,23 @@
use std::sync::{Arc, Mutex};
use super::{events::ClientboundNotification, websocket};
use crate::database::*;
use crate::redis::get_pool;
use crate::util::variables::REDIS_URI;
use futures::FutureExt;
use hive_pubsub::backend::mongo::MongodbPubSub;
use hive_pubsub::backend::redis::RedisPubSub;
use hive_pubsub::PubSub;
use log::{debug, error};
use once_cell::sync::OnceCell;
use serde_json::to_string;
type Hive = MongodbPubSub<String, String, ClientboundNotification>;
static HIVE: OnceCell<Hive> = OnceCell::new();
type Hive<'a> = RedisPubSub<'a, String, String, ClientboundNotification>;
static HIVE: OnceCell<Hive<'static>> = OnceCell::new();
pub async fn init_hive() {
let hive = MongodbPubSub::new(
let pubsub_con = redis::Client::open(REDIS_URI.to_string()).unwrap().get_async_connection().await.unwrap().into_pubsub();
let hive = RedisPubSub::new(
|ids, notification: ClientboundNotification| {
let notif = notification.clone();
async_std::task::spawn(async move {
@@ -26,7 +31,8 @@ pub async fn init_hive() {
error!("Failed to serialise notification.");
}
},
get_collection("pubsub"),
get_pool(),
Arc::new(Mutex::new(pubsub_con))
);
if HIVE.set(hive).is_err() {
@@ -37,6 +43,7 @@ pub async fn init_hive() {
pub async fn listen() {
HIVE.get()
.unwrap()
.clone()
.listen()
.fuse()
.await
@@ -61,6 +68,6 @@ pub fn subscribe_if_exists(user: String, topic: String) -> Result<(), String> {
Ok(())
}
pub fn get_hive() -> &'static Hive {
pub fn get_hive() -> &'static Hive<'static> {
HIVE.get().unwrap()
}

View File

@@ -19,7 +19,11 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
);
}
let server_ids = user.fetch_server_ids().await?;
let members = User::fetch_memberships(&user.id).await?;
let server_ids: Vec<String> = members.iter()
.map(|x| x.id.server.clone())
.collect();
let mut cursor = get_collection("servers")
.find(
doc! {
@@ -100,7 +104,7 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
user_ids.remove(&user.id);
let mut users = if user_ids.len() > 0 {
user.fetch_multiple_users(user_ids.into_iter().collect::<Vec<String>>())
user.fetch_multiple_users(&user_ids.into_iter().collect::<Vec<String>>())
.await?
} else {
vec![]
@@ -108,11 +112,13 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
user.relationship = Some(RelationshipStatus::User);
user.online = Some(true);
users.push(user);
users.push(user.apply_badges());
Ok(ClientboundNotification::Ready {
users,
servers,
channels,
members
})
}

View File

@@ -17,8 +17,7 @@ pub async fn generate_subscriptions(user: &User) -> Result<(), String> {
}
}
let server_ids = user
.fetch_server_ids()
let server_ids = User::fetch_server_ids(&user.id)
.await
.map_err(|_| "Failed to fetch memberships.".to_string())?;

View File

@@ -1,24 +1,28 @@
use crate::database::*;
use crate::notifications::events::Ping;
use crate::util::variables::WS_HOST;
use super::subscriptions;
use async_std::net::{TcpListener, TcpStream};
use async_std::task;
use async_tungstenite::tungstenite::Message;
use futures::channel::mpsc::{unbounded, UnboundedSender};
use async_tungstenite::tungstenite::{handshake::server, Message};
use futures::channel::{
mpsc::{unbounded, UnboundedSender},
oneshot,
};
use futures::stream::TryStreamExt;
use futures::{pin_mut, prelude::*};
use hive_pubsub::PubSub;
use log::{debug, info};
use many_to_many::ManyToMany;
use rauth::{
auth::{Auth, Session},
options::Options,
};
use mongodb::bson::doc;
use rauth::entities::{Model, Session};
use rmp_serde;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex, RwLock};
use url::Url;
use super::{
events::{ClientboundNotification, ServerboundNotification, WebSocketError},
@@ -26,7 +30,7 @@ use super::{
};
type Tx = UnboundedSender<Message>;
type PeerMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>;
type PeerMap = Arc<Mutex<HashMap<SocketAddr, (Tx, MSGFormat)>>>;
lazy_static! {
static ref CONNECTIONS: PeerMap = Arc::new(Mutex::new(HashMap::new()));
@@ -44,167 +48,271 @@ pub async fn launch_server() {
}
}
#[derive(Debug, Clone)]
enum MSGFormat {
JSON,
MSGPACK,
}
struct HeaderCallback {
sender: oneshot::Sender<MSGFormat>,
}
impl server::Callback for HeaderCallback {
fn on_request(
self,
request: &server::Request,
response: server::Response,
) -> Result<server::Response, server::ErrorResponse> {
// we dont get some of the data sometimes so im generating a fake url with the only data we actually need
let url = format!(
"ws://example.com?{}",
request.uri().query().unwrap_or("?format=json")
);
let mut query: HashMap<_, _> = url
.parse::<Url>()
.unwrap()
.query_pairs()
.into_owned()
.collect(); // should be safe to use unwrap here as we just made the url ourself
let format_query: Option<String> = query.remove("format");
let format = match format_query.as_deref().unwrap_or("json") {
"msgpack" => MSGFormat::MSGPACK,
"json" => MSGFormat::JSON,
_ => MSGFormat::JSON, // Fallback to JSON.
};
if self.sender.send(format).is_ok() {
Ok(response)
} else {
Err(server::ErrorResponse::new(None))
}
}
}
async fn accept(stream: TcpStream) {
let addr = stream
.peer_addr()
.expect("Connected streams should have a peer address.");
let ws_stream = async_tungstenite::accept_async(stream)
.await
.expect("Error during websocket handshake.");
let (sender, receiver) = oneshot::channel::<MSGFormat>();
let ws_stream =
async_tungstenite::accept_hdr_async_with_config(stream, HeaderCallback { sender }, None)
.await
.expect("Error during websocket handshake.");
let msg_format = receiver.await.unwrap(); // TODO: not use unwrap
info!("User established WebSocket connection from {}.", &addr);
let (write, read) = ws_stream.split();
let (tx, rx) = unbounded();
CONNECTIONS.lock().unwrap().insert(addr, tx.clone());
CONNECTIONS.lock().unwrap().insert(addr, (tx.clone(), msg_format.clone()));
let send = |notification: ClientboundNotification| {
if let Ok(response) = serde_json::to_string(&notification) {
if let Err(_) = tx.unbounded_send(Message::Text(response)) {
debug!("Failed unbounded_send to websocket stream.");
let res = match msg_format {
MSGFormat::JSON => match serde_json::to_string(&notification) {
Ok(s) => Message::Text(s),
Err(_) => return
}
MSGFormat::MSGPACK => match rmp_serde::to_vec_named(&notification) {
Ok(v) => Message::Binary(v),
Err(_) => return,
},
};
if let Err(_) = tx.unbounded_send(res) {
debug!("Failed unbounded_send to websocket stream.");
}
};
let session: Arc<Mutex<Option<Session>>> = Arc::new(Mutex::new(None));
let mutex_generator = || session.clone();
let user_id: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
let mutex_generator = || user_id.clone();
let fwd = rx.map(Ok).forward(write);
let incoming = read.try_for_each(async move |msg| {
let mutex = mutex_generator();
if let Message::Text(text) = msg {
if let Ok(notification) = serde_json::from_str::<ServerboundNotification>(&text) {
match notification {
ServerboundNotification::Authenticate(new_session) => {
{
if mutex.lock().unwrap().is_some() {
send(ClientboundNotification::Error(
WebSocketError::AlreadyAuthenticated,
));
return Ok(());
let maybe_decoded = match msg {
Message::Text(text) => {
serde_json::from_str::<ServerboundNotification>(&text).map_err(|e| e.to_string())
}
Message::Binary(vec) => {
rmp_serde::decode::from_read::<&[u8], ServerboundNotification>(vec.as_slice())
.map_err(|e| e.to_string())
}
Message::Ping(vec) => Ok(ServerboundNotification::Ping { data: Ping::Binary(vec), responded: Some(()) }),
_ => return Ok(()),
};
let notification = match maybe_decoded {
Err(why) => {
send(ClientboundNotification::Error(
WebSocketError::MalformedData {
msg: why.to_string(),
},
));
return Ok(());
}
Ok(n) => n,
};
match notification {
ServerboundNotification::Authenticate(auth) => {
{
if mutex.lock().unwrap().is_some() {
send(ClientboundNotification::Error(
WebSocketError::AlreadyAuthenticated,
));
return Ok(());
}
}
let id = if let Ok(Some(session)) =
Session::find_one(&get_db(), doc! { "token": &auth.token }, None).await
{
Some(session.user_id)
} else if let Ok(Some(bot)) = get_collection("bots")
.find_one(doc! { "token": auth.token }, None)
.await
{
Some(bot.get_str("_id").unwrap().to_string())
} else {
None
};
if let Some(id) = id {
if let Ok(user) = (Ref { id: id.clone() }).fetch_user().await {
let is_invisible = if let Some(status) = &user.status {
if let Some(presence) = &status.presence {
presence == &Presence::Invisible
} else {
false
}
}
} else {
false
};
let was_online = is_online(&id);
if let Ok(validated_session) =
Auth::new(get_collection("accounts"), Options::new())
.verify_session(new_session)
.await
{
let id = validated_session.user_id.clone();
if let Ok(user) = (Ref { id: id.clone() }).fetch_user().await {
let was_online = is_online(&id);
{
match USERS.write() {
Ok(mut map) => {
map.insert(id.clone(), addr);
}
Err(_) => {
send(ClientboundNotification::Error(
WebSocketError::InternalError {
at: "Writing users map.".to_string(),
},
));
return Ok(());
}
}
match USERS.write() {
Ok(mut map) => {
map.insert(id.clone(), addr);
}
*mutex.lock().unwrap() = Some(validated_session);
if let Err(_) = subscriptions::generate_subscriptions(&user).await {
Err(_) => {
send(ClientboundNotification::Error(
WebSocketError::InternalError {
at: "Generating subscriptions.".to_string(),
at: "Writing users map.".to_string(),
},
));
return Ok(());
}
}
}
send(ClientboundNotification::Authenticated);
*mutex.lock().unwrap() = Some(id.clone());
match super::payload::generate_ready(user).await {
Ok(payload) => {
send(payload);
if let Err(_) = subscriptions::generate_subscriptions(&user).await {
send(ClientboundNotification::Error(
WebSocketError::InternalError {
at: "Generating subscriptions.".to_string(),
},
));
if !was_online {
ClientboundNotification::UserPresence {
id: id.clone(),
online: true,
}
.publish(id);
}
}
Err(_) => {
send(ClientboundNotification::Error(
WebSocketError::InternalError {
at: "Generating payload.".to_string(),
},
));
return Ok(());
return Ok(());
}
send(ClientboundNotification::Authenticated);
match super::payload::generate_ready(user).await {
Ok(payload) => {
send(payload);
if !was_online && !is_invisible {
ClientboundNotification::UserUpdate {
id: id.clone(),
data: json!({
"online": true
}),
clear: None,
}
.publish_as_user(id);
}
} else {
}
Err(_) => {
send(ClientboundNotification::Error(
WebSocketError::OnboardingNotFinished,
WebSocketError::InternalError {
at: "Generating payload.".to_string(),
},
));
return Ok(());
}
} else {
send(ClientboundNotification::Error(
WebSocketError::InvalidSession,
));
}
} else {
send(ClientboundNotification::Error(
WebSocketError::OnboardingNotFinished,
));
}
// ! TEMP: verify user part of channel
// ! Could just run permission check here.
ServerboundNotification::BeginTyping { channel } => {
if mutex.lock().unwrap().is_some() {
let user = {
let mutex = mutex.lock().unwrap();
let session = mutex.as_ref().unwrap();
session.user_id.clone()
};
} else {
send(ClientboundNotification::Error(
WebSocketError::InvalidSession,
));
}
}
// ! TEMP: verify user part of channel
// ! Could just run permission check here.
ServerboundNotification::BeginTyping { channel } => {
if mutex.lock().unwrap().is_some() {
let user = {
let mutex = mutex.lock().unwrap();
mutex.as_ref().unwrap().clone()
};
ClientboundNotification::ChannelStartTyping {
id: channel.clone(),
user,
}
.publish(channel);
} else {
send(ClientboundNotification::Error(
WebSocketError::AlreadyAuthenticated,
));
return Ok(());
}
ClientboundNotification::ChannelStartTyping {
id: channel.clone(),
user,
}
ServerboundNotification::EndTyping { channel } => {
if mutex.lock().unwrap().is_some() {
let user = {
let mutex = mutex.lock().unwrap();
let session = mutex.as_ref().unwrap();
session.user_id.clone()
};
.publish(channel);
} else {
send(ClientboundNotification::Error(
WebSocketError::AlreadyAuthenticated,
));
ClientboundNotification::ChannelStopTyping {
id: channel.clone(),
user,
}
.publish(channel);
} else {
send(ClientboundNotification::Error(
WebSocketError::AlreadyAuthenticated,
));
return Ok(());
}
}
ServerboundNotification::EndTyping { channel } => {
if mutex.lock().unwrap().is_some() {
let user = {
let mutex = mutex.lock().unwrap();
mutex.as_ref().unwrap().clone()
};
return Ok(());
}
ClientboundNotification::ChannelStopTyping {
id: channel.clone(),
user,
}
.publish(channel);
} else {
send(ClientboundNotification::Error(
WebSocketError::AlreadyAuthenticated,
));
return Ok(());
}
}
ServerboundNotification::Ping { data, responded } => {
debug!("Ping received from connection {}. Payload: {:?}", &addr, data);
if responded.is_none() {
send(ClientboundNotification::Pong { data });
}
}
}
Ok(())
});
@@ -216,23 +324,26 @@ async fn accept(stream: TcpStream) {
let mut offline = None;
{
let session = session.lock().unwrap();
if let Some(session) = session.as_ref() {
let user_id = user_id.lock().unwrap();
if let Some(user_id) = user_id.as_ref() {
let mut users = USERS.write().unwrap();
users.remove(&session.user_id, &addr);
if users.get_left(&session.user_id).is_none() {
get_hive().drop_client(&session.user_id).unwrap();
offline = Some(session.user_id.clone());
users.remove(&user_id, &addr);
if users.get_left(&user_id).is_none() {
get_hive().drop_client(&user_id).unwrap();
offline = Some(user_id.clone());
}
}
}
if let Some(id) = offline {
ClientboundNotification::UserPresence {
ClientboundNotification::UserUpdate {
id: id.clone(),
online: false,
data: json!({
"online": false
}),
clear: None,
}
.publish(id);
.publish_as_user(id);
}
}
@@ -259,12 +370,18 @@ pub fn publish(ids: Vec<String>, notification: ClientboundNotification) {
}
}
let msg = Message::Text(serde_json::to_string(&notification).unwrap());
let json_msg = Message::Text(serde_json::to_string(&notification).unwrap());
let msgpack_msg = Message::Binary(rmp_serde::to_vec_named(&notification).unwrap());
let connections = CONNECTIONS.lock().unwrap();
for target in targets {
if let Some(conn) = connections.get(&target) {
if let Err(_) = conn.unbounded_send(msg.clone()) {
if let Some((conn, msg_format)) = connections.get(&target) {
let msg = match msg_format {
MSGFormat::JSON => json_msg.clone(),
MSGFormat::MSGPACK => msgpack_msg.clone()
};
if let Err(_) = conn.unbounded_send(msg) {
debug!("Failed unbounded_send.");
}
}

18
src/redis/mod.rs Normal file
View File

@@ -0,0 +1,18 @@
use crate::util::variables::REDIS_URI;
use mobc::Pool;
use mobc_redis::RedisConnectionManager;
use once_cell::sync::OnceCell;
static REDISPOOL: OnceCell<Pool<RedisConnectionManager>> = OnceCell::new();
pub async fn connect() {
let client = mobc_redis::redis::Client::open(REDIS_URI.to_string()).unwrap();
let manager = mobc_redis::RedisConnectionManager::new(client);
let pool = mobc::Pool::builder().max_open(100).build(manager);
REDISPOOL.set(pool).ok().unwrap();
}
pub fn get_pool() -> &'static Pool<RedisConnectionManager> {
REDISPOOL.get().unwrap()
}

89
src/routes/bots/create.rs Normal file
View File

@@ -0,0 +1,89 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_BOT_COUNT;
use crate::util::regex::RE_USERNAME;
use mongodb::bson::{doc, to_document};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use nanoid::nanoid;
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
name: String,
}
#[post("/create", data = "<info>")]
pub async fn create_bot(user: User, info: Json<Data>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;
if get_collection("bots")
.count_documents(
doc! {
"owner": &user.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "count_documents",
with: "bots",
})? as usize >= *MAX_BOT_COUNT {
return Err(Error::ReachedMaximumBots)
}
let id = Ulid::new().to_string();
let token = nanoid!(64);
let bot = Bot {
id: id.clone(),
owner: user.id.clone(),
token,
public: false,
analytics: false,
discoverable: false,
interactions_url: None
};
if User::is_username_taken(&info.name).await? {
return Err(Error::UsernameTaken);
}
get_collection("users")
.insert_one(
doc! {
"_id": &id,
"username": &info.name,
"bot": {
"owner": &user.id
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "user",
})?;
get_collection("bots")
.insert_one(
to_document(&bot).map_err(|_| Error::DatabaseError { with: "bot", operation: "to_document" })?,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "user",
})?;
Ok(json!(bot))
}

67
src/routes/bots/delete.rs Normal file
View File

@@ -0,0 +1,67 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, EmptyResponse, Result};
use mongodb::bson::doc;
#[delete("/<target>")]
pub async fn delete_bot(user: User, target: Ref) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if bot.owner != user.id {
return Err(Error::MissingPermission);
}
let username = format!("Deleted User {}", &bot.id);
get_collection("users")
.update_one(
doc! {
"_id": &bot.id
},
doc! {
"$set": {
"username": &username,
"flags": 2
},
"$unset": {
"avatar": 1,
"status": 1,
"profile": 1
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "user",
operation: "update_one"
})?;
ClientboundNotification::UserUpdate {
id: target.id.clone(),
data: json!({
"username": username,
"flags": 2
}),
clear: None,
}
.publish_as_user(target.id.clone());
get_collection("bots")
.delete_one(
doc! {
"_id": &bot.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "bot",
operation: "delete_one"
})?;
Ok(EmptyResponse {})
}

120
src/routes/bots/edit.rs Normal file
View File

@@ -0,0 +1,120 @@
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveBotField};
use crate::util::regex::RE_USERNAME;
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
public: Option<bool>,
analytics: Option<bool>,
interactions_url: Option<String>,
remove: Option<RemoveBotField>,
}
#[patch("/<target>", data = "<data>")]
pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none()
&& data.public.is_none()
&& data.analytics.is_none()
&& data.interactions_url.is_none()
&& data.remove.is_none()
{
return Ok(EmptyResponse {});
}
let bot = target.fetch_bot().await?;
if bot.owner != user.id {
return Err(Error::MissingPermission);
}
if let Some(name) = &data.name {
if User::is_username_taken(&name).await? {
return Err(Error::UsernameTaken);
}
get_collection("users")
.update_one(
doc! { "_id": &target.id },
doc! {
"$set": {
"username": name
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user",
})?;
ClientboundNotification::UserUpdate {
id: target.id.clone(),
data: json!({
"username": name
}),
clear: None,
}
.publish_as_user(target.id.clone());
}
let mut set = doc! {};
let mut unset = doc! {};
if let Some(remove) = &data.remove {
match remove {
RemoveBotField::InteractionsURL => {
unset.insert("interactions_url", 1);
}
}
}
if let Some(public) = &data.public {
set.insert("public", public);
}
if let Some(analytics) = &data.analytics {
set.insert("analytics", analytics);
}
if let Some(interactions_url) = &data.interactions_url {
set.insert("interactions_url", interactions_url);
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
}
if unset.len() > 0 {
operations.insert("$unset", unset);
}
if operations.len() > 0 {
get_collection("bots")
.update_one(doc! { "_id": &target.id }, operations, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "bot",
})?;
}
Ok(EmptyResponse {})
}

26
src/routes/bots/fetch.rs Normal file
View File

@@ -0,0 +1,26 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use serde_json::Value;
#[get("/<target>")]
pub async fn fetch_bot(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
let user = Ref::from_unchecked(bot.id.clone()).fetch_user().await?;
Ok(json!({
"bot": bot,
"user": user
}))
}

View File

@@ -0,0 +1,56 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::bson::{Document, doc, from_document};
use serde_json::Value;
#[get("/@me")]
pub async fn fetch_owned_bots(user: User) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bots = get_collection("bots")
.find(
doc! {
"owner": &user.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "bots",
operation: "find"
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| from_document(x).ok())
.collect::<Vec<Bot>>();
let users = get_collection("users")
.find(
doc! {
"bot.owner": &user.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "users",
operation: "find"
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| from_document(x).ok())
.collect::<Vec<User>>();
Ok(json!({
"bots": bots,
"users": users
}))
}

View File

@@ -0,0 +1,28 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use serde_json::Value;
#[get("/<target>/invite")]
pub async fn fetch_public_bot(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
let user = Ref::from_unchecked(bot.id.clone()).fetch_user().await?;
Ok(json!({
"_id": bot.id,
"username": user.username,
"avatar": user.avatar,
"description": user.profile.map(|p| p.content)
}))
}

70
src/routes/bots/invite.rs Normal file
View File

@@ -0,0 +1,70 @@
use crate::database::*;
use crate::util::result::{Error, Result, EmptyResponse};
use rocket::serde::json::Json;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct ServerId {
server: String
}
#[derive(Deserialize)]
pub struct GroupId {
group: String
}
#[derive(Deserialize)]
#[serde(untagged)]
pub enum Destination {
Server(ServerId),
Group(GroupId)
}
#[post("/<target>/invite", data = "<dest>")]
pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
match dest.into_inner() {
Destination::Server(ServerId { server }) => {
let server = Ref::from(server)?.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&server)
.for_server()
.await?;
if !perm.get_manage_server() {
Err(Error::MissingPermission)?
}
server.join_member(&bot.id).await?;
Ok(EmptyResponse {})
}
Destination::Group(GroupId { group }) => {
let channel = Ref::from(group)?.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&channel)
.for_channel()
.await?;
if !perm.get_invite_others() {
Err(Error::MissingPermission)?
}
channel.add_to_group(bot.id, user.id).await?;
Ok(EmptyResponse {})
}
}
}

21
src/routes/bots/mod.rs Normal file
View File

@@ -0,0 +1,21 @@
use rocket::Route;
mod create;
mod invite;
mod fetch_public;
mod fetch;
mod fetch_owned;
mod edit;
mod delete;
pub fn routes() -> Vec<Route> {
routes![
create::create_bot,
invite::invite_bot,
fetch_public::fetch_public_bot,
fetch::fetch_bot,
fetch_owned::fetch_owned_bots,
edit::edit_bot,
delete::delete_bot,
]
}

View File

@@ -1,12 +1,13 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use mongodb::options::UpdateOptions;
use crate::util::result::{Error, Result, EmptyResponse};
#[put("/<target>/ack/<message>")]
pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref, message: Ref) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -18,35 +19,20 @@ pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
Err(Error::MissingPermission)?
}
let id = target.id();
get_collection("channel_unreads")
.update_one(
doc! {
"_id.channel": id,
"_id.user": &user.id
},
doc! {
"$unset": {
"mentions": 1
},
"$set": {
"last_id": &message.id
}
},
UpdateOptions::builder().upsert(true).build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel_unreads",
})?;
crate::task_queue::task_ack::queue(
target.id().into(),
user.id.clone(),
crate::task_queue::task_ack::AckEvent::AckMessage {
id: message.id.clone()
}
).await;
ClientboundNotification::ChannelAck {
id: id.to_string(),
id: target.id().into(),
user: user.id.clone(),
message_id: message.id,
}
.publish(user.id);
Ok(())
Ok(EmptyResponse {})
}

View File

@@ -1,10 +1,10 @@
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::ClientboundNotification};
use mongodb::bson::doc;
#[delete("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -37,7 +37,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
with: "channel",
})?;
Ok(())
Ok(EmptyResponse {})
}
Channel::Group {
id,
@@ -70,7 +70,8 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
target.publish_update(json!({ "owner": new_owner })).await?;
} else {
return target.delete().await;
target.delete().await?;
return Ok(EmptyResponse {});
}
} else {
get_collection("channels")
@@ -103,11 +104,13 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
.await
.ok();
Ok(())
Ok(EmptyResponse {})
}
Channel::TextChannel { .. } => {
Channel::TextChannel { .. } |
Channel::VoiceChannel { .. } => {
if perm.get_manage_channel() {
target.delete().await
target.delete().await?;
Ok(EmptyResponse {})
} else {
Err(Error::MissingPermission)
}

View File

@@ -1,9 +1,9 @@
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveChannelField};
use mongodb::bson::{doc, to_document};
use rocket_contrib::json::Json;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
@@ -18,10 +18,12 @@ pub struct Data {
#[validate(length(min = 1, max = 128))]
icon: Option<String>,
remove: Option<RemoveChannelField>,
#[serde(skip_serializing_if = "Option::is_none")]
nsfw: Option<bool>
}
#[patch("/<target>", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
@@ -30,8 +32,9 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
&& data.description.is_none()
&& data.icon.is_none()
&& data.remove.is_none()
&& data.nsfw.is_none()
{
return Ok(());
return Ok(EmptyResponse {});
}
let target = target.fetch_channel().await?;
@@ -45,7 +48,9 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
}
match &target {
Channel::Group { id, icon, .. } | Channel::TextChannel { id, icon, .. } => {
Channel::Group { id, icon, .. }
| Channel::TextChannel { id, icon, .. }
| Channel::VoiceChannel { id, icon, .. } => {
let mut set = doc! {};
let mut unset = doc! {};
@@ -84,6 +89,10 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
remove_icon = true;
}
if let Some(nsfw) = &data.nsfw {
set.insert("nsfw", nsfw);
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
@@ -144,7 +153,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
}
}
Ok(())
Ok(EmptyResponse {})
}
_ => Err(Error::InvalidOperation),
}

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[get("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)

View File

@@ -1,11 +1,10 @@
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_GROUP_SIZE;
use crate::{database::*, notifications::events::ClientboundNotification};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::database::*;
use mongodb::bson::doc;
#[put("/<target>/recipients/<member>")]
pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref, member: Ref) -> Result<EmptyResponse> {
if get_relationship(&user, &member.id) != RelationshipStatus::Friend {
Err(Error::NotFriends)?
}
@@ -20,51 +19,6 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
Err(Error::MissingPermission)?
}
if let Channel::Group { id, recipients, .. } = &channel {
if recipients.len() >= *MAX_GROUP_SIZE {
Err(Error::GroupTooLarge {
max: *MAX_GROUP_SIZE,
})?
}
if recipients.iter().find(|x| *x == &member.id).is_some() {
Err(Error::AlreadyInGroup)?
}
get_collection("channels")
.update_one(
doc! {
"_id": &id
},
doc! {
"$push": {
"recipients": &member.id
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;
ClientboundNotification::ChannelGroupJoin {
id: id.clone(),
user: member.id.clone(),
}
.publish(id.clone());
Content::SystemMessage(SystemMessage::UserAdded {
id: member.id,
by: user.id,
})
.send_as_system(&channel)
.await
.ok();
Ok(())
} else {
Err(Error::InvalidOperation)
}
channel.add_to_group(member.id, user.id).await?;
Ok(EmptyResponse {})
}

View File

@@ -1,9 +1,10 @@
use crate::database::*;
use crate::util::idempotency::IdempotencyKey;
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_GROUP_SIZE;
use mongodb::bson::doc;
use rocket_contrib::json::{Json, JsonValue};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::iter::FromIterator;
@@ -16,14 +17,17 @@ pub struct Data {
name: String,
#[validate(length(min = 0, max = 1024))]
description: Option<String>,
// Maximum length of 36 allows both ULIDs and UUIDs.
#[validate(length(min = 1, max = 36))]
nonce: String,
users: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
nsfw: Option<bool>
}
#[post("/create", data = "<info>")]
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
pub async fn req(_idempotency: IdempotencyKey, user: User, info: Json<Data>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;
@@ -46,33 +50,17 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
}
}
if get_collection("channels")
.find_one(
doc! {
"nonce": &info.nonce
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "channel",
})?
.is_some()
{
Err(Error::DuplicateNonce)?
}
let id = Ulid::new().to_string();
let channel = Channel::Group {
id,
nonce: Some(info.nonce),
name: info.name,
description: info.description,
owner: user.id,
recipients: set.into_iter().collect::<Vec<String>>(),
icon: None,
last_message: None,
last_message_id: None,
permissions: None,
nsfw: info.nsfw.unwrap_or_default()
};
channel.clone().publish().await?;

View File

@@ -1,10 +1,10 @@
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::ClientboundNotification};
use mongodb::bson::doc;
#[delete("/<target>/recipients/<member>")]
pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref, member: Ref) -> Result<EmptyResponse> {
if &user.id == &member.id {
Err(Error::CannotRemoveYourself)?
}
@@ -59,7 +59,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
.await
.ok();
Ok(())
Ok(EmptyResponse {})
} else {
Err(Error::InvalidOperation)
}

View File

@@ -3,7 +3,7 @@ use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use nanoid::nanoid;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
lazy_static! {
static ref ALPHABET: [char; 54] = [
@@ -14,7 +14,7 @@ lazy_static! {
}
#[post("/<target>/invites")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&target)
@@ -30,7 +30,8 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
Channel::Group { .. } => {
unimplemented!()
}
Channel::TextChannel { id, server, .. } => {
Channel::TextChannel { id, server, .. }
| Channel::VoiceChannel { id, server, .. } => {
Invite::Server {
code: code.clone(),
creator: user.id,

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[get("/<target>/members")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -16,7 +16,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
}
if let Channel::Group { recipients, .. } = target {
Ok(json!(user.fetch_multiple_users(recipients).await?))
Ok(json!(user.fetch_multiple_users(&recipients).await?))
} else {
Err(Error::InvalidOperation)
}

View File

@@ -1,11 +1,12 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
#[delete("/<target>/messages/<msg>")]
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<EmptyResponse> {
let channel = target.fetch_channel().await?;
channel.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&channel)
@@ -23,5 +24,6 @@ pub async fn req(user: User, target: Ref, msg: Ref) -> Result<()> {
}
}
message.delete().await
message.delete().await?;
Ok(EmptyResponse {})
}

View File

@@ -1,24 +1,29 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::routes::channels::message_send::SendableEmbed;
use chrono::Utc;
use mongodb::bson::{doc, Bson, DateTime, Document};
use rocket_contrib::json::Json;
use mongodb::bson::{Bson, Document, doc, to_document};
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 1, max = 2000))]
content: String,
content: Option<String>,
#[validate(length(min = 0, max = 10))]
embeds: Option<Vec<SendableEmbed>>
}
#[patch("/<target>/messages/<msg>", data = "<edit>")]
pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<()> {
pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<EmptyResponse> {
edit.validate()
.map_err(|error| Error::FailedValidation { error })?;
let channel = target.fetch_channel().await?;
channel.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&channel)
.for_channel()
@@ -27,31 +32,56 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
Err(Error::MissingPermission)?
}
let message = msg.fetch_message(&channel).await?;
let mut message = msg.fetch_message(&channel).await?;
if message.author != user.id {
Err(Error::CannotEditMessage)?
}
let edited = Utc::now();
let mut set = doc! {
"content": &edit.content,
"edited": Bson::DateTime(edited)
};
let mut set = doc! { "edited": Bson::DateTime(edited) };
let mut unset = doc! {};
let mut update = json!({ "edited": Bson::DateTime(edited) });
let mut update = json!({ "content": edit.content, "edited": DateTime(edited) });
if let Some(new_content) = &edit.content {
set.insert("content", new_content.clone());
update.as_object_mut().unwrap().insert("content".to_string(), json!(new_content.clone()));
message.content = Content::Text(new_content.clone());
}
let mut new_embeds: Vec<Embed> = vec![];
if let Some(embeds) = &message.embeds {
let new_embeds: Vec<Document> = vec![];
for embed in embeds {
match embed {
Embed::Website(_) | Embed::Image(_) | Embed::None => {} // Otherwise push to new_embeds.
Embed::Text(embed) => new_embeds.push(Embed::Text(embed.clone())),
_ => {}
}
}
}
if let Some(edited_embeds) = &edit.embeds {
new_embeds.clear();
for embed in edited_embeds {
new_embeds.push(embed.clone().into_embed(message.id.clone()).await?);
}
}
if new_embeds.len() > 0 {
let embed_docs: Vec<Document> = new_embeds
.clone()
.into_iter()
.map(|embed| to_document(&embed).unwrap())
.collect();
let obj = update.as_object_mut().unwrap();
obj.insert("embeds".to_string(), json!(new_embeds).0);
set.insert("embeds", new_embeds);
obj.insert("embeds".to_string(), json!(embed_docs));
set.insert("embeds", embed_docs);
message.embeds = Some(new_embeds)
} else if edit.embeds.is_some() {
let obj = update.as_object_mut().unwrap();
obj.insert("embeds".to_string(), json!([]));
unset.insert("embeds", 1 as u32);
}
get_collection("messages")
@@ -60,7 +90,8 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
"_id": &message.id
},
doc! {
"$set": set
"$set": set,
"$unset": unset
},
None,
)
@@ -70,5 +101,6 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
with: "message",
})?;
message.publish_update(update).await
message.publish_update(update).await?;
Ok(EmptyResponse {})
}

View File

@@ -1,11 +1,12 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[get("/<target>/messages/<msg>")]
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<Value> {
let channel = target.fetch_channel().await?;
channel.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&channel)

View File

@@ -3,17 +3,16 @@ use std::collections::HashSet;
use crate::database::*;
use crate::util::result::{Error, Result};
use futures::StreamExt;
use futures::{StreamExt, try_join};
use mongodb::{
bson::{doc, from_document},
options::FindOptions,
};
use rocket::request::Form;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Serialize, Deserialize, FromFormValue)]
#[derive(Serialize, Deserialize, FromFormField)]
pub enum Sort {
Latest,
Oldest,
@@ -28,16 +27,22 @@ pub struct Options {
#[validate(length(min = 26, max = 26))]
after: Option<String>,
sort: Option<Sort>,
// Specifying 'nearby' ignores 'before', 'after' and 'sort'.
// It will also take half of limit rounded as the limits to each side.
// It also fetches the message ID specified.
#[validate(length(min = 26, max = 26))]
nearby: Option<String>,
include_users: Option<bool>,
}
#[get("/<target>/messages?<options..>")]
pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref, options: Options) -> Result<Value> {
options
.validate()
.map_err(|error| Error::FailedValidation { error })?;
let target = target.fetch_channel().await?;
target.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&target)
@@ -47,64 +52,127 @@ pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<Json
Err(Error::MissingPermission)?
}
let mut query = doc! { "channel": target.id() };
let mut messages = vec![];
if let Some(before) = &options.before {
query.insert("_id", doc! { "$lt": before });
}
if let Some(after) = &options.after {
query.insert("_id", doc! { "$gt": after });
}
let sort = if let Sort::Latest = options.sort.as_ref().unwrap_or_else(|| &Sort::Latest) {
-1
} else {
1
};
let mut cursor = get_collection("messages")
.find(
query,
FindOptions::builder()
.limit(options.limit.unwrap_or(50))
.sort(doc! {
"_id": sort
})
.build(),
let collection = get_collection("messages");
let limit = options.limit.unwrap_or(50);
let channel = target.id();
if let Some(nearby) = &options.nearby {
let mut cursors = try_join!(
collection.find(
doc! {
"channel": channel,
"_id": {
"$gte": &nearby
}
},
FindOptions::builder()
.limit(limit / 2 + 1)
.sort(doc! {
"_id": 1
})
.build(),
),
collection.find(
doc! {
"channel": channel,
"_id": {
"$lt": &nearby
}
},
FindOptions::builder()
.limit(limit / 2)
.sort(doc! {
"_id": -1
})
.build(),
)
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "messages",
})?;
let mut messages = vec![];
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
messages.push(
from_document::<Message>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "message",
})?,
);
while let Some(result) = cursors.0.next().await {
if let Ok(doc) = result {
messages.push(
from_document::<Message>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "message",
})?,
);
}
}
while let Some(result) = cursors.1.next().await {
if let Ok(doc) = result {
messages.push(
from_document::<Message>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "message",
})?,
);
}
}
} else {
let mut query = doc! { "channel": target.id() };
if let Some(before) = &options.before {
query.insert("_id", doc! { "$lt": before });
}
if let Some(after) = &options.after {
query.insert("_id", doc! { "$gt": after });
}
let sort: i32 = if let Sort::Latest = options.sort.as_ref().unwrap_or_else(|| &Sort::Latest) {
-1
} else {
1
};
let mut cursor = collection
.find(
query,
FindOptions::builder()
.limit(limit)
.sort(doc! {
"_id": sort
})
.build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "messages",
})?;
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
messages.push(
from_document::<Message>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "message",
})?,
);
}
}
}
if options.include_users.unwrap_or_else(|| false) {
let mut ids = HashSet::new();
for message in &messages {
ids.insert(message.author.clone());
message.add_associated_user_ids(&mut ids);
}
ids.remove(&user.id);
let user_ids = ids.into_iter().collect();
let users = user.fetch_multiple_users(user_ids).await?;
let users = user.fetch_multiple_users(&user_ids).await?;
if let Channel::TextChannel { server, .. } = target {
Ok(json!({
"messages": messages,
"users": users,
"members": Server::fetch_members(&server).await?
"members": Server::fetch_members_with_ids(&server, &user_ids).await?
}))
} else {
Ok(json!({

View File

@@ -3,7 +3,7 @@ use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::bson::{doc, from_document};
use rocket_contrib::json::{Json, JsonValue};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
@@ -12,12 +12,13 @@ pub struct Options {
}
#[post("/<target>/messages/stale", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Options>) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref, data: Json<Options>) -> Result<Value> {
if data.ids.len() > 150 {
return Err(Error::TooManyIds);
}
let target = target.fetch_channel().await?;
target.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&target)

View File

@@ -0,0 +1,162 @@
use std::collections::HashSet;
use crate::database::*;
use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::{
bson::{doc, from_document},
options::FindOptions,
};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Serialize, Deserialize, FromFormField)]
pub enum Sort {
Relevance,
Latest,
Oldest,
}
impl Default for Sort {
fn default() -> Sort {
Sort::Relevance
}
}
#[derive(Validate, Serialize, Deserialize, FromForm)]
pub struct Options {
#[validate(length(min = 1, max = 64))]
query: String,
#[validate(range(min = 1, max = 100))]
limit: Option<i64>,
#[validate(length(min = 26, max = 26))]
before: Option<String>,
#[validate(length(min = 26, max = 26))]
after: Option<String>,
#[serde(default = "Sort::default")]
sort: Sort,
include_users: Option<bool>,
}
#[post("/<target>/search", data = "<options>")]
pub async fn req(user: User, target: Ref, options: Json<Options>) -> Result<Value> {
options
.validate()
.map_err(|error| Error::FailedValidation { error })?;
let target = target.fetch_channel().await?;
target.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&target)
.for_channel()
.await?;
if !perm.get_view() {
Err(Error::MissingPermission)?
}
let mut messages = vec![];
let limit = options.limit.unwrap_or(50);
let mut filter = doc! {
"channel": target.id(),
"$text": {
"$search": &options.query
}
};
if let Some(doc) = match (&options.before, &options.after) {
(Some(before), Some(after)) => Some(doc! {
"lt": before,
"gt": after
}),
(Some(before), _) => Some(doc! {
"lt": before
}),
(_, Some(after)) => Some(doc! {
"gt": after
}),
_ => None
} {
filter.insert("_id", doc);
}
let mut cursor = get_collection("messages")
.find(
filter,
FindOptions::builder()
.projection(
if let Sort::Relevance = &options.sort {
doc! {
"score": {
"$meta": "textScore"
}
}
} else {
doc! {}
}
)
.limit(limit)
.sort(
match &options.sort {
Sort::Relevance => doc! {
"score": {
"$meta": "textScore"
}
},
Sort::Latest => doc! {
"_id": -1 as i32
},
Sort::Oldest => doc! {
"_id": 1 as i32
}
}
)
.build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "messages",
})?;
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
messages.push(
from_document::<Message>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "message",
})?,
);
}
}
if options.include_users.unwrap_or_else(|| false) {
let mut ids = HashSet::new();
for message in &messages {
message.add_associated_user_ids(&mut ids);
}
ids.remove(&user.id);
let user_ids = ids.into_iter().collect();
let users = user.fetch_multiple_users(&user_ids).await?;
if let Channel::TextChannel { server, .. } = target {
Ok(json!({
"messages": messages,
"users": users,
"members": Server::fetch_members_with_ids(&server, &user_ids).await?
}))
} else {
Ok(json!({
"messages": messages,
"users": users,
}))
}
} else {
Ok(json!(messages))
}
}

View File

@@ -1,30 +1,76 @@
use std::collections::HashSet;
use crate::database::*;
use crate::util::idempotency::IdempotencyKey;
use crate::util::ratelimit::{Ratelimiter, RatelimitResponse};
use crate::util::result::{Error, Result};
use mongodb::{bson::doc, options::FindOneOptions};
use mongodb::bson::doc;
use regex::Regex;
use rocket_contrib::json::{Json, JsonValue};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use validator::Validate;
#[derive(Serialize, Deserialize)]
pub struct Reply {
id: String,
mention: bool
}
#[derive(Validate, Serialize, Deserialize, Clone, Debug)]
pub struct SendableEmbed {
icon_url: Option<String>,
url: Option<String>,
#[validate(length(min = 1, max = 100))]
title: Option<String>,
#[validate(length(min = 1, max = 2000))]
description: Option<String>,
media: Option<String>,
colour: Option<String>,
}
impl SendableEmbed {
pub async fn into_embed(self, message_id: String) -> Result<Embed> {
let media = if let Some(id) = self.media {
Some(File::find_and_use(&id, "attachments", "message", &message_id).await?)
} else { None };
Ok(Embed::Text(Text {
icon_url: self.icon_url,
url: self.url,
title: self.title,
description: self.description,
media,
colour: self.colour
}))
}
}
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 0, max = 2000))]
content: String,
// Maximum length of 36 allows both ULIDs and UUIDs.
#[validate(length(min = 1, max = 36))]
nonce: String,
#[validate(length(min = 1, max = 128))]
attachments: Option<Vec<String>>,
nonce: Option<String>,
replies: Option<Vec<Reply>>,
#[validate]
masquerade: Option<Masquerade>,
#[validate(length(min = 1, max = 10))]
embeds: Option<Vec<SendableEmbed>>
}
lazy_static! {
static ref RE_ULID: Regex = Regex::new(r"<@([0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26})>").unwrap();
// ignoring I L O and U is intentional
static ref RE_MENTION: Regex = Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap();
}
#[post("/<target>/messages", data = "<message>")]
pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonValue> {
pub async fn message_send(user: User, _r: Ratelimiter, mut idempotency: IdempotencyKey, target: Ref, message: Json<Data>) -> Result<RatelimitResponse<Value>> {
let message = message.into_inner();
idempotency.consume_nonce(message.nonce.clone());
message
.validate()
.map_err(|error| Error::FailedValidation { error })?;
@@ -36,58 +82,76 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
}
let target = target.fetch_channel().await?;
target.has_messaging()?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&target)
.for_channel()
.await?;
if !perm.get_send_message() {
Err(Error::MissingPermission)?
return Err(Error::MissingPermission)
}
if get_collection("messages")
.find_one(
doc! {
"nonce": &message.nonce
},
FindOneOptions::builder()
.projection(doc! { "_id": 1 })
.build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "message",
})?
.is_some()
{
Err(Error::DuplicateNonce)?
let mut mentions = HashSet::new();
for capture in RE_MENTION.captures_iter(&message.content) {
if let Some(mention) = capture.get(1) {
mentions.insert(mention.as_str().to_string());
}
}
if let Some(_) = &message.masquerade {
if !perm.get_masquerade() {
return Err(Error::MissingPermission)
}
}
let mut replies = HashSet::new();
if let Some(entries) = message.replies {
// ! FIXME: move this to app config
if entries.len() > 5 {
return Err(Error::TooManyReplies)
}
for Reply { id, mention } in entries {
let message = Ref::from_unchecked(id)
.fetch_message(&target)
.await?;
replies.insert(message.id);
if mention {
mentions.insert(message.author);
}
}
}
let id = Ulid::new().to_string();
let attachments = if let Some(ids) = &message.attachments {
let mut attachments = vec![];
if let Some(ids) = &message.attachments {
if ids.len() > 0 && !perm.get_upload_files() {
return Err(Error::MissingPermission)
}
// ! FIXME: move this to app config
if ids.len() >= 5 {
if ids.len() > 5 {
return Err(Error::TooManyAttachments)
}
let mut attachments = vec![];
for attachment_id in ids {
attachments
.push(File::find_and_use(attachment_id, "attachments", "message", &id).await?);
}
Some(attachments)
} else {
None
};
let mut mentions = vec![];
if let Some(captures) = RE_ULID.captures_iter(&message.content).next() {
// ! FIXME: in the future, verify in group so we can send out push
mentions.push(captures[1].to_string());
}
let mut embeds = vec![];
if let Some(sendable_embeds) = message.embeds {
for sendable_embed in sendable_embeds {
embeds.push(sendable_embed.into_embed(id.clone()).await?)
}
}
let msg = Message {
id,
@@ -95,18 +159,23 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
author: user.id,
content: Content::Text(message.content.clone()),
attachments,
nonce: Some(message.nonce.clone()),
nonce: Some(idempotency.key),
edited: None,
embeds: None,
embeds: if embeds.len() > 0 { Some(embeds) } else { None },
attachments: if attachments.len() > 0 { Some(attachments) } else { None },
mentions: if mentions.len() > 0 {
Some(mentions)
Some(mentions.into_iter().collect::<Vec<String>>())
} else {
None
},
replies: if replies.len() > 0 {
Some(replies.into_iter().collect::<Vec<String>>())
} else {
None
},
masquerade: message.masquerade
};
msg.clone().publish(&target).await?;
Ok(json!(msg))
msg.clone().publish(&target, perm.get_embed_links()).await?;
Ok(RatelimitResponse(json!(msg)))
}

View File

@@ -1,32 +1,36 @@
use rocket::Route;
mod channel_ack;
mod delete_channel;
mod edit_channel;
mod fetch_channel;
mod fetch_members;
mod channel_delete;
mod channel_edit;
mod channel_fetch;
mod members_fetch;
mod group_add_member;
mod group_create;
mod group_remove_member;
mod invite_create;
mod join_call;
mod voice_join;
mod message_delete;
mod message_edit;
mod message_fetch;
mod message_query;
mod message_search;
mod message_query_stale;
mod message_send;
mod permissions_set;
mod permissions_set_default;
pub fn routes() -> Vec<Route> {
routes![
channel_ack::req,
fetch_channel::req,
fetch_members::req,
delete_channel::req,
edit_channel::req,
channel_fetch::req,
members_fetch::req,
channel_delete::req,
channel_edit::req,
invite_create::req,
message_send::req,
message_send::message_send,
message_query::req,
message_search::req,
message_query_stale::req,
message_fetch::req,
message_edit::req,
@@ -34,6 +38,8 @@ pub fn routes() -> Vec<Route> {
group_create::req,
group_add_member::req,
group_remove_member::req,
join_call::req,
voice_join::req,
permissions_set::req,
permissions_set_default::req,
]
}

View File

@@ -0,0 +1,68 @@
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Serialize, Deserialize};
use validator::Contains;
use crate::database::*;
use crate::util::result::{Error, Result, EmptyResponse};
use crate::notifications::events::ClientboundNotification;
#[derive(Serialize, Deserialize)]
pub struct Data {
permissions: u32
}
#[put("/<target>/permissions/<role>", data = "<data>", rank = 2)]
pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_channel().await?;
match target {
Channel::TextChannel { id, server, mut role_permissions, .. }
| Channel::VoiceChannel { id, server, mut role_permissions, .. } => {
let target = Ref::from_unchecked(server).fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
return Err(Error::MissingPermission);
}
if !target.roles.has_element(&role) {
return Err(Error::NotFound);
}
let permissions: u32 = data.permissions;
get_collection("channels")
.update_one(
doc! { "_id": &id },
doc! {
"$set": {
"role_permissions.".to_owned() + &role: permissions as i32
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel"
})?;
role_permissions.insert(role, permissions as i32);
ClientboundNotification::ChannelUpdate {
id: id.clone(),
data: json!({
"role_permissions": role_permissions
}),
clear: None
}
.publish(id);
Ok(EmptyResponse {})
}
_ => Err(Error::InvalidOperation)
}
}

View File

@@ -0,0 +1,97 @@
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Serialize, Deserialize};
use crate::database::*;
use crate::database::permissions::channel::{ ChannelPermission, DEFAULT_PERMISSION_DM };
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)]
pub struct Data {
permissions: u32
}
#[put("/<target>/permissions/default", data = "<data>", rank = 1)]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_channel().await?;
match target {
Channel::Group { id, owner, .. } => {
if user.id == owner {
let permissions: u32 = ChannelPermission::View as u32 | (data.permissions & *DEFAULT_PERMISSION_DM);
get_collection("channels")
.update_one(
doc! { "_id": &id },
doc! {
"$set": {
"permissions": permissions as i32
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel"
})?;
ClientboundNotification::ChannelUpdate {
id: id.clone(),
data: json!({
"permissions": permissions as i32
}),
clear: None
}
.publish(id);
Ok(EmptyResponse {})
} else {
Err(Error::MissingPermission)
}
}
Channel::TextChannel { id, server, .. }
| Channel::VoiceChannel { id, server, .. } => {
let target = Ref::from_unchecked(server).fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
return Err(Error::MissingPermission);
}
let permissions: u32 = data.permissions;
get_collection("channels")
.update_one(
doc! { "_id": &id },
doc! {
"$set": {
"default_permissions": permissions as i32
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel"
})?;
ClientboundNotification::ChannelUpdate {
id: id.clone(),
data: json!({
"default_permissions": permissions as i32
}),
clear: None
}
.publish(id);
Ok(EmptyResponse {})
}
_ => Err(Error::InvalidOperation)
}
}

View File

@@ -2,7 +2,7 @@ use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::variables::{USE_VOSO, VOSO_MANAGE_TOKEN, VOSO_URL};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
@@ -11,7 +11,7 @@ struct CreateUserResponse {
}
#[post("/<target>/join_call")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
if !*USE_VOSO {
return Err(Error::VosoUnavailable);
}

View File

@@ -1,12 +1,12 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
#[delete("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
let target = target.fetch_invite().await?;
if target.creator() == &user.id {
target.delete().await
target.delete().await?;
} else {
match &target {
Invite::Server { server, .. } => {
@@ -20,9 +20,11 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
return Err(Error::MissingPermission);
}
target.delete().await
target.delete().await?;
}
_ => unreachable!(),
}
}
Ok(EmptyResponse {})
}

View File

@@ -1,7 +1,7 @@
use crate::database::*;
use crate::util::result::Result;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::Serialize;
#[derive(Serialize, Debug, Clone)]
@@ -21,11 +21,12 @@ pub enum InviteResponse {
user_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
user_avatar: Option<File>,
member_count: i64,
},
}
#[get("/<target>")]
pub async fn req(target: Ref) -> Result<JsonValue> {
pub async fn req(target: Ref) -> Result<Value> {
let target = target.fetch_invite().await?;
match target {
@@ -35,29 +36,25 @@ pub async fn req(target: Ref) -> Result<JsonValue> {
let channel = Ref::from_unchecked(channel).fetch_channel().await?;
let creator = Ref::from_unchecked(creator).fetch_user().await?;
if let Channel::TextChannel {
id,
server,
name,
description,
..
} = channel
{
let server = Ref::from_unchecked(server).fetch_server().await?;
Ok(json!(InviteResponse::Server {
server_id: server.id,
server_name: server.name,
server_icon: server.icon,
server_banner: server.banner,
channel_id: id,
channel_name: name,
channel_description: description,
user_name: creator.username,
user_avatar: creator.avatar
}))
} else {
unreachable!()
match channel {
Channel::TextChannel { id, server, name, description, .. }
| Channel::VoiceChannel { id, server, name, description, .. } => {
let server = Ref::from_unchecked(server).fetch_server().await?;
Ok(json!(InviteResponse::Server {
member_count: Server::get_member_count(&server.id).await?,
server_id: server.id,
server_name: server.name,
server_icon: server.icon,
server_banner: server.banner,
channel_id: id,
channel_name: name,
channel_description: description,
user_name: creator.username,
user_avatar: creator.avatar
}))
}
_ => unreachable!()
}
}
_ => unreachable!(),

View File

@@ -1,19 +1,32 @@
use crate::database::*;
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_SERVER_COUNT;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[post("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
if !User::can_acquire_server(&user.id).await? {
Err(Error::TooManyServers {
max: *MAX_SERVER_COUNT,
})?
}
let target = target.fetch_invite().await?;
match target {
Invite::Server { channel, .. } => {
let channel = Ref::from_unchecked(channel).fetch_channel().await?;
let server = if let Channel::TextChannel { server, .. } = &channel {
Ref::from_unchecked(server.clone()).fetch_server().await?
} else {
unreachable!()
let server = match &channel {
Channel::TextChannel { server, .. }
| Channel::VoiceChannel { server, .. } => {
Ref::from_unchecked(server.clone()).fetch_server().await?
}
_ => unreachable!()
};
server.join_member(&user.id).await?;

View File

@@ -1,7 +1,8 @@
pub use rocket::http::Status;
pub use rocket::response::Redirect;
use rocket::Rocket;
use rocket::{Build, Rocket};
mod bots;
mod channels;
mod invites;
mod onboard;
@@ -11,13 +12,14 @@ mod servers;
mod sync;
mod users;
pub fn mount(rocket: Rocket) -> Rocket {
pub fn mount(rocket: Rocket<Build>) -> Rocket<Build> {
rocket
.mount("/", routes![root::root])
.mount("/", routes![root::root, root::ping])
.mount("/onboard", onboard::routes())
.mount("/users", users::routes())
.mount("/channels", channels::routes())
.mount("/servers", servers::routes())
.mount("/bots", bots::routes())
.mount("/invites", invites::routes())
.mount("/push", push::routes())
.mount("/sync", sync::routes())

View File

@@ -1,17 +1,13 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::util::regex::RE_USERNAME;
use mongodb::bson::doc;
use rauth::auth::Session;
use regex::Regex;
use rocket_contrib::json::Json;
use rauth::entities::Session;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
lazy_static! {
static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9_.]+$").unwrap();
}
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
@@ -19,7 +15,7 @@ pub struct Data {
}
#[post("/complete", data = "<data>")]
pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Result<()> {
pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Result<EmptyResponse> {
if user.is_some() {
Err(Error::AlreadyOnboarded)?
}
@@ -45,5 +41,5 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
with: "user",
})?;
Ok(())
Ok(EmptyResponse {})
}

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use rauth::auth::Session;
use rocket_contrib::json::JsonValue;
use rauth::entities::Session;
use rocket::serde::json::Value;
#[get("/hello")]
pub async fn req(_session: Session, user: Option<User>) -> JsonValue {
pub async fn req(_session: Session, user: Option<User>) -> Value {
json!({
"onboarding": user.is_none()
})

View File

@@ -1,37 +1,19 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{EmptyResponse, Error, Result};
use mongodb::bson::{doc, to_document};
use rauth::auth::Session;
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Subscription {
endpoint: String,
p256dh: String,
auth: String,
}
use mongodb::bson::doc;
use rauth::entities::{Model, Session, WebPushSubscription};
use rocket::serde::json::Json;
#[post("/subscribe", data = "<data>")]
pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
let data = data.into_inner();
get_collection("accounts")
.update_one(
doc! {
"_id": session.user_id,
"sessions.id": session.id.unwrap()
},
doc! {
"$set": {
"sessions.$.subscription": to_document(&data)
.map_err(|_| Error::DatabaseError { operation: "to_document", with: "subscription" })?
}
},
None,
)
pub async fn req(mut session: Session, data: Json<WebPushSubscription>) -> Result<EmptyResponse> {
session.subscription = Some(data.into_inner());
session
.save(&get_db(), None)
.await
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "account" })?;
Ok(())
.map(|_| EmptyResponse)
.map_err(|_| Error::DatabaseError {
operation: "save",
with: "session",
})
}

View File

@@ -1,29 +1,18 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{EmptyResponse, Error, Result};
use mongodb::bson::doc;
use rauth::auth::Session;
use rauth::entities::{Model, Session};
#[post("/unsubscribe")]
pub async fn req(session: Session) -> Result<()> {
get_collection("accounts")
.update_one(
doc! {
"_id": session.user_id,
"sessions.id": session.id.unwrap()
},
doc! {
"$unset": {
"sessions.$.subscription": 1
}
},
None,
)
pub async fn req(mut session: Session) -> Result<EmptyResponse> {
session.subscription = None;
session
.save(&get_db(), None)
.await
.map(|_| EmptyResponse)
.map_err(|_| Error::DatabaseError {
operation: "to_document",
with: "subscription",
})?;
Ok(())
operation: "save",
with: "session",
})
}

View File

@@ -1,13 +1,13 @@
use crate::util::variables::{
use crate::util::{ratelimit::Ratelimiter, variables::{
APP_URL, AUTUMN_URL, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, INVITE_ONLY, JANUARY_URL, USE_AUTUMN,
USE_EMAIL, USE_HCAPTCHA, USE_JANUARY, USE_VOSO, VAPID_PUBLIC_KEY, VOSO_URL, VOSO_WS_HOST,
};
}};
use mongodb::bson::doc;
use rocket_contrib::json::JsonValue;
use rocket::{http::Status, serde::json::Value};
#[get("/")]
pub async fn root() -> JsonValue {
pub async fn root() -> Value {
json!({
"revolt": crate::version::VERSION,
"features": {
@@ -36,3 +36,8 @@ pub async fn root() -> JsonValue {
"vapid": *VAPID_PUBLIC_KEY
})
}
#[get("/ping")]
pub async fn ping(_limitguard: Ratelimiter) -> Status {
Status::Ok
}

View File

@@ -1,8 +1,8 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
use rocket_contrib::json::Json;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
@@ -13,7 +13,7 @@ pub struct Data {
}
#[put("/<server>/bans/<target>", data = "<data>")]
pub async fn req(user: User, server: Ref, target: Ref, data: Json<Data>) -> Result<()> {
pub async fn req(user: User, server: Ref, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
@@ -57,5 +57,6 @@ pub async fn req(user: User, server: Ref, target: Ref, data: Json<Data>) -> Resu
with: "server_ban",
})?;
server.remove_member(&target.id, RemoveMember::Ban).await
server.remove_member(&target.id, RemoveMember::Ban).await?;
Ok(EmptyResponse {})
}

View File

@@ -2,11 +2,20 @@ use crate::database::*;
use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::options::FindOptions;
use serde::{Serialize, Deserialize};
use rocket::serde::json::Value;
use mongodb::bson::{doc, from_document};
use rocket_contrib::json::JsonValue;
#[derive(Serialize, Deserialize)]
struct BannedUser {
_id: String,
username: String,
avatar: Option<File>
}
#[get("/<target>/bans")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -14,8 +23,8 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
.for_server()
.await?;
if !perm.get_manage_members() {
Err(Error::MissingPermission)?
if !perm.get_ban_members() {
return Err(Error::MissingPermission);
}
let mut cursor = get_collection("server_bans")
@@ -32,13 +41,47 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
})?;
let mut bans = vec![];
let mut user_ids = vec![];
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
if let Ok(ban) = from_document::<Ban>(doc) {
user_ids.push(ban.id.user.clone());
bans.push(ban);
}
}
}
Ok(json!(bans))
let mut cursor = get_collection("users")
.find(
doc! {
"_id": {
"$in": user_ids
}
},
FindOptions::builder()
.projection(doc! {
"username": 1,
"avatar": 1
})
.build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "users",
})?;
let mut users = vec![];
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
if let Ok(user) = from_document::<BannedUser>(doc) {
users.push(user);
}
}
}
Ok(json!({
"users": users,
"bans": bans
}))
}

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
#[delete("/<server>/bans/<target>")]
pub async fn req(user: User, server: Ref, target: Ref) -> Result<()> {
pub async fn req(user: User, server: Ref, target: Ref) -> Result<EmptyResponse> {
let server = server.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -39,5 +39,5 @@ pub async fn req(user: User, server: Ref, target: Ref) -> Result<()> {
with: "server_ban",
})?;
Ok(())
Ok(EmptyResponse {})
}

View File

@@ -1,25 +1,41 @@
use std::collections::HashMap;
use crate::database::*;
use crate::util::idempotency::IdempotencyKey;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use rocket_contrib::json::{Json, JsonValue};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use validator::Validate;
#[derive(Serialize, Deserialize)]
enum ChannelType {
Text,
Voice
}
impl Default for ChannelType {
fn default() -> Self {
ChannelType::Text
}
}
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[serde(rename = "type", default = "ChannelType::default")]
channel_type: ChannelType,
#[validate(length(min = 1, max = 32))]
name: String,
#[validate(length(min = 0, max = 1024))]
description: Option<String>,
// Maximum length of 36 allows both ULIDs and UUIDs.
#[validate(length(min = 1, max = 36))]
nonce: String,
#[serde(skip_serializing_if = "Option::is_none")]
nsfw: Option<bool>,
}
#[post("/<target>/channels", data = "<info>")]
pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<JsonValue> {
pub async fn req(_idempotency: IdempotencyKey, user: User, target: Ref, info: Json<Data>) -> Result<Value> {
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;
@@ -30,37 +46,39 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<JsonValue>
.for_server()
.await?;
if !perm.get_manage_server() {
if !perm.get_manage_channels() {
Err(Error::MissingPermission)?
}
if get_collection("channels")
.find_one(
doc! {
"nonce": &info.nonce
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "channel",
})?
.is_some()
{
Err(Error::DuplicateNonce)?
}
let id = Ulid::new().to_string();
let channel = Channel::TextChannel {
id: id.clone(),
server: target.id.clone(),
nonce: Some(info.nonce),
let channel = match info.channel_type {
ChannelType::Text => Channel::TextChannel {
id: id.clone(),
server: target.id.clone(),
name: info.name,
description: info.description,
icon: None,
last_message: None,
name: info.name,
description: info.description,
icon: None,
last_message_id: None,
default_permissions: None,
role_permissions: HashMap::new(),
nsfw: info.nsfw.unwrap_or_default(),
},
ChannelType::Voice => Channel::VoiceChannel {
id: id.clone(),
server: target.id.clone(),
name: info.name,
description: info.description,
icon: None,
default_permissions: None,
role_permissions: HashMap::new(),
nsfw: info.nsfw.unwrap_or_default()
}
};
channel.clone().publish().await?;

View File

@@ -3,7 +3,7 @@ use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::bson::{doc, from_document};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -15,7 +15,7 @@ pub struct ServerInvite {
}
#[get("/<target>/invites")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)

View File

@@ -1,9 +1,11 @@
use std::collections::HashSet;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveMemberField};
use mongodb::bson::{doc, to_document};
use rocket_contrib::json::Json;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
@@ -12,17 +14,18 @@ pub struct Data {
#[validate(length(min = 1, max = 32))]
nickname: Option<String>,
avatar: Option<String>,
roles: Option<Vec<String>>,
remove: Option<RemoveMemberField>,
}
#[patch("/<server>/members/<target>", data = "<data>")]
pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> Result<()> {
pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
if data.nickname.is_none() && data.avatar.is_none() && data.remove.is_none() {
return Ok(());
if data.nickname.is_none() && data.avatar.is_none() && data.roles.is_none() && data.remove.is_none() {
return Ok(EmptyResponse {});
}
let server = server.fetch_server().await?;
@@ -33,6 +36,10 @@ pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> R
.for_server()
.await?;
if data.roles.is_some() && !perm.get_manage_roles() {
return Err(Error::MissingPermission);
}
if target.id.user == user.id {
if (data.nickname.is_some() && !perm.get_change_nickname())
|| (data.avatar.is_some() && !perm.get_change_avatar())
@@ -97,6 +104,18 @@ pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> R
remove_avatar = true;
}
if let Some(role_ids) = &data.roles {
let mut ids = HashSet::new();
for role in role_ids {
if server.roles.contains_key(role) {
ids.insert(role.clone());
}
}
set.insert("roles", ids.into_iter().collect::<Vec<String>>());
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
@@ -135,5 +154,5 @@ pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> R
}
}
Ok(())
Ok(EmptyResponse {})
}

View File

@@ -2,10 +2,10 @@ use crate::database::*;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[get("/<target>/members/<member>")]
pub async fn req(user: User, target: Ref, member: String) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref, member: String) -> Result<Value> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)

View File

@@ -3,12 +3,12 @@ use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::bson::{doc, from_document, Document};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
// ! FIXME: this is a temporary route while permissions are being worked on.
#[get("/<target>/members")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -46,6 +46,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
Ok(json!({
"members": members,
"users": user.fetch_multiple_users(member_ids).await?
"users": user.fetch_multiple_users(&member_ids).await?
}))
}

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
#[delete("/<target>/members/<member>")]
pub async fn req(user: User, target: Ref, member: String) -> Result<()> {
pub async fn req(user: User, target: Ref, member: String) -> Result<EmptyResponse> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -12,7 +12,7 @@ pub async fn req(user: User, target: Ref, member: String) -> Result<()> {
.for_server()
.await?;
if !perm.get_manage_members() {
if !perm.get_kick_members() {
return Err(Error::MissingPermission);
}
@@ -21,11 +21,13 @@ pub async fn req(user: User, target: Ref, member: String) -> Result<()> {
return Err(Error::InvalidOperation);
}
if target.id == target.owner {
if member.id.user == target.owner {
return Err(Error::MissingPermission);
}
target
.remove_member(&member.id.user, RemoveMember::Kick)
.await
.await?;
Ok(EmptyResponse {})
}

View File

@@ -4,6 +4,7 @@ mod server_create;
mod server_delete;
mod server_edit;
mod server_fetch;
mod server_ack;
mod channel_create;
@@ -18,12 +19,19 @@ mod ban_remove;
mod invites_fetch;
mod roles_create;
mod roles_edit;
mod roles_delete;
mod permissions_set;
mod permissions_set_default;
pub fn routes() -> Vec<Route> {
routes![
server_create::req,
server_delete::req,
server_fetch::req,
server_edit::req,
server_ack::req,
channel_create::req,
member_fetch_all::req,
member_remove::req,
@@ -32,6 +40,11 @@ pub fn routes() -> Vec<Route> {
ban_create::req,
ban_remove::req,
ban_list::req,
invites_fetch::req
invites_fetch::req,
roles_create::req,
roles_edit::req,
roles_delete::req,
permissions_set::req,
permissions_set_default::req
]
}

View File

@@ -0,0 +1,73 @@
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Serialize, Deserialize};
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)]
pub struct Values {
server: u32,
channel: u32
}
#[derive(Serialize, Deserialize)]
pub struct Data {
permissions: Values
}
#[put("/<target>/permissions/<role_id>", data = "<data>", rank = 2)]
pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
return Err(Error::MissingPermission);
}
if !target.roles.contains_key(&role_id) {
return Err(Error::NotFound);
}
let server_permissions: u32 = data.permissions.server;
let channel_permissions: u32 = data.permissions.channel;
get_collection("servers")
.update_one(
doc! { "_id": &target.id },
doc! {
"$set": {
"roles.".to_owned() + &role_id + &".permissions": [
server_permissions as i32,
channel_permissions as i32
]
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "server"
})?;
ClientboundNotification::ServerRoleUpdate {
id: target.id.clone(),
role_id,
data: json!({
"permissions": [
server_permissions as i32,
channel_permissions as i32
]
}),
clear: None
}
.publish(target.id);
Ok(EmptyResponse)
}

View File

@@ -0,0 +1,68 @@
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Serialize, Deserialize};
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)]
pub struct Values {
server: u32,
channel: u32
}
#[derive(Serialize, Deserialize)]
pub struct Data {
permissions: Values
}
#[put("/<target>/permissions/default", data = "<data>", rank = 1)]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
return Err(Error::MissingPermission);
}
let server_permissions: u32 = data.permissions.server;
let channel_permissions: u32 = data.permissions.channel;
get_collection("servers")
.update_one(
doc! { "_id": &target.id },
doc! {
"$set": {
"default_permissions": [
server_permissions as i32,
channel_permissions as i32
]
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "server"
})?;
ClientboundNotification::ServerUpdate {
id: target.id.clone(),
data: json!({
"default_permissions": [
server_permissions as i32,
channel_permissions as i32
]
}),
clear: None
}
.publish(target.id);
Ok(EmptyResponse {})
}

View File

@@ -0,0 +1,76 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use ulid::Ulid;
use mongodb::bson::doc;
use validator::Validate;
use serde::{Serialize, Deserialize};
use rocket::serde::json::{Json, Value};
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 1, max = 32))]
name: String
}
#[post("/<target>/roles", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<Value> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
Err(Error::MissingPermission)?
}
let id = Ulid::new().to_string();
let perm_tuple = (
*permissions::server::DEFAULT_PERMISSION as i32,
*permissions::channel::DEFAULT_PERMISSION_SERVER as i32
);
get_collection("servers")
.update_one(
doc! {
"_id": &target.id
},
doc! {
"$set": {
"roles.".to_owned() + &id: {
"name": &data.name,
"permissions": [
&perm_tuple.0,
&perm_tuple.1
]
}
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "servers"
})?;
ClientboundNotification::ServerRoleUpdate {
id: target.id.clone(),
role_id: id.clone(),
data: json!({
"name": data.name,
"permissions": &perm_tuple
}),
clear: None
}
.publish(target.id);
Ok(json!({ "id": id, "permissions": perm_tuple }))
}

View File

@@ -0,0 +1,81 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
#[delete("/<target>/roles/<role_id>")]
pub async fn req(user: User, target: Ref, role_id: String) -> Result<EmptyResponse> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
Err(Error::MissingPermission)?
}
get_collection("servers")
.update_one(
doc! {
"_id": &target.id
},
doc! {
"$unset": {
"roles.".to_owned() + &role_id: 1
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "servers"
})?;
get_collection("channels")
.update_one(
doc! {
"server": &target.id
},
doc! {
"$unset": {
"role_permissions.".to_owned() + &role_id: 1
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channels"
})?;
get_collection("server_members")
.update_many(
doc! {
"_id.server": &target.id
},
doc! {
"$pull": {
"roles": &role_id
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "server_members"
})?;
ClientboundNotification::ServerRoleDelete {
id: target.id.clone(),
role_id
}
.publish(target.id);
Ok(EmptyResponse {})
}

View File

@@ -0,0 +1,110 @@
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveRoleField};
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 1, max = 32))]
name: Option<String>,
#[validate(length(min = 1, max = 32))]
colour: Option<String>,
hoist: Option<bool>,
rank: Option<i64>,
remove: Option<RemoveRoleField>,
}
#[patch("/<target>/roles/<role_id>", data = "<data>")]
pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none() && data.colour.is_none() && data.hoist.is_none() && data.rank.is_none() && data.remove.is_none()
{
return Ok(EmptyResponse {});
}
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
return Err(Error::MissingPermission)
}
if !target.roles.contains_key(&role_id) {
return Err(Error::InvalidRole)
}
let mut set = doc! {};
let mut unset = doc! {};
// ! FIXME: we should probably just require clients to support basic MQL incl. $set / $unset
let mut set_update = doc! {};
let role_key = "roles.".to_owned() + &role_id;
if let Some(remove) = &data.remove {
match remove {
RemoveRoleField::Colour => {
unset.insert(role_key.clone() + ".colour", 1);
}
}
}
if let Some(name) = &data.name {
set.insert(role_key.clone() + ".name", name);
set_update.insert("name", name);
}
if let Some(colour) = &data.colour {
set.insert(role_key.clone() + ".colour", colour);
set_update.insert("colour", colour);
}
if let Some(hoist) = &data.hoist {
set.insert(role_key.clone() + ".hoist", hoist);
set_update.insert("hoist", hoist);
}
if let Some(rank) = &data.rank {
set.insert(role_key.clone() + ".rank", rank);
set_update.insert("rank", rank);
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
}
if unset.len() > 0 {
operations.insert("$unset", unset);
}
if operations.len() > 0 {
get_collection("servers")
.update_one(doc! { "_id": &target.id }, operations, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "server",
})?;
}
ClientboundNotification::ServerRoleUpdate {
id: target.id.clone(),
role_id,
data: json!(set_update),
clear: data.remove,
}
.publish(target.id.clone());
Ok(EmptyResponse {})
}

View File

@@ -0,0 +1,23 @@
use crate::database::*;
use crate::util::result::{Error, Result, EmptyResponse};
#[put("/<target>/ack")]
pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
.for_server()
.await?;
if !perm.get_view() {
Err(Error::MissingPermission)?
}
target.mark_as_read(&user.id).await?;
Ok(EmptyResponse {})
}

View File

@@ -1,8 +1,12 @@
use std::collections::HashMap;
use crate::database::*;
use crate::util::idempotency::IdempotencyKey;
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_SERVER_COUNT;
use mongodb::bson::doc;
use rocket_contrib::json::{Json, JsonValue};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use validator::Validate;
@@ -13,45 +17,38 @@ pub struct Data {
name: String,
#[validate(length(min = 0, max = 1024))]
description: Option<String>,
// Maximum length of 36 allows both ULIDs and UUIDs.
#[validate(length(min = 1, max = 36))]
nonce: String,
#[serde(skip_serializing_if = "Option::is_none")]
nsfw: Option<bool>
}
#[post("/create", data = "<info>")]
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
pub async fn req(_idempotency: IdempotencyKey, user: User, info: Json<Data>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
if !User::can_acquire_server(&user.id).await? {
Err(Error::TooManyServers {
max: *MAX_SERVER_COUNT,
})?
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;
if get_collection("servers")
.find_one(
doc! {
"nonce": &info.nonce
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "server",
})?
.is_some()
{
Err(Error::DuplicateNonce)?
}
let id = Ulid::new().to_string();
let cid = Ulid::new().to_string();
let server = Server {
id: id.clone(),
nonce: Some(info.nonce.clone()),
owner: user.id.clone(),
name: info.name,
description: info.description,
channels: vec![cid.clone()],
categories: None,
system_messages: Some(SystemMessageChannels {
user_joined: Some(cid.clone()),
user_left: Some(cid.clone()),
@@ -59,18 +56,33 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
user_banned: Some(cid.clone()),
}),
roles: HashMap::new(),
default_permissions: (
*permissions::server::DEFAULT_PERMISSION as i32,
*permissions::channel::DEFAULT_PERMISSION_SERVER as i32
),
icon: None,
banner: None,
flags: None,
nsfw: info.nsfw.unwrap_or_default(),
analytics: false,
discoverable: false,
};
Channel::TextChannel {
id: cid,
server: id,
nonce: Some(info.nonce),
name: "general".to_string(),
description: None,
icon: None,
last_message: None,
last_message_id: None,
default_permissions: None,
role_permissions: HashMap::new(),
nsfw: false
}
.publish()
.await?;

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
#[delete("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<()> {
pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target)
@@ -16,8 +16,10 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
}
if user.id == target.owner {
target.delete().await
target.delete().await?;
} else {
target.remove_member(&user.id, RemoveMember::Leave).await
target.remove_member(&user.id, RemoveMember::Leave).await?;
}
Ok(EmptyResponse {})
}

View File

@@ -1,9 +1,9 @@
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveServerField};
use mongodb::bson::{doc, to_document};
use rocket_contrib::json::Json;
use mongodb::bson::{doc, to_bson, to_document};
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
@@ -15,18 +15,22 @@ pub struct Data {
description: Option<String>,
icon: Option<String>,
banner: Option<String>,
categories: Option<Vec<Category>>,
system_messages: Option<SystemMessageChannels>,
remove: Option<RemoveServerField>,
nsfw: Option<bool>,
analytics: Option<bool>,
}
#[patch("/<target>", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none() && data.icon.is_none() && data.banner.is_none() && data.remove.is_none()
if data.name.is_none() && data.description.is_none() && data.icon.is_none() && data.banner.is_none() && data.remove.is_none() && data.categories.is_none() && data.system_messages.is_none() && data.nsfw.is_none() && data.analytics.is_none()
{
return Ok(());
return Ok(EmptyResponse {});
}
let target = target.fetch_server().await?;
@@ -95,6 +99,22 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
remove_banner = true;
}
if let Some(categories) = &data.categories {
set.insert("categories", to_bson(&categories).map_err(|_| Error::DatabaseError { operation: "to_document", with: "categories" })?);
}
if let Some(system_messages) = &data.system_messages {
set.insert("system_messages", to_bson(&system_messages).map_err(|_| Error::DatabaseError { operation: "to_document", with: "system_messages" })?);
}
if let Some(nsfw) = &data.nsfw {
set.insert("nsfw", nsfw);
}
if let Some(analytics) = &data.analytics {
set.insert("analytics", analytics);
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
@@ -135,5 +155,5 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
}
}
Ok(())
Ok(EmptyResponse {})
}

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[get("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
pub async fn req(user: User, target: Ref) -> Result<Value> {
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)

View File

@@ -3,7 +3,7 @@ use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use rocket_contrib::json::{Json, JsonValue};
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
@@ -12,7 +12,11 @@ pub struct Options {
}
#[post("/settings/fetch", data = "<options>")]
pub async fn req(user: User, options: Json<Options>) -> Result<JsonValue> {
pub async fn req(user: User, options: Json<Options>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot);
}
let options = options.into_inner();
let mut projection = doc! {
"_id": 0,

View File

@@ -1,10 +1,14 @@
use crate::database::*;
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use rocket_contrib::json::JsonValue;
use rocket::serde::json::Value;
#[get("/unreads")]
pub async fn req(user: User) -> Result<JsonValue> {
Ok(json!(user.fetch_unreads().await?))
pub async fn req(user: User) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot);
}
Ok(json!(User::fetch_unreads(&user.id).await?))
}

Some files were not shown because too many files have changed in this diff Show More