forked from jmug/stoatchat
@@ -1,6 +1,26 @@
|
|||||||
|
#[cfg(feature = "rocket")]
|
||||||
|
use rocket::FromForm;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// HashMap of user settings
|
/// HashMap of user settings
|
||||||
/// Each key is mapped to a tuple consisting of the
|
/// Each key is mapped to a tuple consisting of the
|
||||||
/// revision timestamp and serialised data (in JSON format)
|
/// revision timestamp and serialised data (in JSON format)
|
||||||
pub type UserSettings = HashMap<String, (i64, String)>;
|
pub type UserSettings = HashMap<String, (i64, String)>;
|
||||||
|
|
||||||
|
auto_derived!(
|
||||||
|
/// Options for fetching settings
|
||||||
|
pub struct OptionsFetchSettings {
|
||||||
|
/// Keys to fetch
|
||||||
|
pub keys: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional options for inserting settings
|
||||||
|
#[cfg_attr(feature = "rocket", derive(FromForm))]
|
||||||
|
pub struct OptionsSetSettings {
|
||||||
|
/// Timestamp of settings change.
|
||||||
|
///
|
||||||
|
/// Used to avoid feedback loops.
|
||||||
|
pub timestamp: Option<i64>,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ pub struct DataOnboard {
|
|||||||
/// This sets a new username, completes onboarding and allows a user to start using Revolt.
|
/// This sets a new username, completes onboarding and allows a user to start using Revolt.
|
||||||
#[openapi(tag = "Onboarding")]
|
#[openapi(tag = "Onboarding")]
|
||||||
#[post("/complete", data = "<data>")]
|
#[post("/complete", data = "<data>")]
|
||||||
pub async fn req(
|
pub async fn complete(
|
||||||
db: &State<Database>,
|
db: &State<Database>,
|
||||||
session: Session,
|
session: Session,
|
||||||
user: Option<User>,
|
user: Option<User>,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
use revolt_quark::{authifier::models::Session, models::User};
|
use authifier::models::Session;
|
||||||
|
use revolt_database::User;
|
||||||
|
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
@@ -14,7 +16,7 @@ pub struct DataHello {
|
|||||||
/// This will tell you whether the current account requires onboarding or whether you can continue to send requests as usual. You may skip calling this if you're restoring an existing session.
|
/// This will tell you whether the current account requires onboarding or whether you can continue to send requests as usual. You may skip calling this if you're restoring an existing session.
|
||||||
#[openapi(tag = "Onboarding")]
|
#[openapi(tag = "Onboarding")]
|
||||||
#[get("/hello")]
|
#[get("/hello")]
|
||||||
pub async fn req(_session: Session, user: Option<User>) -> Json<DataHello> {
|
pub async fn hello(_session: Session, user: Option<User>) -> Json<DataHello> {
|
||||||
Json(DataHello {
|
Json(DataHello {
|
||||||
onboarding: user.is_none(),
|
onboarding: user.is_none(),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ mod complete;
|
|||||||
mod hello;
|
mod hello;
|
||||||
|
|
||||||
pub fn routes() -> (Vec<Route>, OpenApi) {
|
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||||
openapi_get_routes_spec![hello::req, complete::req]
|
openapi_get_routes_spec![hello::hello, complete::complete]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ mod subscribe;
|
|||||||
mod unsubscribe;
|
mod unsubscribe;
|
||||||
|
|
||||||
pub fn routes() -> (Vec<Route>, OpenApi) {
|
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||||
openapi_get_routes_spec![subscribe::req, unsubscribe::req]
|
openapi_get_routes_spec![subscribe::subscribe, unsubscribe::unsubscribe]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
use revolt_quark::{
|
use authifier::{
|
||||||
authifier::{
|
models::{Session, WebPushSubscription},
|
||||||
models::{Session, WebPushSubscription},
|
Authifier,
|
||||||
Authifier,
|
|
||||||
},
|
|
||||||
EmptyResponse, Error, Result,
|
|
||||||
};
|
};
|
||||||
|
use revolt_result::{create_database_error, Result};
|
||||||
use rocket::{serde::json::Json, State};
|
use rocket::{serde::json::Json, State};
|
||||||
|
use rocket_empty::EmptyResponse;
|
||||||
|
|
||||||
/// # Push Subscribe
|
/// # Push Subscribe
|
||||||
///
|
///
|
||||||
@@ -15,7 +13,7 @@ use rocket::{serde::json::Json, State};
|
|||||||
/// If an existing subscription exists on this session, it will be removed.
|
/// If an existing subscription exists on this session, it will be removed.
|
||||||
#[openapi(tag = "Web Push")]
|
#[openapi(tag = "Web Push")]
|
||||||
#[post("/subscribe", data = "<data>")]
|
#[post("/subscribe", data = "<data>")]
|
||||||
pub async fn req(
|
pub async fn subscribe(
|
||||||
authifier: &State<Authifier>,
|
authifier: &State<Authifier>,
|
||||||
mut session: Session,
|
mut session: Session,
|
||||||
data: Json<WebPushSubscription>,
|
data: Json<WebPushSubscription>,
|
||||||
@@ -25,8 +23,5 @@ pub async fn req(
|
|||||||
.save(authifier)
|
.save(authifier)
|
||||||
.await
|
.await
|
||||||
.map(|_| EmptyResponse)
|
.map(|_| EmptyResponse)
|
||||||
.map_err(|_| Error::DatabaseError {
|
.map_err(|_| create_database_error!("save", "session"))
|
||||||
operation: "save",
|
|
||||||
with: "session",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use revolt_quark::{
|
use authifier::{models::Session, Authifier};
|
||||||
authifier::{models::Session, Authifier},
|
|
||||||
EmptyResponse, Error, Result,
|
use revolt_result::{create_database_error, Result};
|
||||||
};
|
use rocket_empty::EmptyResponse;
|
||||||
|
|
||||||
use rocket::State;
|
use rocket::State;
|
||||||
|
|
||||||
@@ -10,14 +10,14 @@ use rocket::State;
|
|||||||
/// Remove the Web Push subscription associated with the current session.
|
/// Remove the Web Push subscription associated with the current session.
|
||||||
#[openapi(tag = "Web Push")]
|
#[openapi(tag = "Web Push")]
|
||||||
#[post("/unsubscribe")]
|
#[post("/unsubscribe")]
|
||||||
pub async fn req(authifier: &State<Authifier>, mut session: Session) -> Result<EmptyResponse> {
|
pub async fn unsubscribe(
|
||||||
|
authifier: &State<Authifier>,
|
||||||
|
mut session: Session,
|
||||||
|
) -> Result<EmptyResponse> {
|
||||||
session.subscription = None;
|
session.subscription = None;
|
||||||
session
|
session
|
||||||
.save(authifier)
|
.save(authifier)
|
||||||
.await
|
.await
|
||||||
.map(|_| EmptyResponse)
|
.map(|_| EmptyResponse)
|
||||||
.map_err(|_| Error::DatabaseError {
|
.map_err(|_| create_database_error!("save", "session"))
|
||||||
operation: "save",
|
|
||||||
with: "session",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,8 @@
|
|||||||
use revolt_quark::{
|
use revolt_database::{Database, User};
|
||||||
models::{User, UserSettings},
|
use revolt_models::v0;
|
||||||
Db, Result,
|
use revolt_result::Result;
|
||||||
};
|
|
||||||
|
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use serde::{Deserialize, Serialize};
|
use rocket::State;
|
||||||
|
|
||||||
/// # Fetch Options
|
|
||||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
|
||||||
pub struct OptionsFetchSettings {
|
|
||||||
/// Keys to fetch
|
|
||||||
keys: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Fetch Settings
|
/// # Fetch Settings
|
||||||
///
|
///
|
||||||
@@ -20,11 +11,11 @@ pub struct OptionsFetchSettings {
|
|||||||
/// This will return an object with the requested keys, each value is a tuple of `(timestamp, value)`, the value is the previously uploaded data.
|
/// This will return an object with the requested keys, each value is a tuple of `(timestamp, value)`, the value is the previously uploaded data.
|
||||||
#[openapi(tag = "Sync")]
|
#[openapi(tag = "Sync")]
|
||||||
#[post("/settings/fetch", data = "<options>")]
|
#[post("/settings/fetch", data = "<options>")]
|
||||||
pub async fn req(
|
pub async fn fetch(
|
||||||
db: &Db,
|
db: &State<Database>,
|
||||||
user: User,
|
user: User,
|
||||||
options: Json<OptionsFetchSettings>,
|
options: Json<v0::OptionsFetchSettings>,
|
||||||
) -> Result<Json<UserSettings>> {
|
) -> Result<Json<v0::UserSettings>> {
|
||||||
db.fetch_user_settings(&user.id, &options.into_inner().keys)
|
db.fetch_user_settings(&user.id, &options.into_inner().keys)
|
||||||
.await
|
.await
|
||||||
.map(Json)
|
.map(Json)
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
use revolt_quark::{
|
use revolt_database::{Database, User};
|
||||||
models::{ChannelUnread, User},
|
use revolt_models::v0;
|
||||||
Db, Result,
|
use revolt_result::Result;
|
||||||
};
|
|
||||||
|
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
use rocket::State;
|
||||||
|
|
||||||
/// # Fetch Unreads
|
/// # Fetch Unreads
|
||||||
///
|
///
|
||||||
/// Fetch information about unread state on channels.
|
/// Fetch information about unread state on channels.
|
||||||
#[openapi(tag = "Sync")]
|
#[openapi(tag = "Sync")]
|
||||||
#[get("/unreads")]
|
#[get("/unreads")]
|
||||||
pub async fn req(db: &Db, user: User) -> Result<Json<Vec<ChannelUnread>>> {
|
pub async fn unreads(db: &State<Database>, user: User) -> Result<Json<Vec<v0::ChannelUnread>>> {
|
||||||
db.fetch_unreads(&user.id).await.map(Json)
|
db.fetch_unreads(&user.id)
|
||||||
|
.await
|
||||||
|
.map(|v| v.into_iter().map(|u| u.into()).collect())
|
||||||
|
.map(Json)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ mod get_unreads;
|
|||||||
mod set_settings;
|
mod set_settings;
|
||||||
|
|
||||||
pub fn routes() -> (Vec<Route>, OpenApi) {
|
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||||
openapi_get_routes_spec![get_settings::req, set_settings::req, get_unreads::req]
|
openapi_get_routes_spec![get_settings::fetch, set_settings::set, get_unreads::unreads]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,24 @@
|
|||||||
use revolt_quark::models::User;
|
use revolt_database::{Database, User, UserSettingsImpl};
|
||||||
use revolt_quark::r#impl::UserSettingsImpl;
|
use revolt_models::v0;
|
||||||
use revolt_quark::{Db, EmptyResponse, Result};
|
|
||||||
|
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use rocket::serde::json::Json;
|
use revolt_result::Result;
|
||||||
use serde::{Deserialize, Serialize};
|
use rocket::{serde::json::Json, State};
|
||||||
|
use rocket_empty::EmptyResponse;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
type Data = HashMap<String, String>;
|
type Data = HashMap<String, String>;
|
||||||
|
|
||||||
/// # Set Options
|
|
||||||
#[derive(FromForm, Serialize, Deserialize, JsonSchema)]
|
|
||||||
pub struct OptionsSetSettings {
|
|
||||||
/// Timestamp of settings change.
|
|
||||||
///
|
|
||||||
/// Used to avoid feedback loops.
|
|
||||||
timestamp: Option<i64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Set Settings
|
/// # Set Settings
|
||||||
///
|
///
|
||||||
/// Upload data to save to settings.
|
/// Upload data to save to settings.
|
||||||
#[openapi(tag = "Sync")]
|
#[openapi(tag = "Sync")]
|
||||||
#[post("/settings/set?<options..>", data = "<data>")]
|
#[post("/settings/set?<options..>", data = "<data>")]
|
||||||
pub async fn req(
|
pub async fn set(
|
||||||
db: &Db,
|
db: &State<Database>,
|
||||||
user: User,
|
user: User,
|
||||||
data: Json<Data>,
|
data: Json<Data>,
|
||||||
options: OptionsSetSettings,
|
options: v0::OptionsSetSettings,
|
||||||
) -> Result<EmptyResponse> {
|
) -> Result<EmptyResponse> {
|
||||||
let data = data.into_inner();
|
let data = data.into_inner();
|
||||||
let current_time = Utc::now().timestamp_millis();
|
let current_time = Utc::now().timestamp_millis();
|
||||||
|
|||||||
Reference in New Issue
Block a user