refactor(quark): port get/set_settings, get_unreads

#283
This commit is contained in:
Paul Makles
2024-04-07 18:10:11 +01:00
parent f6a565385e
commit 2de7598f8d
11 changed files with 69 additions and 68 deletions

View File

@@ -1,17 +1,8 @@
use revolt_quark::{
models::{User, UserSettings},
Db, Result,
};
use revolt_database::{Database, User};
use revolt_models::v0;
use revolt_result::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>,
}
use rocket::State;
/// # 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.
#[openapi(tag = "Sync")]
#[post("/settings/fetch", data = "<options>")]
pub async fn req(
db: &Db,
pub async fn fetch(
db: &State<Database>,
user: User,
options: Json<OptionsFetchSettings>,
) -> Result<Json<UserSettings>> {
options: Json<v0::OptionsFetchSettings>,
) -> Result<Json<v0::UserSettings>> {
db.fetch_user_settings(&user.id, &options.into_inner().keys)
.await
.map(Json)

View File

@@ -1,15 +1,17 @@
use revolt_quark::{
models::{ChannelUnread, User},
Db, Result,
};
use revolt_database::{Database, User};
use revolt_models::v0;
use revolt_result::Result;
use rocket::serde::json::Json;
use rocket::State;
/// # 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)
pub async fn unreads(db: &State<Database>, user: User) -> Result<Json<Vec<v0::ChannelUnread>>> {
db.fetch_unreads(&user.id)
.await
.map(|v| v.into_iter().map(|u| u.into()).collect())
.map(Json)
}

View File

@@ -6,5 +6,5 @@ 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]
openapi_get_routes_spec![get_settings::fetch, set_settings::set, get_unreads::unreads]
}

View File

@@ -1,33 +1,24 @@
use revolt_quark::models::User;
use revolt_quark::r#impl::UserSettingsImpl;
use revolt_quark::{Db, EmptyResponse, Result};
use revolt_database::{Database, User, UserSettingsImpl};
use revolt_models::v0;
use chrono::prelude::*;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use revolt_result::Result;
use rocket::{serde::json::Json, State};
use rocket_empty::EmptyResponse;
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,
pub async fn set(
db: &State<Database>,
user: User,
data: Json<Data>,
options: OptionsSetSettings,
options: v0::OptionsSetSettings,
) -> Result<EmptyResponse> {
let data = data.into_inner();
let current_time = Utc::now().timestamp_millis();