forked from jmug/stoatchat
chore(monorepo): delta, january, quark
This commit is contained in:
31
crates/delta/src/routes/sync/get_settings.rs
Normal file
31
crates/delta/src/routes/sync/get_settings.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use revolt_quark::{
|
||||
models::{User, UserSettings},
|
||||
Db, Result,
|
||||
};
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// # Fetch Options
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct OptionsFetchSettings {
|
||||
/// Keys to fetch
|
||||
keys: Vec<String>,
|
||||
}
|
||||
|
||||
/// # Fetch Settings
|
||||
///
|
||||
/// Fetch settings from server filtered by keys.
|
||||
///
|
||||
/// 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")]
|
||||
#[post("/settings/fetch", data = "<options>")]
|
||||
pub async fn req(
|
||||
db: &Db,
|
||||
user: User,
|
||||
options: Json<OptionsFetchSettings>,
|
||||
) -> Result<Json<UserSettings>> {
|
||||
db.fetch_user_settings(&user.id, &options.into_inner().keys)
|
||||
.await
|
||||
.map(Json)
|
||||
}
|
||||
15
crates/delta/src/routes/sync/get_unreads.rs
Normal file
15
crates/delta/src/routes/sync/get_unreads.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use revolt_quark::{
|
||||
models::{ChannelUnread, User},
|
||||
Db, Result,
|
||||
};
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
|
||||
/// # Fetch Unreads
|
||||
///
|
||||
/// Fetch information about unread state on channels.
|
||||
#[openapi(tag = "Sync")]
|
||||
#[get("/unreads")]
|
||||
pub async fn req(db: &Db, user: User) -> Result<Json<Vec<ChannelUnread>>> {
|
||||
db.fetch_unreads(&user.id).await.map(Json)
|
||||
}
|
||||
10
crates/delta/src/routes/sync/mod.rs
Normal file
10
crates/delta/src/routes/sync/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use rocket::Route;
|
||||
use rocket_okapi::okapi::openapi3::OpenApi;
|
||||
|
||||
mod get_settings;
|
||||
mod get_unreads;
|
||||
mod set_settings;
|
||||
|
||||
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||
openapi_get_routes_spec![get_settings::req, set_settings::req, get_unreads::req]
|
||||
}
|
||||
50
crates/delta/src/routes/sync/set_settings.rs
Normal file
50
crates/delta/src/routes/sync/set_settings.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use revolt_quark::models::User;
|
||||
use revolt_quark::r#impl::generic::users::user_settings::UserSettingsImpl;
|
||||
use revolt_quark::{Db, EmptyResponse, Result};
|
||||
|
||||
use chrono::prelude::*;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
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
|
||||
///
|
||||
/// Upload data to save to settings.
|
||||
#[openapi(tag = "Sync")]
|
||||
#[post("/settings/set?<options..>", data = "<data>")]
|
||||
pub async fn req(
|
||||
db: &Db,
|
||||
user: User,
|
||||
data: Json<Data>,
|
||||
options: OptionsSetSettings,
|
||||
) -> Result<EmptyResponse> {
|
||||
let data = data.into_inner();
|
||||
let current_time = Utc::now().timestamp_millis();
|
||||
let timestamp = if let Some(timestamp) = options.timestamp {
|
||||
if timestamp > current_time {
|
||||
current_time
|
||||
} else {
|
||||
timestamp
|
||||
}
|
||||
} else {
|
||||
current_time
|
||||
};
|
||||
|
||||
let mut settings = HashMap::new();
|
||||
for (key, data) in data {
|
||||
settings.insert(key, (timestamp, data));
|
||||
}
|
||||
|
||||
settings.set(db, &user.id).await.map(|_| EmptyResponse)
|
||||
}
|
||||
Reference in New Issue
Block a user