forked from jmug/stoatchat
Merge branch 'master' into unify-last-message
This commit is contained in:
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
ko_fi: insertish
|
||||
custom: https://insrt.uk/donate
|
||||
101
.github/workflows/docker.yml
vendored
Normal file
101
.github/workflows/docker.yml
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
name: Docker
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "master"
|
||||
tags:
|
||||
- "v*"
|
||||
paths-ignore:
|
||||
- ".github/**"
|
||||
- "!.github/workflows/docker.yml"
|
||||
- ".vscode/**"
|
||||
- ".gitignore"
|
||||
- ".gitlab-ci.yml"
|
||||
- "LICENSE"
|
||||
- "README"
|
||||
pull_request:
|
||||
branches:
|
||||
- "master"
|
||||
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:
|
||||
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,linux/arm64,linux/arm/v7
|
||||
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
|
||||
1021
Cargo.lock
generated
1021
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,7 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
# Utility
|
||||
url = "2.2.2"
|
||||
log = "0.4.11"
|
||||
dotenv = "0.15.0"
|
||||
linkify = "0.6.0"
|
||||
@@ -18,7 +19,6 @@ once_cell = "1.4.1"
|
||||
env_logger = "0.7.1"
|
||||
lazy_static = "1.4.0"
|
||||
ctrlc = { version = "3.0", features = ["termination"] }
|
||||
url = "2.2.2"
|
||||
|
||||
# Lang. Utilities
|
||||
regex = "1"
|
||||
@@ -48,7 +48,7 @@ async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"]
|
||||
web-push = "0.7.2"
|
||||
many-to-many = "0.1.2"
|
||||
lettre = "0.10.0-alpha.4"
|
||||
rauth = { git = "https://gitlab.insrt.uk/insert/rauth", rev = "19bef300ecc269c028e3d746f0a2f09842d3b37b" }
|
||||
rauth = { git = "https://github.com/insertish/rauth", rev = "ad2f5ad1657f4b20c8e483cc1d6c513ff9b22ae1" }
|
||||
hive_pubsub = { git = "https://gitlab.insrt.uk/insert/hive", rev = "a89826df2b30166220e68a6ed01a58b751456604", features = ["mongo"] }
|
||||
|
||||
# web
|
||||
|
||||
5
assets/templates/reset.txt
Normal file
5
assets/templates/reset.txt
Normal 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.
|
||||
6
assets/templates/verify.txt
Normal file
6
assets/templates/verify.txt
Normal 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.
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
export version=0.5.2-alpha.1
|
||||
export version=0.5.3-alpha.0-patch.1
|
||||
echo "pub const VERSION: &str = \"${version}\";" > src/version.rs
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::util::variables::{USE_JANUARY, VAPID_PRIVATE_KEY};
|
||||
use crate::util::variables::{USE_JANUARY, VAPID_PRIVATE_KEY, PUBLIC_URL};
|
||||
use crate::{
|
||||
database::*,
|
||||
notifications::{events::ClientboundNotification, websocket::is_online},
|
||||
@@ -11,12 +11,68 @@ use mongodb::{
|
||||
bson::{doc, to_bson, DateTime, Document},
|
||||
options::FindOptions,
|
||||
};
|
||||
use rauth::entities::{Model, Session};
|
||||
use rocket::serde::json::Value;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use web_push::{
|
||||
ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder,
|
||||
};
|
||||
use web_push::{ContentEncoding, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder};
|
||||
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,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
@@ -43,6 +99,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 {
|
||||
@@ -177,8 +250,7 @@ impl Message {
|
||||
}
|
||||
|
||||
let mentions = self.mentions.clone();
|
||||
let enc = serde_json::to_string(&self).unwrap();
|
||||
ClientboundNotification::Message(self).publish(channel.id().to_string());
|
||||
ClientboundNotification::Message(self.clone()).publish(channel.id().to_string());
|
||||
|
||||
/*
|
||||
Web Push Test Code
|
||||
@@ -204,59 +276,47 @@ impl Message {
|
||||
}
|
||||
|
||||
// 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 target_ids.len() > 0 {
|
||||
if let Ok(mut cursor) = Session::find(
|
||||
&get_db(),
|
||||
doc! {
|
||||
"_id": {
|
||||
"$in": target_ids
|
||||
},
|
||||
"subscription": {
|
||||
"$exists": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if subscriptions.len() > 0 {
|
||||
},
|
||||
None
|
||||
)
|
||||
.await {
|
||||
let enc = serde_json::to_string(&PushNotification::new(self, &c_clone).await).unwrap();
|
||||
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();
|
||||
while let Some(Ok(session)) = cursor.next().await {
|
||||
if let Some(sub) = session.subscription {
|
||||
let subscription = SubscriptionInfo {
|
||||
endpoint: sub.endpoint,
|
||||
keys: SubscriptionKeys {
|
||||
auth: sub.auth,
|
||||
p256dh: sub.p256dh
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -350,7 +410,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
|
||||
@@ -365,7 +425,7 @@ impl Message {
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
operation: "update_many",
|
||||
with: "attachment",
|
||||
})?;
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
@@ -112,4 +113,8 @@ impl File {
|
||||
with: "attachment",
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_autumn_url(&self) -> String {
|
||||
format!("{}/{}/{}", AUTUMN_URL.as_str(), self.tag, self.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@ pub enum Special {
|
||||
None,
|
||||
YouTube {
|
||||
id: String,
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
timestamp: Option<String>,
|
||||
},
|
||||
Twitch {
|
||||
content_type: TwitchType,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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};
|
||||
|
||||
@@ -15,7 +15,7 @@ impl<'r> FromRequest<'r> for User {
|
||||
.get("x-bot-token")
|
||||
.next()
|
||||
.map(|x| x.to_string());
|
||||
|
||||
|
||||
if let Some(bot_token) = header_bot_token {
|
||||
return if let Ok(result) = get_collection("bots")
|
||||
.find_one(
|
||||
@@ -40,7 +40,10 @@ impl<'r> FromRequest<'r> for User {
|
||||
if let Some(doc) = result {
|
||||
Outcome::Success(from_document(doc).unwrap())
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
Outcome::Failure((
|
||||
Status::Forbidden,
|
||||
rauth::util::Error::InvalidSession,
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Outcome::Failure((
|
||||
@@ -62,32 +65,34 @@ impl<'r> FromRequest<'r> for User {
|
||||
with: "bot",
|
||||
},
|
||||
))
|
||||
}
|
||||
};
|
||||
} else {
|
||||
let session: Session = request.guard::<Session>().await.unwrap();
|
||||
|
||||
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 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 {
|
||||
Outcome::Success(from_document(doc).unwrap())
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
}
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
Outcome::Failure((
|
||||
Status::InternalServerError,
|
||||
rauth::util::Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user",
|
||||
},
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Outcome::Failure((
|
||||
Status::InternalServerError,
|
||||
rauth::util::Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user",
|
||||
},
|
||||
))
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::database::{permissions, get_collection, get_db, PermissionTuple};
|
||||
|
||||
use futures::StreamExt;
|
||||
use log::info;
|
||||
use mongodb::{bson::{doc, from_document, to_document}, options::FindOptions};
|
||||
use mongodb::{bson::{Document, doc, from_bson, from_document, to_document}, options::FindOptions};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -11,7 +11,7 @@ struct MigrationInfo {
|
||||
revision: i32,
|
||||
}
|
||||
|
||||
pub const LATEST_REVISION: i32 = 8;
|
||||
pub const LATEST_REVISION: i32 = 9;
|
||||
|
||||
pub async fn migrate_database() {
|
||||
let migrations = get_collection("migrations");
|
||||
@@ -212,6 +212,82 @@ pub async fn run_migrations(revision: i32) -> i32 {
|
||||
.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();
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
98
src/main.rs
98
src/main.rs
@@ -21,20 +21,18 @@ pub mod util;
|
||||
pub mod version;
|
||||
|
||||
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::catchers;
|
||||
use rocket_cors::AllowedOrigins;
|
||||
use std::str::FromStr;
|
||||
use util::variables::{
|
||||
APP_URL, HCAPTCHA_KEY, INVITE_ONLY, PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD,
|
||||
SMTP_USERNAME, USE_EMAIL, USE_HCAPTCHA,
|
||||
APP_URL, HCAPTCHA_KEY, INVITE_ONLY, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_USERNAME,
|
||||
USE_EMAIL, USE_HCAPTCHA,
|
||||
};
|
||||
|
||||
#[async_std::main]
|
||||
@@ -71,79 +69,67 @@ async fn launch_web() {
|
||||
let cors = rocket_cors::CorsOptions {
|
||||
allowed_origins: AllowedOrigins::All,
|
||||
allowed_methods: [
|
||||
"Get",
|
||||
"Put",
|
||||
"Post",
|
||||
"Delete",
|
||||
"Options",
|
||||
"Head",
|
||||
"Trace",
|
||||
"Connect",
|
||||
"Patch",
|
||||
"Get", "Put", "Post", "Delete", "Options", "Head", "Trace", "Connect", "Patch",
|
||||
]
|
||||
.iter()
|
||||
.map(|s| FromStr::from_str(s).unwrap())
|
||||
.collect(),
|
||||
.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: "You're almost there!
|
||||
If you did not perform this action you can safely ignore this email.
|
||||
|
||||
Please verify your account here: {{url}}",
|
||||
html: None,
|
||||
},
|
||||
reset_password: Template {
|
||||
title: "Reset your Revolt password.",
|
||||
text: "You requested a password reset, if you did not perform this action you can safely ignore this email.
|
||||
|
||||
Reset your password here: {{url}}",
|
||||
html: None,
|
||||
},
|
||||
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());
|
||||
config.captcha = Captcha::HCaptcha {
|
||||
secret: HCAPTCHA_KEY.clone(),
|
||||
};
|
||||
}
|
||||
|
||||
let auth = Auth::new(database::get_collection("accounts"), options);
|
||||
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())
|
||||
.attach(cors)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use hive_pubsub::PubSub;
|
||||
use mongodb::bson::doc;
|
||||
use rauth::auth::Session;
|
||||
use rocket::serde::json::Value;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -19,24 +18,24 @@ pub enum WebSocketError {
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct BotAuth {
|
||||
pub token: String
|
||||
pub struct Auth {
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(untagged)]
|
||||
pub enum AuthType {
|
||||
User(Session),
|
||||
Bot(BotAuth)
|
||||
pub enum Ping {
|
||||
Binary(Vec<u8>),
|
||||
Number(usize)
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum ServerboundNotification {
|
||||
Authenticate(AuthType),
|
||||
Authenticate(Auth),
|
||||
BeginTyping { channel: String },
|
||||
EndTyping { channel: String },
|
||||
Ping { data: Vec<u8> }
|
||||
Ping { data: Ping, responded: Option<()> },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
@@ -50,7 +49,7 @@ pub enum RemoveUserField {
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum RemoveChannelField {
|
||||
Icon,
|
||||
Description
|
||||
Description,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
@@ -85,8 +84,9 @@ pub enum ClientboundNotification {
|
||||
users: Vec<User>,
|
||||
servers: Vec<Server>,
|
||||
channels: Vec<Channel>,
|
||||
members: Vec<Member>
|
||||
members: Vec<Member>,
|
||||
},
|
||||
Pong { data: Ping },
|
||||
|
||||
Message(Message),
|
||||
MessageUpdate {
|
||||
@@ -159,11 +159,11 @@ pub enum ClientboundNotification {
|
||||
role_id: String,
|
||||
data: Value,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
clear: Option<RemoveRoleField>
|
||||
clear: Option<RemoveRoleField>,
|
||||
},
|
||||
ServerRoleDelete {
|
||||
id: String,
|
||||
role_id: String
|
||||
role_id: String,
|
||||
},
|
||||
|
||||
UserUpdate {
|
||||
@@ -222,8 +222,7 @@ pub async fn prehandle_hook(notification: &ClientboundNotification) -> Result<()
|
||||
subscribe_if_exists(recipient.clone(), channel_id.to_string()).ok();
|
||||
}
|
||||
}
|
||||
Channel::TextChannel { server, .. }
|
||||
| Channel::VoiceChannel { server, .. } => {
|
||||
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
|
||||
// ! FIXME: write a better algorithm?
|
||||
let members = Server::fetch_member_ids(server).await?;
|
||||
for member in members {
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
use crate::database::*;
|
||||
use crate::notifications::events::{AuthType, BotAuth};
|
||||
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, handshake::server};
|
||||
use futures::channel::{oneshot, 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 mongodb::bson::doc;
|
||||
use rauth::{
|
||||
auth::{Auth},
|
||||
options::Options,
|
||||
};
|
||||
use rauth::entities::{Model, Session};
|
||||
use rmp_serde;
|
||||
use url::Url;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
use url::Url;
|
||||
|
||||
use super::{
|
||||
events::{ClientboundNotification, ServerboundNotification, WebSocketError},
|
||||
@@ -51,28 +51,43 @@ pub async fn launch_server() {
|
||||
#[derive(Debug)]
|
||||
enum MSGFormat {
|
||||
JSON,
|
||||
MSGPACK
|
||||
MSGPACK,
|
||||
}
|
||||
|
||||
struct HeaderCallback {
|
||||
sender: oneshot::Sender<MSGFormat>
|
||||
sender: oneshot::Sender<MSGFormat>,
|
||||
}
|
||||
|
||||
impl server::Callback for HeaderCallback {
|
||||
fn on_request(self, request: &server::Request, response: server::Response) -> Result<server::Response, server::ErrorResponse> {
|
||||
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 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,
|
||||
_ => panic!("unknown format") // TODO: not use panic
|
||||
_ => MSGFormat::JSON, // Fallback to JSON.
|
||||
};
|
||||
|
||||
self.sender.send(format).unwrap(); // TODO: not use unwrap
|
||||
Ok(response)
|
||||
if self.sender.send(format).is_ok() {
|
||||
Ok(response)
|
||||
} else {
|
||||
Err(server::ErrorResponse::new(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,12 +96,13 @@ async fn accept(stream: TcpStream) {
|
||||
.peer_addr()
|
||||
.expect("Connected streams should have a peer address.");
|
||||
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
|
||||
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);
|
||||
|
||||
@@ -98,12 +114,12 @@ async fn accept(stream: TcpStream) {
|
||||
let res = match msg_format {
|
||||
MSGFormat::JSON => match serde_json::to_string(¬ification) {
|
||||
Ok(s) => Message::Text(s),
|
||||
Err(_) => return
|
||||
}
|
||||
Err(_) => return,
|
||||
},
|
||||
MSGFormat::MSGPACK => match rmp_serde::to_vec(¬ification) {
|
||||
Ok(v) => Message::Binary(v),
|
||||
Err(_) => return
|
||||
}
|
||||
Err(_) => return,
|
||||
},
|
||||
};
|
||||
|
||||
if let Err(_) = tx.unbounded_send(res) {
|
||||
@@ -118,21 +134,27 @@ async fn accept(stream: TcpStream) {
|
||||
let mutex = mutex_generator();
|
||||
|
||||
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: vec }),
|
||||
_ => return Ok(())
|
||||
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
|
||||
msg: why.to_string(),
|
||||
},
|
||||
));
|
||||
return Ok(());
|
||||
}
|
||||
Ok(n) => n,
|
||||
};
|
||||
|
||||
match notification {
|
||||
@@ -147,34 +169,20 @@ async fn accept(stream: TcpStream) {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(id) = match auth {
|
||||
AuthType::User(new_session) => {
|
||||
if let Ok(validated_session) =
|
||||
Auth::new(get_collection("accounts"), Options::new())
|
||||
.verify_session(new_session)
|
||||
.await
|
||||
{
|
||||
Some(validated_session.user_id.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
AuthType::Bot(BotAuth { token }) => {
|
||||
if let Ok(doc) = get_collection("bots")
|
||||
.find_one(
|
||||
doc! { "token": token },
|
||||
None
|
||||
).await {
|
||||
if let Some(doc) = doc {
|
||||
Some(doc.get_str("_id").unwrap().to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
} {
|
||||
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 {
|
||||
@@ -229,7 +237,7 @@ async fn accept(stream: TcpStream) {
|
||||
data: json!({
|
||||
"online": true
|
||||
}),
|
||||
clear: None
|
||||
clear: None,
|
||||
}
|
||||
.publish_as_user(id);
|
||||
}
|
||||
@@ -297,8 +305,12 @@ async fn accept(stream: TcpStream) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
ServerboundNotification::Ping { data } => {
|
||||
info!("Ping received from User {}. Payload: {:?}", &addr, data);
|
||||
ServerboundNotification::Ping { data, responded } => {
|
||||
debug!("Ping received from connection {}. Payload: {:?}", &addr, data);
|
||||
|
||||
if responded.is_none() {
|
||||
send(ClientboundNotification::Pong { data });
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -329,7 +341,7 @@ async fn accept(stream: TcpStream) {
|
||||
data: json!({
|
||||
"online": false
|
||||
}),
|
||||
clear: None
|
||||
clear: None,
|
||||
}
|
||||
.publish_as_user(id);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ pub struct Data {
|
||||
|
||||
#[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 })?;
|
||||
|
||||
@@ -6,6 +6,10 @@ 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);
|
||||
|
||||
@@ -26,6 +26,10 @@ pub struct Data {
|
||||
|
||||
#[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 })?;
|
||||
|
||||
@@ -5,6 +5,10 @@ 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 {
|
||||
|
||||
@@ -7,6 +7,10 @@ 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! {
|
||||
|
||||
@@ -5,6 +5,10 @@ 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 {
|
||||
|
||||
@@ -23,6 +23,10 @@ pub enum Destination {
|
||||
|
||||
#[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 {
|
||||
|
||||
@@ -30,7 +30,8 @@ pub struct Data {
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_ULID: Regex = Regex::new(r"<@([0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26})>").unwrap();
|
||||
// ignoring I L O and U is intentional
|
||||
static ref RE_ULID: Regex = Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap();
|
||||
}
|
||||
|
||||
#[post("/<target>/messages", data = "<message>")]
|
||||
@@ -80,9 +81,10 @@ pub async fn message_send(_r: RateLimited<'_>, user: User, target: Ref, message:
|
||||
let id = Ulid::new().to_string();
|
||||
|
||||
let mut mentions = HashSet::new();
|
||||
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.insert(captures[1].to_string());
|
||||
for capture in RE_ULID.captures_iter(&message.content) {
|
||||
if let Some(mention) = capture.get(1) {
|
||||
mentions.insert(mention.as_str().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let mut replies = HashSet::new();
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
pub use rocket::response::Redirect;
|
||||
pub use rocket::http::Status;
|
||||
pub use rocket::response::Redirect;
|
||||
use rocket::{Build, Rocket};
|
||||
|
||||
mod bots;
|
||||
mod channels;
|
||||
mod invites;
|
||||
mod onboard;
|
||||
@@ -10,7 +11,6 @@ mod root;
|
||||
mod servers;
|
||||
mod sync;
|
||||
mod users;
|
||||
mod bots;
|
||||
|
||||
pub fn mount(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
rocket
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::database::*;
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rauth::auth::Session;
|
||||
use rauth::entities::Session;
|
||||
use regex::Regex;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::database::*;
|
||||
|
||||
use rauth::auth::Session;
|
||||
use rauth::entities::Session;
|
||||
use rocket::serde::json::Value;
|
||||
|
||||
#[get("/hello")]
|
||||
|
||||
@@ -1,37 +1,19 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{EmptyResponse, Error, Result};
|
||||
|
||||
use mongodb::bson::{doc, to_document};
|
||||
use rauth::auth::Session;
|
||||
use mongodb::bson::doc;
|
||||
use rauth::entities::{Model, Session, WebPushSubscription};
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Subscription {
|
||||
endpoint: String,
|
||||
p256dh: String,
|
||||
auth: String,
|
||||
}
|
||||
|
||||
#[post("/subscribe", data = "<data>")]
|
||||
pub async fn req(session: Session, data: Json<Subscription>) -> Result<EmptyResponse> {
|
||||
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(EmptyResponse {})
|
||||
.map(|_| EmptyResponse)
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "save",
|
||||
with: "session",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,29 +1,18 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
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<EmptyResponse> {
|
||||
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(EmptyResponse {})
|
||||
operation: "save",
|
||||
with: "session",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use crate::util::{ratelimit::RateLimitGuard, 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 crate::util::{
|
||||
ratelimit::RateLimitGuard,
|
||||
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::{http::Status, serde::json::Value};
|
||||
|
||||
@@ -14,9 +14,9 @@ pub struct Options {
|
||||
#[post("/settings/fetch", data = "<options>")]
|
||||
pub async fn req(user: User, options: Json<Options>) -> Result<Value> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot)
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
|
||||
let options = options.into_inner();
|
||||
let mut projection = doc! {
|
||||
"_id": 0,
|
||||
|
||||
@@ -7,8 +7,8 @@ use rocket::serde::json::Value;
|
||||
#[get("/unreads")]
|
||||
pub async fn req(user: User) -> Result<Value> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot)
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
|
||||
Ok(json!(User::fetch_unreads(&user.id).await?))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::database::*;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
use crate::util::result::{EmptyResponse, Error, Result};
|
||||
|
||||
use chrono::prelude::*;
|
||||
use mongodb::bson::{doc, to_bson};
|
||||
@@ -20,7 +20,7 @@ pub struct Options {
|
||||
#[post("/settings/set?<options..>", data = "<data>")]
|
||||
pub async fn req(user: User, data: Json<Data>, options: Options) -> Result<EmptyResponse> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot)
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
let data = data.into_inner();
|
||||
|
||||
@@ -3,9 +3,8 @@ use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rauth::auth::{Auth, Session};
|
||||
use rauth::entities::Account;
|
||||
use regex::Regex;
|
||||
use rocket::State;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
@@ -26,8 +25,7 @@ pub struct Data {
|
||||
|
||||
#[patch("/<_ignore_id>/username", data = "<data>")]
|
||||
pub async fn req(
|
||||
auth: &State<Auth>,
|
||||
session: Session,
|
||||
account: Account,
|
||||
user: User,
|
||||
data: Json<Data>,
|
||||
_ignore_id: String,
|
||||
@@ -39,8 +37,7 @@ pub async fn req(
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
auth.verify_password(&session, data.password.clone())
|
||||
.await
|
||||
account.verify_password(&data.password)
|
||||
.map_err(|_| Error::InvalidCredentials)?;
|
||||
|
||||
let mut set = doc! {};
|
||||
|
||||
@@ -1 +1 @@
|
||||
pub const VERSION: &str = "0.5.2-alpha.1";
|
||||
pub const VERSION: &str = "0.5.3-alpha.0-patch.1";
|
||||
|
||||
Reference in New Issue
Block a user