feat(sync): implement settings and unreads fetch

This commit is contained in:
Paul Makles
2022-02-10 16:55:59 +00:00
parent a8d5fcec9e
commit f676071641
7 changed files with 146 additions and 37 deletions

View File

@@ -1,8 +1,7 @@
use revolt_quark::{Error, Result};
use revolt_quark::{Result, models::{UserSettings, User}, Db};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use rocket::serde::json::{Json, Value};
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
@@ -11,6 +10,6 @@ pub struct Options {
}
#[post("/settings/fetch", data = "<options>")]
pub async fn req(/*user: UserRef,*/ options: Json<Options>) -> Result<Value> {
todo!()
pub async fn req(db: &Db, user: User, options: Json<Options>) -> Result<Json<UserSettings>> {
db.fetch_user_settings(&user.id, &options.into_inner().keys).await.map(Json)
}

View File

@@ -1,9 +1,9 @@
use revolt_quark::{Error, Result};
use revolt_quark::{Result, models::{ChannelUnread, User}, Db};
use mongodb::bson::doc;
use rocket::serde::json::Value;
use rocket::serde::json::Json;
#[get("/unreads")]
pub async fn req(/*user: UserRef*/) -> Result<Value> {
todo!()
pub async fn req(db: &Db, user: User) -> Result<Json<Vec<ChannelUnread>>> {
db.fetch_unreads(&user.id).await.map(Json)
}

View File

@@ -1,11 +1,9 @@
use revolt_quark::{EmptyResponse, Result};
use revolt_quark::models::User;
use revolt_quark::{EmptyResponse, Result, Db};
use chrono::prelude::*;
use mongodb::bson::{doc, to_bson};
use mongodb::options::UpdateOptions;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
type Data = HashMap<String, String>;
@@ -16,9 +14,26 @@ pub struct Options {
}
#[post("/settings/set?<options..>", data = "<data>")]
pub async fn req(
/*user: UserRef,*/ data: Json<Data>,
options: Options,
) -> Result<EmptyResponse> {
todo!()
pub async fn req(db: &Db, user: User, data: Json<Data>, options: Options) -> 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
));
}
db.set_user_settings(&user.id, &settings).await.map(|_| EmptyResponse)
}