mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 21:47:02 +00:00
Merge branch 'master' into unify-last-message
This commit is contained in:
@@ -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! {};
|
||||
|
||||
Reference in New Issue
Block a user